From 68c1b41a7685dfaf0e26c3abd8615d325e29b7e2 Mon Sep 17 00:00:00 2001 From: katsumi143 <32640219+katsumi143@users.noreply.github.com> Date: Mon, 8 Jul 2024 12:58:08 +1000 Subject: [PATCH] improve users_by_discord access --- crates/mellow/src/commands/server.rs | 4 ++-- crates/mellow/src/commands/syncing.rs | 4 ++-- crates/mellow/src/http/routes.rs | 10 +++++----- crates/mellow/src/model/hakumi/mod.rs | 13 +++++-------- crates/mellow/src/patreon.rs | 2 +- crates/mellow/src/visual_scripting/mod.rs | 11 ++++++++--- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/crates/mellow/src/commands/server.rs b/crates/mellow/src/commands/server.rs index 6231b0a..6ab9cb8 100644 --- a/crates/mellow/src/commands/server.rs +++ b/crates/mellow/src/commands/server.rs @@ -26,10 +26,10 @@ pub async fn setup(_context: Context, interaction: Interaction) -> Result Result Result, p let member_id: Id = Id::new(member_id); if let Some(user_id) = HAKUMI_MODELS.user_by_discord(guild_id, member_id).await? { return Ok(web::Json(if let Some(token) = &body.webhook_token { - sync_with_token(guild_id, *user_id, member_id, token, false, None).await? + sync_with_token(guild_id, user_id, member_id, token, false, None).await? } else if body.is_sign_up.is_some_and(|x| x) { let result = if let Some(item) = SIGN_UPS.read().await.iter().find(|x| x.user_id == member_id && x.guild_id == guild_id) { - Some(sync_with_token(guild_id, *user_id, member_id, &item.interaction_token, true, None).await?) + Some(sync_with_token(guild_id, user_id, member_id, &item.interaction_token, true, None).await?) } else { None }; SIGN_UPS.write().await.retain(|x| x.user_id != member_id); return result.map(web::Json).ok_or(ApiError::SignUpNotFound); } else { - sync_single_user(guild_id, *user_id, member_id, None).await? + sync_single_user(guild_id, user_id, member_id, None).await? })); } Err(ApiError::UserNotFound) @@ -280,10 +280,10 @@ async fn patreon_webhook(payload: web::Payload) -> ApiResult { .map_err(|_| ApiError::GenericInvalidRequest)?; if let Some(user_id) = HAKUMI_MODELS.user_by_discord(guild_id, Id::new(user_id.parse().map_err(|_| ApiError::GenericInvalidRequest)?)).await? { let user = HAKUMI_MODELS - .user(*user_id) + .user(user_id) .await?; let discord_id = Id::new(user.connections.iter().find(|x| matches!(x.kind, ConnectionKind::Discord)).unwrap().id.to_string().parse().map_err(|_| ApiError::GenericInvalidRequest)?); - sync_single_user(guild_id, *user_id, discord_id, Some(ConnectionMetadata { + sync_single_user(guild_id, user_id, discord_id, Some(ConnectionMetadata { patreon_pledges: vec![PatreonPledge { tiers: payload.data.relationships.currently_entitled_tiers.data.iter().map(|x| x.id.clone()).collect(), active: payload.data.attributes.patron_status.map_or(false, |x| x == "active_patron"), diff --git a/crates/mellow/src/model/hakumi/mod.rs b/crates/mellow/src/model/hakumi/mod.rs index 0b0f0eb..b5c6271 100644 --- a/crates/mellow/src/model/hakumi/mod.rs +++ b/crates/mellow/src/model/hakumi/mod.rs @@ -28,7 +28,7 @@ pub static HAKUMI_MODELS: Lazy = Lazy::new(HakumiModels::default); pub struct HakumiModels { pub users: DashMap, User>, //user_connections: DashMap<(HakuId, HakuId), Connection>, - pub users_by_discord: DashMap<(Id, Id), HakuId>, + users_by_discord: DashMap<(Id, Id), HakuId>, pub vs_documents: DashMap, Document> } @@ -57,11 +57,11 @@ impl HakumiModels { }) } - pub async fn user_by_discord(&self, guild_id: Id, user_id: Id) -> Result, Id), HakuId>>> { + pub async fn user_by_discord(&self, guild_id: Id, user_id: Id) -> Result>> { let key = (guild_id, user_id); Ok(if let Some(item) = self.users_by_discord.get(&key) { tracing::info!("users_by_discord.read (guild_id={guild_id}) (discord_id={user_id})"); - Some(item) + Some(*item) } else { let mut new_item = match get_user_reference(user_id).await? { Some(x) => x, @@ -74,11 +74,8 @@ impl HakumiModels { tracing::info!("users_by_discord.write (guild_id={guild_id}) (discord_id={user_id}) (user_id={user_id})"); self.users.insert(user_id, new_item); - Some(self.users_by_discord - .entry(key) - .insert(user_id) - .downgrade() - ) + self.users_by_discord.insert(key, user_id); + Some(user_id) }) } diff --git a/crates/mellow/src/patreon.rs b/crates/mellow/src/patreon.rs index 4c0e4c7..9058b03 100644 --- a/crates/mellow/src/patreon.rs +++ b/crates/mellow/src/patreon.rs @@ -116,7 +116,7 @@ struct IncludedItemAttributes { patron_count: u64 } -pub async fn get_campaign(oauth_authorisation: &OAuthAuthorisation) -> Result { +pub async fn get_campaign(oauth_authorisation: OAuthAuthorisation) -> Result { let access_token = &oauth_authorisation.access_token; Ok(match CACHES.patreon_campaigns.get(access_token) .instrument(info_span!("cache.patreon_campaigns.read", ?access_token)) diff --git a/crates/mellow/src/visual_scripting/mod.rs b/crates/mellow/src/visual_scripting/mod.rs index 22cd7d6..fea33d9 100644 --- a/crates/mellow/src/visual_scripting/mod.rs +++ b/crates/mellow/src/visual_scripting/mod.rs @@ -118,8 +118,8 @@ impl Document { if let Some(member) = Some(variables.read().await.get("member")) { let user_id = member.get("id").cast_id(); let guild_id = member.get("guild_id").cast_id(); - if let Some(haku_id) = HAKUMI_MODELS.users_by_discord.get(&(guild_id, user_id)) { - let result = sync_single_user(guild_id, *haku_id, user_id, None) + if let Some(haku_id) = HAKUMI_MODELS.user_by_discord(guild_id, user_id) .await? { + let result = sync_single_user(guild_id, haku_id, user_id, None) .await?; if result.profile_changed || result.member_status.removed() { MELLOW_MODELS @@ -182,7 +182,12 @@ impl Document { ElementKind::GetLinkedPatreonCampaign => { let guild_id = variables.read().await.get("guild_id").cast_id(); let server = MELLOW_MODELS.server(guild_id).await?; - variables.write().await.set("campaign", crate::patreon::get_campaign(server.oauth_authorisations.first().unwrap()).await?.into()); + variables + .write() + .await + .set("campaign", crate::patreon::get_campaign( + server.oauth_authorisations.first().unwrap().clone() + ).await?.into()); }, ElementKind::InteractionReply(data) => { let variables = &*variables.read().await;