mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can create group membership notifications
This commit is contained in:
		@@ -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!")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user