Actually implement Pleroma's configs API correctly (wow!)
This commit is contained in:
parent
15810c1935
commit
8c972dbabd
@ -1,49 +1,71 @@
|
|||||||
import { type AppController } from '@/app.ts';
|
import { type AppController } from '@/app.ts';
|
||||||
|
import { Conf } from '@/config.ts';
|
||||||
|
import { decryptAdmin, encryptAdmin } from '@/crypto.ts';
|
||||||
import { z } from '@/deps.ts';
|
import { z } from '@/deps.ts';
|
||||||
import { configSchema, elixirTupleSchema } from '@/schemas/pleroma-api.ts';
|
import { configSchema, elixirTupleSchema } from '@/schemas/pleroma-api.ts';
|
||||||
import { eventsDB } from '@/storages.ts';
|
import { eventsDB } from '@/storages.ts';
|
||||||
import { createAdminEvent } from '@/utils/api.ts';
|
import { createAdminEvent } from '@/utils/api.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { jsonSchema } from '@/schema.ts';
|
||||||
|
|
||||||
const frontendConfigController: AppController = async (c) => {
|
const frontendConfigController: AppController = async (c) => {
|
||||||
const [event] = await eventsDB.filter([{
|
const [event] = await eventsDB.filter([{
|
||||||
kinds: [30078],
|
kinds: [30078],
|
||||||
authors: [Conf.pubkey],
|
authors: [Conf.pubkey],
|
||||||
'#d': ['pub.ditto.frontendConfig'],
|
'#d': ['pub.ditto.pleroma.config'],
|
||||||
limit: 1,
|
limit: 1,
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
if (event) {
|
const configs = jsonSchema.pipe(z.array(configSchema)).parse(
|
||||||
const data = JSON.parse(event.content);
|
event?.content ? await decryptAdmin(Conf.pubkey, event.content) : [],
|
||||||
return c.json(data);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return c.json({});
|
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 c.json(data);
|
||||||
|
} else {
|
||||||
|
return c.json({});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Pleroma admin config controller. */
|
/** Pleroma admin config controller. */
|
||||||
const updateConfigController: AppController = async (c) => {
|
const updateConfigController: AppController = async (c) => {
|
||||||
const json = await c.req.json();
|
const { pubkey } = Conf;
|
||||||
const { configs } = z.object({ configs: z.array(configSchema) }).parse(json);
|
|
||||||
|
|
||||||
for (const { group, key, value } of configs) {
|
const [event] = await eventsDB.filter([{
|
||||||
if (group === ':pleroma' && key === ':frontend_configurations') {
|
kinds: [30078],
|
||||||
const schema = elixirTupleSchema.transform(({ tuple }) => tuple).array();
|
authors: [pubkey],
|
||||||
|
'#d': ['pub.ditto.pleroma.config'],
|
||||||
|
limit: 1,
|
||||||
|
}]);
|
||||||
|
|
||||||
const data = schema.parse(value).reduce<Record<string, unknown>>((result, [name, data]) => {
|
const configs = jsonSchema.pipe(z.array(configSchema)).parse(
|
||||||
result[name.replace(/^:/, '')] = data;
|
event?.content ? await decryptAdmin(pubkey, event.content) : [],
|
||||||
return result;
|
);
|
||||||
}, {});
|
|
||||||
|
|
||||||
await createAdminEvent({
|
const { configs: newConfigs } = z.object({ configs: z.array(configSchema) }).parse(await c.req.json());
|
||||||
kind: 30078,
|
|
||||||
content: JSON.stringify(data),
|
for (const { group, key, value } of newConfigs) {
|
||||||
tags: [['d', 'pub.ditto.frontendConfig']],
|
const index = configs.findIndex((c) => c.group === group && c.key === key);
|
||||||
}, c);
|
if (index === -1) {
|
||||||
|
configs.push({ group, key, value });
|
||||||
|
} else {
|
||||||
|
configs[index].value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json([]);
|
await createAdminEvent({
|
||||||
|
kind: 30078,
|
||||||
|
content: await encryptAdmin(pubkey, JSON.stringify(configs)),
|
||||||
|
tags: [['d', 'pub.ditto.pleroma.config']],
|
||||||
|
}, c);
|
||||||
|
|
||||||
|
return c.json({ configs: newConfigs, need_reboot: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
export { frontendConfigController, updateConfigController };
|
export { frontendConfigController, updateConfigController };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user