From acd9d6d86778eec805a38ce9ca6352dcb05af746 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 6 Nov 2025 17:23:47 +0000 Subject: [PATCH] Add new incompatible controllers. --- .../CombinationIncompatbleController.ts | 54 ++++++++++++++++++ .../IncompatibleConfigController.ts | 57 +++++++++++++++++++ .../controllers/IncompatibleController.ts | 5 +- 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/settings/controllers/CombinationIncompatbleController.ts create mode 100644 src/settings/controllers/IncompatibleConfigController.ts diff --git a/src/settings/controllers/CombinationIncompatbleController.ts b/src/settings/controllers/CombinationIncompatbleController.ts new file mode 100644 index 0000000000..c2432eb01d --- /dev/null +++ b/src/settings/controllers/CombinationIncompatbleController.ts @@ -0,0 +1,54 @@ +/* +Copyright 2024 New Vector Ltd. +Copyright 2021 The Matrix.org Foundation C.I.C. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import SettingController from "./SettingController.ts"; +import { type SettingLevel } from "../SettingLevel.ts"; +import IncompatibleController from "./IncompatibleController.ts"; +import IncompatibleConfigController from "./IncompatibleConfigController.ts"; + +/** + * Enforces that a boolean setting cannot be enabled if the incompatible setting + * is also enabled, to prevent cascading undefined behaviour between conflicting + * labs flags. + */ +export default class CombinationIncompatbleController extends SettingController { + public constructor( + private readonly controllers: (IncompatibleConfigController|IncompatibleController)[] + ) { + super(); + } + + public getValueOverride( + level: SettingLevel, + roomId: string, + calculatedValue: any, + calculatedAtLevel: SettingLevel | null, + ): any { + for (const controller of this.controllers) { + const res = controller.getValueOverride(level, roomId, calculatedValue, calculatedAtLevel); + if (res !== null) { + return res; + } + } + return null; + } + + public get settingDisabled(): boolean|string { + for (const controller of this.controllers) { + const res = controller.settingDisabled; + if (res) { + return res; + } + } + return false; + } + + public get incompatibleSetting(): boolean { + return this.controllers.some(s => s.incompatibleSetting); + } +} diff --git a/src/settings/controllers/IncompatibleConfigController.ts b/src/settings/controllers/IncompatibleConfigController.ts new file mode 100644 index 0000000000..fb05595997 --- /dev/null +++ b/src/settings/controllers/IncompatibleConfigController.ts @@ -0,0 +1,57 @@ +/* +Copyright 2024 New Vector Ltd. +Copyright 2021 The Matrix.org Foundation C.I.C. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import SettingController from "./SettingController.ts"; +import { type SettingLevel } from "../SettingLevel.ts"; +import { IConfigOptions } from "../../IConfigOptions.ts"; +import SdkConfig from "../../SdkConfig.ts"; + +/** + * Enforces that a boolean setting cannot be enabled if the incompatible setting + * is also enabled, to prevent cascading undefined behaviour between conflicting + * labs flags. + */ +export default class IncompatibleConfigController extends SettingController { + public constructor( + private readonly getSetting: (c: IConfigOptions) => boolean, + private forcedValue: any = false, + private incompatibleValue: any | ((v: any) => boolean) = true, + private readonly disabledString?: string, + ) { + super(); + console.log(SdkConfig.get()); + } + + public getValueOverride( + level: SettingLevel, + roomId: string, + calculatedValue: any, + calculatedAtLevel: SettingLevel | null, + ): any { + if (this.incompatibleSetting) { + return this.forcedValue; + } + return null; // no override + } + + public get configSettingValue(): boolean { + return this.getSetting(SdkConfig.get()); + } + + public get settingDisabled(): boolean|string { + console.log("IncompatibleConfigController", this.configSettingValue, this.incompatibleSetting ? (this.disabledString ?? true) : false); + return this.incompatibleSetting ? (this.disabledString ?? true) : false; + } + + public get incompatibleSetting(): boolean { + if (typeof this.incompatibleValue === "function") { + return this.incompatibleValue(this.configSettingValue); + } + return this.configSettingValue === this.incompatibleValue; + } +} diff --git a/src/settings/controllers/IncompatibleController.ts b/src/settings/controllers/IncompatibleController.ts index 81c8ca74ac..503a2968c1 100644 --- a/src/settings/controllers/IncompatibleController.ts +++ b/src/settings/controllers/IncompatibleController.ts @@ -21,6 +21,7 @@ export default class IncompatibleController extends SettingController { private settingName: BooleanSettingKey, private forcedValue: any = false, private incompatibleValue: any | ((v: any) => boolean) = true, + private readonly disabledString?: string, ) { super(); } @@ -37,8 +38,8 @@ export default class IncompatibleController extends SettingController { return null; // no override } - public get settingDisabled(): boolean { - return this.incompatibleSetting; + public get settingDisabled(): boolean|string { + return this.incompatibleSetting ? (this.disabledString ?? true) : false; } public get incompatibleSetting(): boolean {