From dc93d58d6bc934a1f8acee812e950921bbc86f0d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 5 Mar 2021 17:07:39 +0100 Subject: [PATCH] Add new field in conversation members table to ease database access --- docs/db_struct.sql | 1 + docs/migration.sql | 3 ++- src/api_data/conversation_api.rs | 2 ++ src/controllers/conversations_controller.rs | 8 +++++--- src/data/conversation.rs | 1 + src/helpers/conversations_helper.rs | 12 ++++++++---- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/db_struct.sql b/docs/db_struct.sql index 0d64e2c..591bf27 100644 --- a/docs/db_struct.sql +++ b/docs/db_struct.sql @@ -98,6 +98,7 @@ CREATE TABLE `comunic_conversations_members` ( `following` int DEFAULT '0', `is_admin` int DEFAULT '1', `last_message_seen` int DEFAULT '0', + `last_access` int DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; diff --git a/docs/migration.sql b/docs/migration.sql index a30f166..303685a 100644 --- a/docs/migration.sql +++ b/docs/migration.sql @@ -16,7 +16,8 @@ ALTER TABLE comunic_conversations_members rename column time_add to added_on; alter table comunic_conversations_members drop column saw_last_message; alter table comunic_conversations_members add column is_admin int default 1, - add column last_message_seen int default 0; + add column last_message_seen int default 0, + add column last_access int default 0; -- Conversations list table alter table comunic_conversations_list drop column user_id; diff --git a/src/api_data/conversation_api.rs b/src/api_data/conversation_api.rs index 2071656..9e849c3 100644 --- a/src/api_data/conversation_api.rs +++ b/src/api_data/conversation_api.rs @@ -11,6 +11,7 @@ use crate::helpers::calls_helper; struct ConversationMembersAPI { user_id: u64, last_message_seen: u64, + last_access: u64, following: bool, is_admin: bool, } @@ -37,6 +38,7 @@ impl ConversationMembersAPI { last_message_seen: m.last_message_seen, following: m.following, is_admin: m.is_admin, + last_access: m.last_access, } } } diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 4ac514d..65e9139 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -206,7 +206,7 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult { let conv = r.post_conv("conversationID")?; let last_message_id = r.post_u64("last_message_id")?; - let messages = match last_message_id { + let mut messages = match last_message_id { // Get latest messages of the conversation 0 => conversations_helper::get_last_messages(conv.conv_id, 10)?, @@ -214,11 +214,13 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult { _ => conversations_helper::get_new_messages(conv.conv_id, last_message_id)?, }; + messages.sort_by(|one, two| one.id.cmp(&two.id)); + if messages.len() > 0 { conversations_helper::mark_user_seen( conv.conv_id, r.user_id_ref()?, - messages.iter().map(|m| m.id).max().unwrap(), + &messages[messages.len() - 1], )?; } @@ -374,7 +376,7 @@ pub fn handle_event(e: &events_helper::Event) -> Res { user_ws_controller::send_message_to_specific_connections( |f| f.conversations.contains(&msg.conv_id), |_| UserWsMessage::no_id_message("new_conv_message", ConversationMessageAPI::new(msg)), - Some(|conn: &UserWsConnection| conversations_helper::mark_user_seen(msg.conv_id, conn.user_id(), msg.id)), + Some(|conn: &UserWsConnection| conversations_helper::mark_user_seen(msg.conv_id, conn.user_id(), msg)), )?; } diff --git a/src/data/conversation.rs b/src/data/conversation.rs index 1e455d0..e2072ff 100644 --- a/src/data/conversation.rs +++ b/src/data/conversation.rs @@ -27,6 +27,7 @@ pub struct ConversationMember { pub following: bool, pub is_admin: bool, pub last_message_seen: u64, + pub last_access: u64, } #[derive(Debug)] diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index ce432c6..bcb9a18 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -352,16 +352,18 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { .add_custom_where_argument_user_id(msg.user_id.as_ref().unwrap_or(&UserID::invalid())) .exec(|r| r.get_user_id("user_id"))?; + let new_message = get_single_message(msg_id)?; + // Mark the user has seen his message if let Some(user_id) = &msg.user_id { - mark_user_seen(msg.conv_id, user_id, msg_id)?; + mark_user_seen(msg.conv_id, user_id, &new_message)?; } // Send an event (updated_number_unread_conversations) events_helper::propagate_event(&Event::UpdatedNumberUnreadConversations(&list_to_notify))?; // Send an event (sent_conversation_message) - events_helper::propagate_event(&Event::NewConversationMessage(&get_single_message(msg_id)?))?; + events_helper::propagate_event(&Event::NewConversationMessage(&new_message))?; Ok(()) } @@ -429,11 +431,12 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError> { } /// Indicate that a user has seen the last messages of a conversation -pub fn mark_user_seen(conv_id: ConvID, user_id: &UserID, last_msg: u64) -> ResultBoxError<()> { +pub fn mark_user_seen(conv_id: ConvID, user_id: &UserID, last_msg: &ConversationMessage) -> ResultBoxError<()> { database::UpdateInfo::new(CONV_MEMBERS_TABLE) .cond_conv_id("conv_id", conv_id) .cond_user_id("user_id", user_id) - .set_u64("last_message_seen", last_msg) + .set_u64("last_message_seen", last_msg.id) + .set_u64("last_access", last_msg.time_sent) .exec()?; // Push an event (updated_number_unread_conversations) @@ -523,6 +526,7 @@ fn db_to_conversation_member(row: &database::RowResult) -> Res