feat: add pagination and sort by amount - zapped_by endpoint

This commit is contained in:
P. Reis 2024-06-23 13:38:40 -03:00
parent 9731fc2572
commit 0d7ef68353

View file

@ -8,14 +8,21 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts'; import { DittoDB } from '@/db/DittoDB.ts';
import { getAmount } from '@/utils/bolt11.ts';
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts'; import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts'; import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
import { renderEventAccounts } from '@/views.ts'; import { renderEventAccounts } from '@/views.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts'; import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
import { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts'; import {
createEvent,
listPaginationSchema,
paginated,
paginatedList,
paginationSchema,
parseBody,
updateListEvent,
} from '@/utils/api.ts';
import { getInvoice, getLnurl } from '@/utils/lnurl.ts'; import { getInvoice, getLnurl } from '@/utils/lnurl.ts';
import { lookupPubkey } from '@/utils/lookup.ts'; import { lookupPubkey } from '@/utils/lookup.ts';
import { addTag, deleteTag } from '@/utils/tags.ts'; import { addTag, deleteTag } from '@/utils/tags.ts';
@ -545,33 +552,26 @@ const zapController: AppController = async (c) => {
const zappedByController: AppController = async (c) => { const zappedByController: AppController = async (c) => {
const id = c.req.param('id'); const id = c.req.param('id');
const params = listPaginationSchema.parse(c.req.query());
const store = await Storages.db(); const store = await Storages.db();
const amountSchema = z.coerce.number().int().nonnegative().catch(0); const db = await DittoDB.getInstance();
const events = (await store.query([{ kinds: [9735], '#e': [id], limit: 100 }])).map((event) => { const zaps = await db.selectFrom('event_zaps')
const zapRequestString = event.tags.find(([name]) => name === 'description')?.[1]; .selectAll()
if (!zapRequestString) return; .where('target_event_id', '=', id)
try { .orderBy('amount_millisats', 'desc')
const zapRequest = n.json().pipe(n.event()).parse(zapRequestString); .limit(params.limit)
const amount = zapRequest?.tags.find(([name]: any) => name === 'amount')?.[1]; .offset(params.offset).execute();
if (!amount) {
const amount = getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1]);
if (!amount) return;
zapRequest.tags.push(['amount', amount]);
}
return zapRequest;
} catch {
return;
}
}).filter(Boolean) as DittoEvent[];
await hydrateEvents({ events, store }); const authors = await store.query([{ kinds: [0], authors: zaps.map((zap) => zap.sender_pubkey) }]);
const results = (await Promise.all( const results = (await Promise.all(
events.map(async (event) => { zaps.map(async (zap) => {
const amount = amountSchema.parse(event.tags.find(([name]) => name === 'amount')?.[1]); const amount = zap.amount_millisats;
const comment = event?.content ?? ''; const comment = zap.comment;
const account = event?.author ? await renderAccount(event.author) : await accountFromPubkey(event.pubkey);
const sender = authors.find((author) => author.pubkey === zap.sender_pubkey);
const account = sender ? await renderAccount(sender) : await accountFromPubkey(zap.sender_pubkey);
return { return {
comment, comment,
@ -581,7 +581,7 @@ const zappedByController: AppController = async (c) => {
}), }),
)).filter(Boolean); )).filter(Boolean);
return c.json(results); return paginatedList(c, params, results);
}; };
export { export {