From 83b6002d48b884f99752ff45ab86c1501c998a7a Mon Sep 17 00:00:00 2001 From: Matt Grogan Date: Thu, 6 Jun 2024 11:47:04 +0100 Subject: [PATCH] Changed `users` args to match `users_v2` args (#5913) --- backend/canisters/user_index/CHANGELOG.md | 1 + backend/canisters/user_index/api/can.did | 1 - .../user_index/api/src/queries/users.rs | 1 - .../user_index/impl/src/queries/users.rs | 63 ++++++++++--------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/backend/canisters/user_index/CHANGELOG.md b/backend/canisters/user_index/CHANGELOG.md index 561af6757a..2ff3108ce8 100644 --- a/backend/canisters/user_index/CHANGELOG.md +++ b/backend/canisters/user_index/CHANGELOG.md @@ -20,6 +20,7 @@ New `users` endpoint to handle volatile user data ([#5900](https://github.com/op - Add 14 day CHIT streak ([#5902](https://github.com/open-chat-labs/open-chat/pull/5902)) - Push `user_claimed_daily_chit` event ([#5906](https://github.com/open-chat-labs/open-chat/pull/5906)) +- Changed `users` args to match `users_v2` args ([#5913](https://github.com/open-chat-labs/open-chat/pull/5913)) ## [[2.0.1186](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1186-user_index)] - 2024-06-04 diff --git a/backend/canisters/user_index/api/can.did b/backend/canisters/user_index/api/can.did index 6376e84745..041cdeb21e 100644 --- a/backend/canisters/user_index/api/can.did +++ b/backend/canisters/user_index/api/can.did @@ -97,7 +97,6 @@ type UsersArgs = record { updated_since : TimestampMillis; }; users_suspended_since : opt TimestampMillis; - current_user_updated_since : opt TimestampMillis; }; type UsersResponse = variant { diff --git a/backend/canisters/user_index/api/src/queries/users.rs b/backend/canisters/user_index/api/src/queries/users.rs index 98bba6ca8f..2db300ec31 100644 --- a/backend/canisters/user_index/api/src/queries/users.rs +++ b/backend/canisters/user_index/api/src/queries/users.rs @@ -6,7 +6,6 @@ use types::{CurrentUserSummary, TimestampMillis, UserId, UserSummaryV2}; pub struct Args { pub user_groups: Vec, pub users_suspended_since: Option, - pub current_user_updated_since: Option, } #[derive(CandidType, Serialize, Deserialize, Debug)] diff --git a/backend/canisters/user_index/impl/src/queries/users.rs b/backend/canisters/user_index/impl/src/queries/users.rs index a343378c62..7fa06dca50 100644 --- a/backend/canisters/user_index/impl/src/queries/users.rs +++ b/backend/canisters/user_index/impl/src/queries/users.rs @@ -12,11 +12,43 @@ fn users(args: Args) -> Response { fn users_impl(args: Args, state: &RuntimeState) -> Response { let now = state.env.now(); + let caller = state.env.caller(); let mut user_ids = HashSet::new(); let mut users = Vec::new(); let mut current_user: Option = None; + if let Some(u) = state.data.users.get_by_principal(&caller) { + if let Some(ts) = args + .user_groups + .iter() + .find(|g| g.users.contains(&u.user_id)) + .map(|g| g.updated_since) + { + if u.date_updated > ts || u.date_updated_volatile > ts { + let suspension_details = u.suspension_details.as_ref().map(|d| d.into()); + + current_user = Some(CurrentUserSummary { + user_id: u.user_id, + username: u.username.clone(), + display_name: u.display_name.clone(), + avatar_id: u.avatar_id, + is_bot: u.is_bot, + is_platform_moderator: state.data.platform_moderators.contains(&u.user_id), + is_platform_operator: state.data.platform_operators.contains(&u.user_id), + suspension_details, + is_suspected_bot: state.data.users.is_suspected_bot(&u.user_id), + diamond_membership_details: u.diamond_membership_details.hydrate(now), + diamond_membership_status: u.diamond_membership_details.status_full(now), + moderation_flags_enabled: u.moderation_flags_enabled, + chit_balance: u.chit_balance, + streak: u.streak.days(now), + next_daily_claim: if u.streak.can_claim(now) { today(now) } else { tomorrow(now) }, + }); + } + } + } + for group in args.user_groups { let updated_since = group.updated_since; users.extend( @@ -24,7 +56,9 @@ fn users_impl(args: Args, state: &RuntimeState) -> Response { .users .into_iter() .filter_map(|u| state.data.users.get_by_user_id(&u)) - .filter(move |u| u.date_updated > updated_since || u.date_updated_volatile > updated_since) + .filter(move |u| { + (u.date_updated > updated_since || u.date_updated_volatile > updated_since) && u.principal != caller + }) .filter(|u| user_ids.insert(u.user_id)) .map(|u| UserSummaryV2 { user_id: u.user_id, @@ -48,33 +82,6 @@ fn users_impl(args: Args, state: &RuntimeState) -> Response { ); } - if let Some(ts) = args.current_user_updated_since { - let caller = state.env.caller(); - if let Some(u) = state.data.users.get_by_principal(&caller) { - if u.date_updated > ts || u.date_updated_volatile > ts { - let suspension_details = u.suspension_details.as_ref().map(|d| d.into()); - - current_user = Some(CurrentUserSummary { - user_id: u.user_id, - username: u.username.clone(), - display_name: u.display_name.clone(), - avatar_id: u.avatar_id, - is_bot: u.is_bot, - is_platform_moderator: state.data.platform_moderators.contains(&u.user_id), - is_platform_operator: state.data.platform_operators.contains(&u.user_id), - suspension_details, - is_suspected_bot: state.data.users.is_suspected_bot(&u.user_id), - diamond_membership_details: u.diamond_membership_details.hydrate(now), - diamond_membership_status: u.diamond_membership_details.status_full(now), - moderation_flags_enabled: u.moderation_flags_enabled, - chit_balance: u.chit_balance, - streak: u.streak.days(now), - next_daily_claim: if u.streak.can_claim(now) { today(now) } else { tomorrow(now) }, - }); - } - } - } - Success(Result { users, current_user,