This commit is contained in:
syuilo 2023-06-02 08:57:13 +09:00
parent 8bdf0dd135
commit 40df6c5168
8 changed files with 32 additions and 9 deletions

View File

@ -16,7 +16,7 @@ export class ModerationLogService {
} }
@bindThis @bindThis
public async insertModerationLog(moderator: { id: User['id'] }, type: string, info?: Record<string, any>) { public async log(moderator: { id: User['id'] }, type: string, info?: Record<string, any>) {
await this.moderationLogsRepository.insert({ await this.moderationLogsRepository.insert({
id: this.idService.genId(), id: this.idService.genId(),
createdAt: new Date(), createdAt: new Date(),

View File

@ -14,6 +14,7 @@ import { StreamMessages } from '@/server/api/stream/types.js';
import { IdService } from '@/core/IdService.js'; import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js';
import type { Packed } from '@/misc/json-schema'; import type { Packed } from '@/misc/json-schema';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import type { OnApplicationShutdown } from '@nestjs/common'; import type { OnApplicationShutdown } from '@nestjs/common';
export type RolePolicies = { export type RolePolicies = {
@ -87,6 +88,7 @@ export class RoleService implements OnApplicationShutdown {
private userEntityService: UserEntityService, private userEntityService: UserEntityService,
private globalEventService: GlobalEventService, private globalEventService: GlobalEventService,
private idService: IdService, private idService: IdService,
private moderationLogService: ModerationLogService,
) { ) {
//this.onMessage = this.onMessage.bind(this); //this.onMessage = this.onMessage.bind(this);
@ -355,9 +357,11 @@ export class RoleService implements OnApplicationShutdown {
} }
@bindThis @bindThis
public async assign(userId: User['id'], roleId: Role['id'], expiresAt: Date | null = null): Promise<void> { public async assign(userId: User['id'], roleId: Role['id'], expiresAt: Date | null = null, moderator?: User): Promise<void> {
const now = new Date(); const now = new Date();
const role = await this.rolesRepository.findOneByOrFail({ id: roleId });
const existing = await this.roleAssignmentsRepository.findOneBy({ const existing = await this.roleAssignmentsRepository.findOneBy({
roleId: roleId, roleId: roleId,
userId: userId, userId: userId,
@ -387,12 +391,23 @@ export class RoleService implements OnApplicationShutdown {
}); });
this.globalEventService.publishInternalEvent('userRoleAssigned', created); this.globalEventService.publishInternalEvent('userRoleAssigned', created);
if (moderator) {
this.moderationLogService.log(moderator, 'roleAssigned', {
roleId: roleId,
roleName: role.name,
userId: userId,
expiresAt: expiresAt,
});
}
} }
@bindThis @bindThis
public async unassign(userId: User['id'], roleId: Role['id']): Promise<void> { public async unassign(userId: User['id'], roleId: Role['id'], moderator?: User): Promise<void> {
const now = new Date(); const now = new Date();
const role = await this.rolesRepository.findOneByOrFail({ id: roleId });
const existing = await this.roleAssignmentsRepository.findOneBy({ roleId, userId }); const existing = await this.roleAssignmentsRepository.findOneBy({ roleId, userId });
if (existing == null) { if (existing == null) {
throw new RoleService.NotAssignedError(); throw new RoleService.NotAssignedError();
@ -411,6 +426,14 @@ export class RoleService implements OnApplicationShutdown {
}); });
this.globalEventService.publishInternalEvent('userRoleUnassigned', existing); this.globalEventService.publishInternalEvent('userRoleUnassigned', existing);
if (moderator) {
this.moderationLogService.log(moderator, 'roleUnassigned', {
roleId: roleId,
roleName: role.name,
userId: userId,
});
}
} }
@bindThis @bindThis

View File

@ -74,7 +74,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction ?? [], roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction ?? [],
}); });
this.moderationLogService.insertModerationLog(me, 'addEmoji', { this.moderationLogService.log(me, 'addEmoji', {
emojiId: emoji.id, emojiId: emoji.id,
}); });

View File

@ -26,7 +26,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
this.queueService.destroy(); this.queueService.destroy();
this.moderationLogService.insertModerationLog(me, 'clearQueue'); this.moderationLogService.log(me, 'clearQueue');
}); });
} }
} }

View File

@ -46,7 +46,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
break; break;
} }
this.moderationLogService.insertModerationLog(me, 'promoteQueue'); this.moderationLogService.log(me, 'promoteQueue');
}); });
} }
} }

View File

@ -56,7 +56,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
isSuspended: true, isSuspended: true,
}); });
this.moderationLogService.insertModerationLog(me, 'suspend', { this.moderationLogService.log(me, 'suspend', {
targetId: user.id, targetId: user.id,
}); });

View File

@ -41,7 +41,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
isSuspended: false, isSuspended: false,
}); });
this.moderationLogService.insertModerationLog(me, 'unsuspend', { this.moderationLogService.log(me, 'unsuspend', {
targetId: user.id, targetId: user.id,
}); });

View File

@ -398,7 +398,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
} }
await this.metaService.update(set); await this.metaService.update(set);
this.moderationLogService.insertModerationLog(me, 'updateMeta'); this.moderationLogService.log(me, 'updateMeta');
}); });
} }
} }