2017-11-05 07:47:18 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Travis Ralston
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-11-05 08:28:35 +03:00
|
|
|
import SettingController from "./SettingController";
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../MatrixClientPeg';
|
2017-11-16 08:09:40 +03:00
|
|
|
|
|
|
|
// XXX: This feels wrong.
|
2019-12-20 05:08:43 +03:00
|
|
|
import {PushProcessor} from "matrix-js-sdk/src/pushprocessor";
|
2017-11-16 08:09:40 +03:00
|
|
|
|
2020-06-17 18:31:42 +03:00
|
|
|
// .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
|
|
|
|
function isPushNotifyDisabled() {
|
2017-11-16 08:09:40 +03:00
|
|
|
// 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.");
|
2020-06-17 18:31:42 +03:00
|
|
|
return true;
|
2017-11-16 08:09:40 +03:00
|
|
|
}
|
|
|
|
|
2020-06-17 18:31:42 +03:00
|
|
|
// If the rule is enabled then check it does not notify on everything
|
|
|
|
return masterRule.enabled && !masterRule.actions.includes("notify");
|
2017-11-16 08:09:40 +03:00
|
|
|
}
|
2017-11-05 08:28:35 +03:00
|
|
|
|
2019-12-21 00:47:49 +03:00
|
|
|
function getNotifier() {
|
|
|
|
let Notifier = require('../../Notifier'); // avoids cyclical references
|
|
|
|
if (Notifier.default) Notifier = Notifier.default; // correct for webpack require() weirdness
|
|
|
|
return Notifier;
|
|
|
|
}
|
|
|
|
|
2017-11-05 08:28:35 +03:00
|
|
|
export class NotificationsEnabledController extends SettingController {
|
2017-12-25 23:29:30 +03:00
|
|
|
getValueOverride(level, roomId, calculatedValue, calculatedAtLevel) {
|
2019-12-21 00:47:49 +03:00
|
|
|
if (!getNotifier().isPossible()) return false;
|
2017-11-16 08:09:40 +03:00
|
|
|
|
2017-12-25 23:29:30 +03:00
|
|
|
if (calculatedValue === null || calculatedAtLevel === "default") {
|
2020-06-17 18:31:42 +03:00
|
|
|
return !isPushNotifyDisabled();
|
2017-11-16 08:09:40 +03:00
|
|
|
}
|
2017-11-05 08:28:35 +03:00
|
|
|
|
2017-11-16 08:09:40 +03:00
|
|
|
return calculatedValue;
|
2017-11-05 08:28:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onChange(level, roomId, newValue) {
|
2019-12-21 00:47:49 +03:00
|
|
|
if (getNotifier().supportsDesktopNotifications()) {
|
|
|
|
getNotifier().setEnabled(newValue);
|
2017-11-05 08:28:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NotificationBodyEnabledController extends SettingController {
|
|
|
|
getValueOverride(level, roomId, calculatedValue) {
|
2019-12-21 00:47:49 +03:00
|
|
|
if (!getNotifier().isPossible()) return false;
|
2017-11-16 08:09:40 +03:00
|
|
|
|
|
|
|
if (calculatedValue === null) {
|
2020-06-17 18:31:42 +03:00
|
|
|
return !isPushNotifyDisabled();
|
2017-11-16 08:09:40 +03:00
|
|
|
}
|
2017-11-05 08:28:35 +03:00
|
|
|
|
2017-11-16 08:09:40 +03:00
|
|
|
return calculatedValue;
|
2017-11-05 08:28:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AudioNotificationsEnabledController extends SettingController {
|
|
|
|
getValueOverride(level, roomId, calculatedValue) {
|
2019-12-21 00:47:49 +03:00
|
|
|
if (!getNotifier().isPossible()) return false;
|
2017-11-05 08:28:35 +03:00
|
|
|
|
2017-11-16 08:09:40 +03:00
|
|
|
// Note: Audio notifications are *not* enabled by default.
|
|
|
|
return calculatedValue;
|
2017-11-05 08:28:35 +03:00
|
|
|
}
|
|
|
|
}
|