From 56a8ff4f50f9a7c66218c8590da056c149e1187e Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Fri, 9 May 2025 21:03:05 -0400 Subject: [PATCH] add isFromInstance role condition --- locales/index.d.ts | 14 +++++++++++++- packages/backend/src/core/RoleService.ts | 13 +++++++++++++ packages/backend/src/models/Role.ts | 10 ++++++++++ .../src/pages/admin/RolesEditorFormula.vue | 15 +++++++++++++++ sharkey-locales/en-US.yml | 3 +++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/locales/index.d.ts b/locales/index.d.ts index 2c9e3e3890..9679cc3acf 100644 --- a/locales/index.d.ts +++ b/locales/index.d.ts @@ -7677,6 +7677,18 @@ export interface Locale extends ILocale { * ~ではない */ "not": string; + /** + * Is from a specific instance + */ + "isFromInstance": string; + /** + * Hostname (case-insensitive) + */ + "isFromInstanceHost": string; + /** + * Match subdomains + */ + "isFromInstanceSubdomains": string; }; }; "_sensitiveMediaDetection": { @@ -12949,7 +12961,7 @@ export interface Locale extends ILocale { "enableProxyAccountDescription": string; "_confirmPollEdit": { /** - * Are you sure you want to edit this poll? + * Are you sure you want to edit this poll */ "title": string; /** diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts index 229781c079..d948325503 100644 --- a/packages/backend/src/core/RoleService.ts +++ b/packages/backend/src/core/RoleService.ts @@ -248,6 +248,19 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit { case 'isRemote': { return this.userEntityService.isRemoteUser(user); } + // User is from a specific instance + case 'isFromInstance': { + if (user.host == null) { + return false; + } + if (value.subdomains) { + const userHost = '.' + user.host.toLowerCase(); + const targetHost = '.' + value.host.toLowerCase(); + return userHost.endsWith(targetHost); + } else { + return user.host.toLowerCase() === value.host.toLowerCase(); + } + } // サスペンド済みユーザである case 'isSuspended': { return user.isSuspended; diff --git a/packages/backend/src/models/Role.ts b/packages/backend/src/models/Role.ts index 4c7da252bd..d7ae8ed38c 100644 --- a/packages/backend/src/models/Role.ts +++ b/packages/backend/src/models/Role.ts @@ -47,6 +47,15 @@ type CondFormulaValueIsRemote = { type: 'isRemote'; }; +/** + * User is from a specific instance + */ +type CondFormulaValueIsFromInstance = { + type: 'isFromInstance'; + host: string; + subdomains: boolean; +}; + /** * 既に指定のマニュアルロールにアサインされている場合のみ成立とする */ @@ -160,6 +169,7 @@ export type RoleCondFormulaValue = { id: string } & ( CondFormulaValueNot | CondFormulaValueIsLocal | CondFormulaValueIsRemote | + CondFormulaValueIsFromInstance | CondFormulaValueIsSuspended | CondFormulaValueIsLocked | CondFormulaValueIsBot | diff --git a/packages/frontend/src/pages/admin/RolesEditorFormula.vue b/packages/frontend/src/pages/admin/RolesEditorFormula.vue index 6c47e6397f..53a4836caa 100644 --- a/packages/frontend/src/pages/admin/RolesEditorFormula.vue +++ b/packages/frontend/src/pages/admin/RolesEditorFormula.vue @@ -9,6 +9,7 @@ SPDX-License-Identifier: AGPL-3.0-only + @@ -61,6 +62,14 @@ SPDX-License-Identifier: AGPL-3.0-only + + + + + + + + @@ -73,6 +82,7 @@ import MkButton from '@/components/MkButton.vue'; import { i18n } from '@/i18n.js'; import { deepClone } from '@/utility/clone.js'; import { rolesCache } from '@/cache.js'; +import MkSwitch from '@/components/MkSwitch.vue'; const Sortable = defineAsyncComponent(() => import('vuedraggable').then(x => x.default)); @@ -102,6 +112,7 @@ watch(v, () => { const type = computed({ get: () => v.value.type, set: (t) => { + // TODO there's a bug here: switching types leaves extra properties in the JSON if (t === 'and') v.value.values = []; if (t === 'or') v.value.values = []; if (t === 'not') v.value.value = { id: uuid(), type: 'isRemote' }; @@ -114,6 +125,10 @@ const type = computed({ if (t === 'followingMoreThanOrEq') v.value.value = 10; if (t === 'notesLessThanOrEq') v.value.value = 10; if (t === 'notesMoreThanOrEq') v.value.value = 10; + if (t === 'isFromInstance') { + v.value.host = ''; + v.value.subdomains = true; + } v.value.type = t; }, }); diff --git a/sharkey-locales/en-US.yml b/sharkey-locales/en-US.yml index a5c3d5f892..034d995b01 100644 --- a/sharkey-locales/en-US.yml +++ b/sharkey-locales/en-US.yml @@ -242,6 +242,9 @@ _role: _condition: isLocked: "Private account" isExplorable: "Account is discoverable" + isFromInstance: "Is from a specific instance" + isFromInstanceHost: "Hostname (case-insensitive)" + isFromInstanceSubdomains: "Match subdomains" _emailUnavailable: banned: "This email address is banned" _signup: