mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-07 07:34:25 +01:00
Add a getInstanceMetadata function to DRY a few controllers
This commit is contained in:
37
src/utils/instance.ts
Normal file
37
src/utils/instance.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user