Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fix various issues surrounding granular settings to date #1613

Merged
merged 10 commits into from
Nov 16, 2017
2 changes: 1 addition & 1 deletion src/components/structures/MatrixChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ module.exports = React.createClass({
*/
_onSetTheme: function(theme) {
if (!theme) {
theme = this.props.config.default_theme || 'light';
theme = SettingsStore.getValueAt(SettingLevel.DEFAULT, "theme");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/ :D

}

// look for the stylesheet elements.
Expand Down
3 changes: 1 addition & 2 deletions src/components/structures/UserSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,7 @@ module.exports = React.createClass({

onLanguageChange: function(newLang) {
if(this.state.language !== newLang) {
// We intentionally promote this to the account level at this point
SettingsStore.setValue("language", null, SettingLevel.ACCOUNT, newLang);
SettingsStore.setValue("language", null, SettingLevel.DEVICE, newLang);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the inevitable question: I have no idea why this got promoted to an account level setting. It's always been a local setting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, i love the idea of changing language on one device magically synchronising over all your accounts :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'love' in a "aaaaaargh no" kinda way

this.setState({
language: newLang,
});
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/room_settings/UrlPreviewSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.

const React = require('react');
const sdk = require("../../../index");
import { _t, _tJsx } from '../../../languageHandler';
import {_t, _td, _tJsx} from '../../../languageHandler';
import SettingsStore, {SettingLevel} from "../../../settings/SettingsStore";


Expand Down Expand Up @@ -63,9 +63,9 @@ module.exports = React.createClass({
</label>
);
} else {
let str = "URL previews are enabled by default for participants in this room.";
let str = _td("URL previews are enabled by default for participants in this room.");
if (!SettingsStore.getValueAt(SettingLevel.ROOM, "urlPreviewsEnabled")) {
str = "URL previews are disabled by default for participants in this room.";
str = _td("URL previews are disabled by default for participants in this room.");
}
previewsForRoom = (<label>{ _t(str) }</label>);
}
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
"Enable Notifications": "Enable Notifications",
"You have <a>enabled</a> URL previews by default.": "You have <a>enabled</a> URL previews by default.",
"You have <a>disabled</a> URL previews by default.": "You have <a>disabled</a> URL previews by default.",
"URL previews are enabled by default for participants in this room.": "URL previews are enabled by default for participants in this room.",
"URL previews are disabled by default for participants in this room.": "URL previews are disabled by default for participants in this room.",
"URL Previews": "URL Previews",
"Cannot add any more widgets": "Cannot add any more widgets",
"The maximum permitted number of widgets have already been added to this room.": "The maximum permitted number of widgets have already been added to this room.",
Expand Down
14 changes: 11 additions & 3 deletions src/settings/SettingsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,21 @@ export default class SettingsStore {
* @return {*} The value, or null if not found
*/
static getValue(settingName, roomId = null, excludeDefault = false) {
return SettingsStore.getValueAt(LEVEL_ORDER[0], settingName, roomId, false, excludeDefault);
// Verify that the setting is actually a setting
if (!SETTINGS[settingName]) {
throw new Error("Setting '" + settingName + "' does not appear to be a setting.");
}

const setting = SETTINGS[settingName];
const levelOrder = (setting.supportedLevelsAreOrdered ? setting.supportedLevels : LEVEL_ORDER);

return SettingsStore.getValueAt(levelOrder[0], settingName, roomId, false, excludeDefault);
}

/**
* Gets a setting's value at a particular level, ignoring all levels that are more specific.
* @param {"device"|"room-device"|"room-account"|"account"|"room"} level The level to
* look at.
* @param {"device"|"room-device"|"room-account"|"account"|"room"|"config"|"default"} level The
* level to look at.
* @param {string} settingName The name of the setting to read.
* @param {String} roomId The room ID to read the setting value in, may be null.
* @param {boolean} explicit If true, this method will not consider other levels, just the one
Expand Down
36 changes: 33 additions & 3 deletions src/settings/controllers/NotificationControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,35 @@ limitations under the License.
*/

import SettingController from "./SettingController";
import MatrixClientPeg from '../../MatrixClientPeg';

// XXX: This feels wrong.
import PushProcessor from "matrix-js-sdk/lib/pushprocessor";

function isMasterRuleEnabled() {
// Return the value of the master push rule as a default
const processor = new PushProcessor(MatrixClientPeg.get());
const masterRule = processor.getPushRuleById(".m.rule.master");

if (!masterRule) {
console.warn("No master push rule! Notifications are disabled for this user.");
return false;
}

// Why enabled == false means "enabled" is beyond me.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the masterRule acts to disable push, apparently.

return !masterRule.enabled;
}

export class NotificationsEnabledController extends SettingController {
getValueOverride(level, roomId, calculatedValue) {
const Notifier = require('../../Notifier'); // avoids cyclical references
if (!Notifier.isPossible()) return false;

if (calculatedValue === null) {
return isMasterRuleEnabled();
}

return calculatedValue && Notifier.isPossible();
return calculatedValue;
}

onChange(level, roomId, newValue) {
Expand All @@ -35,15 +58,22 @@ export class NotificationsEnabledController extends SettingController {
export class NotificationBodyEnabledController extends SettingController {
getValueOverride(level, roomId, calculatedValue) {
const Notifier = require('../../Notifier'); // avoids cyclical references
if (!Notifier.isPossible()) return false;

if (calculatedValue === null) {
return isMasterRuleEnabled();
}

return calculatedValue && Notifier.isEnabled();
return calculatedValue;
}
}

export class AudioNotificationsEnabledController extends SettingController {
getValueOverride(level, roomId, calculatedValue) {
const Notifier = require('../../Notifier'); // avoids cyclical references
if (!Notifier.isPossible()) return false;

return calculatedValue && Notifier.isEnabled();
// Note: Audio notifications are *not* enabled by default.
return calculatedValue;
}
}
3 changes: 3 additions & 0 deletions src/settings/handlers/AccountSettingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class AccountSettingHandler extends SettingsHandler {
// Special case URL previews
if (settingName === "urlPreviewsEnabled") {
const content = this._getSettings("org.matrix.preview_urls");

// Check to make sure that we actually got a boolean
if (typeof(content['disable']) !== "boolean") return null;
return !content['disable'];
}

Expand Down
12 changes: 9 additions & 3 deletions src/settings/handlers/DeviceSettingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ export default class DeviceSettingsHandler extends SettingsHandler {

// Special case notifications
if (settingName === "notificationsEnabled") {
return localStorage.getItem("notifications_enabled") === "true";
const value = localStorage.getItem("notifications_enabled");
if (typeof(value) === "string") return value === "true";
return null; // wrong type or otherwise not set
} else if (settingName === "notificationBodyEnabled") {
return localStorage.getItem("notifications_body_enabled") === "true";
const value = localStorage.getItem("notifications_body_enabled");
if (typeof(value) === "string") return value === "true";
return null; // wrong type or otherwise not set
} else if (settingName === "audioNotificationsEnabled") {
return localStorage.getItem("audio_notifications_enabled") === "true";
const value = localStorage.getItem("audio_notifications_enabled");
if (typeof(value) === "string") return value === "true";
return null; // wrong type or otherwise not set
}

return this._getSettings()[settingName];
Expand Down
3 changes: 3 additions & 0 deletions src/settings/handlers/RoomAccountSettingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export default class RoomAccountSettingsHandler extends SettingsHandler {
// Special case URL previews
if (settingName === "urlPreviewsEnabled") {
const content = this._getSettings(roomId, "org.matrix.room.preview_urls");

// Check to make sure that we actually got a boolean
if (typeof(content['disable']) !== "boolean") return null;
return !content['disable'];
}

Expand Down
3 changes: 3 additions & 0 deletions src/settings/handlers/RoomSettingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export default class RoomSettingsHandler extends SettingsHandler {
// Special case URL previews
if (settingName === "urlPreviewsEnabled") {
const content = this._getSettings(roomId, "org.matrix.room.preview_urls");

// Check to make sure that we actually got a boolean
if (typeof(content['disable']) !== "boolean") return null;
return !content['disable'];
}

Expand Down