feat: nevermind, migrate push notification handling from UnifiedPush to FCM

This commit is contained in:
Linus Rath
2026-04-20 12:06:30 +02:00
parent 8b21851353
commit 578e60c0bc
10 changed files with 183 additions and 395 deletions
+10 -5
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { logger } from '@/lib/logger';
import { subscriptionStore } from '@/lib/push/store';
import { sendUnifiedPush } from '@/lib/push/unified-push';
import { sendFcmPush } from '@/lib/push/fcm';
import { isValidSubscriptionId } from '@/lib/push/validation';
import type { JmapPushBody } from '@/lib/push/types';
@@ -15,8 +15,8 @@ import type { JmapPushBody } from '@/lib/push/types';
* { "@type": "StateChange", changed: { [accountId]: { [type]: state } } }
*
* PushVerification is terminated here — cached against the subscription so
* the client can poll it. StateChange is fanned out as an Expo push so the
* mobile app wakes and re-fetches with its own credentials.
* the client can poll it. StateChange is fanned out via FCM so the mobile
* app wakes and re-fetches with its own credentials.
*/
function getAllowedOrigin(): string | null {
@@ -75,10 +75,15 @@ export async function POST(
}
if (body['@type'] === 'StateChange') {
const ok = await sendUnifiedPush(record, body);
const result = await sendFcmPush(record, body);
record.lastPushAt = Date.now();
await subscriptionStore.put(id, record);
return NextResponse.json({ ok });
if (result.unregistered) {
// FCM told us the token is no longer valid — drop the subscription
// so the device re-registers on next launch.
await subscriptionStore.delete(id);
}
return NextResponse.json({ ok: result.ok });
}
return NextResponse.json({ error: 'Unsupported JMAP push type' }, { status: 400 });
+9 -25
View File
@@ -1,45 +1,31 @@
import { NextRequest, NextResponse } from 'next/server';
import { logger } from '@/lib/logger';
import { subscriptionStore } from '@/lib/push/store';
import {
isValidAuthSecret,
isValidP256dh,
isValidPushEndpoint,
isValidSubscriptionId,
} from '@/lib/push/validation';
import { isValidFcmToken, isValidSubscriptionId } from '@/lib/push/validation';
import type { SubscriptionRecord } from '@/lib/push/types';
/**
* POST /api/push/register
* Body: { subscriptionId, endpoint, p256dh, auth, accountLabel? }
* Body: { subscriptionId, fcmToken, accountLabel? }
*
* Called by the mobile app once its UnifiedPush distributor has produced a
* push endpoint. We hold only the Web Push keys needed to encrypt payloads
* per RFC 8291 — no user credentials pass through the relay.
* Called by the mobile app once Firebase has issued an FCM registration
* token. The relay stores only the opaque token — no user credentials.
*/
export async function POST(request: NextRequest) {
try {
const body = (await request.json().catch(() => null)) as {
subscriptionId?: unknown;
endpoint?: unknown;
p256dh?: unknown;
auth?: unknown;
fcmToken?: unknown;
accountLabel?: unknown;
} | null;
if (!body) return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });
const { subscriptionId, endpoint, p256dh, auth, accountLabel } = body;
const { subscriptionId, fcmToken, accountLabel } = body;
if (!isValidSubscriptionId(subscriptionId)) {
return NextResponse.json({ error: 'Invalid subscriptionId' }, { status: 400 });
}
if (!isValidPushEndpoint(endpoint)) {
return NextResponse.json({ error: 'Invalid endpoint' }, { status: 400 });
}
if (!isValidP256dh(p256dh)) {
return NextResponse.json({ error: 'Invalid p256dh' }, { status: 400 });
}
if (!isValidAuthSecret(auth)) {
return NextResponse.json({ error: 'Invalid auth' }, { status: 400 });
if (!isValidFcmToken(fcmToken)) {
return NextResponse.json({ error: 'Invalid fcmToken' }, { status: 400 });
}
// Preserve verificationCode if a previous record exists — the JMAP server
@@ -47,9 +33,7 @@ export async function POST(request: NextRequest) {
const existing = await subscriptionStore.get(subscriptionId);
const record: SubscriptionRecord = {
endpoint,
p256dh,
auth,
fcmToken,
verificationCode: existing?.verificationCode ?? null,
createdAt: existing?.createdAt ?? Date.now(),
lastPushAt: existing?.lastPushAt ?? null,
-22
View File
@@ -1,22 +0,0 @@
import { NextResponse } from 'next/server';
import { logger } from '@/lib/logger';
import { getVapidKeys } from '@/lib/push/vapid';
/**
* GET /api/push/vapid
*
* Returns the relay's VAPID public key. The mobile app passes this to the
* UnifiedPush distributor so the distributor can lock the endpoint to pushes
* signed by us. Generated on first call; persisted under `data/push/vapid.json`.
*/
export async function GET() {
try {
const keys = await getVapidKeys();
return NextResponse.json({ publicKey: keys.publicKey });
} catch (error) {
logger.error('push: vapid key fetch failed', {
error: error instanceof Error ? error.message : 'Unknown error',
});
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}