From fbc1a598290e389199531fe72ddd9510d3ab9fe1 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 5 Apr 2021 14:18:45 +0200 Subject: [PATCH] Automatically update conversations membership based on group membership update --- src/controllers/groups_controller.rs | 2 -- src/helpers/conversations_helper.rs | 25 +++++++++++++++++++++++++ src/helpers/groups_helper.rs | 27 ++++++++++++++++++++------- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 0579fbd..5c3e95e 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -314,8 +314,6 @@ pub fn send_request(r: &mut HttpRequestHandler) -> RequestResult { following: true, })?; - // TODO : if the user has already the "member" level, gives him access to conversations - // Send a notification, if required if matches!(group.registration_level, GroupRegistrationLevel::MODERATED_REGISTRATION) { notifications_helper::create_group_membership_notification(r.user_id_ref()?, None, diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index aecf375..6efe89b 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -132,6 +132,13 @@ pub fn get_list_user(user_id: &UserID) -> ResultBoxError> { .exec(db_to_conversation_info) } +/// Get the list of conversations of a group +pub fn get_list_group(group_id: &GroupID) -> Res> { + database::QueryInfo::new(CONV_LIST_TABLE) + .cond_group_id("group_id", group_id) + .exec(db_to_conversation_info) +} + /// Get information about a single conversation pub fn get_single(conv_id: ConvID) -> ResultBoxError { // Tables @@ -469,6 +476,15 @@ pub fn remove_user_from_conversation(user_id: &UserID, conv: &Conversation, remo } } +/// Update members list for all the conversations of a given group +pub fn update_members_list_for_group_conversations(group_id: &GroupID) -> Res { + for conv in get_list_group(group_id)? { + update_members_list_for_group_conversation(conv.id)?; + } + + Ok(()) +} + /// Update the list of members for a group conversation pub fn update_members_list_for_group_conversation(conv_id: ConvID) -> Res { let conv = get_single(conv_id)?; @@ -537,6 +553,15 @@ pub fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> { Ok(()) } +/// Delete all the conversations of a group +pub fn delete_all_group_conversations(group_id: &GroupID) -> Res { + for conv in get_list_group(group_id)? { + delete_conversation(&conv)?; + } + + Ok(()) +} + /// Delete a conversation membership pub fn remove_member(user_id: &UserID, conv_id: ConvID, remover: Option<&UserID>) -> ResultBoxError<()> { for msg in get_user_messages_for_conversations(conv_id, user_id)? { diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index f0c39d7..dbe77b6 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -9,7 +9,7 @@ use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::new_group::NewGroup; use crate::data::user::UserID; -use crate::helpers::{database, likes_helper, notifications_helper, posts_helper}; +use crate::helpers::{database, likes_helper, notifications_helper, posts_helper, conversations_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::time; @@ -123,7 +123,13 @@ pub fn insert_member(m: &GroupMember) -> ResultBoxError<()> { .add_user_id("user_id", &m.user_id) .add_u64("time_create", m.time_create) .add_u32("level", m.level.to_db()) - .insert_drop_result() + .insert_drop_result()?; + + if m.level.is_at_least_member() { + conversations_helper::update_members_list_for_group_conversations(&m.group_id)?; + } + + Ok(()) } /// Remove a user's membership @@ -131,9 +137,12 @@ pub fn delete_member(group_id: &GroupID, user_id: &UserID) -> ResultBoxError { database::DeleteQuery::new(GROUPS_MEMBERS_TABLE) .cond_group_id("groups_id", group_id) .cond_user_id("user_id", user_id) - .exec() + .exec()?; - // TODO : Update access to group's conversations + // Update access to group's conversations + conversations_helper::update_members_list_for_group_conversations(&group_id)?; + + Ok(()) } /// Update a user's membership level @@ -142,9 +151,12 @@ pub fn update_membership_level(group_id: &GroupID, user_id: &UserID, new_level: .cond_user_id("user_id", user_id) .cond_group_id("groups_id", group_id) .set_u32("level", new_level.to_db()) - .exec() + .exec()?; - // TODO : Update access to group's conversations + // Update access to group's conversations + conversations_helper::update_members_list_for_group_conversations(&group_id)?; + + Ok(()) } /// Update following status of a user @@ -491,7 +503,8 @@ pub fn delete(group_id: &GroupID) -> ResultBoxError { // Delete all group related notifications notifications_helper::delete_all_related_with_group(group_id)?; - // TODO : delete all conversations related with the group + // Delete all conversations related with the group + conversations_helper::delete_all_group_conversations(group_id)?; // Delete all group members database::DeleteQuery::new(GROUPS_MEMBERS_TABLE)