mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-17 20:34:24 +01:00
Notifications: render notifications for kinds 1, 6, and 7 events
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user