Cotton code
106 lines · 3.0 KB · TypeScriptRaw
/** * THE WIRE CONTRACT of `POST /v1/import/messages` — the shapes of * `crates/cotton-rules/src/import/envelope.rs` as serde serialises them * (camelCase), field by field; its validator runs in the rules module. A * batch that breaks the contract is refused before anything is read, naming * the first problem in the Rust's own words. */import { rule } from "../rules/index.ts";import type { HandleKind } from "./handles.ts";export type Direction = "in" | "out";export interface EnvelopeReaction {  emoji: string;  /** Canonical phone (digits, no `+`) or the literal `"owner"`. */  actor: string;  at?: string;}/** One attachment: a key the Mac already PUT, and what it is. */export interface EnvelopeMedia {  key: string;  contentType: string;  name?: string;  sizeBytes?: number;  width?: number;  height?: number;  contact?: unknown;  durationMs?: number;  playbackKey?: string;  playbackContentType?: string;  /** A sealed file's key (base64url), carried only inside a sealed row (L8). */  fileKey?: string;  /** What it is where the MIME type does not say (`voice`); from the outbox row the Mac sent. */  kind?: string;  /** A voice note's peaks, each 0..1. */  waveform?: number[];  /** A picture's or a video's small preview (base64 JPEG). */  preview?: string;}export interface EnvelopeMessage {  phone?: string;  email?: string;  senderLabel?: string;  displayName?: string;  direction: Direction;  body: string;  media?: EnvelopeMedia[];  sentAt: string;  externalId: string;  replyToExternalId?: string;  reactions?: EnvelopeReaction[];  meta?: Record<string, unknown>;}export interface EnvelopeHandle {  kind: HandleKind;  value: string;}export interface EnvelopeThreadParticipant {  label: string;  phone?: string;  email?: string;  personId?: string;  handle?: EnvelopeHandle;}export interface EnvelopeThread {  kind: "group";  title: string;  externalKey: string;  participants: EnvelopeThreadParticipant[];}export interface ImportBatch {  workspaceId: string;  channel: string;  thread?: EnvelopeThread;  dm?: EnvelopeThreadParticipant;  messages: EnvelopeMessage[];  newThreadId?: string;}export const MAX_BATCH_MESSAGES = 500;/** The problems cotton-rules' `validate_envelope_message` names, in its words. */export function validateEnvelopeMessage(value: unknown, at: string, context: "dm" | "group"): string[] {  return rule<string[]>("envelope.message", { value, at, context });}/** The problems cotton-rules' `validate_import_batch` names, in its words. */export function validateImportBatch(value: unknown): string[] {  return rule<string[]>("envelope.batch", value);}/** * The first reason a batch cannot be imported: the validator's first problem, * else what serde refuses deserialising it into the Rust's `ImportBatch` * (an attachment field it does not know, a handle that is no handle). Null for * a batch the import engine reads whole. */export function batchProblem(value: unknown): string | null {  return rule<string | null>("envelope.check", value);}