Skip to content

Commit

Permalink
Handle display name changes more efficiently (#6585)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Oct 15, 2024
1 parent 288e444 commit 63f071b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Ensure members marked as lapsed in updates queries ([#6573](https://github.com/open-chat-labs/open-chat/pull/6573))
- Reduce size of responses by only returning UserIds for basic members ([#6577](https://github.com/open-chat-labs/open-chat/pull/6577))
- Remove `transaction` from serialized PrizeWinner messages ([#6578](https://github.com/open-chat-labs/open-chat/pull/6578))
- Handle display name changes more efficiently ([#6585](https://github.com/open-chat-labs/open-chat/pull/6585))

## [[2.0.1378](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1378-community)] - 2024-10-10

Expand Down
22 changes: 21 additions & 1 deletion backend/canisters/community/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::jobs::import_groups::finalize_group_import;
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::{read_state, Data};
use crate::model::members::MemberUpdate;
use crate::{mutate_state, read_state, Data};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use community_canister::post_upgrade::Args;
Expand Down Expand Up @@ -29,6 +30,25 @@ fn post_upgrade(args: Args) {
finalize_group_import(group_id);
}

// TODO: Delete after release
mutate_state(|state| {
let updated_members: Vec<_> = state
.data
.members
.iter()
.map(|m| (m.user_id, m.display_name().timestamp))
.filter(|(_, ts)| *ts > 0)
.collect();

for (user_id, timestamp) in updated_members {
state
.data
.members
.updates
.insert((timestamp, user_id, MemberUpdate::DisplayNameChanged));
}
});

info!(version = %args.wasm_version, "Post-upgrade complete");

read_state(|state| {
Expand Down
14 changes: 5 additions & 9 deletions backend/canisters/community/impl/src/model/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const MAX_MEMBERS_PER_COMMUNITY: u32 = 100_000;
#[derive(Serialize, Deserialize)]
pub struct CommunityMembers {
members: HashMap<UserId, CommunityMemberInternal>,
display_names_last_updated: TimestampMillis,
user_groups: UserGroups,
// This includes the userIds of community members and also users invited to the community
principal_to_user_id_map: HashMap<Principal, UserId>,
blocked: HashSet<UserId>,
admin_count: u32,
owner_count: u32,
updates: BTreeSet<(TimestampMillis, UserId, MemberUpdate)>,
// TODO: Remove pub after release
#[serde(default)]
pub updates: BTreeSet<(TimestampMillis, UserId, MemberUpdate)>,
}

impl CommunityMembers {
Expand Down Expand Up @@ -50,7 +51,6 @@ impl CommunityMembers {

CommunityMembers {
members: vec![(creator_user_id, member)].into_iter().collect(),
display_names_last_updated: now,
user_groups: UserGroups::default(),
principal_to_user_id_map: vec![(creator_principal, creator_user_id)].into_iter().collect(),
blocked: HashSet::new(),
Expand Down Expand Up @@ -252,10 +252,6 @@ impl CommunityMembers {
self.user_groups.last_updated()
}

pub fn display_names_last_updated(&self) -> TimestampMillis {
self.display_names_last_updated
}

pub fn update_user_principal(&mut self, old_principal: Principal, new_principal: Principal) {
if let Some(user_id) = self.principal_to_user_id_map.remove(&old_principal) {
self.principal_to_user_id_map.insert(new_principal, user_id);
Expand Down Expand Up @@ -360,7 +356,7 @@ impl CommunityMembers {
pub fn set_display_name(&mut self, user_id: UserId, display_name: Option<String>, now: TimestampMillis) {
if let Some(member) = self.members.get_mut(&user_id) {
member.display_name = Timestamped::new(display_name, now);
self.display_names_last_updated = now;
self.updates.insert((now, user_id, MemberUpdate::DisplayNameChanged));
}
}

Expand Down Expand Up @@ -395,7 +391,6 @@ impl CommunityMembers {
pub fn last_updated(&self) -> TimestampMillis {
[
self.user_groups_last_updated(),
self.display_names_last_updated(),
self.updates.iter().next_back().map_or(0, |(ts, _, _)| *ts),
]
.into_iter()
Expand Down Expand Up @@ -552,4 +547,5 @@ impl From<&CommunityMemberInternal> for CommunityMember {
pub enum MemberUpdate {
Lapsed = 1,
Unlapsed = 2,
DisplayNameChanged = 3,
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,12 @@ fn selected_updates_impl(args: Args, state: &RuntimeState) -> Response {

for (user_id, update) in state.data.members.iter_latest_updates(args.updates_since) {
match update {
MemberUpdate::Lapsed | MemberUpdate::Unlapsed => {
MemberUpdate::Lapsed | MemberUpdate::Unlapsed | MemberUpdate::DisplayNameChanged => {
user_updates_handler.mark_member_updated(&mut result, user_id, false, false);
}
}
}

for member in data.members.iter() {
if member.display_name().timestamp > args.updates_since {
user_updates_handler.mark_member_updated(&mut result, member.user_id, false, false);
}
}

Success(result)
}

Expand Down

0 comments on commit 63f071b

Please sign in to comment.