2023-01-12 21:02:26 +09:00
|
|
|
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';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['admin', 'role'],
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
requireAdmin: true,
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchRole: {
|
|
|
|
message: 'No such role.',
|
|
|
|
code: 'NO_SUCH_ROLE',
|
|
|
|
id: 'cd23ef55-09ad-428a-ac61-95a45e124b32',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
roleId: { type: 'string', format: 'misskey:id' },
|
|
|
|
name: { type: 'string' },
|
|
|
|
description: { type: 'string' },
|
|
|
|
color: { type: 'string', nullable: true },
|
2023-01-13 11:03:54 +09:00
|
|
|
target: { type: 'string' },
|
|
|
|
condFormula: { type: 'object' },
|
2023-01-12 21:02:26 +09:00
|
|
|
isPublic: { type: 'boolean' },
|
|
|
|
isModerator: { type: 'boolean' },
|
|
|
|
isAdministrator: { type: 'boolean' },
|
|
|
|
canEditMembersByModerator: { type: 'boolean' },
|
2023-01-15 20:52:53 +09:00
|
|
|
policies: {
|
2023-01-12 21:02:26 +09:00
|
|
|
type: 'object',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: [
|
|
|
|
'roleId',
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'color',
|
2023-01-13 11:03:54 +09:00
|
|
|
'target',
|
|
|
|
'condFormula',
|
2023-01-12 21:02:26 +09:00
|
|
|
'isPublic',
|
|
|
|
'isModerator',
|
|
|
|
'isAdministrator',
|
|
|
|
'canEditMembersByModerator',
|
2023-01-15 20:52:53 +09:00
|
|
|
'policies',
|
2023-01-12 21:02:26 +09:00
|
|
|
],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.rolesRepository)
|
|
|
|
private rolesRepository: RolesRepository,
|
|
|
|
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps) => {
|
|
|
|
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, {
|
|
|
|
updatedAt: date,
|
|
|
|
name: ps.name,
|
|
|
|
description: ps.description,
|
|
|
|
color: ps.color,
|
2023-01-13 11:03:54 +09:00
|
|
|
target: ps.target,
|
|
|
|
condFormula: ps.condFormula,
|
2023-01-12 21:02:26 +09:00
|
|
|
isPublic: ps.isPublic,
|
|
|
|
isModerator: ps.isModerator,
|
|
|
|
isAdministrator: ps.isAdministrator,
|
|
|
|
canEditMembersByModerator: ps.canEditMembersByModerator,
|
2023-01-15 20:52:53 +09:00
|
|
|
policies: ps.policies,
|
2023-01-12 21:02:26 +09:00
|
|
|
});
|
|
|
|
const updated = await this.rolesRepository.findOneByOrFail({ id: ps.roleId });
|
|
|
|
this.globalEventService.publishInternalEvent('roleUpdated', updated);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|