Skip to content

Commit

Permalink
Make inner fields of GroupMemberInternal private and expose via get…
Browse files Browse the repository at this point in the history
…ters (#6852)
  • Loading branch information
hpeebles authored Nov 20, 2024
1 parent d4cd53d commit 7ff42b1
Show file tree
Hide file tree
Showing 51 changed files with 205 additions and 167 deletions.
6 changes: 3 additions & 3 deletions backend/canisters/community/impl/src/jobs/import_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(crate) fn finalize_group_import(group_id: ChatId) {
chat.events
.set_chat(Chat::Channel(state.env.canister_id().into(), channel_id));

let blocked: Vec<_> = chat.members.blocked.iter().copied().collect();
let blocked: Vec<_> = chat.members.blocked();
if !blocked.is_empty() {
let mut blocked_from_community = Vec::new();

Expand Down Expand Up @@ -210,7 +210,7 @@ pub(crate) async fn process_channel_members(group_id: ChatId, channel_id: Channe
let (members_to_add_to_community, local_user_index_canister_id) = mutate_state(|state| {
let channel = state.data.channels.get(&channel_id).unwrap();
let mut to_add: HashMap<UserId, UserType> = HashMap::new();
for (user_id, user_type) in channel.chat.members.iter().map(|m| (m.user_id, m.user_type)) {
for (user_id, user_type) in channel.chat.members.iter().map(|m| (m.user_id(), m.user_type())) {
if let Some(member) = state.data.members.get_by_user_id_mut(&user_id) {
member.channels.insert(channel_id);
} else {
Expand Down Expand Up @@ -333,7 +333,7 @@ pub(crate) fn mark_import_complete(group_id: ChatId, channel_id: ChannelId) {
},
group_id,
group_name: channel.chat.name.value.clone(),
members: channel.chat.members.iter().map(|m| m.user_id).collect(),
members: channel.chat.members.iter().map(|m| m.user_id()).collect(),
other_public_channels: state
.data
.channels
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl Data {
.members
.iter()
.filter(|m| m.can_member_lapse())
.map(|m| m.user_id)
.map(|m| m.user_id())
.collect();
}
} else {
Expand Down
16 changes: 8 additions & 8 deletions backend/canisters/community/impl/src/model/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ impl Channel {
.and_then(|m| m.display_name().value.clone());

let membership = member.map(|m| GroupMembership {
joined: m.date_added,
role: m.role.value.into(),
joined: m.date_added(),
role: m.role().value.into(),
mentions: m.most_recent_mentions(None, &chat.events),
notifications_muted: m.notifications_muted.value,
my_metrics: chat
.events
.user_metrics(&m.user_id, None)
.user_metrics(&m.user_id(), None)
.map(|m| m.hydrate())
.unwrap_or_default(),
latest_threads: m
Expand All @@ -285,7 +285,7 @@ impl Channel {
.rules_accepted
.as_ref()
.map_or(false, |version| version.value >= chat.rules.text.version),
lapsed: m.lapsed.value,
lapsed: m.lapsed().value,
});

Some(CommunityCanisterChannelSummary {
Expand Down Expand Up @@ -335,7 +335,7 @@ impl Channel {
let member = user_id.and_then(|id| chat.members.get(&id));

if let Some(m) = member {
if m.date_added > since {
if m.date_added() > since {
return ChannelUpdates::Added(
self.summary(user_id, is_community_member, is_public_community, community_members)
.expect("Channel should be accessible"),
Expand All @@ -354,10 +354,10 @@ impl Channel {
.and_then(|m| m.display_name().value.clone());

let membership = member.map(|m| GroupMembershipUpdates {
role: updates.role_changed.then_some(m.role.value.into()),
role: updates.role_changed.then_some(m.role().value.into()),
mentions: updates.mentions,
notifications_muted: m.notifications_muted.if_set_after(since).cloned(),
my_metrics: self.chat.events.user_metrics(&m.user_id, Some(since)).map(|m| m.hydrate()),
my_metrics: self.chat.events.user_metrics(&m.user_id(), Some(since)).map(|m| m.hydrate()),
latest_threads: m
.followed_threads
.updated_since(since)
Expand All @@ -370,7 +370,7 @@ impl Channel {
.as_ref()
.filter(|accepted| updates.rules_changed || accepted.timestamp > since)
.map(|accepted| accepted.value >= chat.rules.text.version),
lapsed: m.lapsed.if_set_after(since).copied(),
lapsed: m.lapsed().if_set_after(since).copied(),
});

ChannelUpdates::Updated(CommunityCanisterChannelSummaryUpdates {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn can_start_video_call(
call_type: VideoCallType,
chat: &GroupChatCore,
) -> bool {
if !member.role.is_permitted(chat.permissions.start_video_call) {
if !member.role().is_permitted(chat.permissions.start_video_call) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn accept_p2p_swap(args: Args) -> Response {
.chat
.members
.get(&message.sender)
.map_or(false, |m| !m.user_type.is_bot())
.map_or(false, |m| !m.user_type().is_bot())
{
let community_id = state.env.canister_id().into();

Expand Down Expand Up @@ -121,7 +121,7 @@ fn reserve_p2p_swap(args: Args, state: &mut RuntimeState) -> Result<ReserveP2PSw
_ => return Err(Box::new(UserNotInChannel)),
};

if channel_member.lapsed.value {
if channel_member.lapsed().value {
return Err(Box::new(UserLapsed));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ fn prepare(args: &Args, state: &RuntimeState) -> Result<PrepareResult, Response>
Err(UserLimitReached(limit))
} else if let Some(channel_member) = channel.chat.members.get(&user_id) {
let permissions = &channel.chat.permissions;
if !channel_member.role.can_add_members(permissions) {
if !channel_member.role().can_add_members(permissions) {
return Err(NotAuthorized);
} else if channel_member.lapsed.value {
} else if channel_member.lapsed().value {
return Err(UserLapsed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn add_reaction_impl(args: Args, state: &mut RuntimeState) -> Response {
args.message_id.into(),
) {
if let Some(sender) = channel.chat.members.get(&message.sender) {
if message.sender != user_id && !sender.user_type.is_bot() {
if message.sender != user_id && !sender.user_type().is_bot() {
let community_id = state.env.canister_id().into();

state.data.user_event_sync_queue.push(
Expand Down
11 changes: 8 additions & 3 deletions backend/canisters/community/impl/src/updates/c2c_join_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,15 @@ fn is_permitted_to_join(

if let Some(channel) = state.data.channels.get(&channel_id) {
if let Some(channel_member) = channel.chat.members.get(&member.user_id) {
if !member.lapsed() && !channel_member.lapsed() {
if !member.lapsed() && !channel_member.lapsed().value {
return Err(AlreadyInChannel(Box::new(
channel
.summary(Some(channel_member.user_id), true, state.data.is_public, &state.data.members)
.summary(
Some(channel_member.user_id()),
true,
state.data.is_public,
&state.data.members,
)
.unwrap(),
)));
}
Expand Down Expand Up @@ -335,7 +340,7 @@ pub(crate) fn join_channel_unchecked(

if matches!(result, AddResult::AlreadyInGroup) {
let member = channel.chat.members.get(&community_member.user_id).unwrap();
if member.lapsed() {
if member.lapsed().value {
return AddResult::Success(AddMemberSuccess {
member: member.clone(),
unlapse: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {
args.message_id.into(),
) {
if let Some(sender) = channel.chat.members.get(&message.sender) {
if message.sender != user_id && !sender.user_type.is_bot() {
if message.sender != user_id && !sender.user_type().is_bot() {
let community_id = state.env.canister_id().into();

state.data.user_event_sync_queue.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ fn delete_channel_impl(channel_id: ChannelId, state: &mut RuntimeState) -> Respo
return UserNotInChannel;
};

if channel_member.lapsed.value {
if channel_member.lapsed().value {
return UserLapsed;
} else if !channel_member.role.can_delete_group() {
} else if !channel_member.role().can_delete_group() {
return NotAuthorized;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ fn delete_channel_impl(channel_id: ChannelId, state: &mut RuntimeState) -> Respo
now,
);

for user_id in channel.chat.members.iter().map(|m| m.user_id) {
for user_id in channel.chat.members.iter().map(|m| m.user_id()) {
state.data.members.mark_member_left_channel(&user_id, channel_id, now);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn edit_message_impl(args: Args, state: &mut RuntimeState) -> Response {
return UserNotInChannel;
};

if channel_member.lapsed.value {
if channel_member.lapsed().value {
return UserLapsed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn register_poll_vote_impl(args: Args, state: &mut RuntimeState) -> Response {
None => return UserNotInChannel,
};

if channel_member.lapsed.value {
if channel_member.lapsed().value {
return UserLapsed;
}

Expand All @@ -65,7 +65,7 @@ fn register_poll_vote_impl(args: Args, state: &mut RuntimeState) -> Response {
match result {
RegisterPollVoteResult::Success(votes, creator) => {
if creator != user_id {
if channel.chat.members.get(&creator).map_or(false, |m| !m.user_type.is_bot()) {
if channel.chat.members.get(&creator).map_or(false, |m| !m.user_type().is_bot()) {
if let Some((message, event_index)) = channel.chat.events.message_internal(
EventIndex::default(),
args.thread_root_message_index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn prepare(args: &Args, state: &RuntimeState) -> Result<PrepareResult, Response>
None => return Err(UserNotInChannel),
};

if channel_member.lapsed.value {
if channel_member.lapsed().value {
return Err(UserLapsed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn register_proposal_vote_impl(args: Args, state: &mut RuntimeState) -> Response
None => return UserNotInChannel,
};

if channel_member.lapsed.value {
if channel_member.lapsed().value {
return UserLapsed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ fn build_c2c_args(args: &Args, state: &RuntimeState) -> Result<(c2c_report_messa

if channel_member.suspended.value {
return Err(UserSuspended);
} else if channel_member.lapsed.value {
} else if channel_member.lapsed().value {
return Err(UserLapsed);
}

if args.delete && !channel_member.role.can_delete_messages(&chat.permissions) {
if args.delete && !channel_member.role().can_delete_messages(&chat.permissions) {
return Err(NotAuthorized);
}

Expand Down
4 changes: 2 additions & 2 deletions backend/canisters/community/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn process_send_message_result(

if let Some(channel) = state.data.channels.get(&channel_id) {
for user_id in users_mentioned.all_users_mentioned {
if user_id != sender && channel.chat.members.get(&user_id).map_or(false, |m| !m.user_type.is_bot()) {
if user_id != sender && channel.chat.members.get(&user_id).map_or(false, |m| !m.user_type().is_bot()) {
activity_events.push((user_id, MessageActivity::Mention));
}
}
Expand All @@ -306,7 +306,7 @@ fn process_send_message_result(
.chat
.members
.get(&message.sender)
.map_or(false, |m| !m.user_type.is_bot())
.map_or(false, |m| !m.user_type().is_bot())
{
activity_events.push((message.sender, MessageActivity::QuoteReply));
}
Expand Down
18 changes: 9 additions & 9 deletions backend/canisters/group/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ impl RuntimeState {
.chat
.members
.iter()
.filter(|m| matches!(m.role.value, GroupRoleInternal::Owner))
.map(|m| m.user_id)
.filter(|m| matches!(m.role().value, GroupRoleInternal::Owner))
.map(|m| m.user_id())
.collect();

let owner_count = owners.len() as u128;
Expand Down Expand Up @@ -174,13 +174,13 @@ impl RuntimeState {
let events_ttl = chat.events.get_events_time_to_live();

let membership = GroupMembership {
joined: member.date_added,
role: member.role.value.into(),
joined: member.date_added(),
role: member.role().value.into(),
mentions: member.most_recent_mentions(None, &chat.events),
notifications_muted: member.notifications_muted.value,
my_metrics: chat
.events
.user_metrics(&member.user_id, None)
.user_metrics(&member.user_id(), None)
.map(|m| m.hydrate())
.unwrap_or_default(),
latest_threads: member
Expand All @@ -194,13 +194,13 @@ impl RuntimeState {
.rules_accepted
.as_ref()
.map_or(false, |version| version.value >= chat.rules.text.version),
lapsed: member.lapsed.value,
lapsed: member.lapsed().value,
};

GroupCanisterGroupChatSummary {
chat_id: self.env.canister_id().into(),
local_user_index_canister_id: self.data.local_user_index_canister_id,
last_updated: chat.last_updated(Some(member.user_id)),
last_updated: chat.last_updated(Some(member.user_id())),
name: chat.name.value.clone(),
description: chat.description.value.clone(),
subtype: chat.subtype.value.clone(),
Expand All @@ -210,7 +210,7 @@ impl RuntimeState {
messages_visible_to_non_members: chat.messages_visible_to_non_members.value,
min_visible_event_index,
min_visible_message_index,
latest_message: main_events_reader.latest_message_event(Some(member.user_id)),
latest_message: main_events_reader.latest_message_event(Some(member.user_id())),
latest_event_index: main_events_reader.latest_event_index().unwrap_or_default(),
latest_message_index: main_events_reader.latest_message_index(),
joined: membership.joined,
Expand Down Expand Up @@ -586,7 +586,7 @@ impl Data {
}

pub fn lookup_user_id(&self, user_id_or_principal: Principal) -> Option<UserId> {
self.get_member(user_id_or_principal).map(|m| m.user_id)
self.get_member(user_id_or_principal).map(|m| m.user_id())
}

pub fn get_member(&self, user_id_or_principal: Principal) -> Option<&GroupMemberInternal> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn c2c_can_issue_access_token_impl(args: Args, state: &RuntimeState) -> bool {
}

fn can_start_video_call(member: &GroupMemberInternal, call_type: VideoCallType, chat: &GroupChatCore) -> bool {
if !member.role.is_permitted(chat.permissions.start_video_call) {
if !member.role().is_permitted(chat.permissions.start_video_call) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn c2c_name_and_members(_args: Args) -> Response {
}

fn c2c_name_and_members_impl(state: &RuntimeState) -> Response {
let members = state.data.chat.members.iter().map(|p| p.user_id).collect();
let members = state.data.chat.members.iter().map(|p| p.user_id()).collect();

Success(SuccessResult {
name: state.data.chat.name.value.clone(),
Expand Down
2 changes: 1 addition & 1 deletion backend/canisters/group/impl/src/queries/invite_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn invite_code(_: Args) -> Response {
fn invite_code_impl(state: &RuntimeState) -> Response {
let caller = state.env.caller();
if let Some(member) = state.data.get_member(caller) {
if member.role.can_invite_users(&state.data.chat.permissions) {
if member.role().can_invite_users(&state.data.chat.permissions) {
return Success(SuccessResult {
code: if state.data.invite_code_enabled { state.data.invite_code } else { None },
});
Expand Down
Loading

0 comments on commit 7ff42b1

Please sign in to comment.