From ce9671789e82fc977c69034cb72eb1f4f4d55dcb Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Thu, 25 Jul 2024 16:00:42 +0100 Subject: [PATCH] Make `is_invited` optional --- backend/canisters/community/impl/src/lib.rs | 6 +++++- backend/canisters/community/impl/src/model/channels.rs | 10 +++++++--- .../canisters/community/impl/src/queries/summary.rs | 2 +- .../community/impl/src/updates/c2c_join_channel.rs | 2 +- .../community/impl/src/updates/c2c_join_community.rs | 6 +++--- backend/libraries/types/src/channel_summary.rs | 2 +- backend/libraries/types/src/community_summary.rs | 2 +- 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/backend/canisters/community/impl/src/lib.rs b/backend/canisters/community/impl/src/lib.rs index 8bb9015500..72cb632b1d 100644 --- a/backend/canisters/community/impl/src/lib.rs +++ b/backend/canisters/community/impl/src/lib.rs @@ -149,7 +149,11 @@ impl RuntimeState { jobs::make_pending_payments::start_job_if_required(self); } - pub fn summary(&self, member: Option<&CommunityMemberInternal>, is_invited: bool) -> CommunityCanisterCommunitySummary { + pub fn summary( + &self, + member: Option<&CommunityMemberInternal>, + is_invited: Option, + ) -> CommunityCanisterCommunitySummary { let data = &self.data; let (channels, membership) = if let Some(m) = member { diff --git a/backend/canisters/community/impl/src/model/channels.rs b/backend/canisters/community/impl/src/model/channels.rs index 170fe3d509..9d88d7ffe3 100644 --- a/backend/canisters/community/impl/src/model/channels.rs +++ b/backend/canisters/community/impl/src/model/channels.rs @@ -226,12 +226,16 @@ impl Channel { 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 { - (member.min_visible_event_index(), member.min_visible_message_index(), false) + (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)) { - (invitation.min_visible_event_index, invitation.min_visible_message_index, true) + ( + invitation.min_visible_event_index, + invitation.min_visible_message_index, + Some(true), + ) } else if chat.is_public.value { let (e, m) = chat.min_visible_indexes_for_new_members.unwrap_or_default(); - (e, m, false) + (e, m, Some(false)) } else { return None; }; diff --git a/backend/canisters/community/impl/src/queries/summary.rs b/backend/canisters/community/impl/src/queries/summary.rs index 35a6f73adf..4ce18bf38c 100644 --- a/backend/canisters/community/impl/src/queries/summary.rs +++ b/backend/canisters/community/impl/src/queries/summary.rs @@ -30,7 +30,7 @@ fn summary_impl(invite_code: Option, on_behalf_of: Option, state let member = state.data.members.get(caller); let is_invited = state.data.is_invited(caller); - let summary = state.summary(member, is_invited); + let summary = state.summary(member, Some(is_invited)); Success(summary) } diff --git a/backend/canisters/community/impl/src/updates/c2c_join_channel.rs b/backend/canisters/community/impl/src/updates/c2c_join_channel.rs index 553b11055d..2023aa5961 100644 --- a/backend/canisters/community/impl/src/updates/c2c_join_channel.rs +++ b/backend/canisters/community/impl/src/updates/c2c_join_channel.rs @@ -41,7 +41,7 @@ async fn c2c_join_channel(args: Args) -> Response { if matches!(response, Success(_) | AlreadyInChannel(_)) { let summary = read_state(|state| { let member = state.data.members.get_by_user_id(&args.user_id); - state.summary(member, false) + state.summary(member, None) }); SuccessJoinedCommunity(Box::new(summary)) } else { diff --git a/backend/canisters/community/impl/src/updates/c2c_join_community.rs b/backend/canisters/community/impl/src/updates/c2c_join_community.rs index e98b9bc85d..661492df1a 100644 --- a/backend/canisters/community/impl/src/updates/c2c_join_community.rs +++ b/backend/canisters/community/impl/src/updates/c2c_join_community.rs @@ -41,7 +41,7 @@ pub(crate) async fn join_community(args: Args) -> Response { } read_state(|state| { if let Some(member) = state.data.members.get_by_user_id(&args.user_id) { - Success(Box::new(state.summary(Some(member), false))) + Success(Box::new(state.summary(Some(member), None))) } else { InternalError("User not found in community".to_string()) } @@ -58,7 +58,7 @@ fn is_permitted_to_join(args: &Args, state: &RuntimeState) -> Result Resu } AddResult::AlreadyInCommunity => { let member = state.data.members.get_by_user_id(&args.user_id).unwrap(); - let summary = state.summary(Some(member), false); + let summary = state.summary(Some(member), None); Err(AlreadyInCommunity(Box::new(summary))) } AddResult::Blocked => Err(UserBlocked), diff --git a/backend/libraries/types/src/channel_summary.rs b/backend/libraries/types/src/channel_summary.rs index 7e481b5c3f..4d6d37bcd0 100644 --- a/backend/libraries/types/src/channel_summary.rs +++ b/backend/libraries/types/src/channel_summary.rs @@ -30,7 +30,7 @@ pub struct CommunityCanisterChannelSummary { pub gate: Option, pub membership: Option, pub video_call_in_progress: Option, - pub is_invited: bool, + pub is_invited: Option, } #[derive(CandidType, Serialize, Deserialize, Clone, Debug)] diff --git a/backend/libraries/types/src/community_summary.rs b/backend/libraries/types/src/community_summary.rs index 9d16aefb18..5580ea19bb 100644 --- a/backend/libraries/types/src/community_summary.rs +++ b/backend/libraries/types/src/community_summary.rs @@ -25,7 +25,7 @@ pub struct CommunityCanisterCommunitySummary { pub channels: Vec, pub membership: Option, pub user_groups: Vec, - pub is_invited: bool, + pub is_invited: Option, pub metrics: ChatMetrics, }