From bb06760437a8b4178fa09993d4a663af71474e53 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Jan 2021 15:30:14 +0100 Subject: [PATCH] Can create group membership notifications --- src/controllers/groups_controller.rs | 7 +++- src/data/group_member.rs | 7 ++++ src/helpers/notifications_helper.rs | 60 +++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index f586061..2e01183 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -16,7 +16,8 @@ use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_group::NewGroup; -use crate::helpers::{groups_helper, virtual_directory_helper}; +use crate::data::notification::NotifEventType; +use crate::helpers::{groups_helper, notifications_helper, virtual_directory_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::utils::date_utils::time; @@ -186,7 +187,9 @@ pub fn invite_user(r: &mut HttpRequestHandler) -> RequestResult { groups_helper::send_invitation(&group_id, &user_id)?; - // TODO : send a notification + // Send a notification + notifications_helper::create_group_membership_notification( + &user_id, Some(r.user_id_ref()?), &group_id, NotifEventType::SENT_GROUP_MEMBERSHIP_INVITATION)?; r.success("The user has been successfully invited to join the group!") } diff --git a/src/data/group_member.rs b/src/data/group_member.rs index 9b922b7..711ed72 100644 --- a/src/data/group_member.rs +++ b/src/data/group_member.rs @@ -53,6 +53,13 @@ pub struct GroupMember { pub following: bool, } +impl GroupMember { + /// Check if a member of a group is a least a moderator of this group + pub fn is_moderator(&self) -> bool { + self.level <= GroupMembershipLevel::MODERATOR + } +} + #[cfg(test)] mod tests { use crate::data::group_member::GroupMembershipLevel; diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index 7d2b6c1..58e9c66 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -37,6 +37,33 @@ pub fn create_friends_notification(from_user: &UserID, dest_user: &UserID, actio push(&mut n) } +/// Create & push a group membership notification +pub fn create_group_membership_notification(user_id: &UserID, moderator_id: Option<&UserID>, group_id: &GroupID, kind: NotifEventType) -> ResultBoxError { + // TODO : Delete related group membership notifications + + let mut n = PartialNotification::new() + .set_on_elem_id(group_id.id()) + .set_on_elem_type(NotifElemType::GROUP_MEMBERSHIP) + .set_type(kind); + + match moderator_id { + + // The notification must be sent to all the moderators of the group + None => { + n = n.set_from_user_id(user_id); + } + + // We specify both the source and the destination of the notification + // not to broadcast the notification to all the group members + Some(moderator_id) => { + n = n.set_from_user_id(moderator_id) + .set_dest_user_id(user_id); + } + } + + push(&mut n) +} + /// Push a new notification fn push(n: &mut PartialNotification) -> ResultBoxError { @@ -119,18 +146,49 @@ fn push(n: &mut PartialNotification) -> ResultBoxError n.container_type = None; return push_private(n); + } + + + // Groups membership notifications + else if matches!(n.on_elem_type, Some(NotifElemType::GROUP_MEMBERSHIP)) { + + // Complete the notification + n.container_type = None; + n.container_id = None; + + // Check whether the notification has to be pushed to a single user + // or to all the moderators of the group + return if let Some(_) = n.dest_user_id + { + // Push the notification in private way (if it has a destination, + // generally the target user of the membership request) + push_private(n) + } else { + push_group_moderators(n, &GroupID::new(n.on_elem_id.unwrap())) + }; } else { unimplemented!(); } } /// Push a notification to group members -pub fn push_group_members(n: &mut PartialNotification, group_id: &GroupID) -> ResultBoxError { +fn push_group_members(n: &mut PartialNotification, group_id: &GroupID) -> ResultBoxError { let mut list = groups_helper::get_list_followers(group_id)?; list = list.into_iter().filter(|f| f != n.from_user_id.as_ref().unwrap()).collect(); push_public(n, list) } +/// Push a notification to all the moderators & administrators of a group +fn push_group_moderators(n: &mut PartialNotification, group_id: &GroupID) -> ResultBoxError { + let list = groups_helper::get_list_members(group_id)?; + let list: Vec = list + .into_iter() + .filter(|e| e.is_moderator()) + .map(|f| f.user_id) + .collect(); + push_public(n, list) +} + /// Push a public notification fn push_public(n: &mut PartialNotification, users: Vec) -> ResultBoxError { n.visibility = Some(NotifEventVisibility::EVENT_PUBLIC);