From 0f81f6a1aaba27e5d9a76c360f2511c0e94a9c08 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 15 Nov 2017 22:09:40 -0700 Subject: [PATCH] Treat the master push rule as authoritative Previously the push rule was ignored, leading to all kinds of interesting issues regarding notifications. This fixes those issues by giving the master push rule the authority it deserves for reasonable defaults. Part 2 of the fix for: * /~https://github.com/vector-im/riot-web/issues/5603 * /~https://github.com/vector-im/riot-web/issues/5606 Signed-off-by: Travis Ralston --- .../controllers/NotificationControllers.js | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/settings/controllers/NotificationControllers.js b/src/settings/controllers/NotificationControllers.js index 261e194d7437..d29f1cf4458b 100644 --- a/src/settings/controllers/NotificationControllers.js +++ b/src/settings/controllers/NotificationControllers.js @@ -15,12 +15,36 @@ 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. + 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 || true) { + console.log(isMasterRuleEnabled()); + return isMasterRuleEnabled(); + } - return calculatedValue && Notifier.isPossible(); + return calculatedValue; } onChange(level, roomId, newValue) { @@ -35,15 +59,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; } }