From 72ff302232099488d586f25801813141df6ef8cd Mon Sep 17 00:00:00 2001 From: Andrew Baldwin Date: Fri, 17 Jan 2025 09:55:46 -0800 Subject: [PATCH] Expand the search time interval when inserting an echo into message history. --- data/src/history.rs | 12 ++++++++---- data/src/message.rs | 16 +++++++++++----- data/src/message/broadcast.rs | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/data/src/history.rs b/data/src/history.rs index 1d17ec01..a026e38c 100644 --- a/data/src/history.rs +++ b/data/src/history.rs @@ -491,7 +491,11 @@ impl History { /// of the incoming message. Either message IDs match, or server times /// have an exact match + target & content. pub fn insert_message(messages: &mut Vec, message: Message) { - const FUZZ_SECONDS: chrono::Duration = chrono::Duration::seconds(1); + let fuzz_seconds = if matches!(message.direction, message::Direction::Received(true)) { + chrono::Duration::seconds(120) + } else { + chrono::Duration::seconds(1) + }; if messages.is_empty() { messages.push(message); @@ -499,8 +503,8 @@ pub fn insert_message(messages: &mut Vec, message: Message) { return; } - let start = message.server_time - FUZZ_SECONDS; - let end = message.server_time + FUZZ_SECONDS; + let start = message.server_time - fuzz_seconds; + let end = message.server_time + fuzz_seconds; let start_index = match messages.binary_search_by(|stored| stored.server_time.cmp(&start)) { Ok(match_index) => match_index, @@ -519,7 +523,7 @@ pub fn insert_message(messages: &mut Vec, message: Message) { if (message.id.is_some() && stored.id == message.id) || ((stored.server_time == message.server_time || (matches!(stored.direction, message::Direction::Sent) - && matches!(message.direction, message::Direction::Received))) + && matches!(message.direction, message::Direction::Received(true)))) && has_matching_content(stored, &message)) { replace_at = Some(current_index); diff --git a/data/src/message.rs b/data/src/message.rs index 10aaaaab..e7f0ff26 100644 --- a/data/src/message.rs +++ b/data/src/message.rs @@ -155,7 +155,8 @@ impl Target { #[derive(Debug, Clone, Copy, Serialize, Deserialize)] pub enum Direction { Sent, - Received, + /// is_echo + Received(bool), } #[derive(Debug, Clone)] @@ -171,7 +172,7 @@ pub struct Message { impl Message { pub fn triggers_unread(&self) -> bool { - matches!(self.direction, Direction::Received) + matches!(self.direction, Direction::Received(false)) && match self.target.source() { Source::User(_) => true, Source::Action(_) => true, @@ -226,6 +227,11 @@ impl Message { ) -> Option { let server_time = server_time(&encoded); let id = message_id(&encoded); + let direction = Direction::Received( + encoded + .user() + .is_some_and(|user| user.nickname() == our_nick), + ); let content = content( &encoded, &our_nick, @@ -250,7 +256,7 @@ impl Message { Some(Message { received_at, server_time, - direction: Direction::Received, + direction, target, content, id, @@ -285,7 +291,7 @@ impl Message { Message { received_at, server_time: Utc::now(), - direction: Direction::Received, + direction: Direction::Received(false), target: Target::Query { query: query.clone(), source: Source::Action(None), @@ -336,7 +342,7 @@ impl Message { Self { received_at, server_time, - direction: Direction::Received, + direction: Direction::Received(false), target: Target::Logs, content, id: None, diff --git a/data/src/message/broadcast.rs b/data/src/message/broadcast.rs index 5de04da7..34532029 100644 --- a/data/src/message/broadcast.rs +++ b/data/src/message/broadcast.rs @@ -27,7 +27,7 @@ fn expand( Message { received_at, server_time: sent_time, - direction: Direction::Received, + direction: Direction::Received(false), target, content, id: None,