mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 13:29:21 +00:00
Count the number of unread conversations of a user
This commit is contained in:
parent
f8934d4e0a
commit
b9a8dce1a5
@ -19,4 +19,5 @@ pub mod conversation_api;
|
||||
mod legacy_api_bool;
|
||||
pub mod res_find_private_conversations;
|
||||
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::helpers::{conversations_helper, user_helper};
|
||||
use crate::utils::string_utils::remove_html_nodes;
|
||||
use crate::api_data::res_count_unread_conversations::ResultCountUnreadConversations;
|
||||
|
||||
/// Create a new conversation
|
||||
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
@ -273,4 +274,11 @@ pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
})?;
|
||||
|
||||
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/get_number_unread", Box::new(conversations_controller::count_unread)),
|
||||
|
||||
|
||||
// Virtual directory controller
|
||||
Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),
|
||||
|
@ -276,6 +276,15 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
||||
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
|
||||
pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
|
||||
database::UpdateInfo::new(CONV_USERS_TABLE)
|
||||
|
@ -142,6 +142,16 @@ impl QueryInfo {
|
||||
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
|
||||
pub fn set_custom_where(mut self, custom_where: &str) -> QueryInfo {
|
||||
self.custom_where = Some(custom_where.to_string());
|
||||
|
Loading…
Reference in New Issue
Block a user