Add a getInstanceMetadata function to DRY a few controllers

This commit is contained in:
Alex Gleason
2024-05-12 12:32:40 -05:00
parent 928ae4ec22
commit bdfa6f8826
3 changed files with 49 additions and 23 deletions

37
src/utils/instance.ts Normal file
View File

@@ -0,0 +1,37 @@
import { NostrEvent, NostrMetadata, NSchema as n } from '@nostrify/nostrify';
import { Conf } from '@/config.ts';
import { serverMetaSchema } from '@/schemas/nostr.ts';
import { Storages } from '@/storages.ts';
/** Like NostrMetadata, but some fields are required and also contains some extra fields. */
export interface InstanceMetadata extends NostrMetadata {
name: string;
about: string;
tagline: string;
email: string;
event?: NostrEvent;
}
/** Get and parse instance metadata from the kind 0 of the admin user. */
export async function getInstanceMetadata(signal?: AbortSignal): Promise<InstanceMetadata> {
const [event] = await Storages.db.query(
[{ kinds: [0], authors: [Conf.pubkey], limit: 1 }],
{ signal },
);
const meta = n
.json()
.pipe(serverMetaSchema)
.catch({})
.parse(event?.content);
return {
...meta,
name: meta.name ?? 'Ditto',
about: meta.about ?? 'Nostr community server',
tagline: meta.tagline ?? meta.about ?? 'Nostr community server',
email: meta.email ?? `postmaster@${Conf.url.host}`,
event,
};
}