2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 04:50:05 +00:00
|
|
|
import * as Redis from 'ioredis';
|
2023-01-12 12:02:26 +00:00
|
|
|
import { In } from 'typeorm';
|
2023-12-21 01:39:11 +00:00
|
|
|
import { ModuleRef } from '@nestjs/core';
|
|
|
|
import type {
|
|
|
|
MiRole,
|
|
|
|
MiRoleAssignment,
|
|
|
|
RoleAssignmentsRepository,
|
|
|
|
RolesRepository,
|
|
|
|
UsersRepository,
|
|
|
|
} from '@/models/_.js';
|
2023-04-06 02:14:43 +00:00
|
|
|
import { MemoryKVCache, MemorySingleCache } from '@/misc/cache.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2023-01-12 12:02:26 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-04-04 08:32:09 +00:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { RoleCondFormulaValue } from '@/models/Role.js';
|
2023-01-13 02:03:54 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2023-09-29 02:29:54 +00:00
|
|
|
import type { GlobalEvents } from '@/core/GlobalEventService.js';
|
2023-03-01 01:20:03 +00:00
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-12-21 01:39:11 +00:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-23 09:28:16 +00:00
|
|
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
2023-06-25 12:13:15 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-11-26 01:02:22 +00:00
|
|
|
import { FanoutTimelineService } from '@/core/FanoutTimelineService.js';
|
2023-12-21 01:39:11 +00:00
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
|
|
|
import type { OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
|
2023-01-12 12:02:26 +00:00
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
export type RolePolicies = {
|
2023-01-12 12:02:26 +00:00
|
|
|
gtlAvailable: boolean;
|
|
|
|
ltlAvailable: boolean;
|
2023-12-04 01:10:51 +00:00
|
|
|
btlAvailable: boolean;
|
2023-01-12 12:02:26 +00:00
|
|
|
canPublicNote: boolean;
|
2023-01-13 05:22:53 +00:00
|
|
|
canInvite: boolean;
|
2023-07-15 00:57:58 +00:00
|
|
|
inviteLimit: number;
|
|
|
|
inviteLimitCycle: number;
|
|
|
|
inviteExpirationTime: number;
|
2023-01-13 05:58:27 +00:00
|
|
|
canManageCustomEmojis: boolean;
|
2023-10-30 06:33:15 +00:00
|
|
|
canManageAvatarDecorations: boolean;
|
2023-03-13 08:52:24 +00:00
|
|
|
canSearchNotes: boolean;
|
2023-09-29 22:54:11 +00:00
|
|
|
canUseTranslator: boolean;
|
2023-01-16 02:21:04 +00:00
|
|
|
canHideAds: boolean;
|
2023-01-12 12:02:26 +00:00
|
|
|
driveCapacityMb: number;
|
2023-05-05 05:18:06 +00:00
|
|
|
alwaysMarkNsfw: boolean;
|
2023-01-14 09:04:56 +00:00
|
|
|
pinLimit: number;
|
2023-01-12 12:02:26 +00:00
|
|
|
antennaLimit: number;
|
2023-01-13 23:04:38 +00:00
|
|
|
wordMuteLimit: number;
|
2023-01-14 01:48:11 +00:00
|
|
|
webhookLimit: number;
|
2023-01-14 07:14:24 +00:00
|
|
|
clipLimit: number;
|
|
|
|
noteEachClipsLimit: number;
|
2023-01-14 08:38:16 +00:00
|
|
|
userListLimit: number;
|
|
|
|
userEachUserListsLimit: number;
|
2023-01-15 07:52:12 +00:00
|
|
|
rateLimitFactor: number;
|
2023-11-12 14:07:32 +00:00
|
|
|
canImportNotes: boolean;
|
2023-12-13 07:56:19 +00:00
|
|
|
avatarDecorationLimit: number;
|
2023-01-12 12:02:26 +00:00
|
|
|
};
|
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
export const DEFAULT_POLICIES: RolePolicies = {
|
2023-01-12 12:02:26 +00:00
|
|
|
gtlAvailable: true,
|
|
|
|
ltlAvailable: true,
|
2023-12-04 01:10:51 +00:00
|
|
|
btlAvailable: false,
|
2023-01-12 12:02:26 +00:00
|
|
|
canPublicNote: true,
|
2023-01-13 05:22:53 +00:00
|
|
|
canInvite: false,
|
2023-07-15 00:57:58 +00:00
|
|
|
inviteLimit: 0,
|
|
|
|
inviteLimitCycle: 60 * 24 * 7,
|
|
|
|
inviteExpirationTime: 0,
|
2023-01-13 05:58:27 +00:00
|
|
|
canManageCustomEmojis: false,
|
2023-10-30 06:33:15 +00:00
|
|
|
canManageAvatarDecorations: false,
|
2023-03-13 08:52:24 +00:00
|
|
|
canSearchNotes: false,
|
2023-09-29 22:54:11 +00:00
|
|
|
canUseTranslator: true,
|
2023-01-16 02:21:04 +00:00
|
|
|
canHideAds: false,
|
2023-01-12 12:02:26 +00:00
|
|
|
driveCapacityMb: 100,
|
2023-05-05 05:18:06 +00:00
|
|
|
alwaysMarkNsfw: false,
|
2023-01-14 09:04:56 +00:00
|
|
|
pinLimit: 5,
|
2023-01-12 12:02:26 +00:00
|
|
|
antennaLimit: 5,
|
2023-01-13 23:04:38 +00:00
|
|
|
wordMuteLimit: 200,
|
2023-01-14 01:48:11 +00:00
|
|
|
webhookLimit: 3,
|
2023-01-14 07:14:24 +00:00
|
|
|
clipLimit: 10,
|
|
|
|
noteEachClipsLimit: 200,
|
2023-01-14 08:38:16 +00:00
|
|
|
userListLimit: 10,
|
|
|
|
userEachUserListsLimit: 50,
|
2023-01-15 07:52:12 +00:00
|
|
|
rateLimitFactor: 1,
|
2023-11-12 14:07:32 +00:00
|
|
|
canImportNotes: true,
|
2023-12-13 07:56:19 +00:00
|
|
|
avatarDecorationLimit: 1,
|
2023-01-12 12:02:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@Injectable()
|
2023-12-21 01:39:11 +00:00
|
|
|
export class RoleService implements OnApplicationShutdown, OnModuleInit {
|
2023-08-16 08:51:28 +00:00
|
|
|
private rolesCache: MemorySingleCache<MiRole[]>;
|
|
|
|
private roleAssignmentByUserIdCache: MemoryKVCache<MiRoleAssignment[]>;
|
2023-12-21 01:39:11 +00:00
|
|
|
private notificationService: NotificationService;
|
2023-01-12 12:02:26 +00:00
|
|
|
|
2023-03-01 01:20:03 +00:00
|
|
|
public static AlreadyAssignedError = class extends Error {};
|
|
|
|
public static NotAssignedError = class extends Error {};
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
constructor(
|
2023-12-21 01:39:11 +00:00
|
|
|
private moduleRef: ModuleRef,
|
|
|
|
|
2023-04-12 02:40:08 +00:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2023-11-21 00:55:49 +00:00
|
|
|
@Inject(DI.redisForTimelines)
|
|
|
|
private redisForTimelines: Redis.Redis,
|
|
|
|
|
2023-04-09 08:09:27 +00:00
|
|
|
@Inject(DI.redisForSub)
|
|
|
|
private redisForSub: Redis.Redis,
|
2023-01-12 12:02:26 +00:00
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.rolesRepository)
|
|
|
|
private rolesRepository: RolesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.roleAssignmentsRepository)
|
|
|
|
private roleAssignmentsRepository: RoleAssignmentsRepository,
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
2023-04-04 08:32:09 +00:00
|
|
|
private cacheService: CacheService,
|
2023-01-13 02:03:54 +00:00
|
|
|
private userEntityService: UserEntityService,
|
2023-03-01 01:20:03 +00:00
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
private idService: IdService,
|
2023-09-23 09:28:16 +00:00
|
|
|
private moderationLogService: ModerationLogService,
|
2023-11-26 01:02:22 +00:00
|
|
|
private fanoutTimelineService: FanoutTimelineService,
|
2023-01-12 12:02:26 +00:00
|
|
|
) {
|
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
|
|
|
|
2023-08-16 08:51:28 +00:00
|
|
|
this.rolesCache = new MemorySingleCache<MiRole[]>(1000 * 60 * 60 * 1);
|
|
|
|
this.roleAssignmentByUserIdCache = new MemoryKVCache<MiRoleAssignment[]>(1000 * 60 * 60 * 1);
|
2023-01-12 12:02:26 +00:00
|
|
|
|
2023-04-09 08:09:27 +00:00
|
|
|
this.redisForSub.on('message', this.onMessage);
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 01:39:11 +00:00
|
|
|
async onModuleInit() {
|
|
|
|
this.notificationService = this.moduleRef.get(NotificationService.name);
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
@bindThis
|
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-09-29 02:29:54 +00:00
|
|
|
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
|
2023-01-12 12:02:26 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'roleCreated': {
|
2023-04-04 08:32:09 +00:00
|
|
|
const cached = this.rolesCache.get();
|
2023-01-12 12:02:26 +00:00
|
|
|
if (cached) {
|
2023-01-25 02:18:16 +00:00
|
|
|
cached.push({
|
|
|
|
...body,
|
|
|
|
updatedAt: new Date(body.updatedAt),
|
|
|
|
lastUsedAt: new Date(body.lastUsedAt),
|
|
|
|
});
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'roleUpdated': {
|
2023-04-04 08:32:09 +00:00
|
|
|
const cached = this.rolesCache.get();
|
2023-01-12 12:02:26 +00:00
|
|
|
if (cached) {
|
|
|
|
const i = cached.findIndex(x => x.id === body.id);
|
|
|
|
if (i > -1) {
|
2023-01-25 02:18:16 +00:00
|
|
|
cached[i] = {
|
|
|
|
...body,
|
|
|
|
updatedAt: new Date(body.updatedAt),
|
|
|
|
lastUsedAt: new Date(body.lastUsedAt),
|
|
|
|
};
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'roleDeleted': {
|
2023-04-04 08:32:09 +00:00
|
|
|
const cached = this.rolesCache.get();
|
2023-01-12 12:02:26 +00:00
|
|
|
if (cached) {
|
2023-04-04 08:32:09 +00:00
|
|
|
this.rolesCache.set(cached.filter(x => x.id !== body.id));
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'userRoleAssigned': {
|
|
|
|
const cached = this.roleAssignmentByUserIdCache.get(body.userId);
|
|
|
|
if (cached) {
|
2023-01-25 02:18:16 +00:00
|
|
|
cached.push({
|
|
|
|
...body,
|
2023-03-01 01:20:03 +00:00
|
|
|
expiresAt: body.expiresAt ? new Date(body.expiresAt) : null,
|
2023-01-25 02:18:16 +00:00
|
|
|
});
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'userRoleUnassigned': {
|
|
|
|
const cached = this.roleAssignmentByUserIdCache.get(body.userId);
|
|
|
|
if (cached) {
|
|
|
|
this.roleAssignmentByUserIdCache.set(body.userId, cached.filter(x => x.id !== body.id));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 02:03:54 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
private evalCond(user: MiUser, value: RoleCondFormulaValue): boolean {
|
2023-01-13 02:03:54 +00:00
|
|
|
try {
|
|
|
|
switch (value.type) {
|
|
|
|
case 'and': {
|
|
|
|
return value.values.every(v => this.evalCond(user, v));
|
|
|
|
}
|
|
|
|
case 'or': {
|
|
|
|
return value.values.some(v => this.evalCond(user, v));
|
|
|
|
}
|
|
|
|
case 'not': {
|
|
|
|
return !this.evalCond(user, value.value);
|
|
|
|
}
|
|
|
|
case 'isLocal': {
|
|
|
|
return this.userEntityService.isLocalUser(user);
|
|
|
|
}
|
|
|
|
case 'isRemote': {
|
|
|
|
return this.userEntityService.isRemoteUser(user);
|
|
|
|
}
|
|
|
|
case 'createdLessThan': {
|
2023-10-16 01:45:22 +00:00
|
|
|
return this.idService.parse(user.id).date.getTime() > (Date.now() - (value.sec * 1000));
|
2023-01-13 02:03:54 +00:00
|
|
|
}
|
|
|
|
case 'createdMoreThan': {
|
2023-10-16 01:45:22 +00:00
|
|
|
return this.idService.parse(user.id).date.getTime() < (Date.now() - (value.sec * 1000));
|
2023-01-13 02:03:54 +00:00
|
|
|
}
|
2023-01-13 23:27:23 +00:00
|
|
|
case 'followersLessThanOrEq': {
|
|
|
|
return user.followersCount <= value.value;
|
|
|
|
}
|
|
|
|
case 'followersMoreThanOrEq': {
|
|
|
|
return user.followersCount >= value.value;
|
|
|
|
}
|
|
|
|
case 'followingLessThanOrEq': {
|
|
|
|
return user.followingCount <= value.value;
|
|
|
|
}
|
|
|
|
case 'followingMoreThanOrEq': {
|
|
|
|
return user.followingCount >= value.value;
|
|
|
|
}
|
2023-03-23 08:18:38 +00:00
|
|
|
case 'notesLessThanOrEq': {
|
|
|
|
return user.notesCount <= value.value;
|
|
|
|
}
|
|
|
|
case 'notesMoreThanOrEq': {
|
|
|
|
return user.notesCount >= value.value;
|
|
|
|
}
|
2023-01-13 02:03:54 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// TODO: log error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-21 09:38:07 +00:00
|
|
|
@bindThis
|
|
|
|
public async getRoles() {
|
|
|
|
const roles = await this.rolesCache.fetch(() => this.rolesRepository.findBy({}));
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getUserAssigns(userId: MiUser['id']) {
|
2023-03-01 01:20:03 +00:00
|
|
|
const now = Date.now();
|
|
|
|
let assigns = await this.roleAssignmentByUserIdCache.fetch(userId, () => this.roleAssignmentsRepository.findBy({ userId }));
|
|
|
|
// 期限切れのロールを除外
|
|
|
|
assigns = assigns.filter(a => a.expiresAt == null || (a.expiresAt.getTime() > now));
|
2023-07-20 01:54:41 +00:00
|
|
|
return assigns;
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getUserRoles(userId: MiUser['id']) {
|
2023-04-04 08:32:09 +00:00
|
|
|
const roles = await this.rolesCache.fetch(() => this.rolesRepository.findBy({}));
|
2023-07-20 01:54:41 +00:00
|
|
|
const assigns = await this.getUserAssigns(userId);
|
|
|
|
const assignedRoles = roles.filter(r => assigns.map(x => x.roleId).includes(r.id));
|
2023-04-04 08:32:09 +00:00
|
|
|
const user = roles.some(r => r.target === 'conditional') ? await this.cacheService.findUserById(userId) : null;
|
2023-01-13 02:03:54 +00:00
|
|
|
const matchedCondRoles = roles.filter(r => r.target === 'conditional' && this.evalCond(user!, r.condFormula));
|
|
|
|
return [...assignedRoles, ...matchedCondRoles];
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 01:37:03 +00:00
|
|
|
/**
|
|
|
|
* 指定ユーザーのバッジロール一覧取得
|
|
|
|
*/
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getUserBadgeRoles(userId: MiUser['id']) {
|
2023-03-01 01:20:03 +00:00
|
|
|
const now = Date.now();
|
|
|
|
let assigns = await this.roleAssignmentByUserIdCache.fetch(userId, () => this.roleAssignmentsRepository.findBy({ userId }));
|
|
|
|
// 期限切れのロールを除外
|
|
|
|
assigns = assigns.filter(a => a.expiresAt == null || (a.expiresAt.getTime() > now));
|
2023-02-05 01:37:03 +00:00
|
|
|
const assignedRoleIds = assigns.map(x => x.roleId);
|
2023-04-04 08:32:09 +00:00
|
|
|
const roles = await this.rolesCache.fetch(() => this.rolesRepository.findBy({}));
|
2023-02-05 01:37:03 +00:00
|
|
|
const assignedBadgeRoles = roles.filter(r => r.asBadge && assignedRoleIds.includes(r.id));
|
2023-02-11 00:03:43 +00:00
|
|
|
const badgeCondRoles = roles.filter(r => r.asBadge && (r.target === 'conditional'));
|
|
|
|
if (badgeCondRoles.length > 0) {
|
2023-04-04 08:32:09 +00:00
|
|
|
const user = roles.some(r => r.target === 'conditional') ? await this.cacheService.findUserById(userId) : null;
|
2023-02-11 00:03:43 +00:00
|
|
|
const matchedBadgeCondRoles = badgeCondRoles.filter(r => this.evalCond(user!, r.condFormula));
|
|
|
|
return [...assignedBadgeRoles, ...matchedBadgeCondRoles];
|
|
|
|
} else {
|
|
|
|
return assignedBadgeRoles;
|
|
|
|
}
|
2023-02-05 01:37:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getUserPolicies(userId: MiUser['id'] | null): Promise<RolePolicies> {
|
2023-01-12 12:02:26 +00:00
|
|
|
const meta = await this.metaService.fetch();
|
2023-01-15 11:52:53 +00:00
|
|
|
const basePolicies = { ...DEFAULT_POLICIES, ...meta.policies };
|
2023-01-12 12:02:26 +00:00
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
if (userId == null) return basePolicies;
|
2023-01-12 12:02:26 +00:00
|
|
|
|
|
|
|
const roles = await this.getUserRoles(userId);
|
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
function calc<T extends keyof RolePolicies>(name: T, aggregate: (values: RolePolicies[T][]) => RolePolicies[T]) {
|
|
|
|
if (roles.length === 0) return basePolicies[name];
|
2023-01-15 10:10:39 +00:00
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
const policies = roles.map(role => role.policies[name] ?? { priority: 0, useDefault: true });
|
2023-01-15 10:10:39 +00:00
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
const p2 = policies.filter(policy => policy.priority === 2);
|
|
|
|
if (p2.length > 0) return aggregate(p2.map(policy => policy.useDefault ? basePolicies[name] : policy.value));
|
2023-01-15 10:10:39 +00:00
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
const p1 = policies.filter(policy => policy.priority === 1);
|
2023-01-15 18:16:44 +00:00
|
|
|
if (p1.length > 0) return aggregate(p1.map(policy => policy.useDefault ? basePolicies[name] : policy.value));
|
2023-01-15 10:10:39 +00:00
|
|
|
|
2023-01-15 11:52:53 +00:00
|
|
|
return aggregate(policies.map(policy => policy.useDefault ? basePolicies[name] : policy.value));
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-01-15 10:10:39 +00:00
|
|
|
gtlAvailable: calc('gtlAvailable', vs => vs.some(v => v === true)),
|
2023-12-04 01:10:51 +00:00
|
|
|
btlAvailable: calc('btlAvailable', vs => vs.some(v => v === true)),
|
2023-01-15 10:10:39 +00:00
|
|
|
ltlAvailable: calc('ltlAvailable', vs => vs.some(v => v === true)),
|
|
|
|
canPublicNote: calc('canPublicNote', vs => vs.some(v => v === true)),
|
|
|
|
canInvite: calc('canInvite', vs => vs.some(v => v === true)),
|
2023-07-15 00:57:58 +00:00
|
|
|
inviteLimit: calc('inviteLimit', vs => Math.max(...vs)),
|
|
|
|
inviteLimitCycle: calc('inviteLimitCycle', vs => Math.max(...vs)),
|
|
|
|
inviteExpirationTime: calc('inviteExpirationTime', vs => Math.max(...vs)),
|
2023-01-15 10:10:39 +00:00
|
|
|
canManageCustomEmojis: calc('canManageCustomEmojis', vs => vs.some(v => v === true)),
|
2023-10-30 06:33:15 +00:00
|
|
|
canManageAvatarDecorations: calc('canManageAvatarDecorations', vs => vs.some(v => v === true)),
|
2023-03-13 08:52:24 +00:00
|
|
|
canSearchNotes: calc('canSearchNotes', vs => vs.some(v => v === true)),
|
2023-09-29 22:54:11 +00:00
|
|
|
canUseTranslator: calc('canUseTranslator', vs => vs.some(v => v === true)),
|
2023-01-16 02:21:04 +00:00
|
|
|
canHideAds: calc('canHideAds', vs => vs.some(v => v === true)),
|
2023-01-15 10:10:39 +00:00
|
|
|
driveCapacityMb: calc('driveCapacityMb', vs => Math.max(...vs)),
|
2023-05-05 05:18:06 +00:00
|
|
|
alwaysMarkNsfw: calc('alwaysMarkNsfw', vs => vs.some(v => v === true)),
|
2023-01-15 10:10:39 +00:00
|
|
|
pinLimit: calc('pinLimit', vs => Math.max(...vs)),
|
|
|
|
antennaLimit: calc('antennaLimit', vs => Math.max(...vs)),
|
|
|
|
wordMuteLimit: calc('wordMuteLimit', vs => Math.max(...vs)),
|
|
|
|
webhookLimit: calc('webhookLimit', vs => Math.max(...vs)),
|
|
|
|
clipLimit: calc('clipLimit', vs => Math.max(...vs)),
|
|
|
|
noteEachClipsLimit: calc('noteEachClipsLimit', vs => Math.max(...vs)),
|
|
|
|
userListLimit: calc('userListLimit', vs => Math.max(...vs)),
|
|
|
|
userEachUserListsLimit: calc('userEachUserListsLimit', vs => Math.max(...vs)),
|
|
|
|
rateLimitFactor: calc('rateLimitFactor', vs => Math.max(...vs)),
|
2023-11-12 14:07:32 +00:00
|
|
|
canImportNotes: calc('canImportNotes', vs => vs.some(v => v === true)),
|
2023-12-13 07:56:19 +00:00
|
|
|
avatarDecorationLimit: calc('avatarDecorationLimit', vs => Math.max(...vs)),
|
2023-01-12 12:02:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async isModerator(user: { id: MiUser['id']; isRoot: MiUser['isRoot'] } | null): Promise<boolean> {
|
2023-01-12 12:02:26 +00:00
|
|
|
if (user == null) return false;
|
|
|
|
return user.isRoot || (await this.getUserRoles(user.id)).some(r => r.isModerator || r.isAdministrator);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async isAdministrator(user: { id: MiUser['id']; isRoot: MiUser['isRoot'] } | null): Promise<boolean> {
|
2023-01-12 12:02:26 +00:00
|
|
|
if (user == null) return false;
|
|
|
|
return user.isRoot || (await this.getUserRoles(user.id)).some(r => r.isAdministrator);
|
|
|
|
}
|
|
|
|
|
2023-05-19 08:12:22 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async isExplorable(role: { id: MiRole['id']} | null): Promise<boolean> {
|
2023-05-19 08:12:22 +00:00
|
|
|
if (role == null) return false;
|
|
|
|
const check = await this.rolesRepository.findOneBy({ id: role.id });
|
|
|
|
if (check == null) return false;
|
|
|
|
return check.isExplorable;
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getModeratorIds(includeAdmins = true): Promise<MiUser['id'][]> {
|
2023-04-04 08:32:09 +00:00
|
|
|
const roles = await this.rolesCache.fetch(() => this.rolesRepository.findBy({}));
|
2023-01-12 12:02:26 +00:00
|
|
|
const moderatorRoles = includeAdmins ? roles.filter(r => r.isModerator || r.isAdministrator) : roles.filter(r => r.isModerator);
|
|
|
|
const assigns = moderatorRoles.length > 0 ? await this.roleAssignmentsRepository.findBy({
|
|
|
|
roleId: In(moderatorRoles.map(r => r.id)),
|
|
|
|
}) : [];
|
|
|
|
// TODO: isRootなアカウントも含める
|
|
|
|
return assigns.map(a => a.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getModerators(includeAdmins = true): Promise<MiUser[]> {
|
2023-01-12 12:02:26 +00:00
|
|
|
const ids = await this.getModeratorIds(includeAdmins);
|
|
|
|
const users = ids.length > 0 ? await this.usersRepository.findBy({
|
|
|
|
id: In(ids),
|
|
|
|
}) : [];
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getAdministratorIds(): Promise<MiUser['id'][]> {
|
2023-04-04 08:32:09 +00:00
|
|
|
const roles = await this.rolesCache.fetch(() => this.rolesRepository.findBy({}));
|
2023-01-12 12:02:26 +00:00
|
|
|
const administratorRoles = roles.filter(r => r.isAdministrator);
|
|
|
|
const assigns = administratorRoles.length > 0 ? await this.roleAssignmentsRepository.findBy({
|
|
|
|
roleId: In(administratorRoles.map(r => r.id)),
|
|
|
|
}) : [];
|
|
|
|
// TODO: isRootなアカウントも含める
|
|
|
|
return assigns.map(a => a.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
public async getAdministrators(): Promise<MiUser[]> {
|
2023-01-12 12:02:26 +00:00
|
|
|
const ids = await this.getAdministratorIds();
|
|
|
|
const users = ids.length > 0 ? await this.usersRepository.findBy({
|
|
|
|
id: In(ids),
|
|
|
|
}) : [];
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
2023-03-01 01:20:03 +00:00
|
|
|
@bindThis
|
2023-09-23 09:28:16 +00:00
|
|
|
public async assign(userId: MiUser['id'], roleId: MiRole['id'], expiresAt: Date | null = null, moderator?: MiUser): Promise<void> {
|
2023-10-16 01:45:22 +00:00
|
|
|
const now = Date.now();
|
2023-03-01 01:20:03 +00:00
|
|
|
|
2023-09-23 09:28:16 +00:00
|
|
|
const role = await this.rolesRepository.findOneByOrFail({ id: roleId });
|
|
|
|
|
2023-03-01 01:20:03 +00:00
|
|
|
const existing = await this.roleAssignmentsRepository.findOneBy({
|
|
|
|
roleId: roleId,
|
|
|
|
userId: userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existing) {
|
2023-10-16 01:45:22 +00:00
|
|
|
if (existing.expiresAt && (existing.expiresAt.getTime() < now)) {
|
2023-03-01 01:20:03 +00:00
|
|
|
await this.roleAssignmentsRepository.delete({
|
|
|
|
roleId: roleId,
|
|
|
|
userId: userId,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new RoleService.AlreadyAssignedError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const created = await this.roleAssignmentsRepository.insert({
|
2023-10-16 01:45:22 +00:00
|
|
|
id: this.idService.gen(now),
|
2023-03-01 01:20:03 +00:00
|
|
|
expiresAt: expiresAt,
|
|
|
|
roleId: roleId,
|
|
|
|
userId: userId,
|
|
|
|
}).then(x => this.roleAssignmentsRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
this.rolesRepository.update(roleId, {
|
|
|
|
lastUsedAt: new Date(),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.globalEventService.publishInternalEvent('userRoleAssigned', created);
|
2023-09-23 09:28:16 +00:00
|
|
|
|
2023-12-21 01:39:11 +00:00
|
|
|
if (role.isPublic) {
|
|
|
|
this.notificationService.createNotification(userId, 'roleAssigned', {
|
|
|
|
roleId: roleId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-23 09:28:16 +00:00
|
|
|
if (moderator) {
|
2023-09-25 01:29:12 +00:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: userId });
|
2023-09-23 09:28:16 +00:00
|
|
|
this.moderationLogService.log(moderator, 'assignRole', {
|
|
|
|
roleId: roleId,
|
|
|
|
roleName: role.name,
|
|
|
|
userId: userId,
|
2023-09-25 01:29:12 +00:00
|
|
|
userUsername: user.username,
|
|
|
|
userHost: user.host,
|
2023-09-23 09:28:16 +00:00
|
|
|
expiresAt: expiresAt ? expiresAt.toISOString() : null,
|
|
|
|
});
|
|
|
|
}
|
2023-03-01 01:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-09-23 09:28:16 +00:00
|
|
|
public async unassign(userId: MiUser['id'], roleId: MiRole['id'], moderator?: MiUser): Promise<void> {
|
2023-03-01 01:20:03 +00:00
|
|
|
const now = new Date();
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2023-03-01 01:20:03 +00:00
|
|
|
const existing = await this.roleAssignmentsRepository.findOneBy({ roleId, userId });
|
|
|
|
if (existing == null) {
|
|
|
|
throw new RoleService.NotAssignedError();
|
|
|
|
} else if (existing.expiresAt && (existing.expiresAt.getTime() < now.getTime())) {
|
|
|
|
await this.roleAssignmentsRepository.delete({
|
|
|
|
roleId: roleId,
|
|
|
|
userId: userId,
|
|
|
|
});
|
|
|
|
throw new RoleService.NotAssignedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.roleAssignmentsRepository.delete(existing.id);
|
|
|
|
|
|
|
|
this.rolesRepository.update(roleId, {
|
|
|
|
lastUsedAt: now,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.globalEventService.publishInternalEvent('userRoleUnassigned', existing);
|
2023-09-23 09:28:16 +00:00
|
|
|
|
|
|
|
if (moderator) {
|
2023-09-25 01:29:12 +00:00
|
|
|
const [user, role] = await Promise.all([
|
|
|
|
this.usersRepository.findOneByOrFail({ id: userId }),
|
|
|
|
this.rolesRepository.findOneByOrFail({ id: roleId }),
|
|
|
|
]);
|
2023-09-23 09:28:16 +00:00
|
|
|
this.moderationLogService.log(moderator, 'unassignRole', {
|
|
|
|
roleId: roleId,
|
|
|
|
roleName: role.name,
|
|
|
|
userId: userId,
|
2023-09-25 01:29:12 +00:00
|
|
|
userUsername: user.username,
|
|
|
|
userHost: user.host,
|
2023-09-23 09:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
2023-03-01 01:20:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 02:40:08 +00:00
|
|
|
@bindThis
|
|
|
|
public async addNoteToRoleTimeline(note: Packed<'Note'>): Promise<void> {
|
|
|
|
const roles = await this.getUserRoles(note.userId);
|
|
|
|
|
2023-11-21 00:55:49 +00:00
|
|
|
const redisPipeline = this.redisForTimelines.pipeline();
|
2023-04-12 02:40:08 +00:00
|
|
|
|
|
|
|
for (const role of roles) {
|
2023-11-26 01:02:22 +00:00
|
|
|
this.fanoutTimelineService.push(`roleTimeline:${role.id}`, note.id, 1000, redisPipeline);
|
2023-04-12 02:40:08 +00:00
|
|
|
this.globalEventService.publishRoleTimelineStream(role.id, 'note', note);
|
|
|
|
}
|
|
|
|
|
|
|
|
redisPipeline.exec();
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:29:12 +00:00
|
|
|
@bindThis
|
|
|
|
public async create(values: Partial<MiRole>, moderator?: MiUser): Promise<MiRole> {
|
|
|
|
const date = new Date();
|
|
|
|
const created = await this.rolesRepository.insert({
|
2023-10-16 01:45:22 +00:00
|
|
|
id: this.idService.gen(date.getTime()),
|
2023-09-25 01:29:12 +00:00
|
|
|
updatedAt: date,
|
|
|
|
lastUsedAt: date,
|
|
|
|
name: values.name,
|
|
|
|
description: values.description,
|
|
|
|
color: values.color,
|
|
|
|
iconUrl: values.iconUrl,
|
|
|
|
target: values.target,
|
|
|
|
condFormula: values.condFormula,
|
|
|
|
isPublic: values.isPublic,
|
|
|
|
isAdministrator: values.isAdministrator,
|
|
|
|
isModerator: values.isModerator,
|
|
|
|
isExplorable: values.isExplorable,
|
|
|
|
asBadge: values.asBadge,
|
|
|
|
canEditMembersByModerator: values.canEditMembersByModerator,
|
|
|
|
displayOrder: values.displayOrder,
|
|
|
|
policies: values.policies,
|
|
|
|
}).then(x => this.rolesRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
this.globalEventService.publishInternalEvent('roleCreated', created);
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
this.moderationLogService.log(moderator, 'createRole', {
|
|
|
|
roleId: created.id,
|
|
|
|
role: created,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
2023-09-23 09:28:16 +00:00
|
|
|
@bindThis
|
|
|
|
public async update(role: MiRole, params: Partial<MiRole>, moderator?: MiUser): Promise<void> {
|
|
|
|
const date = new Date();
|
|
|
|
await this.rolesRepository.update(role.id, {
|
|
|
|
updatedAt: date,
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
|
|
|
|
const updated = await this.rolesRepository.findOneByOrFail({ id: role.id });
|
|
|
|
this.globalEventService.publishInternalEvent('roleUpdated', updated);
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
this.moderationLogService.log(moderator, 'updateRole', {
|
|
|
|
roleId: role.id,
|
|
|
|
before: role,
|
|
|
|
after: updated,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 01:33:30 +00:00
|
|
|
@bindThis
|
|
|
|
public async delete(role: MiRole, moderator?: MiUser): Promise<void> {
|
|
|
|
await this.rolesRepository.delete({ id: role.id });
|
|
|
|
this.globalEventService.publishInternalEvent('roleDeleted', role);
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
this.moderationLogService.log(moderator, 'deleteRole', {
|
|
|
|
roleId: role.id,
|
|
|
|
role: role,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:02:26 +00:00
|
|
|
@bindThis
|
2023-05-29 04:21:26 +00:00
|
|
|
public dispose(): void {
|
2023-04-09 08:09:27 +00:00
|
|
|
this.redisForSub.off('message', this.onMessage);
|
2023-06-10 04:45:11 +00:00
|
|
|
this.roleAssignmentByUserIdCache.dispose();
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|
2023-05-29 04:21:26 +00:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2023-01-12 12:02:26 +00:00
|
|
|
}
|