mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Can remove a member from a conversation
This commit is contained in:
		@@ -293,5 +293,9 @@ pub fn list_unread(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/// Delete a conversation
 | 
					/// Delete a conversation
 | 
				
			||||||
pub fn delete_conversation(r: &mut HttpRequestHandler) -> RequestResult {
 | 
					pub fn delete_conversation(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
    r.success("Delete a conversation")
 | 
					    let conv_id = r.post_conv_id("conversationID")?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    conversations_helper::remove_user_from_conversation(r.user_id()?, conv_id)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    r.success("The conversation has been deleted")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -5,6 +5,7 @@
 | 
				
			|||||||
use crate::data::user::UserID;
 | 
					use crate::data::user::UserID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Information about a single conversation message
 | 
					/// Information about a single conversation message
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub struct ConversationMessage {
 | 
					pub struct ConversationMessage {
 | 
				
			||||||
    pub id: u64,
 | 
					    pub id: u64,
 | 
				
			||||||
    pub time_sent: u64,
 | 
					    pub time_sent: u64,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,6 +13,7 @@ use crate::helpers::database;
 | 
				
			|||||||
use crate::helpers::database::InsertQuery;
 | 
					use crate::helpers::database::InsertQuery;
 | 
				
			||||||
use crate::utils::date_utils::time;
 | 
					use crate::utils::date_utils::time;
 | 
				
			||||||
use crate::data::unread_conversation::UnreadConversation;
 | 
					use crate::data::unread_conversation::UnreadConversation;
 | 
				
			||||||
 | 
					use crate::utils::user_data_utils::user_data_path;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Create a new conversation. This method returns the ID of the created conversation
 | 
					/// Create a new conversation. This method returns the ID of the created conversation
 | 
				
			||||||
pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
 | 
					pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
 | 
				
			||||||
@@ -244,6 +245,14 @@ pub fn get_older_messages(conv_id: u64, start_id: u64, limit: u64) -> ResultBoxE
 | 
				
			|||||||
        })
 | 
					        })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Get all the messages of a single user for a conversation
 | 
				
			||||||
 | 
					pub fn get_user_messages_for_conversations(conv_id: u64, user_id: UserID) -> ResultBoxError<Vec<ConversationMessage>> {
 | 
				
			||||||
 | 
					    database::QueryInfo::new(CONV_MESSAGES_TABLE)
 | 
				
			||||||
 | 
					        .cond_u64("conv_id", conv_id)
 | 
				
			||||||
 | 
					        .cond_user_id("user_id", user_id)
 | 
				
			||||||
 | 
					        .exec(db_to_conversation_message)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Send a new conversation message
 | 
					/// Send a new conversation message
 | 
				
			||||||
pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
 | 
					pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
 | 
				
			||||||
    let t = time();
 | 
					    let t = time();
 | 
				
			||||||
@@ -277,6 +286,24 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
 | 
				
			|||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Remove a message from a conversation
 | 
				
			||||||
 | 
					pub fn delete_message(msg: &ConversationMessage) -> ResultBoxError<()> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Delete associated image (if any)
 | 
				
			||||||
 | 
					    if let Some(img) = &msg.image_path {
 | 
				
			||||||
 | 
					        let path = user_data_path(img.as_ref());
 | 
				
			||||||
 | 
					        if path.exists() {
 | 
				
			||||||
 | 
					            std::fs::remove_file(path)?;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    database::DeleteQuery::new(CONV_MESSAGES_TABLE)
 | 
				
			||||||
 | 
					        .cond_u64("ID", msg.id)
 | 
				
			||||||
 | 
					        .exec()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ok(())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Count the number of unread conversation for a specified user
 | 
					/// Count the number of unread conversation for a specified user
 | 
				
			||||||
pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError<usize> {
 | 
					pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError<usize> {
 | 
				
			||||||
    database::QueryInfo::new(CONV_USERS_TABLE)
 | 
					    database::QueryInfo::new(CONV_USERS_TABLE)
 | 
				
			||||||
@@ -300,12 +327,12 @@ pub fn get_list_unread(user_id: UserID) -> ResultBoxError<Vec<UnreadConversation
 | 
				
			|||||||
        .set_custom_where("list.last_active = messages.time_insert")
 | 
					        .set_custom_where("list.last_active = messages.time_insert")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        .set_order("list.last_active DESC")
 | 
					        .set_order("list.last_active DESC")
 | 
				
			||||||
        .exec(|res| Ok(UnreadConversation{
 | 
					        .exec(|res| Ok(UnreadConversation {
 | 
				
			||||||
            id: res.get_u64("conv_id")?,
 | 
					            id: res.get_u64("conv_id")?,
 | 
				
			||||||
            name: res.get_optional_str("name")?,
 | 
					            name: res.get_optional_str("name")?,
 | 
				
			||||||
            last_active: res.get_u64("last_active")?,
 | 
					            last_active: res.get_u64("last_active")?,
 | 
				
			||||||
            user_id: res.get_user_id("user_id")?,
 | 
					            user_id: res.get_user_id("user_id")?,
 | 
				
			||||||
            message: res.get_str("message")?
 | 
					            message: res.get_str("message")?,
 | 
				
			||||||
        }))
 | 
					        }))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -319,6 +346,27 @@ pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
 | 
				
			|||||||
        .exec()
 | 
					        .exec()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Remove a user from a conversation
 | 
				
			||||||
 | 
					pub fn remove_user_from_conversation(user_id: UserID, conv_id: u64) -> ResultBoxError<()> {
 | 
				
			||||||
 | 
					    if is_user_moderator(user_id, conv_id)? {
 | 
				
			||||||
 | 
					        unimplemented!()
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        delete_member(user_id, conv_id)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Delete a conversation membership
 | 
				
			||||||
 | 
					pub fn delete_member(user_id: UserID, conv_id: u64) -> ResultBoxError<()> {
 | 
				
			||||||
 | 
					    for msg in get_user_messages_for_conversations(conv_id, user_id)? {
 | 
				
			||||||
 | 
					        delete_message(&msg)?;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Delete membership
 | 
				
			||||||
 | 
					    remove_member(conv_id, user_id)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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")?;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user