Skip to content

Commit

Permalink
Changed users args to match users_v2 args (#5913)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Jun 6, 2024
1 parent 803ef8b commit 83b6002
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ type UsersArgs = record {
updated_since : TimestampMillis;
};
users_suspended_since : opt TimestampMillis;
current_user_updated_since : opt TimestampMillis;
};

type UsersResponse = variant {
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/src/queries/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use types::{CurrentUserSummary, TimestampMillis, UserId, UserSummaryV2};
pub struct Args {
pub user_groups: Vec<UserGroup>,
pub users_suspended_since: Option<TimestampMillis>,
pub current_user_updated_since: Option<TimestampMillis>,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
63 changes: 35 additions & 28 deletions backend/canisters/user_index/impl/src/queries/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,53 @@ 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<CurrentUserSummary> = 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(
group
.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,
Expand All @@ -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,
Expand Down

0 comments on commit 83b6002

Please sign in to comment.