mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Count the number of unread conversations of a user
This commit is contained in:
		@@ -20,3 +20,4 @@ mod legacy_api_bool;
 | 
				
			|||||||
pub mod res_find_private_conversations;
 | 
					pub mod res_find_private_conversations;
 | 
				
			||||||
pub mod conversation_message_api;
 | 
					pub mod conversation_message_api;
 | 
				
			||||||
pub mod conversations_refresh_api;
 | 
					pub mod conversations_refresh_api;
 | 
				
			||||||
 | 
					pub mod res_count_unread_conversations;
 | 
				
			||||||
							
								
								
									
										18
									
								
								src/api_data/res_count_unread_conversations.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/api_data/res_count_unread_conversations.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					//! Count the number of unread conversations of a given user
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! @author Pierre Hubert
 | 
				
			||||||
 | 
					use serde::Serialize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Serialize)]
 | 
				
			||||||
 | 
					pub struct ResultCountUnreadConversations {
 | 
				
			||||||
 | 
					    nb_unread: usize
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl ResultCountUnreadConversations {
 | 
				
			||||||
 | 
					    /// Generate an new instance
 | 
				
			||||||
 | 
					    pub fn new(count: usize) -> ResultCountUnreadConversations {
 | 
				
			||||||
 | 
					        ResultCountUnreadConversations {
 | 
				
			||||||
 | 
					            nb_unread: count
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -15,6 +15,7 @@ use crate::data::new_conversation::NewConversation;
 | 
				
			|||||||
use crate::data::new_conversation_message::NewConversationMessage;
 | 
					use crate::data::new_conversation_message::NewConversationMessage;
 | 
				
			||||||
use crate::helpers::{conversations_helper, user_helper};
 | 
					use crate::helpers::{conversations_helper, user_helper};
 | 
				
			||||||
use crate::utils::string_utils::remove_html_nodes;
 | 
					use crate::utils::string_utils::remove_html_nodes;
 | 
				
			||||||
 | 
					use crate::api_data::res_count_unread_conversations::ResultCountUnreadConversations;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Create a new conversation
 | 
					/// Create a new conversation
 | 
				
			||||||
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
 | 
					pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
@@ -274,3 +275,10 @@ pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    r.success("Conversation message was successfully sent!")
 | 
					    r.success("Conversation message was successfully sent!")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Count the number of unread conversation of the user
 | 
				
			||||||
 | 
					pub fn count_unread(r: &mut HttpRequestHandler) -> RequestResult {
 | 
				
			||||||
 | 
					    let num = conversations_helper::count_unread_for_user(r.user_id()?)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    r.set_response(ResultCountUnreadConversations::new(num))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -108,6 +108,8 @@ pub fn get_routes() -> Vec<Route> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        Route::post("/conversations/sendMessage", Box::new(conversations_controller::send_message)),
 | 
					        Route::post("/conversations/sendMessage", Box::new(conversations_controller::send_message)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Route::post("/conversations/get_number_unread", Box::new(conversations_controller::count_unread)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Virtual directory controller
 | 
					        // Virtual directory controller
 | 
				
			||||||
        Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),
 | 
					        Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -276,6 +276,15 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
 | 
				
			|||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Count the number of unread conversation for a specified user
 | 
				
			||||||
 | 
					pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError<usize> {
 | 
				
			||||||
 | 
					    database::QueryInfo::new(CONV_USERS_TABLE)
 | 
				
			||||||
 | 
					        .cond_user_id("user_id", user_id)
 | 
				
			||||||
 | 
					        .cond_legacy_bool("saw_last_message", false)
 | 
				
			||||||
 | 
					        .cond_legacy_bool("following", true)
 | 
				
			||||||
 | 
					        .exec_count()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Indicate that a user has seen the last messages of a conversation
 | 
					/// Indicate that a user has seen the last messages of a conversation
 | 
				
			||||||
pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
 | 
					pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
 | 
				
			||||||
    database::UpdateInfo::new(CONV_USERS_TABLE)
 | 
					    database::UpdateInfo::new(CONV_USERS_TABLE)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -142,6 +142,16 @@ impl QueryInfo {
 | 
				
			|||||||
        self
 | 
					        self
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn cond_legacy_bool(mut self, key: &str, val: bool) -> QueryInfo {
 | 
				
			||||||
 | 
					        let val = match val {
 | 
				
			||||||
 | 
					            true => "1".to_string(),
 | 
				
			||||||
 | 
					            false => "2".to_string()
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.conditions.insert(key.to_string(), val);
 | 
				
			||||||
 | 
					        self
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Set custom where
 | 
					    /// Set custom where
 | 
				
			||||||
    pub fn set_custom_where(mut self, custom_where: &str) -> QueryInfo {
 | 
					    pub fn set_custom_where(mut self, custom_where: &str) -> QueryInfo {
 | 
				
			||||||
        self.custom_where = Some(custom_where.to_string());
 | 
					        self.custom_where = Some(custom_where.to_string());
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user