From b9922f96a0fa7a69c4748b906467366ba49b6c04 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 8 Jun 2024 12:17:06 -0500 Subject: [PATCH] adminActionController: mark "n" tags on the user --- src/app.ts | 4 ++-- src/controllers/api/admin.ts | 28 +++++++++++++++++----------- src/utils/api.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/app.ts b/src/app.ts index bb1c5b6..771b887 100644 --- a/src/app.ts +++ b/src/app.ts @@ -26,7 +26,7 @@ import { updateCredentialsController, verifyCredentialsController, } from '@/controllers/api/accounts.ts'; -import { adminAccountAction, adminAccountsController } from '@/controllers/api/admin.ts'; +import { adminAccountsController, adminActionController } from '@/controllers/api/admin.ts'; import { appCredentialsController, createAppController } from '@/controllers/api/apps.ts'; import { blocksController } from '@/controllers/api/blocks.ts'; import { bookmarksController } from '@/controllers/api/bookmarks.ts'; @@ -251,7 +251,7 @@ app.post( adminReportResolveController, ); -app.post('/api/v1/admin/accounts/:id{[0-9a-f]{64}}/action', requireSigner, requireRole('admin'), adminAccountAction); +app.post('/api/v1/admin/accounts/:id{[0-9a-f]{64}}/action', requireSigner, requireRole('admin'), adminActionController); // Not (yet) implemented. app.get('/api/v1/custom_emojis', emptyArrayController); diff --git a/src/controllers/api/admin.ts b/src/controllers/api/admin.ts index ae2221f..1b5794c 100644 --- a/src/controllers/api/admin.ts +++ b/src/controllers/api/admin.ts @@ -5,8 +5,7 @@ import { Conf } from '@/config.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { booleanParamSchema } from '@/schema.ts'; import { Storages } from '@/storages.ts'; -import { paginated, paginationSchema, parseBody, updateListAdminEvent } from '@/utils/api.ts'; -import { addTag } from '@/utils/tags.ts'; +import { paginated, paginationSchema, parseBody, updateUser } from '@/utils/api.ts'; import { renderAdminAccount } from '@/views/mastodon/admin-accounts.ts'; const adminAccountQuerySchema = z.object({ @@ -64,7 +63,7 @@ const adminAccountActionSchema = z.object({ type: z.enum(['none', 'sensitive', 'disable', 'silence', 'suspend']), }); -const adminAccountAction: AppController = async (c) => { +const adminActionController: AppController = async (c) => { const body = await parseBody(c.req.raw); const result = adminAccountActionSchema.safeParse(body); const authorId = c.req.param('id'); @@ -75,17 +74,24 @@ const adminAccountAction: AppController = async (c) => { const { data } = result; - if (data.type !== 'disable') { - return c.json({ error: 'Record invalid' }, 422); + const n: Record = {}; + + if (data.type === 'sensitive') { + n.sensitive = true; + } + if (data.type === 'disable') { + n.disable = true; + } + if (data.type === 'silence') { + n.silence = true; + } + if (data.type === 'suspend') { + n.suspend = true; } - await updateListAdminEvent( - { kinds: [10000], authors: [Conf.pubkey], limit: 1 }, - (tags) => addTag(tags, ['p', authorId]), - c, - ); + await updateUser(authorId, n, c); return c.json({}, 200); }; -export { adminAccountAction, adminAccountsController }; +export { adminAccountsController, adminActionController }; diff --git a/src/utils/api.ts b/src/utils/api.ts index 3cc8b7d..8010615 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -107,6 +107,36 @@ async function updateAdminEvent( return createAdminEvent(fn(prev), c); } +async function updateUser(pubkey: string, n: Record, c: AppContext): Promise { + const signer = new AdminSigner(); + const admin = await signer.getPublicKey(); + + return updateAdminEvent( + { kinds: [30382], authors: [admin], '#d': [pubkey], limit: 1 }, + (prev) => { + const prevNames = prev?.tags.reduce((acc, [name, value]) => { + if (name === 'n') acc[value] = true; + return acc; + }, {} as Record); + + const names = { ...prevNames, ...n }; + const nTags = Object.entries(names).filter(([, value]) => value).map(([name]) => ['n', name]); + const other = prev?.tags.filter(([name]) => !['d', 'n'].includes(name)) ?? []; + + return { + kind: 30382, + content: prev?.content, + tags: [ + ['d', pubkey], + ...nTags, + ...other, + ], + }; + }, + c, + ); +} + /** Push the event through the pipeline, rethrowing any RelayError. */ async function publishEvent(event: NostrEvent, c: AppContext): Promise { debug('EVENT', event); @@ -267,4 +297,5 @@ export { updateEvent, updateListAdminEvent, updateListEvent, + updateUser, };