Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
improve users_by_discord access
Browse files Browse the repository at this point in the history
  • Loading branch information
katsumi143 committed Jul 8, 2024
1 parent bdf4fbc commit 68c1b41
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions crates/mellow/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ pub async fn setup(_context: Context, interaction: Interaction) -> Result<Comman
.insert(json!({
"id": guild_id,
"name": guild.name,
"creator_id": *user_id,
"creator_id": user_id,
"avatar_url": guild.icon.map(|x| format!("https://cdn.discordapp.com/icons/{guild_id}/{x}.webp")),
"banner_url": guild.splash.map(|x| format!("https://cdn.discordapp.com/splashes/{guild_id}/{x}.webp?size=1032")),
"owner_user_id": *user_id
"owner_user_id": user_id
}))?
.await?;

Expand Down
4 changes: 2 additions & 2 deletions crates/mellow/src/commands/syncing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub async fn sync(_context: Context, interaction: Interaction) -> Result<Command
return Ok(CommandResponse::defer(
interaction.token.clone(),
Box::pin(async move {
sync_with_token(guild_id, *user_id, member_id, &interaction.token, false, None).await?;
sync_with_token(guild_id, user_id, member_id, &interaction.token, false, None).await?;
Ok(())
})
));
Expand Down Expand Up @@ -149,7 +149,7 @@ pub async fn forcesync(_context: Context, interaction: Interaction) -> Result<Co
let member_id = resolved.members.into_iter().next().unwrap().0;
if let Some(user_id) = HAKUMI_MODELS.user_by_discord(guild_id, member_id).await? {
return Ok(CommandResponse::defer(interaction.token.clone(), Box::pin(async move {
sync_with_token(guild_id, *user_id, member_id, &interaction.token, false, Some(interaction.user_id.unwrap())).await?;
sync_with_token(guild_id, user_id, member_id, &interaction.token, false, Some(interaction.user_id.unwrap())).await?;
Ok(())
})));
}
Expand Down
10 changes: 5 additions & 5 deletions crates/mellow/src/http/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ async fn sync_member(request: HttpRequest, body: web::Json<SyncMemberPayload>, p
let member_id: Id<UserMarker> = 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)
Expand Down Expand Up @@ -280,10 +280,10 @@ async fn patreon_webhook(payload: web::Payload) -> ApiResult<HttpResponse> {
.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"),
Expand Down
13 changes: 5 additions & 8 deletions crates/mellow/src/model/hakumi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub static HAKUMI_MODELS: Lazy<HakumiModels> = Lazy::new(HakumiModels::default);
pub struct HakumiModels {
pub users: DashMap<HakuId<HakuUserMarker>, User>,
//user_connections: DashMap<(HakuId<HakuUserMarker>, HakuId<ConnectionMarker>), Connection>,
pub users_by_discord: DashMap<(Id<GuildMarker>, Id<UserMarker>), HakuId<HakuUserMarker>>,
users_by_discord: DashMap<(Id<GuildMarker>, Id<UserMarker>), HakuId<HakuUserMarker>>,

pub vs_documents: DashMap<HakuId<DocumentMarker>, Document>
}
Expand Down Expand Up @@ -57,11 +57,11 @@ impl HakumiModels {
})
}

pub async fn user_by_discord(&self, guild_id: Id<GuildMarker>, user_id: Id<UserMarker>) -> Result<Option<Ref<'_, (Id<GuildMarker>, Id<UserMarker>), HakuId<HakuUserMarker>>>> {
pub async fn user_by_discord(&self, guild_id: Id<GuildMarker>, user_id: Id<UserMarker>) -> Result<Option<HakuId<HakuUserMarker>>> {
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,
Expand All @@ -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)
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/mellow/src/patreon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct IncludedItemAttributes {
patron_count: u64
}

pub async fn get_campaign(oauth_authorisation: &OAuthAuthorisation) -> Result<Campaign2> {
pub async fn get_campaign(oauth_authorisation: OAuthAuthorisation) -> Result<Campaign2> {
let access_token = &oauth_authorisation.access_token;
Ok(match CACHES.patreon_campaigns.get(access_token)
.instrument(info_span!("cache.patreon_campaigns.read", ?access_token))
Expand Down
11 changes: 8 additions & 3 deletions crates/mellow/src/visual_scripting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 68c1b41

Please sign in to comment.