diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 6c35eb2..7f4ec80 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -172,10 +172,10 @@ pub fn refresh_list(r: &mut HttpRequestHandler) -> RequestResult { r.forbidden(format!("Your do not belongs to conversation {} !", conv_id))?; } - let msgs_conv = conversations_helper::get_last_messages(conv_id as u64, 10)?; - list.insert(conv_id as u64, msgs_conv); + let list_conv = conversations_helper::get_last_messages(conv_id as u64, 10)?; + list.insert(conv_id as u64, list_conv); - //TODO : mark user seen + conversations_helper::mark_user_seen(conv_id as u64, r.user_id()?)?; } // TODO : Check for refresh of already initialized conversations diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 10c5dfc..9c70078 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -2,15 +2,15 @@ //! //! @author Pierre Hubert -use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_USERS_TABLE, CONV_MESSAGES_TABLE}; +use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_USERS_TABLE}; use crate::data::conversation::Conversation; +use crate::data::conversation_message::ConversationMessage; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::new_conversation::NewConversation; use crate::data::user::UserID; -use crate::helpers::database::InsertQuery; use crate::helpers::database; +use crate::helpers::database::InsertQuery; use crate::utils::date_utils::time; -use crate::data::conversation_message::ConversationMessage; /// Create a new conversation. This method returns the ID of the created conversation pub fn create(conv: &NewConversation) -> ResultBoxError { @@ -197,7 +197,7 @@ pub fn find_private(user_1: UserID, user_2: UserID) -> ResultBoxError> .cond_user_id("t2.user_id", user_2) .set_custom_where(format!("(SELECT COUNT(*) FROM {} WHERE conv_id = t1.conv_id) = 2", CONV_USERS_TABLE).as_ref()) .add_field("t1.conv_id AS conv_id") - .exec(|f|f.get_u64("conv_id")) + .exec(|f| f.get_u64("conv_id")) } /// Get the last messages posted in a conversation @@ -208,11 +208,21 @@ pub fn get_last_messages(conv_id: u64, number_of_messages: u64) -> ResultBoxErro .set_order("id DESC") .exec(db_to_conversation_message) .map(|mut l| { - l.sort_by(|a,b| a.id.partial_cmp(&b.id).unwrap()); + l.sort_by(|a, b| a.id.partial_cmp(&b.id).unwrap()); l }) } +/// 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) + .cond_u64("conv_id", conv_id) + .cond_user_id("user_id", user_id) + .cond_legacy_bool("saw_last_message", false) + .set_legacy_bool("saw_last_message", true) + .exec() +} + /// Turn a database entry into a ConversationInfo object fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError { let conv_id = row.get_u64("id")?; @@ -238,6 +248,6 @@ fn db_to_conversation_message(row: &database::RowResult) -> ResultBoxError UpdateInfo { + let num = match val { + true => 1, + false => 0 + }; + + self.cond.insert(name.to_string(), Value::Int(num)); + self + } + /// Set an new optional string /// /// None => Empty string