enhance(backend): refine moderation log (#10939)

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update DriveService.ts
This commit is contained in:
syuilo 2023-09-23 18:28:16 +09:00 committed by GitHub
parent ba6e85482e
commit 9e4d3ebe5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 563 additions and 39 deletions

View file

@ -18,6 +18,7 @@ import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { StreamMessages } from '@/server/api/stream/types.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import type { Packed } from '@/misc/json-schema.js';
import type { OnApplicationShutdown } from '@nestjs/common';
@ -98,6 +99,7 @@ export class RoleService implements OnApplicationShutdown {
private userEntityService: UserEntityService,
private globalEventService: GlobalEventService,
private idService: IdService,
private moderationLogService: ModerationLogService,
) {
//this.onMessage = this.onMessage.bind(this);
@ -374,9 +376,11 @@ export class RoleService implements OnApplicationShutdown {
}
@bindThis
public async assign(userId: MiUser['id'], roleId: MiRole['id'], expiresAt: Date | null = null): Promise<void> {
public async assign(userId: MiUser['id'], roleId: MiRole['id'], expiresAt: Date | null = null, moderator?: MiUser): Promise<void> {
const now = new Date();
const role = await this.rolesRepository.findOneByOrFail({ id: roleId });
const existing = await this.roleAssignmentsRepository.findOneBy({
roleId: roleId,
userId: userId,
@ -406,10 +410,19 @@ export class RoleService implements OnApplicationShutdown {
});
this.globalEventService.publishInternalEvent('userRoleAssigned', created);
if (moderator) {
this.moderationLogService.log(moderator, 'assignRole', {
roleId: roleId,
roleName: role.name,
userId: userId,
expiresAt: expiresAt ? expiresAt.toISOString() : null,
});
}
}
@bindThis
public async unassign(userId: MiUser['id'], roleId: MiRole['id']): Promise<void> {
public async unassign(userId: MiUser['id'], roleId: MiRole['id'], moderator?: MiUser): Promise<void> {
const now = new Date();
const existing = await this.roleAssignmentsRepository.findOneBy({ roleId, userId });
@ -430,6 +443,15 @@ export class RoleService implements OnApplicationShutdown {
});
this.globalEventService.publishInternalEvent('userRoleUnassigned', existing);
if (moderator) {
const role = await this.rolesRepository.findOneByOrFail({ id: roleId });
this.moderationLogService.log(moderator, 'unassignRole', {
roleId: roleId,
roleName: role.name,
userId: userId,
});
}
}
@bindThis
@ -451,6 +473,26 @@ export class RoleService implements OnApplicationShutdown {
redisPipeline.exec();
}
@bindThis
public async update(role: MiRole, params: Partial<MiRole>, moderator?: MiUser): Promise<void> {
const date = new Date();
await this.rolesRepository.update(role.id, {
updatedAt: date,
...params,
});
const updated = await this.rolesRepository.findOneByOrFail({ id: role.id });
this.globalEventService.publishInternalEvent('roleUpdated', updated);
if (moderator) {
this.moderationLogService.log(moderator, 'updateRole', {
roleId: role.id,
before: role,
after: updated,
});
}
}
@bindThis
public dispose(): void {
this.redisForSub.off('message', this.onMessage);