/** * Ids two writers agree on without talking: cotton-rules' `derived_id` and * `chat_ids` (crates/cotton-rules/src/ids.rs, chat_ids.rs), asked through the * rules module. The rows the Mac already imported carry these ids, so a batch * the object applies lands on them because both run the one rule. */ import { rule } from "../rules/index.ts"; /** sha256 over the table name and the key parts joined by `|`, uuid-shaped. */ export function derivedId(table: string, parts: string[]): string { return rule("ids.derive", { table, parts }); } /** A message contact's id — `(namespace, phone_e164)`, digits only. */ export const msgContactId = (namespace: string, phone: string): string => derivedId("cotton_msg_contacts", [namespace, phone]); /** A contact reached only by a social handle — `(namespace, kind, value)`. */ export const msgContactHandleId = (namespace: string, kind: string, value: string): string => derivedId("cotton_msg_contacts", [namespace, kind, value]); /** A contact handle row's id — `(namespace, kind, value)`, value normalised. */ export const contactHandleId = (namespace: string, kind: string, value: string): string => derivedId("cotton_msg_contact_handles", [namespace, kind, value]); /** A one-to-one thread's id — `(namespace, contact)`. */ export const dmThreadId = (namespace: string, contactId: string): string => derivedId("cotton_msg_threads:dm", [namespace, contactId]); /** A group thread's id — `(namespace, external key)`. */ export const groupThreadId = (namespace: string, externalKey: string): string => derivedId("cotton_msg_threads:group", [namespace, externalKey]); /** A call log entry's id — `(namespace, source, external id)`. */ export const callLogId = (namespace: string, source: string, externalId: string): string => derivedId("cotton_call_log", [namespace, source, externalId]); /** A person handle's id — `(namespace, kind, normalised)`. */ export const personHandleId = (namespace: string, kind: string, normalised: string): string => derivedId("cotton_person_handles", [namespace, kind, normalised]); /** A detected face — the source's own face key under the record it was found on. */ export const mediaFaceId = (recordKind: string, recordId: string, source: string, sourceId: string): string => derivedId("cotton_media_faces", [recordKind, recordId, source, sourceId]); /** Where a photo or a video was seen; no source id is the empty string. */ export const mediaSightingId = (recordKind: string, recordId: string, source: string, sourceId: string | null): string => derivedId("cotton_media_sightings", [recordKind, recordId, source, sourceId ?? ""]); export const photoRecordId = (sha256: string): string => `photo:${sha256}`; export const videoRecordId = (sha256: string): string => `video:${sha256}`; /** The reaction actor that means the file's owner (`REACTION_ACTOR_SELF`). */ export const REACTION_ACTOR_SELF = "self"; /** `react::`; null where the rule refuses (no message id, an actor that is not self, a phone or a relay name). */ export function reactionRowId(messageId: string, actor: string): string | null { return rule("ids.reaction", { messageId, actor }); }