diff --git a/src/controllers/api/pleroma.ts b/src/controllers/api/pleroma.ts index 2c025b8e..31d8545f 100644 --- a/src/controllers/api/pleroma.ts +++ b/src/controllers/api/pleroma.ts @@ -1,20 +1,26 @@ +import { NSchema as n, NStore } from '@nostrify/nostrify'; import { z } from 'zod'; import { type AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { configSchema } from '@/schemas/pleroma-api.ts'; +import { configSchema, elixirTupleSchema, type PleromaConfig } from '@/schemas/pleroma-api.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts'; import { Storages } from '@/storages.ts'; import { createAdminEvent, updateAdminEvent, updateUser } from '@/utils/api.ts'; -import { getConfigs, getPleromaConfig } from '@/utils/frontendConfig.ts'; import { lookupPubkey } from '@/utils/lookup.ts'; const frontendConfigController: AppController = async (c) => { const store = await Storages.db(); - const frontendConfig = await getPleromaConfig(store, c.req.raw.signal); + const configs = await getConfigs(store, c.req.raw.signal); + const frontendConfig = configs.find(({ group, key }) => group === ':pleroma' && key === ':frontend_configurations'); if (frontendConfig) { - return c.json(frontendConfig); + const schema = elixirTupleSchema.transform(({ tuple }) => tuple).array(); + const data = schema.parse(frontendConfig.value).reduce>((result, [name, data]) => { + result[name.replace(/^:/, '')] = data; + return result; + }, {}); + return c.json(data); } else { return c.json({}); } @@ -64,6 +70,24 @@ const pleromaAdminDeleteStatusController: AppController = async (c) => { return c.json({}); }; +async function getConfigs(store: NStore, signal: AbortSignal): Promise { + const { pubkey } = Conf; + + const [event] = await store.query([{ + kinds: [30078], + authors: [pubkey], + '#d': ['pub.ditto.pleroma.config'], + limit: 1, + }], { signal }); + + try { + const decrypted = await new AdminSigner().nip44.decrypt(Conf.pubkey, event.content); + return n.json().pipe(configSchema.array()).catch([]).parse(decrypted); + } catch (_e) { + return []; + } +} + const pleromaAdminTagSchema = z.object({ nicknames: z.string().array(), tags: z.string().array(), diff --git a/src/utils/frontendConfig.ts b/src/utils/frontendConfig.ts deleted file mode 100644 index a4f3f592..00000000 --- a/src/utils/frontendConfig.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { NSchema as n, NStore } from '@nostrify/nostrify'; - -import { AdminSigner } from '@/signers/AdminSigner.ts'; -import { Conf } from '@/config.ts'; -import { configSchema, elixirTupleSchema, type PleromaConfig } from '@/schemas/pleroma-api.ts'; - -export async function getPleromaConfig( - store: NStore, - signal?: AbortSignal, -): Promise> { - const configs = await getConfigs(store, signal ?? AbortSignal.timeout(1000)); - const frontendConfig = configs.find(({ group, key }) => group === ':pleroma' && key === ':frontend_configurations'); - if (frontendConfig) { - const schema = elixirTupleSchema.transform(({ tuple }) => tuple).array(); - const data = schema.parse(frontendConfig.value).reduce>((result, [name, data]) => { - result[name.replace(/^:/, '')] = data; - return result; - }, {}); - return data; - } - return undefined; -} - -export async function getConfigs(store: NStore, signal: AbortSignal): Promise { - const { pubkey } = Conf; - - const [event] = await store.query([{ - kinds: [30078], - authors: [pubkey], - '#d': ['pub.ditto.pleroma.config'], - limit: 1, - }], { signal }); - - try { - const decrypted = await new AdminSigner().nip44.decrypt(Conf.pubkey, event.content); - return n.json().pipe(configSchema.array()).catch([]).parse(decrypted); - } catch (_e) { - return []; - } -}