Cotton code
107 lines · 4.1 KB · RustRaw
//! Unread, as a cursor.//!//! The port of `packages/core/src/messages/unread.ts`.//!//! Until v9 unread was "inbound live rows whose `read_at` is null", and marking a//! thread read wrote one op PER ROW — capped at 500 on the server because the op//! log is what every device replays forever. From v9 the thread carries a//! `read_seq` and marking a thread read is ONE patch, whatever the backlog.//!//! The unit is the server's per-conversation `seq`, not a clock: two phones with//! skewed clocks would disagree about "before", and a receipt cursor has to mean//! the same thing in every file that holds the conversation.use cotton_store::protocol::js::parse_date_ms;use crate::constants::MUTE_ALWAYS_UNTIL;/// How many inbound live messages sit past the read cursor.////// A row with NO seq is never counted. `None` means the server has not folded it/// — an outbound row still in the outbox, or an imported archive row that never/// travelled — and a row nobody else has seen cannot be one this person owes a/// reply to.pub fn unread_count(read_seq: i64, inbound_live_seqs: &[Option<i64>]) -> usize {    inbound_live_seqs        .iter()        .filter(|seq| seq.is_some_and(|seq| seq > read_seq))        .count()}/// The seq the "N unread messages" divider sits above, or `None`.pub fn first_unread_seq(read_seq: i64, inbound_live_seqs: &[Option<i64>]) -> Option<i64> {    inbound_live_seqs        .iter()        .filter_map(|seq| *seq)        .filter(|seq| *seq > read_seq)        .min()}/// The cursor after a read.////// Monotonic, always: two devices reading the same thread at different scroll/// positions must not let the one further back drag the cursor down, and a fold/// of the other device's older patch must be a no-op rather than a resurrection/// of read messages.pub fn next_read_seq(current: i64, up_to: i64) -> i64 {    current.max(up_to)}#[derive(Debug, Clone, PartialEq, Eq)]pub struct ThreadUnread<'a> {    pub read_seq: i64,    /// The person said "mark unread" — a deliberate flag, not a count.    pub unread_marked_at: Option<&'a str>,    pub unread_count: usize,}/// Whether the list row wears an unread mark.pub fn is_thread_unread(thread: &ThreadUnread<'_>) -> bool {    thread.unread_count > 0 || thread.unread_marked_at.is_some()}/// The frozen far date means "always", not "until the year 9999".////// Compared as an INSTANT, not as a string, because the sentinel reaches a file/// in two spellings and both mean forever. The store's timestamp codec normalises/// everything it writes to `…T00:00:00.000Z`, so a mute the person sets today/// comes back with milliseconds; the v9 migrator writes the constant's own/// `…T00:00:00Z` straight through SQL, so a file migrated from v8 does not. A/// string comparison would call one of them "muted until the year 9999" and/// render a date nobody chose.pub fn is_muted_forever(muted_until: Option<&str>) -> bool {    let Some(muted_until) = muted_until else {        return false;    };    // `Date.parse(MUTE_ALWAYS_UNTIL)` is a module-level constant in the    // TypeScript. Recomputed here per call rather than cached in a `OnceLock`:    // the parse is a few dozen bytes of arithmetic, and a lazily-initialised    // global would be state in a crate whose whole claim is that it has none.    let always = parse_date_ms(MUTE_ALWAYS_UNTIL);    match (parse_date_ms(muted_until), always) {        (Some(at), Some(always)) => at == always,        _ => false,    }}/// Whether this thread is muted right now.////// A timestamp that will not parse reads as NOT muted: the failure mode of a/// wrong "false" is a notification the person did not want, and of a wrong "true"/// is a message they never learn about. The first is recoverable.pub fn is_muted(muted_until: Option<&str>, now: &str) -> bool {    let Some(muted_until) = muted_until else {        return false;    };    if is_muted_forever(Some(muted_until)) {        return true;    }    match (parse_date_ms(muted_until), parse_date_ms(now)) {        (Some(until), Some(at)) => until > at,        _ => false,    }}#[cfg(test)]#[path = "unread_tests.rs"]mod tests;