Merge branch 'fix-zap-amount-zero' into 'main'

Get zap amount from zap receipt if not present in zap request

See merge request soapbox-pub/ditto!385
This commit is contained in:
Alex Gleason
2024-06-18 00:16:09 +00:00
5 changed files with 60 additions and 12 deletions

View File

@@ -8,8 +8,9 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
import { getAmount } from '@/utils/bolt11.ts';
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
import { renderEventAccounts } from '@/views.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
import { Storages } from '@/storages.ts';
@@ -547,15 +548,22 @@ const zappedByController: AppController = async (c) => {
const store = await Storages.db();
const amountSchema = z.coerce.number().int().nonnegative().catch(0);
const events: DittoEvent[] = (await store.query([{ kinds: [9735], '#e': [id], limit: 100 }])).map((event) => {
const zapRequest = event.tags.find(([name]) => name === 'description')?.[1];
if (!zapRequest) return;
const events = (await store.query([{ kinds: [9735], '#e': [id], limit: 100 }])).map((event) => {
const zapRequestString = event.tags.find(([name]) => name === 'description')?.[1];
if (!zapRequestString) return;
try {
return JSON.parse(zapRequest);
const zapRequest = n.json().pipe(n.event()).parse(zapRequestString);
const amount = zapRequest?.tags.find(([name]: any) => name === 'amount')?.[1];
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);
}).filter(Boolean) as DittoEvent[];
await hydrateEvents({ events, store });

19
src/utils/bolt11.test.ts Normal file
View File

@@ -0,0 +1,19 @@
import { assertEquals } from '@std/assert';
import { getAmount } from '@/utils/bolt11.ts';
Deno.test('Invoice is invalid', () => {
assertEquals(getAmount('hello'), undefined);
});
Deno.test('Invoice is undefined', () => {
assertEquals(getAmount(undefined), undefined);
});
Deno.test('Amount is 200000', () => {
assertEquals(
getAmount(
'lnbc2u1pn8qatypp5dweqaltlry2vgpxxyc0puxnc50335yznevj2g46wrhfm2694lhgqhp576ekte7lhhtsxdk6tfvkpyp8gdk2xccmuccdxwjd0fqdh34wfseqcqzzsxqyz5vqsp5n44zva7xndawg5l2r9d85v0tszwejtfzkc7v90d6c7d3nsdt0qds9qxpqysgqx2v2artsxmnfkpapdm9f5pahjs8etlpe7kcjue2kffhjg3jrtearstjvenr6lxzhpw3es4hpchzzeet7ul88elurfmvr9v94v0655rgpy7m7r5',
),
'200000',
);
});

17
src/utils/bolt11.ts Normal file
View File

@@ -0,0 +1,17 @@
import bolt11 from 'light-bolt11-decoder';
/** Decodes the invoice and returns the amount in millisatoshis */
function getAmount(invoice: string | undefined): string | undefined {
if (!invoice) return;
try {
const amount = (bolt11.decode(invoice).sections as { name: string; value: string }[]).find(
({ name }) => name === 'amount',
)?.value;
return amount;
} catch {
return;
}
}
export { getAmount };