Notifications: render notifications for kinds 1, 6, and 7 events

This commit is contained in:
Alex Gleason
2024-05-02 14:36:28 -05:00
parent 3d70edfb39
commit 220f16feb8
5 changed files with 130 additions and 25 deletions

View File

@@ -1,20 +1,40 @@
import { type AppController } from '@/app.ts';
import { Storages } from '@/storages.ts';
import { NostrFilter } from '@nostrify/nostrify';
import { AppContext, AppController } from '@/app.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginated, paginationSchema } from '@/utils/api.ts';
import { renderNotification } from '@/views/mastodon/notifications.ts';
const notificationsController: AppController = async (c) => {
const notificationsController: AppController = (c) => {
const pubkey = c.get('pubkey')!;
const { since, until } = paginationSchema.parse(c.req.query());
const { signal } = c.req.raw;
const events = await Storages.db.query(
[{ kinds: [1], '#p': [pubkey], since, until }],
{ signal },
);
const statuses = await Promise.all(events.map((event) => renderNotification(event, pubkey)));
return paginated(c, events, statuses);
return renderNotifications(c, [{ kinds: [1, 6, 7], '#p': [pubkey], since, until }]);
};
async function renderNotifications(c: AppContext, filters: NostrFilter[]) {
const store = c.get('store');
const pubkey = c.get('pubkey')!;
const { signal } = c.req.raw;
const events = await store
.query(filters, { signal })
.then((events) => events.filter((event) => event.pubkey !== pubkey))
.then((events) => hydrateEvents({ events, storage: store, signal }));
if (!events.length) {
return c.json([]);
}
const notifications = (await Promise
.all(events.map((event) => renderNotification(event, { viewerPubkey: pubkey }))))
.filter(Boolean);
if (!notifications.length) {
return c.json([]);
}
return paginated(c, events, notifications);
}
export { notificationsController };