From 40df6c5168be78271d37cb90fa83d9affd9e3c3a Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 2 Jun 2023 08:57:13 +0900 Subject: [PATCH] wip --- .../backend/src/core/ModerationLogService.ts | 2 +- packages/backend/src/core/RoleService.ts | 27 +++++++++++++++++-- .../server/api/endpoints/admin/emoji/add.ts | 2 +- .../server/api/endpoints/admin/queue/clear.ts | 2 +- .../api/endpoints/admin/queue/promote.ts | 2 +- .../api/endpoints/admin/suspend-user.ts | 2 +- .../api/endpoints/admin/unsuspend-user.ts | 2 +- .../server/api/endpoints/admin/update-meta.ts | 2 +- 8 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/core/ModerationLogService.ts b/packages/backend/src/core/ModerationLogService.ts index 80e8cb9e5..e0ec993bd 100644 --- a/packages/backend/src/core/ModerationLogService.ts +++ b/packages/backend/src/core/ModerationLogService.ts @@ -16,7 +16,7 @@ export class ModerationLogService { } @bindThis - public async insertModerationLog(moderator: { id: User['id'] }, type: string, info?: Record) { + public async log(moderator: { id: User['id'] }, type: string, info?: Record) { await this.moderationLogsRepository.insert({ id: this.idService.genId(), createdAt: new Date(), diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts index 40ae10666..a315f6907 100644 --- a/packages/backend/src/core/RoleService.ts +++ b/packages/backend/src/core/RoleService.ts @@ -14,6 +14,7 @@ import { StreamMessages } from '@/server/api/stream/types.js'; import { IdService } from '@/core/IdService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; import type { Packed } from '@/misc/json-schema'; +import { ModerationLogService } from '@/core/ModerationLogService.js'; import type { OnApplicationShutdown } from '@nestjs/common'; export type RolePolicies = { @@ -87,6 +88,7 @@ export class RoleService implements OnApplicationShutdown { private userEntityService: UserEntityService, private globalEventService: GlobalEventService, private idService: IdService, + private moderationLogService: ModerationLogService, ) { //this.onMessage = this.onMessage.bind(this); @@ -355,9 +357,11 @@ export class RoleService implements OnApplicationShutdown { } @bindThis - public async assign(userId: User['id'], roleId: Role['id'], expiresAt: Date | null = null): Promise { + public async assign(userId: User['id'], roleId: Role['id'], expiresAt: Date | null = null, moderator?: User): Promise { const now = new Date(); + const role = await this.rolesRepository.findOneByOrFail({ id: roleId }); + const existing = await this.roleAssignmentsRepository.findOneBy({ roleId: roleId, userId: userId, @@ -387,11 +391,22 @@ export class RoleService implements OnApplicationShutdown { }); this.globalEventService.publishInternalEvent('userRoleAssigned', created); + + if (moderator) { + this.moderationLogService.log(moderator, 'roleAssigned', { + roleId: roleId, + roleName: role.name, + userId: userId, + expiresAt: expiresAt, + }); + } } @bindThis - public async unassign(userId: User['id'], roleId: Role['id']): Promise { + public async unassign(userId: User['id'], roleId: Role['id'], moderator?: User): Promise { const now = new Date(); + + const role = await this.rolesRepository.findOneByOrFail({ id: roleId }); const existing = await this.roleAssignmentsRepository.findOneBy({ roleId, userId }); if (existing == null) { @@ -411,6 +426,14 @@ export class RoleService implements OnApplicationShutdown { }); this.globalEventService.publishInternalEvent('userRoleUnassigned', existing); + + if (moderator) { + this.moderationLogService.log(moderator, 'roleUnassigned', { + roleId: roleId, + roleName: role.name, + userId: userId, + }); + } } @bindThis diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts index 509224e9c..ba88ae857 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts @@ -74,7 +74,7 @@ export default class extends Endpoint { roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction ?? [], }); - this.moderationLogService.insertModerationLog(me, 'addEmoji', { + this.moderationLogService.log(me, 'addEmoji', { emojiId: emoji.id, }); diff --git a/packages/backend/src/server/api/endpoints/admin/queue/clear.ts b/packages/backend/src/server/api/endpoints/admin/queue/clear.ts index 099e2ff22..6e7ae4be2 100644 --- a/packages/backend/src/server/api/endpoints/admin/queue/clear.ts +++ b/packages/backend/src/server/api/endpoints/admin/queue/clear.ts @@ -26,7 +26,7 @@ export default class extends Endpoint { super(meta, paramDef, async (ps, me) => { this.queueService.destroy(); - this.moderationLogService.insertModerationLog(me, 'clearQueue'); + this.moderationLogService.log(me, 'clearQueue'); }); } } diff --git a/packages/backend/src/server/api/endpoints/admin/queue/promote.ts b/packages/backend/src/server/api/endpoints/admin/queue/promote.ts index 4e57e6613..e5acd84f1 100644 --- a/packages/backend/src/server/api/endpoints/admin/queue/promote.ts +++ b/packages/backend/src/server/api/endpoints/admin/queue/promote.ts @@ -46,7 +46,7 @@ export default class extends Endpoint { break; } - this.moderationLogService.insertModerationLog(me, 'promoteQueue'); + this.moderationLogService.log(me, 'promoteQueue'); }); } } diff --git a/packages/backend/src/server/api/endpoints/admin/suspend-user.ts b/packages/backend/src/server/api/endpoints/admin/suspend-user.ts index eabbceac0..578224c37 100644 --- a/packages/backend/src/server/api/endpoints/admin/suspend-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/suspend-user.ts @@ -56,7 +56,7 @@ export default class extends Endpoint { isSuspended: true, }); - this.moderationLogService.insertModerationLog(me, 'suspend', { + this.moderationLogService.log(me, 'suspend', { targetId: user.id, }); diff --git a/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts b/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts index 2805c21a7..93b03d8d4 100644 --- a/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts @@ -41,7 +41,7 @@ export default class extends Endpoint { isSuspended: false, }); - this.moderationLogService.insertModerationLog(me, 'unsuspend', { + this.moderationLogService.log(me, 'unsuspend', { targetId: user.id, }); diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index 0e94f56cf..68aa838ba 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -398,7 +398,7 @@ export default class extends Endpoint { } await this.metaService.update(set); - this.moderationLogService.insertModerationLog(me, 'updateMeta'); + this.moderationLogService.log(me, 'updateMeta'); }); } }