1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can update the list of members of the conversation

This commit is contained in:
Pierre HUBERT 2020-06-15 10:46:10 +02:00
parent 16298d54fb
commit 856d22fed8
3 changed files with 61 additions and 2 deletions

View File

@ -71,18 +71,27 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult {
conversations_helper::set_following( conversations_helper::set_following(
r.user_id()?, r.user_id()?,
conv_id, conv_id,
r.post_bool("following")? r.post_bool("following")?,
)?; )?;
} }
// Update members list // Update members list
if r.has_post_parameter("members") { 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)?; let can_everyone_add_members = conversations_helper::can_everyone_add_members(conv_id)?;
if !is_moderator && !can_everyone_add_members { if !is_moderator && !can_everyone_add_members {
r.forbidden("You can not update the list of members of this conversation!".to_string())?; 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!") r.success("Conversation information successfully updated!")

View File

@ -49,6 +49,14 @@ pub fn add_member(conv_id: u64, user_id: UserID, following: bool) -> ResultBoxEr
Ok(()) 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 /// Get the list of conversations of a specific user
pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<Conversation>> { pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<Conversation>> {
database::QueryInfo::new(CONV_LIST_TABLE) database::QueryInfo::new(CONV_LIST_TABLE)
@ -132,6 +140,33 @@ pub fn set_following(user_id: UserID, conv_id: u64, following: bool) -> ResultBo
.exec() .exec()
} }
/// Set a new list of members for a given conversation
pub fn set_members(conv_id: u64, new_list: &Vec<UserID>, 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 /// Turn a database entry into a ConversationInfo object
fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> { fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> {
let conv_id = row.get_u64("id")?; let conv_id = row.get_u64("id")?;

View File

@ -506,6 +506,21 @@ impl DeleteQuery {
self.conditions.insert(key.to_string(), Value::from(value)); self.conditions.insert(key.to_string(), Value::from(value));
self 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 /// Delete an entry from the database