2023-07-27 00:31:52 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-12 06:02:26 -06:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-02-28 19:20:03 -06:00
|
|
|
import type { RolesRepository, UsersRepository } from '@/models/index.js';
|
2023-01-12 06:02:26 -06:00
|
|
|
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'],
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchRole: {
|
|
|
|
message: 'No such role.',
|
|
|
|
code: 'NO_SUCH_ROLE',
|
|
|
|
id: '6503c040-6af4-4ed9-bf07-f2dd16678eab',
|
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: '558ea170-f653-4700-94d0-5a818371d0df',
|
|
|
|
},
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
message: 'Only administrators can edit members of the role.',
|
|
|
|
code: 'ACCESS_DENIED',
|
|
|
|
id: '25b5bc31-dc79-4ebd-9bd2-c84978fd052c',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
roleId: { type: 'string', format: 'misskey:id' },
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
2023-02-28 19:20:03 -06:00
|
|
|
expiresAt: {
|
|
|
|
type: 'integer',
|
|
|
|
nullable: true,
|
|
|
|
},
|
2023-01-12 06:02:26 -06:00
|
|
|
},
|
|
|
|
required: [
|
|
|
|
'roleId',
|
|
|
|
'userId',
|
|
|
|
],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
@Injectable()
|
2023-08-17 07:20:58 -05:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2023-01-12 06:02:26 -06:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.rolesRepository)
|
|
|
|
private rolesRepository: RolesRepository,
|
|
|
|
|
|
|
|
private roleService: RoleService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const role = await this.rolesRepository.findOneBy({ id: ps.roleId });
|
|
|
|
if (role == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!role.canEditMembersByModerator && !(await this.roleService.isAdministrator(me))) {
|
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: ps.userId });
|
|
|
|
if (user == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
}
|
|
|
|
|
2023-02-28 19:20:03 -06:00
|
|
|
if (ps.expiresAt && ps.expiresAt <= Date.now()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-12 06:02:26 -06:00
|
|
|
|
2023-02-28 19:20:03 -06:00
|
|
|
await this.roleService.assign(user.id, role.id, ps.expiresAt ? new Date(ps.expiresAt) : null);
|
2023-01-12 06:02:26 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|