Add Web Push skeleton

This commit is contained in:
Alex Gleason 2024-09-30 19:12:53 -05:00
parent be5350a0a5
commit bece384124
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 83 additions and 0 deletions

View file

@ -68,6 +68,7 @@ import {
updateConfigController,
} from '@/controllers/api/pleroma.ts';
import { preferencesController } from '@/controllers/api/preferences.ts';
import { pushSubscribeController } from '@/controllers/api/push.ts';
import { deleteReactionController, reactionController, reactionsController } from '@/controllers/api/reactions.ts';
import { relayController } from '@/controllers/nostr/relay.ts';
import {
@ -262,6 +263,8 @@ app.get('/api/v1/mutes', requireSigner, mutesController);
app.get('/api/v1/markers', requireProof(), markersController);
app.post('/api/v1/markers', requireProof(), updateMarkersController);
app.post('/api/v1/push/subscription', requireSigner, pushSubscribeController);
app.get('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/reactions', reactionsController);
app.get('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/reactions/:emoji', reactionsController);
app.put('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/reactions/:emoji', requireSigner, reactionController);

View file

@ -0,0 +1,39 @@
import { z } from 'zod';
import { AppController } from '@/app.ts';
import { parseBody } from '@/utils/api.ts';
const pushSubscribeSchema = z.object({
subscription: z.object({
endpoint: z.string(),
keys: z.object({
p256dh: z.string(),
auth: z.string(),
}),
data: z.object({
alerts: z.object({
mention: z.boolean().optional(),
status: z.boolean().optional(),
reblog: z.boolean().optional(),
follow: z.boolean().optional(),
follow_request: z.boolean().optional(),
favourite: z.boolean().optional(),
poll: z.boolean().optional(),
update: z.boolean().optional(),
'admin.sign_up': z.boolean().optional(),
'admin.report': z.boolean().optional(),
}).optional(),
policy: z.enum(['all', 'followed', 'follower', 'none']).optional(),
}),
}),
});
export const pushSubscribeController: AppController = async (c) => {
const data = pushSubscribeSchema.safeParse(await parseBody(c.req.raw));
if (!data.success) {
return c.json({ error: 'Invalid request', schema: data.error }, 400);
}
return c.json({});
};

View file

@ -9,6 +9,7 @@ export interface DittoTables extends NPostgresSchema {
event_stats: EventStatsRow;
pubkey_domains: PubkeyDomainRow;
event_zaps: EventZapRow;
push_subscriptions: PushSubscriptionRow;
}
type NostrEventsRow = NPostgresSchema['nostr_events'] & {
@ -55,3 +56,28 @@ interface EventZapRow {
amount_millisats: number;
comment: string;
}
interface PushSubscriptionRow {
id: bigint;
pubkey: string;
endpoint: string;
key_p256dh: string;
key_auth: string;
data: {
alerts?: {
mention?: boolean;
status?: boolean;
reblog?: boolean;
follow?: boolean;
follow_request?: boolean;
favourite?: boolean;
poll?: boolean;
update?: boolean;
'admin.sign_up'?: boolean;
'admin.report'?: boolean;
};
policy?: 'all' | 'followed' | 'follower' | 'none';
} | null;
created_at: Date;
updated_at: Date;
}

15
src/types/MastodonPush.ts Normal file
View file

@ -0,0 +1,15 @@
/**
* Mastodon push payload.
*
* This is the object the server sends to the client (with the Web Push API)
* to notify of a new push event.
*/
export interface MastodonPush {
access_token: string;
preferred_locale: string;
notification_id: string;
notification_type: string;
icon: string;
title: string;
body: string;
}