Skip to content

Commit

Permalink
Cleanup unread logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Sep 18, 2024
1 parent ae9aa40 commit 686cbf1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
15 changes: 11 additions & 4 deletions data/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,17 @@ impl History {
} => {
// Read marker is prior to last message on disk
// or prior to unflushed messages in memory
last_on_disk.zip(metadata.read_marker).map_or(
last_on_disk.is_some() && metadata.read_marker.is_none(),
|(a, b)| a > b.date_time(),
) || metadata.unread_count(messages) > 0
if let Some(read_marker) = metadata.read_marker {
last_on_disk.is_some_and(|last| read_marker.date_time() < last)
|| messages.iter().any(|message| {
read_marker.date_time() < message.server_time
&& message.triggers_unread()
})
}
// Default state == unread
else {
true
}
}
History::Full { .. } => false,
}
Expand Down
18 changes: 0 additions & 18 deletions data/src/history/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ impl Metadata {
read_marker: ReadMarker::latest(messages).or(self.read_marker),
}
}

pub fn unread_count(&self, messages: &[Message]) -> usize {
messages
.iter()
.filter(|message| self.triggers_unread(message))
.count()
}

pub fn triggers_unread(&self, message: &Message) -> bool {
message.triggers_unread() && self.after_read_marker(message)
}

pub fn after_read_marker(&self, message: &Message) -> bool {
self.read_marker.is_none()
|| self
.read_marker
.is_some_and(|read_marker| message.server_time > read_marker.0)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Deserialize, Serialize)]
Expand Down

0 comments on commit 686cbf1

Please sign in to comment.