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

Commit

Permalink
track user connection usage
Browse files Browse the repository at this point in the history
  • Loading branch information
katsumi143 committed Dec 18, 2023
1 parent 05db5bd commit a53ca52
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 8 deletions.
71 changes: 71 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mellow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ hex = "0.4.3"
log = "0.4.20"
tokio = { version = "1.34.0", features = ["full"] }
serde = { version = "1.0.193", features = ["derive"] }
chrono = "0.4.31"
reqwest = { version = "0.11.22", features = ["json"] }
actix-web = "4.4.0"
postgrest = "1.6.0"
Expand Down
5 changes: 3 additions & 2 deletions mellow/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ impl UserConnection {
}
}

#[derive(Deserialize, Clone, Debug)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct UserServerConnection {
pub id: String,
pub connection: UserConnection
}

Expand All @@ -59,7 +60,7 @@ pub struct UserResponse {

pub async fn get_users_by_discord(ids: Vec<String>, server_id: String) -> Vec<UserResponse> {
serde_json::from_str(&DATABASE.from("user_connections")
.select("sub,user:users(id,connections:mellow_user_server_connections(connection:user_connections(sub,type,username,display_name)))")
.select("sub,user:users(id,connections:mellow_user_server_connections(id,connection:user_connections(sub,type,username,display_name)))")
.in_("sub", ids)
.eq("users.mellow_user_server_connections.server_id", server_id)
.execute().await.unwrap().text().await.unwrap()
Expand Down
1 change: 1 addition & 0 deletions mellow/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub async fn send_logs(server: &Server, logs: Vec<Log>) {
embeds.push(Embed {
title: Some(format!("{} synced their profile", data.member.user.global_name.clone().unwrap_or(data.member.user.username))),
author: Some(EmbedAuthor {
url: Some(format!("https://hakumi.cafe/mellow/server/{}/member/{}", server.id, data.member.user.id)),
name: data.member.user.global_name,
icon_url: data.member.avatar.or(data.member.user.avatar).map(|x| format!("https://cdn.discordapp.com/avatars/{}/{x}.webp?size=48", data.member.user.id)),
..Default::default()
Expand Down
25 changes: 19 additions & 6 deletions mellow/src/syncing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use async_recursion::async_recursion;
use crate::{
roblox::get_user_group_roles,
discord::{ DiscordRole, DiscordMember, DiscordModifyMemberPayload, modify_member, get_guild_roles },
database::{ User, Server, UserResponse, UserConnection, ProfileSyncAction, UserConnectionKind, ProfileSyncActionKind, ProfileSyncActionRequirementKind, ProfileSyncActionRequirementsKind, get_server }
database::{ User, Server, UserResponse, UserConnection, ProfileSyncAction, UserConnectionKind, UserServerConnection, ProfileSyncActionKind, ProfileSyncActionRequirementKind, ProfileSyncActionRequirementsKind, DATABASE, get_server }
};

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -110,7 +110,7 @@ pub async fn sync_member(user: Option<&User>, member: &DiscordMember, server: &S
let mut roles = member.roles.clone();
let mut role_changes: Vec<RoleChange> = vec![];
let mut requirement_cache: HashMap<String, bool> = HashMap::new();
let mut used_connections: Vec<UserConnection> = vec![];
let mut used_connections: Vec<UserServerConnection> = vec![];

let actions2 = server.actions.clone();
for action in server.actions.iter() {
Expand Down Expand Up @@ -174,12 +174,25 @@ pub async fn sync_member(user: Option<&User>, member: &DiscordMember, server: &S
}).await;
}

if !used_connections.is_empty() {
let connections = used_connections.clone();
tokio::spawn(async move {
DATABASE
.from("mellow_user_server_connections")
.update(format!(r#"{{ "last_used_at": "{}" }}"#, chrono::Local::now()))
.in_("id", connections.iter().map(|x| x.id.clone()))
.execute()
.await
.unwrap();
});
}

SyncMemberResult {
server: server.clone(),
role_changes,
profile_changed,
nickname_change,
relevant_connections: used_connections
relevant_connections: used_connections.into_iter().map(|x| x.connection).collect()
}
}

Expand All @@ -190,7 +203,7 @@ pub async fn member_meets_action_requirements(
all_actions: &Vec<ProfileSyncAction>,
connection_metadata: &ConnectionMetadata,
cache: &mut HashMap<String, bool>,
used_connections: &mut Vec<UserConnection>
used_connections: &mut Vec<UserServerConnection>
) -> bool {
let mut total_met = 0;
let requires_one = matches!(action.requirements_type, ProfileSyncActionRequirementsKind::MeetOne);
Expand All @@ -202,8 +215,8 @@ pub async fn member_meets_action_requirements(
ProfileSyncActionRequirementKind::RobloxHaveGroupRankInRange => {
let connection = user.and_then(|x| x.connections.iter().find(|x| matches!(x.connection.kind, UserConnectionKind::Roblox)));
if let Some(connection) = connection.cloned() {
if !used_connections.contains(&connection.connection) {
used_connections.push(connection.connection);
if !used_connections.contains(&connection) {
used_connections.push(connection);
}
}

Expand Down

0 comments on commit a53ca52

Please sign in to comment.