mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Revert "refactor: move getConfigs() function and frontendConfig logic to 'src/utils/frontendConfig.ts'"
This reverts commit ab85360d2f.
The discussion started publicly in Gitlab: https://gitlab.com/soapbox-pub/ditto/-/merge_requests/537#note_2148430111
Then it kept going in Element, basically the purpose of the commit is
correct, but the way Patrick did it is not good.
This commit is contained in:
parent
20caaa9ebd
commit
fc5e9b2990
2 changed files with 28 additions and 44 deletions
|
|
@ -1,20 +1,26 @@
|
||||||
|
import { NSchema as n, NStore } from '@nostrify/nostrify';
|
||||||
import { z } from 'zod';
|
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 } from '@/schemas/pleroma-api.ts';
|
import { configSchema, elixirTupleSchema, type PleromaConfig } 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 { getConfigs, getPleromaConfig } from '@/utils/frontendConfig.ts';
|
|
||||||
import { lookupPubkey } from '@/utils/lookup.ts';
|
import { lookupPubkey } from '@/utils/lookup.ts';
|
||||||
|
|
||||||
const frontendConfigController: AppController = async (c) => {
|
const frontendConfigController: AppController = async (c) => {
|
||||||
const store = await Storages.db();
|
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) {
|
if (frontendConfig) {
|
||||||
return c.json(frontendConfig);
|
const schema = elixirTupleSchema.transform(({ tuple }) => tuple).array();
|
||||||
|
const data = schema.parse(frontendConfig.value).reduce<Record<string, unknown>>((result, [name, data]) => {
|
||||||
|
result[name.replace(/^:/, '')] = data;
|
||||||
|
return result;
|
||||||
|
}, {});
|
||||||
|
return c.json(data);
|
||||||
} else {
|
} else {
|
||||||
return c.json({});
|
return c.json({});
|
||||||
}
|
}
|
||||||
|
|
@ -64,6 +70,24 @@ const pleromaAdminDeleteStatusController: AppController = async (c) => {
|
||||||
return c.json({});
|
return c.json({});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function getConfigs(store: NStore, signal: AbortSignal): Promise<PleromaConfig[]> {
|
||||||
|
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({
|
const pleromaAdminTagSchema = z.object({
|
||||||
nicknames: z.string().array(),
|
nicknames: z.string().array(),
|
||||||
tags: z.string().array(),
|
tags: z.string().array(),
|
||||||
|
|
|
||||||
|
|
@ -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<undefined | Record<string, unknown>> {
|
|
||||||
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<Record<string, unknown>>((result, [name, data]) => {
|
|
||||||
result[name.replace(/^:/, '')] = data;
|
|
||||||
return result;
|
|
||||||
}, {});
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getConfigs(store: NStore, signal: AbortSignal): Promise<PleromaConfig[]> {
|
|
||||||
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 [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue