1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Add new field in conversation members table to ease database access

This commit is contained in:
Pierre HUBERT 2021-03-05 17:07:39 +01:00
parent fd6970f6fe
commit dc93d58d6b
6 changed files with 19 additions and 8 deletions

View File

@ -98,6 +98,7 @@ CREATE TABLE `comunic_conversations_members` (
`following` int DEFAULT '0', `following` int DEFAULT '0',
`is_admin` int DEFAULT '1', `is_admin` int DEFAULT '1',
`last_message_seen` int DEFAULT '0', `last_message_seen` int DEFAULT '0',
`last_access` int DEFAULT '0',
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

View File

@ -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 drop column saw_last_message;
alter table comunic_conversations_members alter table comunic_conversations_members
add column is_admin int default 1, 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 -- Conversations list table
alter table comunic_conversations_list drop column user_id; alter table comunic_conversations_list drop column user_id;

View File

@ -11,6 +11,7 @@ use crate::helpers::calls_helper;
struct ConversationMembersAPI { struct ConversationMembersAPI {
user_id: u64, user_id: u64,
last_message_seen: u64, last_message_seen: u64,
last_access: u64,
following: bool, following: bool,
is_admin: bool, is_admin: bool,
} }
@ -37,6 +38,7 @@ impl ConversationMembersAPI {
last_message_seen: m.last_message_seen, last_message_seen: m.last_message_seen,
following: m.following, following: m.following,
is_admin: m.is_admin, is_admin: m.is_admin,
last_access: m.last_access,
} }
} }
} }

View File

@ -206,7 +206,7 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult {
let conv = r.post_conv("conversationID")?; let conv = r.post_conv("conversationID")?;
let last_message_id = r.post_u64("last_message_id")?; 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 // Get latest messages of the conversation
0 => conversations_helper::get_last_messages(conv.conv_id, 10)?, 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)?, _ => 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 { if messages.len() > 0 {
conversations_helper::mark_user_seen( conversations_helper::mark_user_seen(
conv.conv_id, conv.conv_id,
r.user_id_ref()?, 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( user_ws_controller::send_message_to_specific_connections(
|f| f.conversations.contains(&msg.conv_id), |f| f.conversations.contains(&msg.conv_id),
|_| UserWsMessage::no_id_message("new_conv_message", ConversationMessageAPI::new(msg)), |_| 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)),
)?; )?;
} }

View File

@ -27,6 +27,7 @@ pub struct ConversationMember {
pub following: bool, pub following: bool,
pub is_admin: bool, pub is_admin: bool,
pub last_message_seen: u64, pub last_message_seen: u64,
pub last_access: u64,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -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())) .add_custom_where_argument_user_id(msg.user_id.as_ref().unwrap_or(&UserID::invalid()))
.exec(|r| r.get_user_id("user_id"))?; .exec(|r| r.get_user_id("user_id"))?;
let new_message = get_single_message(msg_id)?;
// Mark the user has seen his message // Mark the user has seen his message
if let Some(user_id) = &msg.user_id { 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) // Send an event (updated_number_unread_conversations)
events_helper::propagate_event(&Event::UpdatedNumberUnreadConversations(&list_to_notify))?; events_helper::propagate_event(&Event::UpdatedNumberUnreadConversations(&list_to_notify))?;
// Send an event (sent_conversation_message) // 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(()) Ok(())
} }
@ -429,11 +431,12 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<ConvID>> {
} }
/// Indicate that a user has seen the last messages of a conversation /// 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) database::UpdateInfo::new(CONV_MEMBERS_TABLE)
.cond_conv_id("conv_id", conv_id) .cond_conv_id("conv_id", conv_id)
.cond_user_id("user_id", user_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()?; .exec()?;
// Push an event (updated_number_unread_conversations) // Push an event (updated_number_unread_conversations)
@ -523,6 +526,7 @@ fn db_to_conversation_member(row: &database::RowResult) -> Res<ConversationMembe
following: row.get_legacy_bool("following")?, following: row.get_legacy_bool("following")?,
is_admin: row.get_legacy_bool("is_admin")?, is_admin: row.get_legacy_bool("is_admin")?,
last_message_seen: row.get_u64("last_message_seen")?, last_message_seen: row.get_u64("last_message_seen")?,
last_access: row.get_optional_u64("last_access")?.unwrap_or(0),
}) })
} }