From 1413be08a0cdbdb8734b93a6ad23ea3d0448994c Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Wed, 9 Jul 2025 19:04:35 -0400 Subject: [PATCH] apply note mandatory CW through NoteEditService --- packages/backend/src/core/NoteCreateService.ts | 2 ++ packages/backend/src/core/NoteEditService.ts | 12 +++++------- .../src/server/api/endpoints/admin/cw-note.ts | 14 ++++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index 0732624a99..d23eb520cf 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -144,6 +144,7 @@ type Option = { url?: string | null; app?: MiApp | null; processErrors?: string[] | null; + mandatoryCW?: string | null; }; export type PureRenoteOption = Option & { renote: MiNote } & ({ text?: null } | { cw?: null } | { reply?: null } | { poll?: null } | { files?: null | [] }); @@ -477,6 +478,7 @@ export class NoteCreateService implements OnApplicationShutdown { renoteUserHost: data.renote ? data.renote.userHost : null, userHost: user.host, processErrors: data.processErrors, + mandatoryCW: data.mandatoryCW, }); // should really not happen, but better safe than sorry diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index f8c732c4f1..3aaf9b165c 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -224,13 +224,7 @@ export class NoteEditService implements OnApplicationShutdown { } @bindThis - public async edit(user: MiUser & { - id: MiUser['id']; - username: MiUser['username']; - host: MiUser['host']; - isBot: MiUser['isBot']; - noindex: MiUser['noindex']; - }, editid: MiNote['id'], data: Option, silent = false): Promise { + public async edit(user: MiUser, editid: MiNote['id'], data: Option, silent = false): Promise { if (!editid) { throw new UnrecoverableError('edit failed: missing editid'); } @@ -457,6 +451,9 @@ export class NoteEditService implements OnApplicationShutdown { if (oldnote.hasPoll !== !!data.poll) { update.hasPoll = !!data.poll; } + if (data.mandatoryCW !== undefined && oldnote.mandatoryCW !== data.mandatoryCW) { + update.mandatoryCW = data.mandatoryCW; + } // TODO deep-compare files const filesChanged = oldnote.fileIds.length || data.files?.length; @@ -518,6 +515,7 @@ export class NoteEditService implements OnApplicationShutdown { renoteUserHost: data.renote ? data.renote.userHost : null, userHost: user.host, reactionAndUserPairCache: oldnote.reactionAndUserPairCache, + mandatoryCW: data.mandatoryCW, }); if (data.uri != null) note.uri = data.uri; diff --git a/packages/backend/src/server/api/endpoints/admin/cw-note.ts b/packages/backend/src/server/api/endpoints/admin/cw-note.ts index 498ad464ab..cf513a4922 100644 --- a/packages/backend/src/server/api/endpoints/admin/cw-note.ts +++ b/packages/backend/src/server/api/endpoints/admin/cw-note.ts @@ -8,6 +8,7 @@ import { Endpoint } from '@/server/api/endpoint-base.js'; import type { MiNote, MiUser, NotesRepository } from '@/models/_.js'; import { DI } from '@/di-symbols.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; +import { NoteEditService } from '@/core/NoteEditService.js'; export const meta = { tags: ['admin'], @@ -32,6 +33,7 @@ export default class extends Endpoint { // eslint- @Inject(DI.notesRepository) private readonly notesRepository: NotesRepository, + private readonly noteEditService: NoteEditService, private readonly moderationLogService: ModerationLogService, ) { super(meta, paramDef, async (ps, me) => { @@ -40,13 +42,16 @@ export default class extends Endpoint { // eslint- relations: { user: true }, }) as MiNote & { user: MiUser }; + // Collapse empty strings to null + const mandatoryCW = ps.cw || null; + // Skip if there's nothing to do - if (note.mandatoryCW === ps.cw) return; + if (note.mandatoryCW === mandatoryCW) return; // Log event first. // This ensures that we don't "lose" the log if an error occurs await this.moderationLogService.log(me, 'setMandatoryCWForNote', { - newCW: ps.cw, + newCW: mandatoryCW, oldCW: note.mandatoryCW, noteId: note.id, noteUserId: note.user.id, @@ -54,10 +59,7 @@ export default class extends Endpoint { // eslint- noteUserHost: note.user.host, }); - await this.notesRepository.update(ps.noteId, { - // Collapse empty strings to null - mandatoryCW: ps.cw || null, - }); + await this.noteEditService.edit(note.user, note.id, { mandatoryCW }); }); } }