//! When a share link stops opening — the ONE rule, for every kind of link. //! //! The sharing spec (2026-09-06, S-D8) makes the expiring link the fallback //! for people without a Cotton app: the slug index row gains `expires_at` //! (default 7 days, settable, `null` = until revoked) checked on every request //! like the revoke is, so "auto-expires" is true. Every index row that can //! expire carries the column; this module is what "checked" means, so no two //! of them can count a day differently. //! //! Registry-only: `expires_at` is a column in `registry.db`, never in the //! person's file (S-D12) — the file has no expiry today and that spec moves no //! migration for one. use chrono::{DateTime, Utc}; use serde_json::Value; use crate::clock::iso; /// Seven days, when the caller does not say. pub const DEFAULT_SHARE_EXPIRY_DAYS: i64 = 7; /// A year is the longest a link may be given up front; longer is "until /// revoked", said so. pub const MAX_SHARE_EXPIRY_DAYS: i64 = 365; /// How long a link lives, once a caller's value has been read. /// /// The TypeScript's `number | null | undefined` collapses to two layers here: /// `parse_expires_in_days` answers the `undefined` case by returning the /// default, so what travels onward is only ever "a number of days" or "until /// revoked". pub type ExpiresInDays = Option; /// Read `expiresInDays` off a request body or tool arguments. /// /// A REASON rather than a throw, because the two surfaces that call this (an /// owner route and an MCP verb) refuse in different envelopes. /// /// `value` is what the body held at that key — `None` for a key that was not /// there at all, which is the TypeScript's `undefined` and means "the default". /// A JSON `null` is the deliberate "until revoked" and is a different answer. pub fn parse_expires_in_days(value: Option<&Value>) -> Result { match value { None => Ok(Some(DEFAULT_SHARE_EXPIRY_DAYS)), Some(Value::Null) => Ok(None), Some(Value::Number(number)) => match whole_number(number) { // `Number.isInteger` is a question about the VALUE, not about how // the JSON was spelled: JavaScript reads `7.0` as the integer 7 and // so does this, or a body a browser serialised from a float would // be refused for a difference nobody can see. Some(days) if (1..=MAX_SHARE_EXPIRY_DAYS).contains(&days) => Ok(Some(days)), _ => Err(refusal()), }, Some(_) => Err(refusal()), } } fn refusal() -> String { format!( "expiresInDays must be a whole number of days from 1 to {MAX_SHARE_EXPIRY_DAYS}, \ or null for a link that stays open until you revoke it" ) } fn whole_number(number: &serde_json::Number) -> Option { if let Some(value) = number.as_i64() { return Some(value); } let value = number.as_f64()?; if value.fract() == 0.0 && value.abs() < 9.007_199_254_740_992e15 { Some(value as i64) } else { None } } /// The instant a link minted now stops opening, or `None` for "until revoked". pub fn expires_at_from(now: DateTime, days: ExpiresInDays) -> Option { let days = days?; Some(iso(now + chrono::Duration::days(days))) } /// Has this link's time run out? /// /// `expires_at` is an ISO instant or null. Null never expires. A value that is /// not a date is read as EXPIRED — a column somebody edited by hand into junk /// must close the link, not open it for ever; the same direction /// `resolve_share` takes for every other doubt. pub fn is_expired(expires_at: Option<&str>, now: DateTime) -> bool { let Some(expires_at) = expires_at else { return false; }; match DateTime::parse_from_rfc3339(expires_at) { Ok(at) => at.with_timezone(&Utc) <= now, Err(_) => true, } } #[cfg(test)] #[path = "expiry_tests.rs"] mod expiry_tests;