Add new incompatible controllers.

This commit is contained in:
Half-Shot 2025-11-06 17:23:47 +00:00
parent eade32a80c
commit acd9d6d867
3 changed files with 114 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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 {