1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00

Count the number of unread conversations of a user

This commit is contained in:
Pierre HUBERT 2020-06-22 14:32:14 +02:00
parent f8934d4e0a
commit b9a8dce1a5
6 changed files with 49 additions and 1 deletions

View File

@ -20,3 +20,4 @@ mod legacy_api_bool;
pub mod res_find_private_conversations;
pub mod conversation_message_api;
pub mod conversations_refresh_api;
pub mod res_count_unread_conversations;

View 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
}
}
}

View File

@ -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 {
@ -274,3 +275,10 @@ 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))
}

View File

@ -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)),

View File

@ -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)

View File

@ -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());