1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Can create group membership notifications

This commit is contained in:
2021-01-23 15:30:14 +01:00
parent fd5000aef9
commit bb06760437
3 changed files with 71 additions and 3 deletions

View File

@ -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<UserID> = 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<UserID>) -> ResultBoxError {
n.visibility = Some(NotifEventVisibility::EVENT_PUBLIC);