Compare commits

...

3 commits

Author SHA1 Message Date
syuilo
0085b1aca9 wip 2023-06-02 15:11:54 +09:00
syuilo
bcacebc89f Merge branch 'develop' into refine-moderation-log 2023-06-02 14:56:40 +09:00
syuilo
40df6c5168 wip 2023-06-02 08:57:13 +09:00
9 changed files with 57 additions and 16 deletions

View file

@ -16,7 +16,7 @@ export class ModerationLogService {
}
@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({
id: this.idService.genId(),
createdAt: new Date(),

View file

@ -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<void> {
public async assign(userId: User['id'], roleId: Role['id'], expiresAt: Date | null = null, moderator?: User): Promise<void> {
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<void> {
public async unassign(userId: User['id'], roleId: Role['id'], moderator?: User): Promise<void> {
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
@ -432,6 +455,26 @@ export class RoleService implements OnApplicationShutdown {
redisPipeline.exec();
}
@bindThis
public async update(role: Role, params: Partial<Role>, moderator?: User): 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, 'roleUpdated', {
roleId: role.id,
before: role,
after: updated,
});
}
}
@bindThis
public dispose(): void {
this.redisForSub.off('message', this.onMessage);

View file

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

View file

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

View file

@ -1,9 +1,9 @@
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { RolesRepository } from '@/models/index.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '@/server/api/error.js';
import { RoleService } from '@/core/RoleService.js';
export const meta = {
tags: ['admin', 'role'],
@ -66,16 +66,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
@Inject(DI.rolesRepository)
private rolesRepository: RolesRepository,
private globalEventService: GlobalEventService,
private roleService: RoleService,
) {
super(meta, paramDef, async (ps) => {
super(meta, paramDef, async (ps, me) => {
const role = await this.rolesRepository.findOneBy({ id: ps.roleId });
if (role == null) {
throw new ApiError(meta.errors.noSuchRole);
}
const date = new Date();
await this.rolesRepository.update(ps.roleId, {
await this.roleService.update(role, {
updatedAt: date,
name: ps.name,
description: ps.description,
@ -91,9 +91,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
canEditMembersByModerator: ps.canEditMembersByModerator,
displayOrder: ps.displayOrder,
policies: ps.policies,
});
const updated = await this.rolesRepository.findOneByOrFail({ id: ps.roleId });
this.globalEventService.publishInternalEvent('roleUpdated', updated);
}, me);
});
}
}

View file

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

View file

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

View file

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