Merge branch 'nip05-request' into 'main'

Rework reports, start adding nip05 endpoints

See merge request soapbox-pub/ditto!363
This commit is contained in:
Alex Gleason 2024-06-09 15:10:30 +00:00
commit 177f25ad96
12 changed files with 273 additions and 55 deletions

View File

@ -30,7 +30,7 @@ import { adminAccountsController, adminActionController } from '@/controllers/ap
import { appCredentialsController, createAppController } from '@/controllers/api/apps.ts'; import { appCredentialsController, createAppController } from '@/controllers/api/apps.ts';
import { blocksController } from '@/controllers/api/blocks.ts'; import { blocksController } from '@/controllers/api/blocks.ts';
import { bookmarksController } from '@/controllers/api/bookmarks.ts'; import { bookmarksController } from '@/controllers/api/bookmarks.ts';
import { adminRelaysController, adminSetRelaysController } from '@/controllers/api/ditto.ts'; import { adminRelaysController, adminSetRelaysController, nameRequestController } from '@/controllers/api/ditto.ts';
import { emptyArrayController, emptyObjectController, notImplementedController } from '@/controllers/api/fallback.ts'; import { emptyArrayController, emptyObjectController, notImplementedController } from '@/controllers/api/fallback.ts';
import { instanceController } from '@/controllers/api/instance.ts'; import { instanceController } from '@/controllers/api/instance.ts';
import { markersController, updateMarkersController } from '@/controllers/api/markers.ts'; import { markersController, updateMarkersController } from '@/controllers/api/markers.ts';
@ -243,6 +243,7 @@ app.delete('/api/v1/pleroma/admin/statuses/:id', requireRole('admin'), pleromaAd
app.get('/api/v1/admin/ditto/relays', requireRole('admin'), adminRelaysController); app.get('/api/v1/admin/ditto/relays', requireRole('admin'), adminRelaysController);
app.put('/api/v1/admin/ditto/relays', requireRole('admin'), adminSetRelaysController); app.put('/api/v1/admin/ditto/relays', requireRole('admin'), adminSetRelaysController);
app.post('/api/v1/ditto/names', requireSigner, nameRequestController);
app.post('/api/v1/ditto/zap', requireSigner, zapController); app.post('/api/v1/ditto/zap', requireSigner, zapController);
app.post('/api/v1/reports', requireSigner, reportController); app.post('/api/v1/reports', requireSigner, reportController);

View File

@ -1,12 +1,13 @@
import { NostrEvent } from '@nostrify/nostrify';
import { z } from 'zod'; 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 { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { booleanParamSchema } from '@/schema.ts'; import { booleanParamSchema } from '@/schema.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { paginated, paginationSchema, parseBody, updateUser } from '@/utils/api.ts'; import { paginated, paginationSchema, parseBody, updateUser } from '@/utils/api.ts';
import { renderAdminAccount } from '@/views/mastodon/admin-accounts.ts'; import { renderAdminAccount, renderAdminAccountFromPubkey } from '@/views/mastodon/admin-accounts.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
const adminAccountQuerySchema = z.object({ const adminAccountQuerySchema = z.object({
local: booleanParamSchema.optional(), local: booleanParamSchema.optional(),
@ -35,25 +36,51 @@ const adminAccountsController: AppController = async (c) => {
} = adminAccountQuerySchema.parse(c.req.query()); } = adminAccountQuerySchema.parse(c.req.query());
// Not supported. // Not supported.
if (pending || disabled || silenced || suspended || sensitized) { if (disabled || silenced || suspended || sensitized) {
return c.json([]); return c.json([]);
} }
const store = await Storages.db(); const store = await Storages.db();
const { since, until, limit } = paginationSchema.parse(c.req.query()); const params = paginationSchema.parse(c.req.query());
const { signal } = c.req.raw; const { signal } = c.req.raw;
const events = await store.query([{ kinds: [30382], authors: [Conf.pubkey], since, until, limit }], { signal }); const pubkeys = new Set<string>();
const pubkeys = events.map((event) => event.tags.find(([name]) => name === 'd')?.[1]!); const events: NostrEvent[] = [];
const authors = await store.query([{ kinds: [0], authors: pubkeys }], { signal });
for (const event of events) { if (pending) {
const d = event.tags.find(([name]) => name === 'd')?.[1]; for (const event of await store.query([{ kinds: [3036], '#p': [Conf.pubkey], ...params }], { signal })) {
(event as DittoEvent).d_author = authors.find((author) => author.pubkey === d); pubkeys.add(event.pubkey);
events.push(event);
}
} else {
for (const event of await store.query([{ kinds: [30360], authors: [Conf.pubkey], ...params }], { signal })) {
const pubkey = event.tags.find(([name]) => name === 'd')?.[1];
if (pubkey) {
pubkeys.add(pubkey);
events.push(event);
}
}
} }
const authors = await store.query([{ kinds: [0], authors: [...pubkeys] }], { signal })
.then((events) => hydrateEvents({ store, events, signal }));
const accounts = await Promise.all( const accounts = await Promise.all(
events.map((event) => renderAdminAccount(event)), [...pubkeys].map(async (pubkey) => {
const author = authors.find((event) => event.pubkey === pubkey);
const account = author ? await renderAdminAccount(author) : await renderAdminAccountFromPubkey(pubkey);
const request = events.find((event) => event.kind === 3036 && event.pubkey === pubkey);
const grant = events.find(
(event) => event.kind === 30360 && event.tags.find(([name]) => name === 'd')?.[1] === pubkey,
);
return {
...account,
invite_request: request?.content ?? null,
invite_request_username: request?.tags.find(([name]) => name === 'r')?.[1] ?? null,
approved: !!grant,
};
}),
); );
return paginated(c, events, accounts); return paginated(c, events, accounts);

View File

@ -3,8 +3,11 @@ import { z } from 'zod';
import { AppController } from '@/app.ts'; import { AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { Storages } from '@/storages.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts';
import { Storages } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { createEvent } from '@/utils/api.ts';
import { renderNameRequest } from '@/views/ditto.ts';
const markerSchema = z.enum(['read', 'write']); const markerSchema = z.enum(['read', 'write']);
@ -58,3 +61,40 @@ function renderRelays(event: NostrEvent): RelayEntity[] {
return acc; return acc;
}, [] as RelayEntity[]); }, [] as RelayEntity[]);
} }
const nameRequestSchema = z.object({
nip05: z.string().email(),
reason: z.string().max(500).optional(),
});
export const nameRequestController: AppController = async (c) => {
const { nip05, reason } = nameRequestSchema.parse(await c.req.json());
const event = await createEvent({
kind: 3036,
content: reason,
tags: [
['r', nip05],
['L', 'nip05.domain'],
['l', nip05.split('@')[1], 'nip05.domain'],
['p', Conf.pubkey],
],
}, c);
await hydrateEvents({ events: [event], store: await Storages.db() });
const nameRequest = await renderNameRequest(event);
return c.json(nameRequest);
};
export const nameRequestsController: AppController = async (c) => {
const store = await Storages.db();
const signer = c.get('signer')!;
const pubkey = await signer.getPublicKey();
const events = await store.query([{ kinds: [3036], authors: [pubkey], limit: 20 }])
.then((events) => hydrateEvents({ events, store }));
const nameRequests = await Promise.all(events.map(renderNameRequest));
return c.json(nameRequests);
};

View File

@ -1,12 +1,14 @@
import { NSchema as n } from '@nostrify/nostrify'; import { NostrFilter, NSchema as n } from '@nostrify/nostrify';
import { z } from 'zod'; 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 { createAdminEvent, createEvent, parseBody } from '@/utils/api.ts'; import { createEvent, paginationSchema, parseBody, updateEventInfo } from '@/utils/api.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { renderAdminReport } from '@/views/mastodon/reports.ts'; import { renderAdminReport } from '@/views/mastodon/reports.ts';
import { renderReport } from '@/views/mastodon/reports.ts'; import { renderReport } from '@/views/mastodon/reports.ts';
import { getTagSet } from '@/utils/tags.ts';
import { booleanParamSchema } from '@/schema.ts';
const reportSchema = z.object({ const reportSchema = z.object({
account_id: n.id(), account_id: n.id(),
@ -52,18 +54,60 @@ const reportController: AppController = async (c) => {
return c.json(await renderReport(event)); return c.json(await renderReport(event));
}; };
const adminReportsSchema = z.object({
resolved: booleanParamSchema.optional(),
account_id: n.id().optional(),
target_account_id: n.id().optional(),
});
/** https://docs.joinmastodon.org/methods/admin/reports/#get */ /** https://docs.joinmastodon.org/methods/admin/reports/#get */
const adminReportsController: AppController = async (c) => { const adminReportsController: AppController = async (c) => {
const store = c.get('store'); const store = c.get('store');
const viewerPubkey = await c.get('signer')?.getPublicKey(); const viewerPubkey = await c.get('signer')?.getPublicKey();
const reports = await store.query([{ kinds: [1984], '#P': [Conf.pubkey] }]) const params = paginationSchema.parse(c.req.query());
.then((events) => hydrateEvents({ store, events: events, signal: c.req.raw.signal })) const { resolved, account_id, target_account_id } = adminReportsSchema.parse(c.req.query());
.then((events) =>
Promise.all( const filter: NostrFilter = {
events.map((event) => renderAdminReport(event, { viewerPubkey })), kinds: [30383],
) authors: [Conf.pubkey],
); ...params,
};
if (typeof resolved === 'boolean') {
filter['#n'] = [resolved ? 'closed' : 'open'];
}
if (account_id) {
filter['#p'] = [account_id];
}
if (target_account_id) {
filter['#P'] = [target_account_id];
}
const orig = await store.query([filter]);
const ids = new Set<string>();
for (const event of orig) {
const d = event.tags.find(([name]) => name === 'd')?.[1];
if (d) {
ids.add(d);
}
}
const events = await store.query([{ kinds: [1984], ids: [...ids] }])
.then((events) => hydrateEvents({ store, events: events, signal: c.req.raw.signal }));
const reports = await Promise.all(
events.map((event) => {
const internal = orig.find(({ tags }) => tags.some(([name, value]) => name === 'd' && value === event.id));
const names = getTagSet(internal?.tags ?? [], 'n');
return renderAdminReport(event, {
viewerPubkey,
actionTaken: names.has('closed'),
});
}),
);
return c.json(reports); return c.json(reports);
}; };
@ -82,12 +126,13 @@ const adminReportController: AppController = async (c) => {
}], { signal }); }], { signal });
if (!event) { if (!event) {
return c.json({ error: 'This action is not allowed' }, 403); return c.json({ error: 'Not found' }, 404);
} }
await hydrateEvents({ events: [event], store, signal }); await hydrateEvents({ events: [event], store, signal });
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey })); const report = await renderAdminReport(event, { viewerPubkey: pubkey });
return c.json(report);
}; };
/** https://docs.joinmastodon.org/methods/admin/reports/#resolve */ /** https://docs.joinmastodon.org/methods/admin/reports/#resolve */
@ -104,18 +149,15 @@ const adminReportResolveController: AppController = async (c) => {
}], { signal }); }], { signal });
if (!event) { if (!event) {
return c.json({ error: 'This action is not allowed' }, 403); return c.json({ error: 'Not found' }, 404);
} }
await updateEventInfo(eventId, { open: false, closed: true }, c);
await hydrateEvents({ events: [event], store, signal }); await hydrateEvents({ events: [event], store, signal });
await createAdminEvent({ const report = await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true });
kind: 5, return c.json(report);
tags: [['e', event.id]],
content: 'Report closed.',
}, c);
return c.json(await renderAdminReport(event, { viewerPubkey: pubkey, actionTaken: true }));
}; };
export { adminReportController, adminReportResolveController, adminReportsController, reportController }; export { adminReportController, adminReportResolveController, adminReportsController, reportController };

View File

@ -10,9 +10,12 @@ const nameSchema = z.string().min(1).regex(/^\w+$/);
* https://github.com/nostr-protocol/nips/blob/master/05.md * https://github.com/nostr-protocol/nips/blob/master/05.md
*/ */
const nostrController: AppController = async (c) => { const nostrController: AppController = async (c) => {
const store = c.get('store');
const result = nameSchema.safeParse(c.req.query('name')); const result = nameSchema.safeParse(c.req.query('name'));
const name = result.success ? result.data : undefined; const name = result.success ? result.data : undefined;
const pointer = name ? await localNip05Lookup(c.get('store'), name) : undefined;
const pointer = name ? await localNip05Lookup(store, name) : undefined;
if (!name || !pointer) { if (!name || !pointer) {
return c.json({ names: {}, relays: {} }); return c.json({ names: {}, relays: {} });

View File

@ -21,7 +21,6 @@ export interface DittoEvent extends NostrEvent {
author_domain?: string; author_domain?: string;
author_stats?: AuthorStats; author_stats?: AuthorStats;
event_stats?: EventStats; event_stats?: EventStats;
d_author?: DittoEvent;
user?: DittoEvent; user?: DittoEvent;
repost?: DittoEvent; repost?: DittoEvent;
quote?: DittoEvent; quote?: DittoEvent;
@ -35,4 +34,6 @@ export interface DittoEvent extends NostrEvent {
* https://github.com/nostr-protocol/nips/blob/master/56.md * https://github.com/nostr-protocol/nips/blob/master/56.md
*/ */
reported_notes?: DittoEvent[]; reported_notes?: DittoEvent[];
/** Admin event relationship. */
info?: DittoEvent;
} }

View File

@ -3,10 +3,12 @@ import Debug from '@soapbox/stickynotes/debug';
import { sql } from 'kysely'; import { sql } from 'kysely';
import { LRUCache } from 'lru-cache'; import { LRUCache } from 'lru-cache';
import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts'; import { DittoDB } from '@/db/DittoDB.ts';
import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { deleteAttachedMedia } from '@/db/unattached-media.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { RelayError } from '@/RelayError.ts'; import { RelayError } from '@/RelayError.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { eventAge, parseNip05, Time } from '@/utils.ts'; import { eventAge, parseNip05, Time } from '@/utils.ts';
@ -53,6 +55,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
await Promise.all([ await Promise.all([
storeEvent(event, signal), storeEvent(event, signal),
parseMetadata(event, signal), parseMetadata(event, signal),
generateSetEvents(event),
processMedia(event), processMedia(event),
streamOut(event), streamOut(event),
]); ]);
@ -176,4 +179,46 @@ async function streamOut(event: NostrEvent): Promise<void> {
} }
} }
async function generateSetEvents(event: NostrEvent): Promise<void> {
const tagsAdmin = event.tags.some(([name, value]) => ['p', 'P'].includes(name) && value === Conf.pubkey);
if (event.kind === 1984 && tagsAdmin) {
const signer = new AdminSigner();
const rel = await signer.signEvent({
kind: 30383,
content: '',
tags: [
['d', event.id],
['p', event.pubkey],
['k', '1984'],
['n', 'open'],
...[...getTagSet(event.tags, 'p')].map((pubkey) => ['P', pubkey]),
...[...getTagSet(event.tags, 'e')].map((pubkey) => ['e', pubkey]),
],
created_at: Math.floor(Date.now() / 1000),
});
await handleEvent(rel, AbortSignal.timeout(1000));
}
if (event.kind === 3036 && tagsAdmin) {
const signer = new AdminSigner();
const rel = await signer.signEvent({
kind: 30383,
content: '',
tags: [
['d', event.id],
['p', event.pubkey],
['k', '3036'],
['n', 'pending'],
],
created_at: Math.floor(Date.now() / 1000),
});
await handleEvent(rel, AbortSignal.timeout(1000));
}
}
export { handleEvent }; export { handleEvent };

View File

@ -29,9 +29,9 @@ class EventsDB implements NStore {
'a': ({ count }) => count < 15, 'a': ({ count }) => count < 15,
'd': ({ event, count }) => count === 0 && NKinds.parameterizedReplaceable(event.kind), 'd': ({ event, count }) => count === 0 && NKinds.parameterizedReplaceable(event.kind),
'e': ({ event, count, value }) => ((event.kind === 10003) || count < 15) && isNostrId(value), 'e': ({ event, count, value }) => ((event.kind === 10003) || count < 15) && isNostrId(value),
'k': ({ count, value }) => count === 0 && Number.isInteger(Number(value)),
'L': ({ event, count }) => event.kind === 1985 || count === 0, 'L': ({ event, count }) => event.kind === 1985 || count === 0,
'l': ({ event, count }) => event.kind === 1985 || count === 0, 'l': ({ event, count }) => event.kind === 1985 || count === 0,
'media': ({ count, value }) => (count < 4) && isURL(value),
'n': ({ count, value }) => count < 50 && value.length < 50, 'n': ({ count, value }) => count < 50 && value.length < 50,
'P': ({ count, value }) => count === 0 && isNostrId(value), 'P': ({ count, value }) => count === 0 && isNostrId(value),
'p': ({ event, count, value }) => (count < 15 || event.kind === 3) && isNostrId(value), 'p': ({ event, count, value }) => (count < 15 || event.kind === 3) && isNostrId(value),

View File

@ -44,6 +44,10 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
cache.push(event); cache.push(event);
} }
for (const event of await gatherInfo({ events: cache, store, signal })) {
cache.push(event);
}
for (const event of await gatherReportedProfiles({ events: cache, store, signal })) { for (const event of await gatherReportedProfiles({ events: cache, store, signal })) {
cache.push(event); cache.push(event);
} }
@ -83,6 +87,7 @@ export function assembleEvents(
for (const event of a) { for (const event of a) {
event.author = b.find((e) => matchFilter({ kinds: [0], authors: [event.pubkey] }, e)); event.author = b.find((e) => matchFilter({ kinds: [0], authors: [event.pubkey] }, e));
event.user = b.find((e) => matchFilter({ kinds: [30382], authors: [admin], '#d': [event.pubkey] }, e)); event.user = b.find((e) => matchFilter({ kinds: [30382], authors: [admin], '#d': [event.pubkey] }, e));
event.info = b.find((e) => matchFilter({ kinds: [30383], authors: [admin], '#d': [event.id] }, e));
if (event.kind === 1) { if (event.kind === 1) {
const id = findQuoteTag(event.tags)?.[1] || findQuoteInContent(event.content); const id = findQuoteTag(event.tags)?.[1] || findQuoteInContent(event.content);
@ -106,20 +111,21 @@ export function assembleEvents(
} }
if (event.kind === 1984) { if (event.kind === 1984) {
const targetAccountId = event.tags.find(([name]) => name === 'p')?.[1]; const pubkey = event.tags.find(([name]) => name === 'p')?.[1];
if (targetAccountId) { if (pubkey) {
event.reported_profile = b.find((e) => matchFilter({ kinds: [0], authors: [targetAccountId] }, e)); event.reported_profile = b.find((e) => matchFilter({ kinds: [0], authors: [pubkey] }, e));
} }
const reportedEvents: DittoEvent[] = [];
const status_ids = event.tags.filter(([name]) => name === 'e').map((tag) => tag[1]); const reportedEvents: DittoEvent[] = [];
if (status_ids.length > 0) { const ids = event.tags.filter(([name]) => name === 'e').map(([_name, value]) => value);
for (const id of status_ids) {
const reportedEvent = b.find((e) => matchFilter({ kinds: [1], ids: [id] }, e)); for (const id of ids) {
if (reportedEvent) reportedEvents.push(reportedEvent); const reported = b.find((e) => matchFilter({ kinds: [1], ids: [id] }, e));
if (reported) {
reportedEvents.push(reported);
} }
event.reported_notes = reportedEvents;
} }
event.reported_notes = reportedEvents;
} }
event.author_stats = stats.authors.find((stats) => stats.pubkey === event.pubkey); event.author_stats = stats.authors.find((stats) => stats.pubkey === event.pubkey);
@ -206,6 +212,26 @@ function gatherUsers({ events, store, signal }: HydrateOpts): Promise<DittoEvent
); );
} }
/** Collect info events from the events. */
function gatherInfo({ events, store, signal }: HydrateOpts): Promise<DittoEvent[]> {
const ids = new Set<string>();
for (const event of events) {
if (event.kind === 3036) {
ids.add(event.id);
}
}
if (!ids.size) {
return Promise.resolve([]);
}
return store.query(
[{ ids: [...ids], limit: ids.size }],
{ signal },
);
}
/** Collect reported notes from the events. */ /** Collect reported notes from the events. */
function gatherReportedNotes({ events, store, signal }: HydrateOpts): Promise<DittoEvent[]> { function gatherReportedNotes({ events, store, signal }: HydrateOpts): Promise<DittoEvent[]> {
const ids = new Set<string>(); const ids = new Set<string>();

View File

@ -107,12 +107,20 @@ async function updateAdminEvent<E extends EventStub>(
return createAdminEvent(fn(prev), c); return createAdminEvent(fn(prev), c);
} }
async function updateUser(pubkey: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> { function updateUser(pubkey: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
return updateNames(30382, pubkey, n, c);
}
function updateEventInfo(id: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
return updateNames(30383, id, n, c);
}
async function updateNames(k: number, d: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
const signer = new AdminSigner(); const signer = new AdminSigner();
const admin = await signer.getPublicKey(); const admin = await signer.getPublicKey();
return updateAdminEvent( return updateAdminEvent(
{ kinds: [30382], authors: [admin], '#d': [pubkey], limit: 1 }, { kinds: [k], authors: [admin], '#d': [d], limit: 1 },
(prev) => { (prev) => {
const prevNames = prev?.tags.reduce((acc, [name, value]) => { const prevNames = prev?.tags.reduce((acc, [name, value]) => {
if (name === 'n') acc[value] = true; if (name === 'n') acc[value] = true;
@ -124,10 +132,10 @@ async function updateUser(pubkey: string, n: Record<string, boolean>, c: AppCont
const other = prev?.tags.filter(([name]) => !['d', 'n'].includes(name)) ?? []; const other = prev?.tags.filter(([name]) => !['d', 'n'].includes(name)) ?? [];
return { return {
kind: 30382, kind: k,
content: prev?.content ?? '', content: prev?.content ?? '',
tags: [ tags: [
['d', pubkey], ['d', d],
...nTags, ...nTags,
...other, ...other,
], ],
@ -296,6 +304,7 @@ export {
parseBody, parseBody,
updateAdminEvent, updateAdminEvent,
updateEvent, updateEvent,
updateEventInfo,
updateListAdminEvent, updateListAdminEvent,
updateListEvent, updateListEvent,
updateUser, updateUser,

View File

@ -45,16 +45,15 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
{ max: 500, ttl: Time.hours(1) }, { max: 500, ttl: Time.hours(1) },
); );
async function localNip05Lookup(store: NStore, name: string): Promise<nip19.ProfilePointer | undefined> { async function localNip05Lookup(store: NStore, localpart: string): Promise<nip19.ProfilePointer | undefined> {
const [label] = await store.query([{ const [grant] = await store.query([{
kinds: [1985], kinds: [30360],
'#d': [`${localpart}@${Conf.url.host}`],
authors: [Conf.pubkey], authors: [Conf.pubkey],
'#L': ['nip05'],
'#l': [`${name}@${Conf.url.host}`],
limit: 1, limit: 1,
}]); }]);
const pubkey = label?.tags.find(([name]) => name === 'p')?.[1]; const pubkey = grant?.tags.find(([name]) => name === 'p')?.[1];
if (pubkey) { if (pubkey) {
return { pubkey, relays: [Conf.relay] }; return { pubkey, relays: [Conf.relay] };

25
src/views/ditto.ts Normal file
View File

@ -0,0 +1,25 @@
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { getTagSet } from '@/utils/tags.ts';
export async function renderNameRequest(event: DittoEvent) {
const n = getTagSet(event.info?.tags ?? [], 'n');
let approvalStatus = 'pending';
if (n.has('approved')) {
approvalStatus = 'approved';
}
if (n.has('rejected')) {
approvalStatus = 'rejected';
}
return {
id: event.id,
account: event.author ? await renderAccount(event.author) : accountFromPubkey(event.pubkey),
name: event.tags.find(([name]) => name === 'r')?.[1] || '',
reason: event.content,
approval_status: approvalStatus,
created_at: new Date(event.created_at * 1000).toISOString(),
};
}