Add GET /api/v1/notifications/:id endpoint

This commit is contained in:
Alex Gleason 2024-10-14 17:27:30 -05:00
parent 30a5d9a20f
commit a7d8d86fa7
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 29 additions and 2 deletions

View file

@ -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);

View file

@ -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<string>,
@ -106,4 +131,4 @@ async function renderNotifications(
return paginated(c, events, notifications);
}
export { notificationsController };
export { notificationController, notificationsController };