Cotton code
67 lines · 3.0 KB · TypeScriptRaw
/** * A phone is rung by its platform's push (Cotton Calling, 12 Nov 2026): an * iPhone by an APNs VoIP push (src/calling/apns.ts), an Android phone by an * FCM high-priority data message (src/calling/fcm.ts). Both carry the same * two payloads — ring `{ room, video, box? }` and stop `{ room, stop }` — and * neither names who is calling in the clear: the ring's `box` is sealed for * the rung phone's own chat key (src/chat/seal.ts), so it can name the caller * at once; the push services read nothing of it. */import type { Env } from "../env.ts";import { canPush, pushVoip } from "./apns.ts";import { canPushAndroid, pushFcm } from "./fcm.ts";export type Platform = "ios" | "android";export const PLATFORMS: Platform[] = ["ios", "android"];export interface PushDevice {  platform: Platform;  token: string;  /** iOS: the app's bundle id; the push topic is `<topic>.voip`. */  topic: string;  /** iOS: a development build's token, pushed through Apple's sandbox. */  sandbox: boolean;}/** A ring may carry `box`: who is calling, sealed by the object for that phone's chat key (src/chat/seal.ts), which Apple and Google cannot read. */export type PushPayload = { room: string; video: boolean; box?: string; at?: number } | { room: string; stop: string; box?: string; at?: number; ended?: number };/** * A payload as the FCM data message and the socket frame carry it: strings only. A stop to a phone that speaks the new * way carries the caller sealed and the ring's own times (`at`, `ended`, ms), so its Missed row says when it rang. */export function frameData(payload: PushPayload, kind: "stop" | "missed" = "stop"): Record<string, string> {  if (!("stop" in payload)) return { kind: "ring", room: payload.room, video: payload.video ? "1" : "0", ...(payload.box ? { box: payload.box } : {}), ...(payload.at !== undefined ? { at: String(payload.at) } : {}) };  return {    kind,    room: payload.room,    stop: payload.stop,    ...(payload.box ? { box: payload.box } : {}),    ...(payload.at !== undefined ? { at: String(payload.at) } : {}),    ...(payload.ended !== undefined ? { ended: String(payload.ended) } : {}),  };}/** What a push came to: sent, the token gone (drop its row), or refused for another reason (kept). */export type PushOutcome = "sent" | "gone" | "failed";export interface PushSender {  /** Whether this Worker holds what the sender needs. */  ready(env: Env): boolean;  send(env: Env, device: PushDevice, payload: PushPayload): Promise<PushOutcome>;}export const SENDERS: Record<Platform, PushSender> = {  ios: { ready: canPush, send: pushVoip },  android: { ready: canPushAndroid, send: pushFcm },};/** Whether any phone of these platforms can be pushed from here. */export function canRing(env: Env, platforms: Platform[]): boolean {  return platforms.some((p) => SENDERS[p].ready(env));}export function push(env: Env, device: PushDevice, payload: PushPayload): Promise<PushOutcome> {  const sender = SENDERS[device.platform];  return sender.ready(env) ? sender.send(env, device, payload) : Promise.resolve("failed");}