diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index 2049b81..30c3859 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -1,7 +1,22 @@ import { type AppController } from '@/app.ts'; +import * as mixer from '@/mixer.ts'; +import { buildLinkHeader, paginationSchema } from '@/utils/web.ts'; +import { toNotification } from '@/transformers/nostr-to-mastoapi.ts'; +import { Time } from '@/utils.ts'; -const notificationsController: AppController = (c) => { - return c.json([]); +const notificationsController: AppController = async (c) => { + const pubkey = c.get('pubkey')!; + const { since, until } = paginationSchema.parse(c.req.query()); + + const events = await mixer.getFilters( + [{ kinds: [1], '#p': [pubkey], since, until }], + { timeout: Time.seconds(3) }, + ); + + const statuses = await Promise.all(events.map(toNotification)); + + const link = buildLinkHeader(c.req.url, events); + return c.json(statuses, 200, link ? { link } : undefined); }; export { notificationsController }; diff --git a/src/transformers/nostr-to-mastoapi.ts b/src/transformers/nostr-to-mastoapi.ts index 2c474e1..79d4596 100644 --- a/src/transformers/nostr-to-mastoapi.ts +++ b/src/transformers/nostr-to-mastoapi.ts @@ -276,4 +276,24 @@ async function toRelationship(sourcePubkey: string, targetPubkey: string) { }; } -export { toAccount, toRelationship, toStatus }; +function toNotification(event: Event) { + switch (event.kind) { + case 1: + return toNotificationMention(event as Event<1>); + } +} + +async function toNotificationMention(event: Event<1>) { + const status = await toStatus(event); + if (!status) return; + + return { + id: event.id, + type: 'mention', + created_at: nostrDate(event.created_at).toISOString(), + account: status.account, + status: status, + }; +} + +export { toAccount, toNotification, toRelationship, toStatus };