Merge branch 'metadata-conf' into 'main'

Remove `@/config.ts` import from utils/instance.ts

See merge request soapbox-pub/ditto!702
This commit is contained in:
Alex Gleason 2025-02-27 23:34:46 +00:00
commit ee58a9f265
7 changed files with 28 additions and 23 deletions

View file

@ -14,10 +14,10 @@ export class DittoPush {
private server: Promise<ApplicationServer | undefined>; private server: Promise<ApplicationServer | undefined>;
constructor(opts: DittoPushOpts) { constructor(opts: DittoPushOpts) {
const { conf, relay } = opts; const { conf } = opts;
this.server = (async () => { this.server = (async () => {
const meta = await getInstanceMetadata(relay); const meta = await getInstanceMetadata(opts);
const keys = await conf.vapidKeys; const keys = await conf.vapidKeys;
if (keys) { if (keys) {

View file

@ -327,7 +327,7 @@ const updateInstanceSchema = z.object({
}); });
export const updateInstanceController: AppController = async (c) => { export const updateInstanceController: AppController = async (c) => {
const { conf, relay, signal } = c.var; const { conf } = c.var;
const body = await parseBody(c.req.raw); const body = await parseBody(c.req.raw);
const result = updateInstanceSchema.safeParse(body); const result = updateInstanceSchema.safeParse(body);
@ -337,7 +337,7 @@ export const updateInstanceController: AppController = async (c) => {
return c.json(result.error, 422); return c.json(result.error, 422);
} }
const meta = await getInstanceMetadata(relay, signal); const meta = await getInstanceMetadata(c.var);
await updateAdminEvent( await updateAdminEvent(
{ kinds: [0], authors: [pubkey], limit: 1 }, { kinds: [0], authors: [pubkey], limit: 1 },

View file

@ -15,9 +15,9 @@ const features = [
]; ];
const instanceV1Controller: AppController = async (c) => { const instanceV1Controller: AppController = async (c) => {
const { conf, relay, signal } = c.var; const { conf } = c.var;
const { host, protocol } = conf.url; const { host, protocol } = conf.url;
const meta = await getInstanceMetadata(relay, signal); const meta = await getInstanceMetadata(c.var);
/** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */ /** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */
const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:'; const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:';
@ -75,9 +75,9 @@ const instanceV1Controller: AppController = async (c) => {
}; };
const instanceV2Controller: AppController = async (c) => { const instanceV2Controller: AppController = async (c) => {
const { conf, relay, signal } = c.var; const { conf } = c.var;
const { host, protocol } = conf.url; const { host, protocol } = conf.url;
const meta = await getInstanceMetadata(relay, signal); const meta = await getInstanceMetadata(c.var);
/** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */ /** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */
const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:'; const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:';
@ -164,9 +164,7 @@ const instanceV2Controller: AppController = async (c) => {
}; };
const instanceDescriptionController: AppController = async (c) => { const instanceDescriptionController: AppController = async (c) => {
const { relay, signal } = c.var; const meta = await getInstanceMetadata(c.var);
const meta = await getInstanceMetadata(relay, signal);
return c.json({ return c.json({
content: meta.about, content: meta.about,

View file

@ -40,7 +40,7 @@ async function getEntities(c: AppContext, params: { acct?: string; statusId?: st
const { relay } = c.var; const { relay } = c.var;
const entities: MetadataEntities = { const entities: MetadataEntities = {
instance: await getInstanceMetadata(relay), instance: await getInstanceMetadata(c.var),
}; };
if (params.statusId) { if (params.statusId) {

View file

@ -3,9 +3,7 @@ import { WebManifestCombined } from '@/types/webmanifest.ts';
import { getInstanceMetadata } from '@/utils/instance.ts'; import { getInstanceMetadata } from '@/utils/instance.ts';
export const manifestController: AppController = async (c) => { export const manifestController: AppController = async (c) => {
const { relay, signal } = c.var; const meta = await getInstanceMetadata(c.var);
const meta = await getInstanceMetadata(relay, signal);
const manifest: WebManifestCombined = { const manifest: WebManifestCombined = {
description: meta.about, description: meta.about,

View file

@ -4,9 +4,9 @@ import { AppController } from '@/app.ts';
import { getInstanceMetadata } from '@/utils/instance.ts'; import { getInstanceMetadata } from '@/utils/instance.ts';
const relayInfoController: AppController = async (c) => { const relayInfoController: AppController = async (c) => {
const { conf, relay, signal } = c.var; const { conf } = c.var;
const meta = await getInstanceMetadata(relay, signal); const meta = await getInstanceMetadata(c.var);
c.res.headers.set('access-control-allow-origin', '*'); c.res.headers.set('access-control-allow-origin', '*');

View file

@ -1,9 +1,10 @@
import { NostrEvent, NostrMetadata, NSchema as n, NStore } from '@nostrify/nostrify'; import { NostrEvent, NostrMetadata, NSchema as n, NStore } from '@nostrify/nostrify';
import { z } from 'zod'; import { z } from 'zod';
import { Conf } from '@/config.ts';
import { screenshotsSchema, serverMetaSchema } from '@/schemas/nostr.ts'; import { screenshotsSchema, serverMetaSchema } from '@/schemas/nostr.ts';
import type { DittoConf } from '@ditto/conf';
/** Like NostrMetadata, but some fields are required and also contains some extra fields. */ /** Like NostrMetadata, but some fields are required and also contains some extra fields. */
export interface InstanceMetadata extends NostrMetadata { export interface InstanceMetadata extends NostrMetadata {
about: string; about: string;
@ -15,10 +16,18 @@ export interface InstanceMetadata extends NostrMetadata {
screenshots: z.infer<typeof screenshotsSchema>; screenshots: z.infer<typeof screenshotsSchema>;
} }
interface GetInstanceMetadataOpts {
conf: DittoConf;
relay: NStore;
signal?: AbortSignal;
}
/** Get and parse instance metadata from the kind 0 of the admin user. */ /** Get and parse instance metadata from the kind 0 of the admin user. */
export async function getInstanceMetadata(store: NStore, signal?: AbortSignal): Promise<InstanceMetadata> { export async function getInstanceMetadata(opts: GetInstanceMetadataOpts): Promise<InstanceMetadata> {
const [event] = await store.query( const { conf, relay, signal } = opts;
[{ kinds: [0], authors: [await Conf.signer.getPublicKey()], limit: 1 }],
const [event] = await relay.query(
[{ kinds: [0], authors: [await conf.signer.getPublicKey()], limit: 1 }],
{ signal }, { signal },
); );
@ -33,8 +42,8 @@ export async function getInstanceMetadata(store: NStore, signal?: AbortSignal):
name: meta.name ?? 'Ditto', name: meta.name ?? 'Ditto',
about: meta.about ?? 'Nostr community server', about: meta.about ?? 'Nostr community server',
tagline: meta.tagline ?? meta.about ?? 'Nostr community server', tagline: meta.tagline ?? meta.about ?? 'Nostr community server',
email: meta.email ?? `postmaster@${Conf.url.host}`, email: meta.email ?? `postmaster@${conf.url.host}`,
picture: meta.picture ?? Conf.local('/images/thumbnail.png'), picture: meta.picture ?? conf.local('/images/thumbnail.png'),
event, event,
screenshots: meta.screenshots ?? [], screenshots: meta.screenshots ?? [],
}; };