2017-10-31 11:09:24 +08:00
|
|
|
# Settings Reference
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
This document serves as developer documentation for using "Granular Settings". Granular Settings allow users to specify
|
|
|
|
different values for a setting at particular levels of interest. For example, a user may say that in a particular room
|
|
|
|
they want URL previews off, but in all other rooms they want them enabled. The `SettingsStore` helps mask the complexity
|
|
|
|
of dealing with the different levels and exposes easy to use getters and setters.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
## Levels
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Granular Settings rely on a series of known levels in order to use the correct value for the scenario. These levels, in
|
2020-08-18 03:32:33 +08:00
|
|
|
order of priority, are:
|
2017-10-31 11:09:24 +08:00
|
|
|
* `device` - The current user's device
|
|
|
|
* `room-device` - The current user's device, but only when in a specific room
|
|
|
|
* `room-account` - The current user's account, but only when in a specific room
|
|
|
|
* `account` - The current user's account
|
|
|
|
* `room` - A specific room (setting for all members of the room)
|
2020-08-20 01:52:02 +08:00
|
|
|
* `config` - Values are defined by the `settingDefaults` key (usually) in `config.json`
|
2017-10-31 11:09:24 +08:00
|
|
|
* `default` - The hardcoded default for the settings
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Individual settings may control which levels are appropriate for them as part of the defaults. This is often to ensure
|
|
|
|
that room administrators cannot force account-only settings upon participants.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
## Settings
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Settings are the different options a user may set or experience in the application. These are pre-defined in
|
2022-01-17 23:04:37 +08:00
|
|
|
`src/settings/Settings.tsx` under the `SETTINGS` constant, and match the `ISetting` interface as defined there.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Settings that support the config level can be set in the config file under the `settingDefaults` key (note that some
|
|
|
|
settings, like the "theme" setting, are special cased in the config file):
|
2020-04-01 17:37:17 +08:00
|
|
|
```json
|
2019-05-31 21:00:26 +08:00
|
|
|
{
|
|
|
|
...
|
2020-04-01 17:37:17 +08:00
|
|
|
"settingDefaults": {
|
|
|
|
"settingName": true
|
2019-05-31 21:00:26 +08:00
|
|
|
},
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
### Getting values for a setting
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
After importing `SettingsStore`, simply make a call to `SettingsStore.getValue`. The `roomId` parameter should always
|
|
|
|
be supplied where possible, even if the setting does not have a per-room level value. This is to ensure that the value
|
|
|
|
returned is best represented in the room, particularly if the setting ever gets a per-room level in the future.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
In settings pages it is often desired to have the value at a particular level instead of getting the calculated value.
|
|
|
|
Call `SettingsStore.getValueAt` to get the value of a setting at a particular level, and optionally make it explicitly
|
|
|
|
at that level. By default `getValueAt` will traverse the tree starting at the provided level; making it explicit means
|
|
|
|
it will not go beyond the provided level. When using `getValueAt`, please be sure to use `SettingLevel` to represent the
|
|
|
|
target level.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
### Setting values for a setting
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Values are defined at particular levels and should be done in a safe manner. There are two checks to perform to ensure a
|
|
|
|
clean save: is the level supported and can the user actually set the value. In most cases, neither should be an issue
|
|
|
|
although there are circumstances where this changes. An example of a safe call is:
|
2017-11-06 05:33:40 +08:00
|
|
|
```javascript
|
|
|
|
const isSupported = SettingsStore.isLevelSupported(SettingLevel.ROOM);
|
|
|
|
if (isSupported) {
|
|
|
|
const canSetValue = SettingsStore.canSetValue("mySetting", "!curbf:matrix.org", SettingLevel.ROOM);
|
|
|
|
if (canSetValue) {
|
|
|
|
SettingsStore.setValue("mySetting", "!curbf:matrix.org", SettingLevel.ROOM, newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
These checks may also be performed in different areas of the application to avoid the verbose example above. For
|
|
|
|
instance, the component which allows changing the setting may be hidden conditionally on the above conditions.
|
2017-11-06 05:33:40 +08:00
|
|
|
|
|
|
|
##### `SettingsFlag` component
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Where possible, the `SettingsFlag` component should be used to set simple "flip-a-bit" (true/false) settings. The
|
|
|
|
`SettingsFlag` also supports simple radio button options, such as the theme the user would like to use.
|
2017-11-06 05:33:40 +08:00
|
|
|
```html
|
|
|
|
<SettingsFlag name="theSettingId"
|
|
|
|
level={SettingsLevel.ROOM}
|
|
|
|
roomId="!curbf:matrix.org"
|
|
|
|
label={_td("Your label here")} // optional, if falsey then the `SettingsStore` will be used
|
|
|
|
onChange={function(newValue) { }} // optional, called after saving
|
|
|
|
isExplicit={false} // this is passed along to `SettingsStore.getValueAt`, defaulting to false
|
|
|
|
manualSave={false} // if true, saving is delayed. You will need to call .save() on this component
|
|
|
|
|
|
|
|
// Options for radio buttons
|
|
|
|
group="your-radio-group" // this enables radio button support
|
|
|
|
value="yourValueHere" // the value for this particular option
|
|
|
|
/>
|
|
|
|
```
|
|
|
|
|
|
|
|
### Getting the display name for a setting
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Simply call `SettingsStore.getDisplayName`. The appropriate display name will be returned and automatically translated
|
|
|
|
for you. If a display name cannot be found, it will return `null`.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Feature flags are just like regular settings with some underlying semantics for how they are meant to be used. Usually
|
|
|
|
a feature flag is used when a portion of the application is under development or not ready for full release yet, such
|
|
|
|
as new functionality or experimental ideas. In these cases, the feature name *should* be named with the `feature_*`
|
|
|
|
convention and must be tagged with `isFeature: true` in the setting definition. By doing so, the feature will automatically
|
|
|
|
appear in the "labs" section of the user's settings.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Features can be controlled at the config level using the following structure:
|
2018-10-12 00:22:28 +08:00
|
|
|
```json
|
|
|
|
"features": {
|
2020-08-18 03:32:33 +08:00
|
|
|
"feature_lazyloading": true
|
2018-10-12 00:22:28 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
When `true`, the user will see the feature as enabled. Similarly, when `false` the user will see the feature as disabled.
|
|
|
|
The user will only be able to change/see these states if `showLabsSettings: true` is in the config.
|
2018-10-12 00:22:28 +08:00
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
### Determining if a feature is enabled
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Call `SettingsStore.getValue()` as you would for any other setting.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
### Enabling a feature
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Call `SettingsStore.setValue("feature_name", null, SettingLevel.DEVICE, true)`.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-09-15 12:27:40 +08:00
|
|
|
### A note on UI features
|
|
|
|
|
|
|
|
UI features are a different concept to plain features. Instead of being representative of unstable or
|
|
|
|
unpredicatable behaviour, they are logical chunks of UI which can be disabled by deployments for ease
|
|
|
|
of understanding with users. They are simply represented as boring settings with a convention of being
|
|
|
|
named as `UIFeature.$location` where `$location` is a rough descriptor of what is toggled, such as
|
|
|
|
`URLPreviews` or `Communities`.
|
|
|
|
|
|
|
|
UI features also tend to have their own setting controller (see below) to manipulate settings which might
|
|
|
|
be affected by the UI feature being disabled. For example, if URL previews are disabled as a UI feature
|
|
|
|
then the URL preview options will use the `UIFeatureController` to ensure they remain disabled while the
|
|
|
|
UI feature is disabled.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
## Setting controllers
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Settings may have environmental factors that affect their value or need additional code to be called when they are
|
|
|
|
modified. A setting controller is able to override the calculated value for a setting and react to changes in that
|
|
|
|
setting. Controllers are not a replacement for the level handlers and should only be used to ensure the environment is
|
|
|
|
kept up to date with the setting where it is otherwise not possible. An example of this is the notification settings:
|
|
|
|
they can only be considered enabled if the platform supports notifications, and enabling notifications requires
|
|
|
|
additional steps to actually enable notifications.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
For more information, see `src/settings/controllers/SettingController.ts`.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
## Local echo
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
`SettingsStore` will perform local echo on all settings to ensure that immediately getting values does not cause a
|
|
|
|
split-brain scenario. As mentioned in the "Setting values for a setting" section, the appropriate checks should be done
|
|
|
|
to ensure that the user is allowed to set the value. The local echo system assumes that the user has permission and that
|
|
|
|
the request will go through successfully. The local echo only takes effect until the request to save a setting has
|
|
|
|
completed (either successfully or otherwise).
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
```javascript
|
|
|
|
SettingsStore.setValue(...).then(() => {
|
|
|
|
// The value has actually been stored at this point.
|
|
|
|
});
|
|
|
|
SettingsStore.getValue(...); // this will return the value set in `setValue` above.
|
|
|
|
```
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
|
2019-02-23 07:33:20 +08:00
|
|
|
## Watching for changes
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Most use cases do not need to set up a watcher because they are able to react to changes as they are made, or the
|
|
|
|
changes which are made are not significant enough for it to matter. Watchers are intended to be used in scenarios where
|
|
|
|
it is important to react to changes made by other logged in devices. Typically, this would be done within the component
|
|
|
|
itself, however the component should not be aware of the intricacies of setting inversion or remapping to particular
|
|
|
|
data structures. Instead, a generic watcher interface is provided on `SettingsStore` to watch (and subsequently unwatch)
|
|
|
|
for changes in a setting.
|
2019-02-23 07:33:20 +08:00
|
|
|
|
|
|
|
An example of a watcher in action would be:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
class MyComponent extends React.Component {
|
|
|
|
|
|
|
|
settingWatcherRef = null;
|
|
|
|
|
|
|
|
componentWillMount() {
|
2019-02-27 03:47:20 +08:00
|
|
|
const callback = (settingName, roomId, level, newValAtLevel, newVal) => {
|
|
|
|
this.setState({color: newVal});
|
|
|
|
};
|
|
|
|
this.settingWatcherRef = SettingsStore.watchSetting("roomColor", "!example:matrix.org", callback);
|
2019-02-23 07:33:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
SettingsStore.unwatchSetting(this.settingWatcherRef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
# Maintainers Reference
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
The granular settings system has a few complex parts to power it. This section is to document how the `SettingsStore` is
|
|
|
|
supposed to work.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2017-11-06 05:33:40 +08:00
|
|
|
### General information
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
The `SettingsStore` uses the hardcoded `LEVEL_ORDER` constant to ensure that it is using the correct override procedure.
|
|
|
|
The array is checked from left to right, simulating the behaviour of overriding values from the higher levels. Each
|
|
|
|
level should be defined in this array, including `default`.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Handlers (`src/settings/handlers/SettingsHandler.ts`) represent a single level and are responsible for getting and
|
2019-02-27 03:47:20 +08:00
|
|
|
setting values at that level. Handlers also provide additional information to the `SettingsStore` such as if the level
|
|
|
|
is supported or if the current user may set values at the level. The `SettingsStore` will use the handler to enforce
|
|
|
|
checks and manipulate settings. Handlers are also responsible for dealing with migration patterns or legacy settings for
|
|
|
|
their level (for example, a setting being renamed or using a different key from other settings in the underlying store).
|
|
|
|
Handlers are provided to the `SettingsStore` via the `LEVEL_HANDLERS` constant. `SettingsStore` will optimize lookups by
|
|
|
|
only considering handlers that are supported on the platform.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
Local echo is achieved through `src/settings/handlers/LocalEchoWrapper.ts` which acts as a wrapper around a given
|
2019-02-27 03:47:20 +08:00
|
|
|
handler. This is automatically applied to all defined `LEVEL_HANDLERS` and proxies the calls to the wrapped handler
|
|
|
|
where possible. The echo is achieved by a simple object cache stored within the class itself. The cache is invalidated
|
|
|
|
immediately upon the proxied save call succeeding or failing.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Controllers are notified of changes by the `SettingsStore`, and are given the opportunity to override values after the
|
|
|
|
`SettingsStore` has deemed the value calculated. Controllers are invoked as the last possible step in the code.
|
2017-10-31 11:09:24 +08:00
|
|
|
|
|
|
|
### Features
|
|
|
|
|
2020-08-18 03:32:33 +08:00
|
|
|
See above for feature reference.
|
2019-02-23 07:33:20 +08:00
|
|
|
|
|
|
|
### Watchers
|
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Watchers can appear complicated under the hood: there is a central `WatchManager` which handles the actual invocation
|
|
|
|
of callbacks, and callbacks are managed by the SettingsStore by redirecting the caller's callback to a dedicated
|
|
|
|
callback. This is done so that the caller can reuse the same function as their callback without worrying about whether
|
|
|
|
or not it'll unsubscribe all watchers.
|
2019-02-27 03:43:10 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
Setting changes are emitted into the default `WatchManager`, which calculates the new value for the setting. Ideally,
|
|
|
|
we'd also try and suppress updates which don't have a consequence on this value, however there's not an easy way to do
|
|
|
|
this. Instead, we just dispatch an update for all changes and leave it up to the consumer to deduplicate.
|
2019-02-23 07:33:20 +08:00
|
|
|
|
2019-02-27 03:47:20 +08:00
|
|
|
In practice, handlers which rely on remote changes (account data, room events, etc) will always attach a listener to the
|
|
|
|
`MatrixClient`. They then watch for changes to events they care about and send off appropriate updates to the
|
|
|
|
generalized `WatchManager` - a class specifically designed to deduplicate the logic of managing watchers. The handlers
|
|
|
|
which are localized to the local client (device) generally just trigger the `WatchManager` when they manipulate the
|
|
|
|
setting themselves as there's nothing to really 'watch'.
|