Skip to content

Commit

Permalink
Move members to MembersMap in prep for stable memory (#6927)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 29, 2024
1 parent fc9e91e commit 5ef5e66
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 161 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Remove entries from expiring members queue when channel deleted ([#6924](https://github.com/open-chat-labs/open-chat/pull/6924))
- Reduce size of chat members when serialized ([#6925](https://github.com/open-chat-labs/open-chat/pull/6925))
- Consolidate member verification logic into `get_verified_member` ([#6926](https://github.com/open-chat-labs/open-chat/pull/6926))
- Move members to `MembersMap` in prep for stable memory ([#6927](https://github.com/open-chat-labs/open-chat/pull/6927))

## [[2.0.1479](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1479-community)] - 2024-11-28

Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/community/impl/src/jobs/import_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub(crate) async fn process_channel_members(group_id: ChatId, channel_id: Channe
let mut to_add: HashMap<UserId, UserType> = HashMap::new();

for user_id in channel.chat.members.member_ids().iter() {
if state.data.members.member_ids().contains(user_id) {
if state.data.members.contains(user_id) {
state.data.members.mark_member_joined_channel(*user_id, channel_id);
} else {
let user_type = bots.get(user_id).copied().unwrap_or_default();
Expand Down
8 changes: 4 additions & 4 deletions backend/canisters/community/impl/src/model/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl Channel {
let chat = &self.chat;
let member = user_id.and_then(|user_id| chat.members.get(&user_id));

let (min_visible_event_index, min_visible_message_index, is_invited) = if let Some(member) = member {
let (min_visible_event_index, min_visible_message_index, is_invited) = if let Some(member) = &member {
(member.min_visible_event_index(), member.min_visible_message_index(), None)
} else if let Some(invitation) = user_id.and_then(|user_id| chat.invited_users.get(&user_id)) {
(
Expand All @@ -264,7 +264,7 @@ impl Channel {
.and_then(|m| community_members.get_by_user_id(&m.event.sender))
.and_then(|m| m.display_name().value.clone());

let membership = member.map(|m| GroupMembership {
let membership = member.as_ref().map(|m| GroupMembership {
joined: m.date_added(),
role: m.role().value.into(),
mentions: chat.most_recent_mentions(m, None),
Expand Down Expand Up @@ -334,7 +334,7 @@ impl Channel {
let chat = &self.chat;
let member = user_id.and_then(|id| chat.members.get(&id));

if let Some(m) = member {
if let Some(m) = &member {
if m.date_added() > since {
return ChannelUpdates::Added(
self.summary(user_id, is_community_member, is_public_community, community_members)
Expand All @@ -353,7 +353,7 @@ impl Channel {
.and_then(|m| community_members.get_by_user_id(&m.event.sender))
.and_then(|m| m.display_name().value.clone());

let membership = member.map(|m| GroupMembershipUpdates {
let membership = member.as_ref().map(|m| GroupMembershipUpdates {
role: updates.role_changed.then_some(m.role().value.into()),
mentions: updates.mentions,
notifications_muted: m.notifications_muted().if_set_after(since).cloned(),
Expand Down
8 changes: 6 additions & 2 deletions backend/canisters/community/impl/src/model/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ impl CommunityMembers {
self.members.get_mut(user_id)
}

pub fn contains(&self, user_id: &UserId) -> bool {
self.member_ids.contains(user_id)
}

pub fn len(&self) -> u32 {
self.members.len() as u32
}
Expand Down Expand Up @@ -586,8 +590,8 @@ impl CommunityMembers {
impl Members for CommunityMembers {
type Member = CommunityMemberInternal;

fn get(&self, user_id: &UserId) -> Option<&CommunityMemberInternal> {
self.get_by_user_id(user_id)
fn get(&self, user_id: &UserId) -> Option<CommunityMemberInternal> {
self.get_by_user_id(user_id).cloned()
}

fn iter_members_who_can_lapse(&self) -> Box<dyn Iterator<Item = UserId> + '_> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{read_state, RuntimeState};
use canister_api_macros::query;
use community_canister::selected_channel_initial::{Response::*, *};
use std::collections::HashSet;
use types::GroupMember;

#[query(candid = true, msgpack = true)]
fn selected_channel_initial(args: Args) -> Response {
Expand Down Expand Up @@ -40,7 +41,7 @@ fn selected_channel_initial_impl(args: Args, state: &RuntimeState) -> Response {
for user_id in chat.members.member_ids().iter() {
if non_basic_members.contains(user_id) {
if let Some(member) = chat.members.get(user_id) {
members.push(member.into());
members.push(GroupMember::from(&member));
}
} else {
basic_members.push(*user_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn commit(channel_id: ChannelId, user_id: UserId, args: Args, state: &mut Runtim
{
RecordProposalVoteResult::Success => {
let now = state.env.now();
channel.chat.members.register_proposal_vote(user_id, args.message_index, now);
channel.chat.members.register_proposal_vote(&user_id, args.message_index, now);

handle_activity_notification(state);
Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn register_proposal_vote_impl(args: Args, state: &mut RuntimeState) -> Response
{
RecordProposalVoteResult::Success => {
let now = state.env.now();
channel.chat.members.register_proposal_vote(user_id, args.message_index, now);
channel.chat.members.register_proposal_vote(&user_id, args.message_index, now);

handle_activity_notification(state);
Success
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Reduce size of chat members when serialized ([#6925](https://github.com/open-chat-labs/open-chat/pull/6925))
- Consolidate member verification logic into `get_verified_member` ([#6926](https://github.com/open-chat-labs/open-chat/pull/6926))
- Move members to `MembersMap` in prep for stable memory ([#6927](https://github.com/open-chat-labs/open-chat/pull/6927))

## [[2.0.1480](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1480-group)] - 2024-11-28

Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/group/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl Data {
self.chat.members.contains(&user_id).then_some(user_id)
}

pub fn get_member(&self, user_id_or_principal: Principal) -> Option<&GroupMemberInternal> {
pub fn get_member(&self, user_id_or_principal: Principal) -> Option<GroupMemberInternal> {
let user_id = self
.principal_to_user_id_map
.get(&user_id_or_principal)
Expand Down
3 changes: 2 additions & 1 deletion backend/canisters/group/impl/src/queries/selected_initial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{read_state, RuntimeState};
use canister_api_macros::query;
use group_canister::selected_initial::{Response::*, *};
use std::collections::HashSet;
use types::GroupMember;

#[query(candid = true, msgpack = true)]
fn selected_initial(_args: Args) -> Response {
Expand All @@ -26,7 +27,7 @@ fn selected_initial_impl(state: &RuntimeState) -> Response {
for user_id in chat.members.member_ids().iter() {
if non_basic_members.contains(user_id) {
if let Some(member) = chat.members.get(user_id) {
members.push(member.into());
members.push(GroupMember::from(&member));
}
} else {
basic_members.push(*user_id);
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/group/impl/src/queries/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn summary_impl(on_behalf_of: Option<Principal>, state: &RuntimeState) -> Respon
};

if let Some(member) = state.data.get_member(caller) {
let summary = state.summary(member);
let summary = state.summary(&member);
Success(SuccessResult { summary })
} else {
CallerNotInGroup
Expand Down
4 changes: 2 additions & 2 deletions backend/canisters/group/impl/src/updates/c2c_join_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn is_permitted_to_join(

if let Some(member) = state.data.chat.members.get(&args.user_id) {
if !member.lapsed().value {
let summary = state.summary(member);
let summary = state.summary(&member);
return Err(AlreadyInGroupV2(Box::new(summary)));
}
} else if state.data.chat.members.is_blocked(&args.user_id) {
Expand Down Expand Up @@ -155,7 +155,7 @@ fn c2c_join_group_impl(args: Args, payments: Vec<GatePayment>, state: &mut Runti
state.data.chat.members.update_lapsed(args.user_id, false, now);

let member = state.data.chat.members.get(&args.user_id).unwrap();
let summary = state.summary(member);
let summary = state.summary(&member);
Success(Box::new(summary))
}
AddResult::Blocked => Blocked,
Expand Down
13 changes: 3 additions & 10 deletions backend/canisters/group/impl/src/updates/register_proposal_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use canister_api_macros::update;
use canister_tracing_macros::trace;
use chat_events::{MessageContentInternal, Reader, RecordProposalVoteResult};
use group_canister::register_proposal_vote::{Response::*, *};
use types::{CanisterId, ProposalId, UserId};
use types::{CanisterId, EventIndex, ProposalId, UserId};

#[update(candid = true, msgpack = true)]
#[trace]
Expand Down Expand Up @@ -93,26 +93,19 @@ fn prepare(args: &Args, state: &RuntimeState) -> Result<PrepareResult, Response>
}

fn commit(user_id: UserId, args: Args, state: &mut RuntimeState) -> Response {
let member = match state.data.chat.members.get_mut(&user_id) {
Some(p) => p,
None => return CallerNotInGroup,
};

let min_visible_event_index = member.min_visible_event_index();

match state
.data
.chat
.events
.record_proposal_vote(user_id, min_visible_event_index, args.message_index, args.adopt)
.record_proposal_vote(user_id, EventIndex::default(), args.message_index, args.adopt)
{
RecordProposalVoteResult::Success => {
let now = state.env.now();
state
.data
.chat
.members
.register_proposal_vote(user_id, args.message_index, now);
.register_proposal_vote(&user_id, args.message_index, now);

handle_activity_notification(state);
Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn register_proposal_vote_impl(args: Args, state: &mut RuntimeState) -> Response
.data
.chat
.members
.register_proposal_vote(user_id, args.message_index, now);
.register_proposal_vote(&user_id, args.message_index, now);

handle_activity_notification(state);
Success
Expand Down
Loading

0 comments on commit 5ef5e66

Please sign in to comment.