Cotton code
106 lines · 4.0 KB · RustRaw
//! How long a thing you said stays changeable.//!//! The port of `packages/core/src/messages/editWindow.ts`.//!//! Two windows, both the messenger conventions a thumb already knows: 15 minutes//! to fix a typo, 48 hours to take a message back from everyone. They are product//! rules, not constraints — the file will happily hold an `edited_at` from a year//! later — so this module is the only place that says no, and both the composer//! (which hides the menu item) and the service (which refuses the write) ask it.//!//! `now` is a parameter on every function. A default of `new Date()` inside a//! pure rule is a test that passes today and fails on a date somebody can//! calculate, which is the exact failure this codebase has already been bitten//! by. The instants arrive as the ISO STRINGS the row holds, parsed with//! [`cotton_store::protocol::js::parse_date_ms`] — `Date.parse`, refusing exactly//! where JavaScript gives `NaN`.use cotton_store::protocol::js::parse_date_ms;use crate::constants::{MessageDirection, MessageOrigin};/// 15 minutes.pub const EDIT_WINDOW_MS: i64 = 15 * 60 * 1000;/// 48 hours.pub const DELETE_FOR_EVERYONE_WINDOW_MS: i64 = 48 * 60 * 60 * 1000;#[derive(Debug, Clone, PartialEq, Eq)]pub struct WindowSubject<'a> {    pub direction: MessageDirection,    pub sent_at: &'a str,    pub deleted_at: Option<&'a str>,    /// An imported row is somebody's archive — it was never ours to change.    pub origin: Option<MessageOrigin>,}/// Milliseconds between two instants, or `None` if either will not parse.////// `None` rather than NaN, and refused rather than defaulted: an unparseable/// timestamp must close a window, never open one. A bubble whose menu offered/// "Edit" because a date failed to parse would produce a write the server then/// refuses, which the person reads as the app lying to them.fn elapsed(sent_at: &str, now: &str) -> Option<i64> {    let from = parse_date_ms(sent_at)?;    let to = parse_date_ms(now)?;    Some(to - from)}fn within_window(subject: &WindowSubject<'_>, now: &str, window: i64) -> bool {    if subject.direction != MessageDirection::Out {        return false;    }    // The TypeScript's `if (subject.deletedAt)` is a truthiness test, so an empty    // string is NOT a deletion — a row whose column came back `''` still has an    // editable message in it.    if subject.deleted_at.is_some_and(|at| !at.is_empty()) {        return false;    }    if subject.origin == Some(MessageOrigin::Imported) {        return false;    }    let Some(age) = elapsed(subject.sent_at, now) else {        return false;    };    // A negative age means the row's clock is ahead of ours — a device with a    // wrong clock, or a mirror the relay has not clamped. Refuse, rather than    // leave the window open until its clock catches up.    if age < 0 {        return false;    }    age <= window}/// Whether this message may still be edited.pub fn can_edit(subject: &WindowSubject<'_>, now: &str) -> bool {    within_window(subject, now, EDIT_WINDOW_MS)}/// Whether this message may still be taken back from everyone.pub fn can_delete_for_everyone(subject: &WindowSubject<'_>, now: &str) -> bool {    within_window(subject, now, DELETE_FOR_EVERYONE_WINDOW_MS)}/// How long is left to edit, for a countdown. Floors at zero, never negative.////// An unparseable instant reads as zero — no window at all — for the same reason/// [`elapsed`] answers `None`: the countdown must never invite a write the server/// will refuse.pub fn edit_window_remaining_ms(sent_at: &str, now: &str) -> i64 {    match elapsed(sent_at, now) {        Some(age) => (EDIT_WINDOW_MS - age).max(0),        None => 0,    }}/// How long is left to take a message back from everyone. Floors at zero; an unparseable instant is no window.pub fn delete_window_remaining_ms(sent_at: &str, now: &str) -> i64 {    match elapsed(sent_at, now) {        Some(age) => (DELETE_FOR_EVERYONE_WINDOW_MS - age).max(0),        None => 0,    }}#[cfg(test)]#[path = "edit_window_tests.rs"]mod tests;