Disable URL Preview setting if disabled on the homeserver (#33279)

* Disallow URL Previews if disabled by homeserver.

* Cleanup nonsense

* cleanup

* cleanup

* fixes

* Remove import

* Add an error handler

* cleanup
This commit is contained in:
Will Hunt
2026-04-28 15:08:22 +00:00
committed by GitHub
parent 72d316df79
commit 2b943768ea
8 changed files with 181 additions and 9 deletions
+1
View File
@@ -476,6 +476,7 @@
"description": "Description",
"deselect_all": "Deselect all",
"device": "Device",
"disabled_by_homeserver": "Disabled by homeserver",
"edited": "edited",
"email_address": "Email address",
"emoji": "Emoji",
+7 -1
View File
@@ -1126,7 +1126,13 @@ export const SETTINGS: Settings = {
supportedLevelsAreOrdered: true,
displayName: _td("settings|inline_url_previews_default"),
default: true,
controller: new UIFeatureController(UIFeature.URLPreviews),
controller: new RequiresSettingsController([UIFeature.URLPreviews], false, (c) => {
if (c["io.element.msc4452.preview_url"]?.enabled !== false) {
// If the capability is not listed, or explicitly true then do not disable.
return false;
}
return _t("common|disabled_by_homeserver");
}),
},
"urlPreviewsEnabled_e2ee": {
// Can only be enabled per-device to ensure neither the homeserver nor client config
@@ -5,30 +5,75 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details.
*/
import SettingController from "./SettingController";
import { logger as rootLogger } from "matrix-js-sdk/src/logger";
import type { Capabilities } from "matrix-js-sdk/src/matrix";
import SettingsStore from "../SettingsStore";
import type { BooleanSettingKey } from "../Settings.tsx";
import MatrixClientBackedController from "./MatrixClientBackedController.ts";
const logger = rootLogger.getChild("RequiresSettingsController");
/**
* Disables a setting & forces it's value if one or more settings are not enabled
* and/or a capability on the client check does not pass.
*/
export default class RequiresSettingsController extends SettingController {
export default class RequiresSettingsController extends MatrixClientBackedController {
public constructor(
public readonly settingNames: BooleanSettingKey[],
private forcedValue = false,
private readonly forcedValue = false,
/**
* Function to check the capabilites of the client.
* If defined this will be called when the MatrixClient is instantiated to check
* the returned capabilites.
* @returns `true` or a string if the feature is disabled by the feature, otherwise false.
*/
private readonly isCapabilityDisabled?: (caps: Capabilities) => boolean | string,
) {
super();
}
protected initMatrixClient(): void {
if (this.client && this.isCapabilityDisabled) {
// Ensure we fetch capabilies at least once.
this.client.getCapabilities().catch((ex) => {
logger.warn("Failed to fetch capabilities", ex);
});
}
}
/**
* Checks if the `isCapabilityDisabled` function blocks the setting.
* @returns `true` or a string if the feature is disabled by the feature, otherwise false.
*/
private get isBlockedByCapabilites(): boolean | string {
if (!this.isCapabilityDisabled) {
return false;
}
// This works because the cached caps are stored forever, and we have made
// at least one call to get capaibilies.
const cachedCaps = this.client?.getCachedCapabilities();
if (!cachedCaps) {
// If we do not have any capabilites yet, then assume the setting IS blocked.
return true;
}
return this.isCapabilityDisabled(cachedCaps);
}
public get settingDisabled(): boolean | string {
if (this.settingNames.some((s) => !SettingsStore.getValue(s))) {
return true;
}
return this.isBlockedByCapabilites;
}
public getValueOverride(): any {
if (this.settingDisabled) {
// per the docs: we force a disabled state when the feature isn't active
return this.forcedValue;
}
if (this.isBlockedByCapabilites) {
return this.forcedValue;
}
return null; // no override
}
public get settingDisabled(): boolean {
return this.settingNames.some((s) => !SettingsStore.getValue(s));
}
}