Skip to content

Commit

Permalink
Merge pull request #714 from squidowl/expand-echo-search
Browse files Browse the repository at this point in the history
Expand Echo Search
  • Loading branch information
andymandias authored Jan 24, 2025
2 parents 49b491e + 72ff302 commit 6f50724
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
12 changes: 8 additions & 4 deletions data/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,20 @@ 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: 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);

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,
Expand All @@ -519,7 +523,7 @@ pub fn insert_message(messages: &mut Vec<Message>, 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);
Expand Down
16 changes: 11 additions & 5 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ impl Target {
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Direction {
Sent,
Received,
/// is_echo
Received(bool),
}

#[derive(Debug, Clone)]
Expand All @@ -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,
Expand Down Expand Up @@ -226,6 +227,11 @@ impl Message {
) -> Option<Message> {
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,
Expand All @@ -250,7 +256,7 @@ impl Message {
Some(Message {
received_at,
server_time,
direction: Direction::Received,
direction,
target,
content,
id,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -336,7 +342,7 @@ impl Message {
Self {
received_at,
server_time,
direction: Direction::Received,
direction: Direction::Received(false),
target: Target::Logs,
content,
id: None,
Expand Down
2 changes: 1 addition & 1 deletion data/src/message/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn expand(
Message {
received_at,
server_time: sent_time,
direction: Direction::Received,
direction: Direction::Received(false),
target,
content,
id: None,
Expand Down

0 comments on commit 6f50724

Please sign in to comment.