ditto/src/views/mastodon/notifications.ts

89 lines
2.5 KiB
TypeScript

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 {
viewerPubkey: string;
}
function renderNotification(event: DittoEvent, opts: RenderNotificationOpts) {
const mentioned = !!event.tags.find(([name, value]) => name === 'p' && value === opts.viewerPubkey);
if (event.kind === 1 && mentioned) {
return renderMention(event, opts);
}
if (event.kind === 6) {
return renderReblog(event, opts);
}
if (event.kind === 7 && event.content === '+') {
return renderFavourite(event, opts);
}
if (event.kind === 7) {
return renderReaction(event, opts);
}
}
async function renderMention(event: DittoEvent, opts: RenderNotificationOpts) {
const status = await renderStatus(event, opts);
if (!status) return;
return {
id: event.id,
type: 'mention',
created_at: nostrDate(event.created_at).toISOString(),
account: status.account,
status: status,
};
}
async function renderReblog(event: DittoEvent, opts: RenderNotificationOpts) {
if (event.repost?.kind !== 1) return;
const status = await renderStatus(event.repost, opts);
if (!status) return;
const account = event.author ? await renderAccount(event.author) : accountFromPubkey(event.pubkey);
return {
id: event.id,
type: 'reblog',
created_at: nostrDate(event.created_at).toISOString(),
account,
status,
};
}
async function renderFavourite(event: DittoEvent, opts: RenderNotificationOpts) {
if (event.reacted?.kind !== 1) return;
const status = await renderStatus(event.reacted, opts);
if (!status) return;
const account = event.author ? await renderAccount(event.author) : accountFromPubkey(event.pubkey);
return {
id: event.id,
type: 'favourite',
created_at: nostrDate(event.created_at).toISOString(),
account,
status,
};
}
async function renderReaction(event: DittoEvent, opts: RenderNotificationOpts) {
if (event.reacted?.kind !== 1) return;
const status = await renderStatus(event.reacted, opts);
if (!status) return;
const account = event.author ? await renderAccount(event.author) : accountFromPubkey(event.pubkey);
return {
id: event.id,
type: 'pleroma:emoji_reaction',
emoji: event.content,
created_at: nostrDate(event.created_at).toISOString(),
account,
status,
};
}
export { renderNotification };