Support Pleroma admin tags

This commit is contained in:
Alex Gleason 2024-06-08 13:44:57 -05:00
parent 284ae9aab7
commit d2238e80f9
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 79 additions and 3 deletions

View File

@ -42,6 +42,8 @@ import {
configController,
frontendConfigController,
pleromaAdminDeleteStatusController,
pleromaAdminTagController,
pleromaAdminUntagController,
updateConfigController,
} from '@/controllers/api/pleroma.ts';
import { preferencesController } from '@/controllers/api/preferences.ts';
@ -253,6 +255,9 @@ app.post(
app.post('/api/v1/admin/accounts/:id{[0-9a-f]{64}}/action', requireSigner, requireRole('admin'), adminActionController);
app.put('/api/v1/pleroma/admin/users/tag', requireRole('admin'), pleromaAdminTagController);
app.delete('/api/v1/pleroma/admin/users/tag', requireRole('admin'), pleromaAdminUntagController);
// Not (yet) implemented.
app.get('/api/v1/custom_emojis', emptyArrayController);
app.get('/api/v1/filters', emptyArrayController);

View File

@ -6,7 +6,8 @@ import { Conf } from '@/config.ts';
import { configSchema, elixirTupleSchema, type PleromaConfig } from '@/schemas/pleroma-api.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { Storages } from '@/storages.ts';
import { createAdminEvent } from '@/utils/api.ts';
import { createAdminEvent, updateAdminEvent } from '@/utils/api.ts';
import { lookupPubkey } from '@/utils/lookup.ts';
const frontendConfigController: AppController = async (c) => {
const store = await Storages.db();
@ -87,4 +88,70 @@ async function getConfigs(store: NStore, signal: AbortSignal): Promise<PleromaCo
}
}
export { configController, frontendConfigController, pleromaAdminDeleteStatusController, updateConfigController };
const pleromaAdminTagsSchema = z.object({
nicknames: z.string().array(),
tags: z.string().array(),
});
const pleromaAdminTagController: AppController = async (c) => {
const params = pleromaAdminTagsSchema.parse(await c.req.json());
for (const nickname of params.nicknames) {
const pubkey = await lookupPubkey(nickname);
if (!pubkey) continue;
await updateAdminEvent(
{ kinds: [30382], authors: [Conf.pubkey], '#d': [pubkey], limit: 1 },
(prev) => {
const tags = prev?.tags ?? [['d', pubkey]];
for (const tag of params.tags) {
const existing = prev?.tags.some(([name, value]) => name === 't' && value === tag);
if (!existing) {
tags.push(['t', tag]);
}
}
return {
kind: 30382,
content: prev?.content ?? '',
tags,
};
},
c,
);
}
return new Response(null, { status: 204 });
};
const pleromaAdminUntagController: AppController = async (c) => {
const params = pleromaAdminTagsSchema.parse(await c.req.json());
for (const nickname of params.nicknames) {
const pubkey = await lookupPubkey(nickname);
if (!pubkey) continue;
await updateAdminEvent(
{ kinds: [30382], authors: [Conf.pubkey], '#d': [pubkey], limit: 1 },
(prev) => ({
kind: 30382,
content: prev?.content ?? '',
tags: (prev?.tags ?? [['d', pubkey]])
.filter(([name, value]) => !(name === 't' && params.tags.includes(value))),
}),
c,
);
}
return new Response(null, { status: 204 });
};
export {
configController,
frontendConfigController,
pleromaAdminDeleteStatusController,
pleromaAdminTagController,
pleromaAdminUntagController,
updateConfigController,
};

View File

@ -73,6 +73,8 @@ function updateListEvent(
async function createAdminEvent(t: EventStub, c: AppContext): Promise<NostrEvent> {
const signer = new AdminSigner();
console.log(t);
const event = await signer.signEvent({
content: '',
created_at: nostrNow(),
@ -125,7 +127,7 @@ async function updateUser(pubkey: string, n: Record<string, boolean>, c: AppCont
return {
kind: 30382,
content: prev?.content,
content: prev?.content ?? '',
tags: [
['d', pubkey],
...nTags,
@ -294,6 +296,7 @@ export {
type PaginationParams,
paginationSchema,
parseBody,
updateAdminEvent,
updateEvent,
updateListAdminEvent,
updateListEvent,

View File

@ -81,6 +81,7 @@ async function renderAccount(
is_moderator: roles.has('admin') || roles.has('moderator'),
is_local: parsed05?.domain === Conf.url.host,
settings_store: undefined as unknown,
tags: [...getTagSet(event.user?.tags ?? [], 't')],
},
nostr: {
pubkey,