sharkey/packages/backend/src/server/api/endpoints/admin/roles/update.ts

89 lines
2.3 KiB
TypeScript
Raw Normal View History

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' },
isPublic: { type: 'boolean' },
isModerator: { type: 'boolean' },
isAdministrator: { type: 'boolean' },
canEditMembersByModerator: { type: 'boolean' },
options: {
type: 'object',
},
},
required: [
'roleId',
'name',
'description',
'color',
2023-01-13 11:03:54 +09:00
'target',
'condFormula',
'isPublic',
'isModerator',
'isAdministrator',
'canEditMembersByModerator',
'options',
],
} 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,
isPublic: ps.isPublic,
isModerator: ps.isModerator,
isAdministrator: ps.isAdministrator,
canEditMembersByModerator: ps.canEditMembersByModerator,
options: ps.options,
});
const updated = await this.rolesRepository.findOneByOrFail({ id: ps.roleId });
this.globalEventService.publishInternalEvent('roleUpdated', updated);
});
}
}