-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathRequiresSettingsController.ts
More file actions
70 lines (63 loc) · 2.47 KB
/
RequiresSettingsController.ts
File metadata and controls
70 lines (63 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Copyright 2026 Element Creations Ltd.
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 type { Capabilities } from "matrix-js-sdk/src/matrix";
import SettingsStore from "../SettingsStore";
import type { BooleanSettingKey } from "../Settings.tsx";
import MatrixClientBackedController from "./MatrixClientBackedController.ts";
/**
* 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 MatrixClientBackedController {
public constructor(
public readonly settingNames: BooleanSettingKey[],
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.
void this.client.getCapabilities();
}
}
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) {
// return forced value if caps aren't available yet.
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
}
}