Add PleromaConfigDB module

This commit is contained in:
Alex Gleason 2024-11-04 10:12:21 -06:00
parent d46af51604
commit 1a50578b39
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 42 additions and 14 deletions

View file

@ -3,16 +3,17 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { configSchema, elixirTupleSchema, type PleromaConfig } from '@/schemas/pleroma-api.ts'; import { configSchema, elixirTupleSchema } from '@/schemas/pleroma-api.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { createAdminEvent, updateAdminEvent, updateUser } from '@/utils/api.ts'; import { createAdminEvent, updateAdminEvent, updateUser } from '@/utils/api.ts';
import { lookupPubkey } from '@/utils/lookup.ts'; import { lookupPubkey } from '@/utils/lookup.ts';
import { PleromaConfigDB } from '@/utils/PleromaConfigDB.ts';
const frontendConfigController: AppController = async (c) => { const frontendConfigController: AppController = async (c) => {
const store = await Storages.db(); const store = await Storages.db();
const configs = await getConfigs(store, c.req.raw.signal); const configDB = await getConfigs(store, c.req.raw.signal);
const frontendConfig = configs.find(({ group, key }) => group === ':pleroma' && key === ':frontend_configurations'); const frontendConfig = configDB.get(':pleroma', ':frontend_configurations');
if (frontendConfig) { if (frontendConfig) {
const schema = elixirTupleSchema.transform(({ tuple }) => tuple).array(); const schema = elixirTupleSchema.transform(({ tuple }) => tuple).array();
@ -40,14 +41,7 @@ const updateConfigController: AppController = async (c) => {
const configs = await getConfigs(store, c.req.raw.signal); const configs = await getConfigs(store, c.req.raw.signal);
const { configs: newConfigs } = z.object({ configs: z.array(configSchema) }).parse(await c.req.json()); const { configs: newConfigs } = z.object({ configs: z.array(configSchema) }).parse(await c.req.json());
for (const { group, key, value } of newConfigs) { configs.merge(newConfigs);
const index = configs.findIndex((c) => c.group === group && c.key === key);
if (index === -1) {
configs.push({ group, key, value });
} else {
configs[index].value = value;
}
}
await createAdminEvent({ await createAdminEvent({
kind: 30078, kind: 30078,
@ -70,7 +64,7 @@ const pleromaAdminDeleteStatusController: AppController = async (c) => {
return c.json({}); return c.json({});
}; };
async function getConfigs(store: NStore, signal: AbortSignal): Promise<PleromaConfig[]> { async function getConfigs(store: NStore, signal: AbortSignal): Promise<PleromaConfigDB> {
const { pubkey } = Conf; const { pubkey } = Conf;
const [event] = await store.query([{ const [event] = await store.query([{
@ -80,11 +74,16 @@ async function getConfigs(store: NStore, signal: AbortSignal): Promise<PleromaCo
limit: 1, limit: 1,
}], { signal }); }], { signal });
if (!event) {
return new PleromaConfigDB([]);
}
try { try {
const decrypted = await new AdminSigner().nip44.decrypt(Conf.pubkey, event.content); const decrypted = await new AdminSigner().nip44.decrypt(Conf.pubkey, event.content);
return n.json().pipe(configSchema.array()).catch([]).parse(decrypted); const configs = n.json().pipe(configSchema.array()).catch([]).parse(decrypted);
return new PleromaConfigDB(configs);
} catch (_e) { } catch (_e) {
return []; return new PleromaConfigDB([]);
} }
} }

View file

@ -0,0 +1,29 @@
import type { PleromaConfig } from '@/schemas/pleroma-api.ts';
export class PleromaConfigDB {
constructor(private configs: PleromaConfig[]) {
}
get(group: string, key: string): PleromaConfig | undefined {
return this.configs.find((c) => c.group === group && c.key === key);
}
set(group: string, key: string, value: PleromaConfig): void {
const index = this.configs.findIndex((c) => c.group === group && c.key === key);
if (index === -1) {
this.configs.push(value);
} else {
this.configs[index] = value;
}
}
merge(configs: PleromaConfig[]): void {
for (const { group, key, value } of configs) {
this.set(group, key, { group, key, value });
}
}
toJSON(): PleromaConfig[] {
return this.configs;
}
}