
* chore: 著作権とライセンスについての情報を各ファイルに追加する * chore: Add the SPDX information to each file Add copyright and licensing information as defined in version 3.0 of the REUSE Specification. * tweak format --------- Co-authored-by: syuilo <Syuilotan@yahoo.co.jp> * chore: Add SPDX-License-Identifier [skip ci] * add missing SPDX-License-Identifier * remove unused file --------- Co-authored-by: Shun Sakai <sorairolake@protonmail.ch> Co-authored-by: syuilo <Syuilotan@yahoo.co.jp> Co-authored-by: Chocolate Pie <106949016+chocolate-pie@users.noreply.github.com>
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { Brackets } from 'typeorm';
|
|
import { DI } from '@/di-symbols.js';
|
|
import type { RoleAssignmentsRepository, RolesRepository } from '@/models/index.js';
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
import type { User } from '@/models/entities/User.js';
|
|
import type { Role } from '@/models/entities/Role.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
import { DEFAULT_POLICIES } from '@/core/RoleService.js';
|
|
import { Packed } from '@/misc/json-schema.js';
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
@Injectable()
|
|
export class RoleEntityService {
|
|
constructor(
|
|
@Inject(DI.rolesRepository)
|
|
private rolesRepository: RolesRepository,
|
|
|
|
@Inject(DI.roleAssignmentsRepository)
|
|
private roleAssignmentsRepository: RoleAssignmentsRepository,
|
|
|
|
private userEntityService: UserEntityService,
|
|
) {
|
|
}
|
|
|
|
@bindThis
|
|
public async pack(
|
|
src: Role['id'] | Role,
|
|
me: { id: User['id'] } | null | undefined,
|
|
) : Promise<Packed<'Role'>> {
|
|
const role = typeof src === 'object' ? src : await this.rolesRepository.findOneByOrFail({ id: src });
|
|
|
|
const assignedCount = await this.roleAssignmentsRepository.createQueryBuilder('assign')
|
|
.where('assign.roleId = :roleId', { roleId: role.id })
|
|
.andWhere(new Brackets(qb => { qb
|
|
.where('assign.expiresAt IS NULL')
|
|
.orWhere('assign.expiresAt > :now', { now: new Date() });
|
|
}))
|
|
.getCount();
|
|
|
|
const policies = { ...role.policies };
|
|
for (const [k, v] of Object.entries(DEFAULT_POLICIES)) {
|
|
if (policies[k] == null) policies[k] = {
|
|
useDefault: true,
|
|
priority: 0,
|
|
value: v,
|
|
};
|
|
}
|
|
|
|
return await awaitAll({
|
|
id: role.id,
|
|
createdAt: role.createdAt.toISOString(),
|
|
updatedAt: role.updatedAt.toISOString(),
|
|
name: role.name,
|
|
description: role.description,
|
|
color: role.color,
|
|
iconUrl: role.iconUrl,
|
|
target: role.target,
|
|
condFormula: role.condFormula,
|
|
isPublic: role.isPublic,
|
|
isAdministrator: role.isAdministrator,
|
|
isModerator: role.isModerator,
|
|
isExplorable: role.isExplorable,
|
|
asBadge: role.asBadge,
|
|
canEditMembersByModerator: role.canEditMembersByModerator,
|
|
displayOrder: role.displayOrder,
|
|
policies: policies,
|
|
usersCount: assignedCount,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public async packMany(
|
|
roles: (Role['id'] | Role)[],
|
|
me: { id: User['id'] } | null | undefined,
|
|
) : Promise<Packed<'Role'>[]> {
|
|
return (await Promise.allSettled(roles.map(x => this.pack(x, me))))
|
|
.filter(result => result.status === 'fulfilled')
|
|
.map(result => (result as PromiseFulfilledResult<Packed<'Role'>>).value);
|
|
}
|
|
}
|