From 568d82a9746d3d67a756b13fc007beb057dcc011 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Tue, 28 Jan 2025 11:39:41 -0500 Subject: record ModLog entry when setting a user's content warning --- .../src/server/api/endpoints/admin/cw-user.ts | 26 +++++++++++++++++----- packages/backend/src/types.ts | 8 +++++++ 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'packages/backend/src') diff --git a/packages/backend/src/server/api/endpoints/admin/cw-user.ts b/packages/backend/src/server/api/endpoints/admin/cw-user.ts index d48ca565a4..bdcfa6a0d9 100644 --- a/packages/backend/src/server/api/endpoints/admin/cw-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/cw-user.ts @@ -9,6 +9,7 @@ import type { UsersRepository } from '@/models/_.js'; import { DI } from '@/di-symbols.js'; import { CacheService } from '@/core/CacheService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; +import { ModerationLogService } from '@/core/ModerationLogService.js'; export const meta = { tags: ['admin'], @@ -34,18 +35,31 @@ export default class extends Endpoint { // eslint- private readonly usersRepository: UsersRepository, private readonly globalEventService: GlobalEventService, + private readonly cacheService: CacheService, + private readonly moderationLogService: ModerationLogService, ) { - super(meta, paramDef, async ps => { - const result = await this.usersRepository.update(ps.userId, { + super(meta, paramDef, async (ps, me) => { + const user = await this.cacheService.findUserById(ps.userId); + + // Skip if there's nothing to do + if (user.mandatoryCW === ps.cw) return; + + // Log event first. + // This ensures that we don't "lose" the log if an error occurs + await this.moderationLogService.log(me, 'setMandatoryCW', { + newCW: ps.cw, + oldCW: user.mandatoryCW, + userId: user.id, + userUsername: user.username, + userHost: user.host, + }); + + await this.usersRepository.update(ps.userId, { // Collapse empty strings to null // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing mandatoryCW: ps.cw || null, }); - if (result.affected && result.affected < 1) { - throw new Error('No such user'); - } - // Synchronize caches and other processes this.globalEventService.publishInternalEvent('localUserUpdated', { id: ps.userId }); }); diff --git a/packages/backend/src/types.ts b/packages/backend/src/types.ts index 067481d9da..b359fa5a39 100644 --- a/packages/backend/src/types.ts +++ b/packages/backend/src/types.ts @@ -100,6 +100,7 @@ export const moderationLogTypes = [ 'deleteGlobalAnnouncement', 'deleteUserAnnouncement', 'resetPassword', + 'setMandatoryCW', 'setRemoteInstanceNSFW', 'unsetRemoteInstanceNSFW', 'suspendRemoteInstance', @@ -261,6 +262,13 @@ export type ModerationLogPayloads = { userUsername: string; userHost: string | null; }; + setMandatoryCW: { + newCW: string | null; + oldCW: string | null; + userId: string; + userUsername: string; + userHost: string | null; + }; setRemoteInstanceNSFW: { id: string; host: string; -- cgit v1.2.3-freya