This commit is contained in:
tamaina 2023-05-27 19:17:29 +00:00
parent 8d36911f90
commit 654c93b0ce
6 changed files with 273 additions and 44 deletions

View file

@ -8,6 +8,8 @@ import type { Role } from '@/models/entities/Role.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { DEFAULT_POLICIES } from '@/core/RoleService.js'; import { DEFAULT_POLICIES } from '@/core/RoleService.js';
import { UserEntityService } from './UserEntityService.js'; import { UserEntityService } from './UserEntityService.js';
import { Packed } from 'misskey-js';
import { Serialized } from 'schema-type';
@Injectable() @Injectable()
export class RoleEntityService { export class RoleEntityService {
@ -26,7 +28,7 @@ export class RoleEntityService {
public async pack( public async pack(
src: Role['id'] | Role, src: Role['id'] | Role,
me?: { id: User['id'] } | null | undefined, me?: { id: User['id'] } | null | undefined,
) { ): Promise<Serialized<Packed<'Role'>>> {
const role = typeof src === 'object' ? src : await this.rolesRepository.findOneByOrFail({ id: src }); const role = typeof src === 'object' ? src : await this.rolesRepository.findOneByOrFail({ id: src });
const assignedCount = await this.roleAssignmentsRepository.createQueryBuilder('assign') const assignedCount = await this.roleAssignmentsRepository.createQueryBuilder('assign')
@ -37,7 +39,7 @@ export class RoleEntityService {
})) }))
.getCount(); .getCount();
const policies = { ...role.policies }; const policies: { [x: string]: Packed<'RolePolicy'> } = { ...role.policies };
for (const [k, v] of Object.entries(DEFAULT_POLICIES)) { for (const [k, v] of Object.entries(DEFAULT_POLICIES)) {
if (policies[k] == null) policies[k] = { if (policies[k] == null) policies[k] = {
useDefault: true, useDefault: true,
@ -72,8 +74,7 @@ export class RoleEntityService {
public packMany( public packMany(
roles: any[], roles: any[],
me: { id: User['id'] }, me: { id: User['id'] },
) { ): Promise<Serialized<Packed<'Role'>>[]> {
return Promise.all(roles.map(x => this.pack(x, me))); return Promise.all(roles.map(x => this.pack(x, me)));
} }
} }

View file

@ -39,6 +39,10 @@ export type Channel = Packed<'Channel'>;
export type Following = Packed<'Following'>; export type Following = Packed<'Following'>;
export type Blocking = Packed<'Blocking'>; export type Blocking = Packed<'Blocking'>;
export type Relay = Packed<'Relay'>; export type Relay = Packed<'Relay'>;
export type Role = Packed<'Role'>;
export type RoleAssign = Packed<'RoleAssign'>;
export type RolePolicy = Packed<'RolePolicy'>;
export type RoleCondFormula = Packed<'RoleCondFormula'>;
export type LiteInstanceMetadata = { export type LiteInstanceMetadata = {
maintainerName: string | null; maintainerName: string | null;

View file

@ -36,7 +36,12 @@ import { packedFlashSchema } from './schemas/flash.js';
import { packedAdSchema } from './schemas/ad.js'; import { packedAdSchema } from './schemas/ad.js';
import { packedAnnouncementSchema } from './schemas/announcement.js'; import { packedAnnouncementSchema } from './schemas/announcement.js';
import { packedRelaySchema } from './schemas/relay.js'; import { packedRelaySchema } from './schemas/relay.js';
import { packedRoleAssignSchema } from './schemas/role.js'; import {
packedRoleSchema,
packedRoleAssignSchema,
packedRolePolicySchema,
packedRoleCondFormulaSchema,
} from './schemas/role.js';
import { Error, ApiError } from './schemas/error.js'; import { Error, ApiError } from './schemas/error.js';
import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray } from 'schema-type'; import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray } from 'schema-type';
@ -78,8 +83,10 @@ export const refs = {
Ad: packedAdSchema, Ad: packedAdSchema,
Announcement: packedAnnouncementSchema, Announcement: packedAnnouncementSchema,
Relay: packedRelaySchema, Relay: packedRelaySchema,
Role: packedRoleSchema,
RoleAssign: packedRoleAssignSchema, RoleAssign: packedRoleAssignSchema,
RolePolicy: packedRolePolicySchema,
RoleCondFormula: packedRoleCondFormulaSchema,
Error: Error, Error: Error,
ApiError: ApiError, ApiError: ApiError,

View file

@ -31,14 +31,64 @@ export const packedRoleSchema = {
'manual', 'manual',
'conditional', 'conditional',
], ],
} },
condFormula: {
type: 'object',
// 循環参照なので難しい
//$ref: 'https://misskey-hub.net/api/schemas/RoleCondFormula',
},
isPublic: {
type: 'boolean',
},
isAdministrator: {
type: 'boolean',
},
isModerator: {
type: 'boolean',
},
isExplorable: {
type: 'boolean',
},
asBadge: {
type: 'boolean',
},
canEditMembersByModerator: {
type: 'boolean',
},
displayOrder: {
type: 'number',
},
policies: {
type: 'object',
additionalProperties: {
$ref: 'https://misskey-hub.net/api/schemas/RolePolicy',
},
},
usersCount: {
type: 'number',
},
}, },
required: [ required: [
'id', 'id',
'createdAt', 'createdAt',
'updatedAt', 'updatedAt',
'name',
'description',
'color',
'iconUrl',
'target',
'condFormula',
'isPublic',
'isAdministrator',
'isModerator',
'isExplorable',
'asBadge',
'canEditMembersByModerator',
'displayOrder',
'policies',
'usersCount',
], ],
} } as const satisfies JSONSchema7Definition;
export const packedRoleAssignSchema = { export const packedRoleAssignSchema = {
$id: 'https://misskey-hub.net/api/schemas/RoleAssign', $id: 'https://misskey-hub.net/api/schemas/RoleAssign',
@ -67,3 +117,167 @@ export const packedRoleAssignSchema = {
'expiresAt', 'expiresAt',
], ],
} as const satisfies JSONSchema7Definition; } as const satisfies JSONSchema7Definition;
export const packedRolePolicySchema = {
$id: 'https://misskey-hub.net/api/schemas/RolePolicy',
type: 'object',
properties: {
useDefault: { type: 'boolean' },
priority: { type: 'number' },
value: { additionalProperties: true },
},
required: [
'useDefault',
'priority',
'value',
],
} as const satisfies JSONSchema7Definition;
export const packedRoleCondFormulaSchema = {
$id: 'https://misskey-hub.net/api/schemas/RoleCondFormula',
oneOf: [
{ $ref: '#/$defs/and' },
{ $ref: '#/$defs/or' },
{ $ref: '#/$defs/not' },
{ $ref: '#/$defs/isLocal' },
{ $ref: '#/$defs/isRemote' },
{ $ref: '#/$defs/createdLessThan' },
{ $ref: '#/$defs/createdMoreThan' },
{ $ref: '#/$defs/createdLessThanOrEq' },
{ $ref: '#/$defs/createdMoreThanOrEq' },
{ $ref: '#/$defs/followersLessThanOrEq' },
{ $ref: '#/$defs/followersMoreThanOrEq' },
{ $ref: '#/$defs/followingLessThanOrEq' },
{ $ref: '#/$defs/followingMoreThanOrEq' },
{ $ref: '#/$defs/notesLessThanOrEq' },
{ $ref: '#/$defs/notesMoreThanOrEq' },
],
$defs: {
and: {
type: 'object',
properties: {
type: { const: 'and' },
values: {
type: 'array',
items: { $ref: '#' },
},
},
required: ['type', 'values'],
},
or: {
type: 'object',
properties: {
type: { const: 'or' },
values: {
type: 'array',
items: { $ref: '#' },
},
},
required: ['type', 'values'],
},
not: {
type: 'object',
properties: {
type: { const: 'not' },
value: { $ref: '#' },
},
required: ['type', 'value'],
},
isLocal: {
type: 'object',
properties: {
type: { const: 'isLocal' },
},
required: ['type'],
},
isRemote: {
type: 'object',
properties: {
type: { const: 'isRemote' },
},
required: ['type'],
},
createdLessThan: {
type: 'object',
properties: {
type: { const: 'createdLessThan' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
createdMoreThan: {
type: 'object',
properties: {
type: { const: 'createdMoreThan' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
createdLessThanOrEq: {
type: 'object',
properties: {
type: { const: 'createdLessThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
createdMoreThanOrEq: {
type: 'object',
properties: {
type: { const: 'createdMoreThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
followersLessThanOrEq: {
type: 'object',
properties: {
type: { const: 'followersLessThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
followersMoreThanOrEq: {
type: 'object',
properties: {
type: { const: 'followersMoreThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
followingLessThanOrEq: {
type: 'object',
properties: {
type: { const: 'followingLessThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
followingMoreThanOrEq: {
type: 'object',
properties: {
type: { const: 'followingMoreThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
notesLessThanOrEq: {
type: 'object',
properties: {
type: { const: 'notesLessThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
notesMoreThanOrEq: {
type: 'object',
properties: {
type: { const: 'notesMoreThanOrEq' },
value: { type: 'number' },
},
required: ['type', 'value'],
},
}
} as const satisfies JSONSchema7Definition;

View file

@ -100,9 +100,12 @@ describe('schemas', () => {
test('relay', () => { test('relay', () => {
type Relay = Packed<'Relay'>; type Relay = Packed<'Relay'>;
}); });
test('role': () => { test('role', () => {
type Role = Packed<'Role'>;
type CF = Role['condFormula'];
type RoleAssign = Packed<'RoleAssign'>; type RoleAssign = Packed<'RoleAssign'>;
type RolePolicy = Packed<'RolePolicy'>;
type RoleCondFormula = Packed<'RoleCondFormula'>;
}); });
test('error', () => { test('error', () => {
type Error = Packed<'Error'>; type Error = Packed<'Error'>;

View file

@ -626,7 +626,7 @@ importers:
version: 29.5.0 version: 29.5.0
schema-type: schema-type:
specifier: github:misskey-dev/schema-type specifier: github:misskey-dev/schema-type
version: github.com/misskey-dev/schema-type/ecfe907fb71f89eb0e41ed4449246ccd8366a260(typescript@5.0.4) version: github.com/misskey-dev/schema-type/04addbe29c488e9e749bb9fe09812407da132e11(typescript@5.0.4)
packages/frontend: packages/frontend:
dependencies: dependencies:
@ -1026,7 +1026,7 @@ importers:
version: 4.4.0 version: 4.4.0
schema-type: schema-type:
specifier: github:misskey-dev/schema-type specifier: github:misskey-dev/schema-type
version: github.com/misskey-dev/schema-type/ecfe907fb71f89eb0e41ed4449246ccd8366a260(typescript@5.0.4) version: github.com/misskey-dev/schema-type/04addbe29c488e9e749bb9fe09812407da132e11(typescript@5.0.4)
ts-essentials: ts-essentials:
specifier: ^9.3.2 specifier: ^9.3.2
version: 9.3.2(typescript@5.0.4) version: 9.3.2(typescript@5.0.4)
@ -20518,9 +20518,9 @@ packages:
version: 0.0.0 version: 0.0.0
dev: false dev: false
github.com/misskey-dev/schema-type/ecfe907fb71f89eb0e41ed4449246ccd8366a260(typescript@5.0.4): github.com/misskey-dev/schema-type/04addbe29c488e9e749bb9fe09812407da132e11(typescript@5.0.4):
resolution: {tarball: https://codeload.github.com/misskey-dev/schema-type/tar.gz/ecfe907fb71f89eb0e41ed4449246ccd8366a260} resolution: {tarball: https://codeload.github.com/misskey-dev/schema-type/tar.gz/04addbe29c488e9e749bb9fe09812407da132e11}
id: github.com/misskey-dev/schema-type/ecfe907fb71f89eb0e41ed4449246ccd8366a260 id: github.com/misskey-dev/schema-type/04addbe29c488e9e749bb9fe09812407da132e11
name: schema-type name: schema-type
version: 1.0.0 version: 1.0.0
dependencies: dependencies: