Add interfaces for Mastodon entity types, hide deactivated accounts

This commit is contained in:
Alex Gleason
2024-06-15 19:36:34 -05:00
parent 06db5d2b1d
commit e63ee9b5a3
7 changed files with 143 additions and 27 deletions

View File

@@ -3,6 +3,7 @@ import { escape } from 'entities';
import { nip19, UnsignedEvent } from 'nostr-tools';
import { Conf } from '@/config.ts';
import { MastodonAccount } from '@/entities/MastodonAccount.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { getLnurl } from '@/utils/lnurl.ts';
import { nip05Cache } from '@/utils/nip05.ts';
@@ -17,10 +18,17 @@ interface ToAccountOpts {
async function renderAccount(
event: Omit<DittoEvent, 'id' | 'sig'>,
opts: ToAccountOpts = {},
) {
): Promise<MastodonAccount> {
const { withSource = false } = opts;
const { pubkey } = event;
const names = getTagSet(event.user?.tags ?? [], 'n');
if (names.has('disabled') || names.has('suspended')) {
const account = await accountFromPubkey(pubkey, opts);
account.pleroma.deactivated = true;
return account;
}
const {
name,
nip05,
@@ -34,7 +42,6 @@ async function renderAccount(
const npub = nip19.npubEncode(pubkey);
const parsed05 = await parseAndVerifyNip05(nip05, pubkey);
const names = getTagSet(event.user?.tags ?? [], 'n');
return {
id: pubkey,
@@ -77,6 +84,7 @@ async function renderAccount(
accepts_zaps: Boolean(getLnurl({ lud06, lud16 })),
},
pleroma: {
deactivated: names.has('disabled') || names.has('suspended'),
is_admin: names.has('admin'),
is_moderator: names.has('admin') || names.has('moderator'),
is_suggested: names.has('suggested'),
@@ -92,7 +100,7 @@ async function renderAccount(
};
}
function accountFromPubkey(pubkey: string, opts: ToAccountOpts = {}) {
function accountFromPubkey(pubkey: string, opts: ToAccountOpts = {}): Promise<MastodonAccount> {
const event: UnsignedEvent = {
kind: 0,
pubkey,

View File

@@ -2,6 +2,8 @@ import { NostrEvent } from '@nostrify/nostrify';
import { nip19 } from 'nostr-tools';
import { Conf } from '@/config.ts';
import { MastodonMention } from '@/entities/MastodonMention.ts';
import { MastodonStatus } from '@/entities/MastodonStatus.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { Storages } from '@/storages.ts';
import { nostrDate } from '@/utils.ts';
@@ -17,7 +19,7 @@ interface RenderStatusOpts {
depth?: number;
}
async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<any> {
async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<MastodonStatus | undefined> {
const { viewerPubkey, depth = 1 } = opts;
if (depth > 2 || depth < 0) return;
@@ -130,12 +132,14 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
};
}
async function renderReblog(event: DittoEvent, opts: RenderStatusOpts) {
async function renderReblog(event: DittoEvent, opts: RenderStatusOpts): Promise<MastodonStatus | undefined> {
const { viewerPubkey } = opts;
if (!event.repost) return;
const status = await renderStatus(event, {}); // omit viewerPubkey intentionally
const reblog = await renderStatus(event.repost, { viewerPubkey });
if (!status) return;
const reblog = await renderStatus(event.repost, { viewerPubkey }) ?? null;
return {
...status,
@@ -145,7 +149,7 @@ async function renderReblog(event: DittoEvent, opts: RenderStatusOpts) {
};
}
async function toMention(pubkey: string, event?: NostrEvent) {
async function toMention(pubkey: string, event?: NostrEvent): Promise<MastodonMention> {
const account = event ? await renderAccount(event) : undefined;
if (account) {
@@ -166,9 +170,7 @@ async function toMention(pubkey: string, event?: NostrEvent) {
}
}
type Mention = Awaited<ReturnType<typeof toMention>>;
function buildInlineRecipients(mentions: Mention[]): string {
function buildInlineRecipients(mentions: MastodonMention[]): string {
if (!mentions.length) return '';
const elements = mentions.reduce<string[]>((acc, { url, username }) => {