mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can add member to conversations
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MEMBERS_TABLE, CONV_MESSAGES_TABLE};
|
||||
use crate::data::conversation::{Conversation, ConversationMember, ConversationMemberSetting, ConvID, NewConversationSettings};
|
||||
use crate::data::conversation::{Conversation, ConversationMember, ConvID, NewConversationSettings};
|
||||
use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType};
|
||||
use crate::data::error::{ExecError, Res, ResultBoxError};
|
||||
use crate::data::new_conversation::NewConversation;
|
||||
@ -42,14 +42,14 @@ pub fn create(conv: &NewConversation) -> Res<ConvID> {
|
||||
admin = true;
|
||||
}
|
||||
|
||||
add_member(conv_id, member, follow, admin)?;
|
||||
add_member(conv_id, member, follow, admin, &conv.owner_id)?;
|
||||
}
|
||||
|
||||
Ok(conv_id)
|
||||
}
|
||||
|
||||
/// Add a member to a conversation
|
||||
pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool) -> Res {
|
||||
pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool, _adder: &UserID) -> Res {
|
||||
InsertQuery::new(CONV_MEMBERS_TABLE)
|
||||
.add_conv_id("conv_id", conv_id)
|
||||
.add_user_id("user_id", user_id)
|
||||
@ -73,18 +73,6 @@ pub fn set_admin(conv_id: &ConvID, user_id: &UserID, admin: bool) -> Res {
|
||||
.exec()
|
||||
}
|
||||
|
||||
/// Remove a member from a conversation
|
||||
pub fn remove_member(conv_id: ConvID, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec()?;
|
||||
|
||||
// TODO : create a message
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the list of conversations of a specific user
|
||||
pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<Conversation>> {
|
||||
database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
@ -147,36 +135,6 @@ pub fn set_following(user_id: &UserID, conv_id: ConvID, following: bool) -> Resu
|
||||
.exec()
|
||||
}
|
||||
|
||||
/// Set a new list of members for a given conversation
|
||||
pub fn set_members(conv_id: ConvID, new_list: &Vec<ConversationMemberSetting>, can_delete: bool) -> ResultBoxError<()> {
|
||||
let curr_list = get_list_members(conv_id)?;
|
||||
|
||||
// Add new members
|
||||
for member in new_list {
|
||||
if let Some(user) = curr_list.iter().filter(|m| m.user_id == member.user_id).next() {
|
||||
// Check if we have to update admin state
|
||||
if user.is_admin != member.set_admin {
|
||||
set_admin(&conv_id, &member.user_id, member.set_admin)?;
|
||||
}
|
||||
} else {
|
||||
add_member(conv_id, &member.user_id, true, member.set_admin)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove a member
|
||||
if can_delete {
|
||||
for member in curr_list {
|
||||
if new_list.iter().any(|m| m.user_id.eq(&member.user_id)) {
|
||||
continue;
|
||||
}
|
||||
remove_member(conv_id, &member.user_id)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set a new name to the conversation
|
||||
pub fn set_settings(settings: NewConversationSettings) -> Res {
|
||||
database::UpdateInfo::new(CONV_LIST_TABLE)
|
||||
@ -450,7 +408,7 @@ pub fn remove_user_from_conversation(user_id: &UserID, conv: &Conversation, remo
|
||||
if conv.is_last_admin(user_id) {
|
||||
delete_conversation(conv)
|
||||
} else {
|
||||
delete_member(user_id, conv.id, remover)
|
||||
remove_member(user_id, conv.id, remover)
|
||||
}
|
||||
}
|
||||
|
||||
@ -480,13 +438,18 @@ pub fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Delete a conversation membership
|
||||
pub fn delete_member(user_id: &UserID, conv_id: ConvID, _remover: &UserID) -> ResultBoxError<()> {
|
||||
pub fn remove_member(user_id: &UserID, conv_id: ConvID, _remover: &UserID) -> ResultBoxError<()> {
|
||||
for msg in get_user_messages_for_conversations(conv_id, user_id)? {
|
||||
delete_message(&msg)?;
|
||||
}
|
||||
|
||||
// Delete membership
|
||||
remove_member(conv_id, user_id)?;
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec()?;
|
||||
|
||||
// TODO : create a message
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user