Merge remote-tracking branch 'upstream/develop' into reorganize-preferences

This commit is contained in:
Šimon Brandner 2021-07-01 18:53:53 +02:00
commit e355bf0db1
No known key found for this signature in database
GPG key ID: 9760693FDD98A790
838 changed files with 21747 additions and 13629 deletions

View file

@ -1,6 +1,6 @@
/*
Copyright 2017 Travis Ralston
Copyright 2018, 2019, 2020 The Matrix.org Foundation C.I.C.
Copyright 2018 - 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -94,6 +94,9 @@ export interface ISetting {
[level: SettingLevel]: string;
};
// Optional description which will be shown as microCopy under SettingsFlags
description?: string;
// The supported levels are required. Preferably, use the preset arrays
// at the top of this file to define this rather than a custom array.
supportedLevels?: SettingLevel[];
@ -127,10 +130,18 @@ export interface ISetting {
image: string; // require(...)
feedbackSubheading?: string;
feedbackLabel?: string;
extraSettings?: string[];
};
}
export const SETTINGS: {[setting: string]: ISetting} = {
"feature_report_to_moderators": {
isFeature: true,
displayName: _td("Report to moderators prototype. " +
"In rooms that support moderation, the `report` button will let you report abuse to room moderators"),
supportedLevels: LEVELS_FEATURE,
default: false,
},
"feature_spaces": {
isFeature: true,
displayName: _td("Spaces prototype. Incompatible with Communities, Communities v2 and Custom Tags. " +
@ -167,8 +178,33 @@ export const SETTINGS: {[setting: string]: ISetting} = {
feedbackSubheading: _td("Your feedback will help make spaces better. " +
"The more detail you can go into, the better."),
feedbackLabel: "spaces-feedback",
extraSettings: [
"feature_spaces.all_rooms",
"feature_spaces.space_member_dms",
"feature_spaces.space_dm_badges",
],
},
},
"feature_spaces.all_rooms": {
displayName: _td("Show all rooms in Home"),
supportedLevels: LEVELS_FEATURE,
default: true,
controller: new ReloadOnChangeController(),
},
"feature_spaces.space_member_dms": {
displayName: _td("Show people in spaces"),
description: _td("If disabled, you can still add Direct Messages to Personal Spaces. " +
"If enabled, you'll automatically see everyone who is a member of the Space."),
supportedLevels: LEVELS_FEATURE,
default: true,
controller: new ReloadOnChangeController(),
},
"feature_spaces.space_dm_badges": {
displayName: _td("Show notification badges for People in Spaces"),
supportedLevels: LEVELS_FEATURE,
default: false,
controller: new ReloadOnChangeController(),
},
"feature_dnd": {
isFeature: true,
displayName: _td("Show options to enable 'Do not disturb' mode"),
@ -570,14 +606,6 @@ export const SETTINGS: {[setting: string]: ISetting} = {
default: false,
controller: new UIFeatureController(UIFeature.URLPreviews),
},
"roomColor": {
supportedLevels: LEVELS_ROOM_SETTINGS_WITH_ROOM,
displayName: _td("Room Colour"),
default: {
primary_color: null, // Hex string, eg: #000000
secondary_color: null, // Hex string, eg: #000000
},
},
"notificationsEnabled": {
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS,
default: false,
@ -601,10 +629,6 @@ export const SETTINGS: {[setting: string]: ISetting} = {
displayName: _td('Enable widget screenshots on supported widgets'),
default: false,
},
"PinnedEvents.isOpen": {
supportedLevels: [SettingLevel.ROOM_DEVICE],
default: false,
},
"promptBeforeInviteUnknownUsers": {
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
displayName: _td('Prompt before sending invites to potentially invalid matrix IDs'),

View file

@ -26,7 +26,7 @@ import { _t } from '../languageHandler';
import dis from '../dispatcher/dispatcher';
import { ISetting, SETTINGS } from "./Settings";
import LocalEchoWrapper from "./handlers/LocalEchoWrapper";
import { WatchManager } from "./WatchManager";
import { WatchManager, CallbackFn as WatchCallbackFn } from "./WatchManager";
import { SettingLevel } from "./SettingLevel";
import SettingsHandler from "./handlers/SettingsHandler";
@ -117,8 +117,8 @@ export default class SettingsStore {
// We also maintain a list of monitors which are special watchers: they cause dispatches
// when the setting changes. We track which rooms we're monitoring though to ensure we
// don't duplicate updates on the bus.
private static watchers = {}; // { callbackRef => { callbackFn } }
private static monitors = {}; // { settingName => { roomId => callbackRef } }
private static watchers = new Map<string, WatchCallbackFn>();
private static monitors = new Map<string, Map<string, string>>(); // { settingName => { roomId => callbackRef } }
// Counter used for generation of watcher IDs
private static watcherCount = 1;
@ -163,7 +163,7 @@ export default class SettingsStore {
callbackFn(originalSettingName, changedInRoomId, atLevel, newValAtLevel, newValue);
};
SettingsStore.watchers[watcherId] = localizedCallback;
SettingsStore.watchers.set(watcherId, localizedCallback);
defaultWatchManager.watchSetting(settingName, roomId, localizedCallback);
return watcherId;
@ -176,13 +176,13 @@ export default class SettingsStore {
* to cancel.
*/
public static unwatchSetting(watcherReference: string) {
if (!SettingsStore.watchers[watcherReference]) {
if (!SettingsStore.watchers.has(watcherReference)) {
console.warn(`Ending non-existent watcher ID ${watcherReference}`);
return;
}
defaultWatchManager.unwatchSetting(SettingsStore.watchers[watcherReference]);
delete SettingsStore.watchers[watcherReference];
defaultWatchManager.unwatchSetting(SettingsStore.watchers.get(watcherReference));
SettingsStore.watchers.delete(watcherReference);
}
/**
@ -196,10 +196,10 @@ export default class SettingsStore {
public static monitorSetting(settingName: string, roomId: string) {
roomId = roomId || null; // the thing wants null specifically to work, so appease it.
if (!this.monitors[settingName]) this.monitors[settingName] = {};
if (!this.monitors.has(settingName)) this.monitors.set(settingName, new Map());
const registerWatcher = () => {
this.monitors[settingName][roomId] = SettingsStore.watchSetting(
this.monitors.get(settingName).set(roomId, SettingsStore.watchSetting(
settingName, roomId, (settingName, inRoomId, level, newValueAtLevel, newValue) => {
dis.dispatch({
action: 'setting_updated',
@ -210,19 +210,20 @@ export default class SettingsStore {
newValue,
});
},
);
));
};
const hasRoom = Object.keys(this.monitors[settingName]).find((r) => r === roomId || r === null);
const rooms = Array.from(this.monitors.get(settingName).keys());
const hasRoom = rooms.find((r) => r === roomId || r === null);
if (!hasRoom) {
registerWatcher();
} else {
if (roomId === null) {
// Unregister all existing watchers and register the new one
for (const roomId of Object.keys(this.monitors[settingName])) {
SettingsStore.unwatchSetting(this.monitors[settingName][roomId]);
}
this.monitors[settingName] = {};
rooms.forEach(roomId => {
SettingsStore.unwatchSetting(this.monitors.get(settingName).get(roomId));
});
this.monitors.get(settingName).clear();
registerWatcher();
} // else a watcher is already registered for the room, so don't bother registering it again
}
@ -247,6 +248,16 @@ export default class SettingsStore {
return _t(displayName as string);
}
/**
* Gets the translated description for a given setting
* @param {string} settingName The setting to look up.
* @return {String} The description for the setting, or null if not found.
*/
public static getDescription(settingName: string) {
if (!SETTINGS[settingName]?.description) return null;
return _t(SETTINGS[settingName].description);
}
/**
* Determines if a setting is also a feature.
* @param {string} settingName The setting to look up.

View file

@ -18,11 +18,7 @@ import { SettingLevel } from "./SettingLevel";
export type CallbackFn = (changedInRoomId: string, atLevel: SettingLevel, newValAtLevel: any) => void;
const IRRELEVANT_ROOM: string = null;
interface RoomWatcherMap {
[roomId: string]: CallbackFn[];
}
const IRRELEVANT_ROOM = Symbol("irrelevant-room");
/**
* Generalized management class for dealing with watchers on a per-handler (per-level)
@ -30,25 +26,25 @@ interface RoomWatcherMap {
* class, which are then proxied outwards to any applicable watchers.
*/
export class WatchManager {
private watchers: {[settingName: string]: RoomWatcherMap} = {};
private watchers = new Map<string, Map<string | symbol, CallbackFn[]>>(); // settingName -> roomId -> CallbackFn[]
// Proxy for handlers to delegate changes to this manager
public watchSetting(settingName: string, roomId: string | null, cb: CallbackFn) {
if (!this.watchers[settingName]) this.watchers[settingName] = {};
if (!this.watchers[settingName][roomId]) this.watchers[settingName][roomId] = [];
this.watchers[settingName][roomId].push(cb);
if (!this.watchers.has(settingName)) this.watchers.set(settingName, new Map());
if (!this.watchers.get(settingName).has(roomId)) this.watchers.get(settingName).set(roomId, []);
this.watchers.get(settingName).get(roomId).push(cb);
}
// Proxy for handlers to delegate changes to this manager
public unwatchSetting(cb: CallbackFn) {
for (const settingName of Object.keys(this.watchers)) {
for (const roomId of Object.keys(this.watchers[settingName])) {
this.watchers.forEach((map) => {
map.forEach((callbacks) => {
let idx;
while ((idx = this.watchers[settingName][roomId].indexOf(cb)) !== -1) {
this.watchers[settingName][roomId].splice(idx, 1);
while ((idx = callbacks.indexOf(cb)) !== -1) {
callbacks.splice(idx, 1);
}
}
}
});
});
}
public notifyUpdate(settingName: string, inRoomId: string | null, atLevel: SettingLevel, newValueAtLevel: any) {
@ -56,21 +52,20 @@ export class WatchManager {
// we also don't have a reliable way to get the old value of a setting. Instead, we'll just
// let it fall through regardless and let the receiver dedupe if they want to.
if (!this.watchers[settingName]) return;
if (!this.watchers.has(settingName)) return;
const roomWatchers = this.watchers[settingName];
const roomWatchers = this.watchers.get(settingName);
const callbacks = [];
if (inRoomId !== null && roomWatchers[inRoomId]) {
callbacks.push(...roomWatchers[inRoomId]);
if (inRoomId !== null && roomWatchers.has(inRoomId)) {
callbacks.push(...roomWatchers.get(inRoomId));
}
if (!inRoomId) {
// Fire updates to all the individual room watchers too, as they probably
// care about the change higher up.
callbacks.push(...Object.values(roomWatchers).flat(1));
} else if (roomWatchers[IRRELEVANT_ROOM]) {
callbacks.push(...roomWatchers[IRRELEVANT_ROOM]);
// Fire updates to all the individual room watchers too, as they probably care about the change higher up.
callbacks.push(...Array.from(roomWatchers.values()).flat(1));
} else if (roomWatchers.has(IRRELEVANT_ROOM)) {
callbacks.push(...roomWatchers.get(IRRELEVANT_ROOM));
}
for (const callback of callbacks) {

View file

@ -16,11 +16,11 @@ limitations under the License.
*/
import SettingController from "./SettingController";
import {MatrixClientPeg} from '../../MatrixClientPeg';
import { MatrixClientPeg } from '../../MatrixClientPeg';
import { SettingLevel } from "../SettingLevel";
// XXX: This feels wrong.
import {PushProcessor} from "matrix-js-sdk/src/pushprocessor";
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor";
// .m.rule.master being enabled means all events match that push rule
// default action on this rule is dont_notify, but it could be something else

View file

@ -16,7 +16,7 @@ limitations under the License.
*/
import SettingController from "./SettingController";
import {DEFAULT_THEME, enumerateThemes} from "../../theme";
import { DEFAULT_THEME, enumerateThemes } from "../../theme";
import { SettingLevel } from "../SettingLevel";
export default class ThemeController extends SettingController {

View file

@ -15,10 +15,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {MatrixClientPeg} from '../../MatrixClientPeg';
import { MatrixClientPeg } from '../../MatrixClientPeg';
import MatrixClientBackedSettingsHandler from "./MatrixClientBackedSettingsHandler";
import {objectClone, objectKeyChanges} from "../../utils/objects";
import {SettingLevel} from "../SettingLevel";
import { objectClone, objectKeyChanges } from "../../utils/objects";
import { SettingLevel } from "../SettingLevel";
import { WatchManager } from "../WatchManager";
import { MatrixClient } from "matrix-js-sdk/src/client";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
@ -73,7 +73,7 @@ export default class AccountSettingsHandler extends MatrixClientBackedSettingsHa
const val = event.getContent()['enabled'];
this.watchers.notifyUpdate("recent_emoji", null, SettingLevel.ACCOUNT, val);
}
}
};
public getValue(settingName: string, roomId: string): any {
// Special case URL previews

View file

@ -17,7 +17,7 @@ limitations under the License.
import SettingsHandler from "./SettingsHandler";
import SdkConfig from "../../SdkConfig";
import {isNullOrUndefined} from "matrix-js-sdk/src/utils";
import { isNullOrUndefined } from "matrix-js-sdk/src/utils";
/**
* Gets and sets settings at the "config" level. This handler does not make use of the

View file

@ -17,8 +17,8 @@ limitations under the License.
*/
import SettingsHandler from "./SettingsHandler";
import {MatrixClientPeg} from "../../MatrixClientPeg";
import {SettingLevel} from "../SettingLevel";
import { MatrixClientPeg } from "../../MatrixClientPeg";
import { SettingLevel } from "../SettingLevel";
import { CallbackFn, WatchManager } from "../WatchManager";
import { Layout } from "../Layout";
@ -109,7 +109,7 @@ export default class DeviceSettingsHandler extends SettingsHandler {
"lastRightPanelPhaseForRoom",
"lastRightPanelPhaseForGroup",
].includes(settingName)) {
localStorage.setItem(`mx_${settingName}`, JSON.stringify({value: newValue}));
localStorage.setItem(`mx_${settingName}`, JSON.stringify({ value: newValue }));
this.watchers.notifyUpdate(settingName, null, SettingLevel.DEVICE, newValue);
return Promise.resolve();
}

View file

@ -54,8 +54,6 @@ export default class RoomAccountSettingsHandler extends MatrixClientBackedSettin
}
this.watchers.notifyUpdate("urlPreviewsEnabled", roomId, SettingLevel.ROOM_ACCOUNT, val);
} else if (event.getType() === "org.matrix.room.color_scheme") {
this.watchers.notifyUpdate("roomColor", roomId, SettingLevel.ROOM_ACCOUNT, event.getContent());
} else if (event.getType() === "im.vector.web.settings") {
// Figure out what changed and fire those updates
const prevContent = prevEvent ? prevEvent.getContent() : {};
@ -79,14 +77,6 @@ export default class RoomAccountSettingsHandler extends MatrixClientBackedSettin
return !content['disable'];
}
// Special case room color
if (settingName === "roomColor") {
// The event content should already be in an appropriate format, we just need
// to get the right value.
// don't fallback to {} because thats truthy and would imply there is an event specifying tint
return this.getSettings(roomId, "org.matrix.room.color_scheme");
}
// Special case allowed widgets
if (settingName === "allowedWidgets") {
return this.getSettings(roomId, ALLOWED_WIDGETS_EVENT_TYPE);
@ -104,12 +94,6 @@ export default class RoomAccountSettingsHandler extends MatrixClientBackedSettin
return MatrixClientPeg.get().setRoomAccountData(roomId, "org.matrix.room.preview_urls", content);
}
// Special case room color
if (settingName === "roomColor") {
// The new value should match our requirements, we just need to store it in the right place.
return MatrixClientPeg.get().setRoomAccountData(roomId, "org.matrix.room.color_scheme", newValue);
}
// Special case allowed widgets
if (settingName === "allowedWidgets") {
return MatrixClientPeg.get().setRoomAccountData(roomId, ALLOWED_WIDGETS_EVENT_TYPE, newValue);

View file

@ -57,7 +57,7 @@ export default class RoomDeviceSettingsHandler extends SettingsHandler {
if (newValue === null) {
localStorage.removeItem(this.getKey(settingName, roomId));
} else {
newValue = JSON.stringify({value: newValue});
newValue = JSON.stringify({ value: newValue });
localStorage.setItem(this.getKey(settingName, roomId), newValue);
}

View file

@ -63,7 +63,7 @@ export class FontWatcher implements IWatcher {
(<HTMLElement>document.querySelector(":root")).style.fontSize = toPx(fontSize);
};
private setSystemFont = ({useSystemFont, font}) => {
private setSystemFont = ({ useSystemFont, font }) => {
document.body.style.fontFamily = useSystemFont ? font : "";
};
}