From b9a8dce1a56267a3f086ada4901b7f9b043cec10 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 22 Jun 2020 14:32:14 +0200 Subject: [PATCH] Count the number of unread conversations of a user --- src/api_data/mod.rs | 3 ++- src/api_data/res_count_unread_conversations.rs | 18 ++++++++++++++++++ src/controllers/conversations_controller.rs | 8 ++++++++ src/controllers/routes.rs | 2 ++ src/helpers/conversations_helper.rs | 9 +++++++++ src/helpers/database.rs | 10 ++++++++++ 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/api_data/res_count_unread_conversations.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 42bf7ba..7db3c48 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -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; \ No newline at end of file +pub mod conversations_refresh_api; +pub mod res_count_unread_conversations; \ No newline at end of file diff --git a/src/api_data/res_count_unread_conversations.rs b/src/api_data/res_count_unread_conversations.rs new file mode 100644 index 0000000..0829db4 --- /dev/null +++ b/src/api_data/res_count_unread_conversations.rs @@ -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 + } + } +} \ No newline at end of file diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 5bd2e6b..88dfc19 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -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)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index b41ab26..271aa7e 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -108,6 +108,8 @@ pub fn get_routes() -> Vec { 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)), diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 56451fc..870b7b5 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -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 { + 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) diff --git a/src/helpers/database.rs b/src/helpers/database.rs index aea059a..c8bd0ba 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -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());