feat(views): render and return zap notification

This commit is contained in:
P. Reis 2024-09-02 09:53:33 -03:00
parent 729471d692
commit 96e99f38c4

View file

@ -1,13 +1,19 @@
import { NostrEvent } from '@nostrify/nostrify';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { Conf } from '@/config.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { nostrDate } from '@/utils.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
interface RenderNotificationOpts {
export interface RenderNotificationOpts {
viewerPubkey: string;
zap?: {
zapSender?: NostrEvent | NostrEvent['pubkey']; // kind 0 or pubkey
zappedPost?: NostrEvent;
amount?: number;
message?: string;
};
}
function renderNotification(event: DittoEvent, opts: RenderNotificationOpts) {
@ -32,6 +38,10 @@ function renderNotification(event: DittoEvent, opts: RenderNotificationOpts) {
if (event.kind === 30360 && event.pubkey === Conf.pubkey) {
return renderNameGrant(event);
}
if (event.kind === 9735) {
return renderZap(event, opts);
}
}
async function renderMention(event: DittoEvent, opts: RenderNotificationOpts) {
@ -109,6 +119,27 @@ async function renderNameGrant(event: DittoEvent) {
};
}
async function renderZap(event: DittoEvent, opts: RenderNotificationOpts) {
if (!opts.zap?.zapSender) return;
const { amount = 0, message = '' } = opts.zap;
if (amount < 1) return;
const account = typeof opts.zap.zapSender !== 'string'
? await renderAccount(opts.zap.zapSender)
: await accountFromPubkey(opts.zap.zapSender);
return {
id: notificationId(event),
type: 'ditto:zap',
amount,
message,
created_at: nostrDate(event.created_at).toISOString(),
account,
...(opts.zap?.zappedPost ? { status: await renderStatus(opts.zap?.zappedPost, opts) } : {}),
};
}
/** This helps notifications be sorted in the correct order. */
function notificationId({ id, created_at }: NostrEvent): string {
return `${created_at}-${id}`;