diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 79ae135..9b5ea51 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -71,18 +71,27 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult { conversations_helper::set_following( r.user_id()?, conv_id, - r.post_bool("following")? + r.post_bool("following")?, )?; } // Update members list if r.has_post_parameter("members") { - let members = r.post_numbers_list("members", 1); + let mut members = r.post_numbers_list("members", 1)?; let can_everyone_add_members = conversations_helper::can_everyone_add_members(conv_id)?; if !is_moderator && !can_everyone_add_members { r.forbidden("You can not update the list of members of this conversation!".to_string())?; } + + if !members.contains(&r.user_id()?) { + members.push(r.user_id()?); + } + + conversations_helper::set_members( + conv_id, + &members, + can_everyone_add_members)?; } r.success("Conversation information successfully updated!") diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 86f8859..97320fe 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -49,6 +49,14 @@ pub fn add_member(conv_id: u64, user_id: UserID, following: bool) -> ResultBoxEr Ok(()) } +/// Remove a member from a conversation +pub fn remove_member(conv_id: u64, user_id: UserID) -> ResultBoxError<()> { + database::DeleteQuery::new(CONV_USERS_TABLE) + .cond_u64("conv_id", conv_id) + .cond_user_id("user_id", user_id) + .exec() +} + /// Get the list of conversations of a specific user pub fn get_list_user(user_id: UserID) -> ResultBoxError> { database::QueryInfo::new(CONV_LIST_TABLE) @@ -132,6 +140,33 @@ pub fn set_following(user_id: UserID, conv_id: u64, following: bool) -> ResultBo .exec() } +/// Set a new list of members for a given conversation +pub fn set_members(conv_id: u64, new_list: &Vec, can_delete: bool) -> ResultBoxError<()> { + let curr_list = get_list_members(conv_id)?; + + // Add new members + for member in new_list { + if curr_list.contains(member) { + continue; + } + + add_member(conv_id, member.clone(), true)?; + } + + if can_delete { + for member in curr_list { + if new_list.contains(&member) { + continue; + } + + remove_member(conv_id, member)?; + } + } + + + Ok(()) +} + /// Turn a database entry into a ConversationInfo object fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError { let conv_id = row.get_u64("id")?; diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 3faa786..6319207 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -506,6 +506,21 @@ impl DeleteQuery { self.conditions.insert(key.to_string(), Value::from(value)); self } + + pub fn cond_u64(mut self, key: &str, value: u64) -> DeleteQuery { + self.conditions.insert(key.to_string(), Value::from(value)); + self + } + + pub fn cond_user_id(mut self, key: &str, value: UserID) -> DeleteQuery { + self.conditions.insert(key.to_string(), Value::from(value)); + self + } + + /// Execute the delete query + pub fn exec(self) -> ResultBoxError<()> { + delete(self) + } } /// Delete an entry from the database