Files
TREK/server/src/nest/notifications/push/push.controller.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

78 lines
3.2 KiB
TypeScript

import type { User } from '../../../types';
import { CurrentUser } from '../../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../../auth-core/jwt-auth.guard';
import { PushSubscribeDto, PushUnsubscribeDto } from '../notifications.dto';
import { checkPushSubscription } from './push-subscription.helpers';
import { PushSubscriptionsService } from './push-subscriptions.service';
import { PUSH_UNAVAILABLE_ERROR, PushUnavailableError, VapidKeysService } from './vapid-keys.service';
import { Body, Controller, Delete, Get, Headers, HttpCode, HttpException, Post, UseGuards } from '@nestjs/common';
import type { PushPublicKeyResult, PushSubscribeResult, PushUnsubscribeResult } from '@trek/shared';
/**
* /api/notifications/push: how a browser signs up for Web Push (#894).
*
* The browser fetches the server's public key, subscribes with its push
* service, and hands the subscription over here; logging out or switching push
* off in that browser deletes it again. Which events arrive is the ordinary
* preference matrix ('push' column), and the test send is the generic
* POST /api/notifications/test/push.
*
* No MCP tool mirrors these routes on purpose: a subscription is created by a
* browser's push manager for that one browser, and there is nothing an MCP
* client could subscribe with.
*/
@Controller('api/notifications/push')
@UseGuards(JwtAuthGuard)
export class PushController {
constructor(
private readonly keys: VapidKeysService,
private readonly subscriptions: PushSubscriptionsService,
) {}
@Get('public-key')
async publicKey(): Promise<PushPublicKeyResult> {
return { publicKey: await this.currentPublicKey() };
}
@Post('subscriptions')
@HttpCode(200)
async subscribe(
@CurrentUser() user: User,
@Body() body: PushSubscribeDto,
@Headers('user-agent') userAgent?: string,
): Promise<PushSubscribeResult> {
const checked = checkPushSubscription(body.subscription);
if ('error' in checked) throw new HttpException({ error: checked.error }, 400);
const devices = await this.subscriptions.upsert(user.id, checked.value, await this.currentPublicKey(), userAgent);
return { success: true, devices };
}
/**
* Idempotent: the push service may already have expired the row, and a
* browser logging out must not get an error for that. Only the caller's own
* endpoint can go; the same URL on someone else's account is left alone.
* Works while push is unavailable too, so a device can always be forgotten.
*/
@Delete('subscriptions')
@HttpCode(200)
async unsubscribe(@CurrentUser() user: User, @Body() body: PushUnsubscribeDto): Promise<PushUnsubscribeResult> {
await this.subscriptions.removeForUser(user.id, body.endpoint);
return { success: true };
}
/**
* The key browsers subscribe with. While the key pair cannot be used, stored
* or from VAPID_* (the log says why), the answer is a 503: nothing could be
* signed for a new subscription, and the devices already subscribed stay as
* they are.
*/
private async currentPublicKey(): Promise<string> {
try {
return await this.keys.getPublicKey();
} catch (err) {
if (err instanceof PushUnavailableError) throw new HttpException({ error: PUSH_UNAVAILABLE_ERROR }, 503);
throw err;
}
}
}