adminActionController: mark "n" tags on the user

This commit is contained in:
Alex Gleason 2024-06-08 12:17:06 -05:00
parent 7d54a5c7d0
commit b9922f96a0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 50 additions and 13 deletions

View File

@ -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);

View File

@ -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<string, boolean> = {};
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 };

View File

@ -107,6 +107,36 @@ async function updateAdminEvent<E extends EventStub>(
return createAdminEvent(fn(prev), c);
}
async function updateUser(pubkey: string, n: Record<string, boolean>, c: AppContext): Promise<NostrEvent> {
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<string, boolean>);
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<NostrEvent> {
debug('EVENT', event);
@ -267,4 +297,5 @@ export {
updateEvent,
updateListAdminEvent,
updateListEvent,
updateUser,
};