Skip to content
87 linesCodeBlameRaw
1import { env } from "cloudflare:workers";
2import { data } from "react-router";
3
4import { notifyClient, type NotifyPreferencesChange, type PresenceChange, type PushDevice, type PushSubscriptionJson } from "@g1t/contracts";
5import type { SoundSettingsChange } from "@g1t/contracts/sounds";
6
7import type { Route } from "./+types/api";
8import { identity } from "../../lib/services.server";
9import { assertSameOrigin, requireUser } from "../../lib/session.server";
10
11function client() {
12 if (!env.NOTIFY) throw data({ error: "Notifications are not set up here." }, { status: 503 });
13 return notifyClient(env.NOTIFY);
14}
15
16/**
17 * The signed-in person's notification settings, as JSON: `GET` their
18 * status (with `?endpoint=` for whether this browser gets pushes, and how
19 * many phones do), `POST` a change: `subscribe`, `unsubscribe`,
20 * `register_device` and `unregister_device` (the phone app, with its Expo
21 * push token), `preferences`, `test`, `presence` (your status, being away,
22 * Do Not Disturb), or `sounds` (what you hear when chat moves, kept with
23 * the account by identity).
24 *
25 * The phone app calls this with its access token and no `Origin`, which
26 * the same-origin check lets through (lib/same-origin.ts).
27 */
28export async function loader({ context, request }: Route.LoaderArgs) {
29 const user = requireUser(context, request);
30 const endpoint = new URL(request.url).searchParams.get("endpoint");
31 const status = await client().status(user, endpoint);
32 return Response.json(status, { headers: { "cache-control": "no-store" } });
33}
34
35type Sent = {
36 intent?: string;
37 subscription?: PushSubscriptionJson & { expirationTime?: number | null };
38 endpoint?: string;
39 device?: PushDevice;
40 token?: string;
41 preferences?: NotifyPreferencesChange;
42 change?: PresenceChange | SoundSettingsChange;
43};
44
45export async function action({ context, request }: Route.ActionArgs) {
46 assertSameOrigin(request);
47 const user = requireUser(context, request);
48 const sent = (await request.json().catch(() => ({}))) as Sent;
49 const notify = client();
50 switch (sent.intent) {
51 case "subscribe": {
52 const s = sent.subscription;
53 if (!s?.endpoint || !s.keys?.p256dh || !s.keys?.auth) return Response.json({ ok: false }, { status: 400 });
54 const subscription: PushSubscriptionJson = { endpoint: s.endpoint, keys: { p256dh: s.keys.p256dh, auth: s.keys.auth } };
55 return Response.json(await notify.subscribe(user, subscription, request.headers.get("user-agent")));
56 }
57 case "unsubscribe":
58 return Response.json(await notify.unsubscribe(user, String(sent.endpoint ?? "")));
59 case "register_device": {
60 const d = sent.device;
61 if (typeof d?.token !== "string" || (d.platform !== "android" && d.platform !== "ios")) return Response.json({ ok: false }, { status: 400 });
62 const device: PushDevice = { token: d.token, platform: d.platform, name: typeof d.name === "string" ? d.name : null };
63 const registered = await notify.registerDevice(user, device);
64 // The service checks the token is Expo's.
65 return Response.json(registered, { status: registered.ok ? 200 : 400 });
66 }
67 case "unregister_device":
68 return Response.json(await notify.unregisterDevice(user, String(sent.token ?? "")));
69 case "preferences":
70 return Response.json(await notify.setPreferences(user, sent.preferences ?? {}));
71 case "test":
72 return Response.json(await notify.test(user));
73 case "sounds": {
74 const saved = await identity.setSoundSettings(user, (sent.change ?? {}) as SoundSettingsChange);
75 if (!saved.ok) return Response.json({ ok: false, error: saved.error.message }, { status: 400 });
76 return Response.json(saved.value);
77 }
78 case "presence": {
79 // Set by hand here: a calendar's or an integration's comes through their own door.
80 const change = (sent.change ?? {}) as PresenceChange;
81 const status = change.status ? { ...change.status, source: "manual" as const } : change.status;
82 return Response.json(await notify.setPresence(user, { ...change, ...("status" in change ? { status } : {}) }));
83 }
84 default:
85 return Response.json({ ok: false }, { status: 400 });
86 }
87}