Skip to content

Commit

Permalink
enhance: 新しいコンディショナルロール条件の実装 (misskey-dev#13732)
Browse files Browse the repository at this point in the history
* enhance: 新しいコンディショナルロールの実装

* fix: CHANGELOG.md
  • Loading branch information
samunohito authored Apr 19, 2024
1 parent ea9aa6f commit cd7f727
Show file tree
Hide file tree
Showing 12 changed files with 617 additions and 67 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
- Enhance: アンテナでBotによるノートを除外できるように
(Cherry-picked from https://github.com/MisskeyIO/misskey/pull/545)
- Enhance: クリップのノート数を表示するように
- Enhance: コンディショナルロールの条件として以下を新たに追加 (#13667)
- 猫ユーザーか
- botユーザーか
- サスペンド済みユーザーか
- 鍵アカウントユーザーか
- 「アカウントを見つけやすくする」が有効なユーザーか
- Fix: Play作成時に設定した公開範囲が機能していない問題を修正

### Client
Expand Down
20 changes: 20 additions & 0 deletions locales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6592,6 +6592,26 @@ export interface Locale extends ILocale {
* リモートユーザー
*/
"isRemote": string;
/**
* 猫ユーザー
*/
"isCat": string;
/**
* botユーザー
*/
"isBot": string;
/**
* サスペンド済みユーザー
*/
"isSuspended": string;
/**
* 鍵アカウントユーザー
*/
"isLocked": string;
/**
* 「アカウントを見つけやすくする」が有効なユーザー
*/
"isExplorable": string;
/**
* アカウント作成から~以内
*/
Expand Down
5 changes: 5 additions & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,11 @@ _role:
roleAssignedTo: "マニュアルロールにアサイン済み"
isLocal: "ローカルユーザー"
isRemote: "リモートユーザー"
isCat: "猫ユーザー"
isBot: "botユーザー"
isSuspended: "サスペンド済みユーザー"
isLocked: "鍵アカウントユーザー"
isExplorable: "「アカウントを見つけやすくする」が有効なユーザー"
createdLessThan: "アカウント作成から~以内"
createdMoreThan: "アカウント作成から~経過"
followersLessThanOrEq: "フォロワー数が~以下"
Expand Down
34 changes: 34 additions & 0 deletions packages/backend/src/core/RoleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,45 +205,79 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
private evalCond(user: MiUser, roles: MiRole[], value: RoleCondFormulaValue): boolean {
try {
switch (value.type) {
// ~かつ~
case 'and': {
return value.values.every(v => this.evalCond(user, roles, v));
}
// ~または~
case 'or': {
return value.values.some(v => this.evalCond(user, roles, v));
}
// ~ではない
case 'not': {
return !this.evalCond(user, roles, value.value);
}
// マニュアルロールがアサインされている
case 'roleAssignedTo': {
return roles.some(r => r.id === value.roleId);
}
// ローカルユーザのみ
case 'isLocal': {
return this.userEntityService.isLocalUser(user);
}
// リモートユーザのみ
case 'isRemote': {
return this.userEntityService.isRemoteUser(user);
}
// サスペンド済みユーザである
case 'isSuspended': {
return user.isSuspended;
}
// 鍵アカウントユーザである
case 'isLocked': {
return user.isLocked;
}
// botユーザである
case 'isBot': {
return user.isBot;
}
// 猫である
case 'isCat': {
return user.isCat;
}
// 「ユーザを見つけやすくする」が有効なアカウント
case 'isExplorable': {
return user.isExplorable;
}
// ユーザが作成されてから指定期間経過した
case 'createdLessThan': {
return this.idService.parse(user.id).date.getTime() > (Date.now() - (value.sec * 1000));
}
// ユーザが作成されてから指定期間経っていない
case 'createdMoreThan': {
return this.idService.parse(user.id).date.getTime() < (Date.now() - (value.sec * 1000));
}
// フォロワー数が指定値以下
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;
}
// ノート数が指定値以下
case 'notesLessThanOrEq': {
return user.notesCount <= value.value;
}
// ノート数が指定値以上
case 'notesMoreThanOrEq': {
return user.notesCount >= value.value;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/misc/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
packedRoleCondFormulaValueCreatedSchema,
packedRoleCondFormulaFollowersOrFollowingOrNotesSchema,
packedRoleCondFormulaValueSchema,
packedRoleCondFormulaValueUserSettingBooleanSchema,
} from '@/models/json-schema/role.js';
import { packedAdSchema } from '@/models/json-schema/ad.js';
import { packedReversiGameLiteSchema, packedReversiGameDetailedSchema } from '@/models/json-schema/reversi-game.js';
Expand Down Expand Up @@ -97,6 +98,7 @@ export const refs = {
RoleCondFormulaLogics: packedRoleCondFormulaLogicsSchema,
RoleCondFormulaValueNot: packedRoleCondFormulaValueNot,
RoleCondFormulaValueIsLocalOrRemote: packedRoleCondFormulaValueIsLocalOrRemoteSchema,
RoleCondFormulaValueUserSettingBooleanSchema: packedRoleCondFormulaValueUserSettingBooleanSchema,
RoleCondFormulaValueAssignedRole: packedRoleCondFormulaValueAssignedRoleSchema,
RoleCondFormulaValueCreated: packedRoleCondFormulaValueCreatedSchema,
RoleCondFormulaFollowersOrFollowingOrNotes: packedRoleCondFormulaFollowersOrFollowingOrNotesSchema,
Expand Down
85 changes: 85 additions & 0 deletions packages/backend/src/models/Role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,149 @@
import { Entity, Column, PrimaryColumn } from 'typeorm';
import { id } from './util/id.js';

/**
* ~かつ~
* 複数の条件を同時に満たす場合のみ成立とする
*/
type CondFormulaValueAnd = {
type: 'and';
values: RoleCondFormulaValue[];
};

/**
* ~または~
* 複数の条件のうち、いずれかを満たす場合のみ成立とする
*/
type CondFormulaValueOr = {
type: 'or';
values: RoleCondFormulaValue[];
};

/**
* ~ではない
* 条件を満たさない場合のみ成立とする
*/
type CondFormulaValueNot = {
type: 'not';
value: RoleCondFormulaValue;
};

/**
* ローカルユーザーのみ成立とする
*/
type CondFormulaValueIsLocal = {
type: 'isLocal';
};

/**
* リモートユーザーのみ成立とする
*/
type CondFormulaValueIsRemote = {
type: 'isRemote';
};

/**
* 既に指定のマニュアルロールにアサインされている場合のみ成立とする
*/
type CondFormulaValueRoleAssignedTo = {
type: 'roleAssignedTo';
roleId: string;
};

/**
* サスペンド済みアカウントの場合のみ成立とする
*/
type CondFormulaValueIsSuspended = {
type: 'isSuspended';
};

/**
* 鍵アカウントの場合のみ成立とする
*/
type CondFormulaValueIsLocked = {
type: 'isLocked';
};

/**
* botアカウントの場合のみ成立とする
*/
type CondFormulaValueIsBot = {
type: 'isBot';
};

/**
* 猫アカウントの場合のみ成立とする
*/
type CondFormulaValueIsCat = {
type: 'isCat';
};

/**
* 「ユーザを見つけやすくする」が有効なアカウントの場合のみ成立とする
*/
type CondFormulaValueIsExplorable = {
type: 'isExplorable';
};

/**
* ユーザが作成されてから指定期間経過した場合のみ成立とする
*/
type CondFormulaValueCreatedLessThan = {
type: 'createdLessThan';
sec: number;
};

/**
* ユーザが作成されてから指定期間経っていない場合のみ成立とする
*/
type CondFormulaValueCreatedMoreThan = {
type: 'createdMoreThan';
sec: number;
};

/**
* フォロワー数が指定値以下の場合のみ成立とする
*/
type CondFormulaValueFollowersLessThanOrEq = {
type: 'followersLessThanOrEq';
value: number;
};

/**
* フォロワー数が指定値以上の場合のみ成立とする
*/
type CondFormulaValueFollowersMoreThanOrEq = {
type: 'followersMoreThanOrEq';
value: number;
};

/**
* フォロー数が指定値以下の場合のみ成立とする
*/
type CondFormulaValueFollowingLessThanOrEq = {
type: 'followingLessThanOrEq';
value: number;
};

/**
* フォロー数が指定値以上の場合のみ成立とする
*/
type CondFormulaValueFollowingMoreThanOrEq = {
type: 'followingMoreThanOrEq';
value: number;
};

/**
* 投稿数が指定値以下の場合のみ成立とする
*/
type CondFormulaValueNotesLessThanOrEq = {
type: 'notesLessThanOrEq';
value: number;
};

/**
* 投稿数が指定値以上の場合のみ成立とする
*/
type CondFormulaValueNotesMoreThanOrEq = {
type: 'notesMoreThanOrEq';
value: number;
Expand All @@ -80,6 +160,11 @@ export type RoleCondFormulaValue = { id: string } & (
CondFormulaValueNot |
CondFormulaValueIsLocal |
CondFormulaValueIsRemote |
CondFormulaValueIsSuspended |
CondFormulaValueIsLocked |
CondFormulaValueIsBot |
CondFormulaValueIsCat |
CondFormulaValueIsExplorable |
CondFormulaValueRoleAssignedTo |
CondFormulaValueCreatedLessThan |
CondFormulaValueCreatedMoreThan |
Expand Down
17 changes: 17 additions & 0 deletions packages/backend/src/models/json-schema/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ export const packedRoleCondFormulaValueIsLocalOrRemoteSchema = {
},
} as const;

export const packedRoleCondFormulaValueUserSettingBooleanSchema = {
type: 'object',
properties: {
id: {
type: 'string', optional: false,
},
type: {
type: 'string',
nullable: false, optional: false,
enum: ['isSuspended', 'isLocked', 'isBot', 'isCat', 'isExplorable'],
},
},
} as const;

export const packedRoleCondFormulaValueAssignedRoleSchema = {
type: 'object',
properties: {
Expand Down Expand Up @@ -135,6 +149,9 @@ export const packedRoleCondFormulaValueSchema = {
{
ref: 'RoleCondFormulaValueIsLocalOrRemote',
},
{
ref: 'RoleCondFormulaValueUserSettingBooleanSchema',
},
{
ref: 'RoleCondFormulaValueAssignedRole',
},
Expand Down
Loading

0 comments on commit cd7f727

Please sign in to comment.