From ab85360d2ff5de1e3aa42e4a35aa4b640cccaf31 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 3 Oct 2024 11:17:21 -0300 Subject: [PATCH] refactor: move getConfigs() function and frontendConfig logic to 'src/utils/frontendConfig.ts' --- src/controllers/api/pleroma.ts | 32 ++++----------------------- src/utils/frontendConfig.ts | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 src/utils/frontendConfig.ts diff --git a/src/controllers/api/pleroma.ts b/src/controllers/api/pleroma.ts index 31d8545f..2c025b8e 100644 --- a/src/controllers/api/pleroma.ts +++ b/src/controllers/api/pleroma.ts @@ -1,26 +1,20 @@ -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, elixirTupleSchema, type PleromaConfig } from '@/schemas/pleroma-api.ts'; +import { configSchema } 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 configs = await getConfigs(store, c.req.raw.signal); - const frontendConfig = configs.find(({ group, key }) => group === ':pleroma' && key === ':frontend_configurations'); + const frontendConfig = await getPleromaConfig(store, c.req.raw.signal); 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 c.json(data); + return c.json(frontendConfig); } else { return c.json({}); } @@ -70,24 +64,6 @@ 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 new file mode 100644 index 00000000..a4f3f592 --- /dev/null +++ b/src/utils/frontendConfig.ts @@ -0,0 +1,40 @@ +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 []; + } +}