//! Derived row ids — the ones two devices must agree on without talking. //! //! The port of `packages/core/src/messages/ids.ts`; cotton-core re-exports it //! as `messages::ids`, and the Worker asks for a reaction id through the //! boundary (`ids.reaction`). //! //! A random id would make this person's phone and laptop write two rows for one //! reaction, and LWW cannot merge two rows. Deriving the id from the pair that IS //! the identity makes the second write an upsert onto the first. //! //! Both shapes are frozen (spec §4.8). The migrator spells the reaction form in //! SQL (`'react:' || m.id || ':' || …`) because a migration cannot call //! TypeScript; the surviving pin on the literal is //! `crates/cotton-core/src/messages/ids_tests.rs`. //! //! Since pairing (N-D4) a person can be a HANDLE as well as a phone, and both go //! in the same slot. They cannot collide, by GRAMMAR rather than by a CHECK: a //! phone is 8–15 digits and nothing else, a relay name always carries a //! non-digit. So each id keeps ONE function taking ONE argument — an all-digit //! string is a phone, and there is no second reading of it to disagree with. use crate::ids::{is_relay_name, REACTION_ACTOR_SELF}; /// An id nobody could split back apart, refused before it is minted. /// /// The TypeScript throws a plain `Error`; a pure Rust library returns instead, /// and `Display` is the thrown sentence VERBATIM so a handler can pass it /// straight to an error envelope. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum ChatIdError { #[error("cotton-chat: a reaction id needs a message id")] NoMessageId, #[error("cotton-chat: a receipt id needs a thread id")] NoThreadId, #[error( "cotton-chat: a reaction actor is \"{self_literal}\", a phone as digits or a relay name, got \"{actor}\"" )] BadReactionActor { /// `REACTION_ACTOR_SELF`, interpolated by the TypeScript rather than /// spelled out, so the sentence cannot drift from the constant. self_literal: &'static str, actor: String, }, #[error( "cotton-chat: a receipt reader is a phone as digits or a relay name, got \"{reader}\"" )] BadReceiptReader { reader: String }, } /// `/^[0-9]{8,15}$/` — ASCII digits, 8 to 15 of them. /// /// Written as a length-and-bytes test rather than a regex because JavaScript's /// `[0-9]` is exactly the ASCII digits: an Arabic-Indic numeral is not a phone /// number on either side, and `regex`'s Unicode-aware classes would have to be /// switched off to say the same thing. fn is_digits(value: &str) -> bool { (8..=15).contains(&value.len()) && value.bytes().all(|byte| byte.is_ascii_digit()) } /// A phone as digits, or a relay name — the two ways a person is named. fn is_actor_value(value: &str) -> bool { is_digits(value) || is_relay_name(value) } /// `react::` — one reaction per person per message. /// /// The actor is `self`, a phone as digits, or a relay name; anything else would /// put a `:` or a `+` inside the id and make it ambiguous to split, so it /// refuses rather than minting an id nobody can parse back. pub fn reaction_row_id(message_id: &str, actor: &str) -> Result { if message_id.is_empty() { return Err(ChatIdError::NoMessageId); } if actor != REACTION_ACTOR_SELF && !is_actor_value(actor) { return Err(ChatIdError::BadReactionActor { self_literal: REACTION_ACTOR_SELF, actor: actor.to_string(), }); } Ok(format!("react:{message_id}:{actor}")) } /// `receipt::` — one cursor row per reader per thread. /// /// The reader is a phone as digits or a relay name, which is also exactly what /// `cotton_msg_receipts.phone` admits since schema v45. pub fn receipt_row_id(thread_id: &str, reader: &str) -> Result { if thread_id.is_empty() { return Err(ChatIdError::NoThreadId); } if !is_actor_value(reader) { return Err(ChatIdError::BadReceiptReader { reader: reader.to_string(), }); } Ok(format!("receipt:{thread_id}:{reader}")) } #[cfg(test)] #[path = "chat_ids_tests.rs"] mod tests;