normalize new CW in all mandatory CW endpoints

This commit is contained in:
Hazelnoot 2025-07-26 18:55:19 -04:00
parent e5dfcf6f64
commit 807aa1be3d
3 changed files with 26 additions and 26 deletions

View file

@ -34,21 +34,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
super(meta, paramDef, async (ps, me) => {
const instance = await this.federatedInstanceService.fetchOrRegister(ps.host);
// Collapse empty strings to null
const newCW = ps.cw || null;
const oldCW = instance.mandatoryCW;
// Skip if there's nothing to do
if (instance.mandatoryCW === ps.cw) return;
if (oldCW === newCW) return;
// This synchronizes caches automatically
await this.federatedInstanceService.update(instance.id, { mandatoryCW: newCW });
// Log event first.
// This ensures that we don't "lose" the log if an error occurs
await this.moderationLogService.log(me, 'setMandatoryCWForInstance', {
newCW: ps.cw,
oldCW: instance.mandatoryCW,
newCW,
oldCW,
host: ps.host,
});
await this.federatedInstanceService.update(instance.id, {
// Collapse empty strings to null
mandatoryCW: ps.cw || null,
});
});
}
}

View file

@ -43,23 +43,22 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}) as MiNote & { user: MiUser };
// Collapse empty strings to null
const mandatoryCW = ps.cw || null;
const newCW = ps.cw || null;
const oldCW = note.mandatoryCW;
// Skip if there's nothing to do
if (note.mandatoryCW === mandatoryCW) return;
if (oldCW === newCW) return;
await this.noteEditService.edit(note.user, note.id, { mandatoryCW: newCW });
// Log event first.
// This ensures that we don't "lose" the log if an error occurs
await this.moderationLogService.log(me, 'setMandatoryCWForNote', {
newCW: mandatoryCW,
oldCW: note.mandatoryCW,
newCW,
oldCW,
noteId: note.id,
noteUserId: note.user.id,
noteUserUsername: note.user.username,
noteUserHost: note.user.host,
});
await this.noteEditService.edit(note.user, note.id, { mandatoryCW });
});
}
}

View file

@ -41,20 +41,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
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;
// Collapse empty strings to null
const newCW = ps.cw || null;
const oldCW = user.mandatoryCW;
await this.usersRepository.update(ps.userId, {
// Collapse empty strings to null
mandatoryCW: ps.cw || null,
});
// Skip if there's nothing to do
if (oldCW === newCW) return;
await this.usersRepository.update(ps.userId, { mandatoryCW: newCW });
// Synchronize caches and other processes
this.globalEventService.publishInternalEvent('localUserUpdated', { id: ps.userId });
await this.moderationLogService.log(me, 'setMandatoryCW', {
newCW: ps.cw,
oldCW: user.mandatoryCW,
newCW,
oldCW,
userId: user.id,
userUsername: user.username,
userHost: user.host,