From a7d8d86fa7598a4a15372b27f8f9b891b02102a4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 14 Oct 2024 17:27:30 -0500 Subject: [PATCH] Add GET /api/v1/notifications/:id endpoint --- src/app.ts | 4 +++- src/controllers/api/notifications.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index 1c4b79aa..b2a34765 100644 --- a/src/app.ts +++ b/src/app.ts @@ -60,7 +60,7 @@ import { import { markersController, updateMarkersController } from '@/controllers/api/markers.ts'; import { mediaController, updateMediaController } from '@/controllers/api/media.ts'; import { mutesController } from '@/controllers/api/mutes.ts'; -import { notificationsController } from '@/controllers/api/notifications.ts'; +import { notificationController, notificationsController } from '@/controllers/api/notifications.ts'; import { createTokenController, oauthAuthorizeController, oauthController } from '@/controllers/api/oauth.ts'; import { configController, @@ -273,6 +273,8 @@ app.get('/api/v1/suggestions', suggestionsV1Controller); app.get('/api/v2/suggestions', suggestionsV2Controller); app.get('/api/v1/notifications', requireSigner, notificationsController); +app.get('/api/v1/notifications/:id', requireSigner, notificationController); + app.get('/api/v1/favourites', requireSigner, favouritesController); app.get('/api/v1/bookmarks', requireSigner, bookmarksController); app.get('/api/v1/blocks', requireSigner, blocksController); diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index 144064a6..1c251563 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -74,6 +74,31 @@ const notificationsController: AppController = async (c) => { return renderNotifications(filters, types, params, c); }; +const notificationController: AppController = async (c) => { + const id = c.req.param('id'); + const pubkey = await c.get('signer')?.getPublicKey()!; + const store = c.get('store'); + + // Remove the timestamp from the ID. + const eventId = id.replace(/^\d+-/, ''); + + const [event] = await store.query([{ ids: [eventId] }]); + + if (!event) { + return c.json({ error: 'Event not found' }, { status: 404 }); + } + + await hydrateEvents({ events: [event], store }); + + const notification = await renderNotification(event, { viewerPubkey: pubkey }); + + if (!notification) { + return c.json({ error: 'Notification not found' }, { status: 404 }); + } + + return c.json(notification); +}; + async function renderNotifications( filters: NostrFilter[], types: Set, @@ -106,4 +131,4 @@ async function renderNotifications( return paginated(c, events, notifications); } -export { notificationsController }; +export { notificationController, notificationsController };