2016-08-17 17:26:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
|
|
|
import PushProcessor from 'matrix-js-sdk/lib/pushprocessor';
|
2017-07-12 12:58:14 +00:00
|
|
|
import Promise from 'bluebird';
|
2016-08-17 17:26:37 +00:00
|
|
|
|
2016-08-18 12:44:58 +00:00
|
|
|
export const ALL_MESSAGES_LOUD = 'all_messages_loud';
|
|
|
|
export const ALL_MESSAGES = 'all_messages';
|
|
|
|
export const MENTIONS_ONLY = 'mentions_only';
|
|
|
|
export const MUTE = 'mute';
|
|
|
|
|
2016-08-17 17:26:37 +00:00
|
|
|
export function getRoomNotifsState(roomId) {
|
2016-09-01 15:58:18 +00:00
|
|
|
if (MatrixClientPeg.get().isGuest()) return ALL_MESSAGES;
|
2016-08-18 13:00:14 +00:00
|
|
|
|
2016-08-17 17:26:37 +00:00
|
|
|
// look through the override rules for a rule affecting this room:
|
|
|
|
// if one exists, it will take precedence.
|
|
|
|
const muteRule = findOverrideMuteRule(roomId);
|
2016-08-18 13:00:14 +00:00
|
|
|
if (muteRule) {
|
2016-08-18 12:44:58 +00:00
|
|
|
return MUTE;
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for everything else, look at the room rule.
|
|
|
|
const roomRule = MatrixClientPeg.get().getRoomPushRule('global', roomId);
|
|
|
|
|
|
|
|
// XXX: We have to assume the default is to notify for all messages
|
|
|
|
// (in particular this will be 'wrong' for one to one rooms because
|
|
|
|
// they will notify loudly for all messages)
|
2016-08-18 12:44:58 +00:00
|
|
|
if (!roomRule || !roomRule.enabled) return ALL_MESSAGES;
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
// a mute at the room level will still allow mentions
|
|
|
|
// to notify
|
2016-08-18 12:44:58 +00:00
|
|
|
if (isMuteRule(roomRule)) return MENTIONS_ONLY;
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions);
|
2016-08-18 12:44:58 +00:00
|
|
|
if (actionsObject.tweaks.sound) return ALL_MESSAGES_LOUD;
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setRoomNotifsState(roomId, newState) {
|
2017-07-01 13:28:12 +00:00
|
|
|
if (newState === MUTE) {
|
2016-08-18 13:00:14 +00:00
|
|
|
return setRoomNotifsStateMuted(roomId);
|
|
|
|
} else {
|
|
|
|
return setRoomNotifsStateUnmuted(roomId, newState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setRoomNotifsStateMuted(roomId) {
|
2016-08-17 17:26:37 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const promises = [];
|
|
|
|
|
2016-08-18 13:00:14 +00:00
|
|
|
// delete the room rule
|
|
|
|
const roomRule = cli.getRoomPushRule('global', roomId);
|
|
|
|
if (roomRule) {
|
|
|
|
promises.push(cli.deletePushRule('global', 'room', roomRule.rule_id));
|
|
|
|
}
|
2016-08-17 17:26:37 +00:00
|
|
|
|
2016-08-18 14:21:46 +00:00
|
|
|
// add/replace an override rule to squelch everything in this room
|
|
|
|
// NB. We use the room ID as the name of this rule too, although this
|
|
|
|
// is an override rule, not a room rule: it still pertains to this room
|
|
|
|
// though, so using the room ID as the rule ID is logical and prevents
|
|
|
|
// duplicate copies of the rule.
|
2016-08-18 13:00:14 +00:00
|
|
|
promises.push(cli.addPushRule('global', 'override', roomId, {
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
kind: 'event_match',
|
|
|
|
key: 'room_id',
|
|
|
|
pattern: roomId,
|
2017-07-01 13:28:12 +00:00
|
|
|
},
|
2016-08-18 13:00:14 +00:00
|
|
|
],
|
|
|
|
actions: [
|
|
|
|
'dont_notify',
|
2017-07-01 13:28:12 +00:00
|
|
|
],
|
2016-08-18 13:00:14 +00:00
|
|
|
}));
|
|
|
|
|
2017-07-12 13:04:20 +00:00
|
|
|
return Promise.all(promises);
|
2016-08-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setRoomNotifsStateUnmuted(roomId, newState) {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
const overrideMuteRule = findOverrideMuteRule(roomId);
|
|
|
|
if (overrideMuteRule) {
|
|
|
|
promises.push(cli.deletePushRule('global', 'override', overrideMuteRule.rule_id));
|
|
|
|
}
|
|
|
|
|
2017-07-01 13:28:12 +00:00
|
|
|
if (newState === 'all_messages') {
|
2016-08-18 15:59:25 +00:00
|
|
|
const roomRule = cli.getRoomPushRule('global', roomId);
|
|
|
|
if (roomRule) {
|
|
|
|
promises.push(cli.deletePushRule('global', 'room', roomRule.rule_id));
|
|
|
|
}
|
2017-07-01 13:28:12 +00:00
|
|
|
} else if (newState === 'mentions_only') {
|
2016-08-18 13:00:14 +00:00
|
|
|
promises.push(cli.addPushRule('global', 'room', roomId, {
|
2016-08-17 17:26:37 +00:00
|
|
|
actions: [
|
|
|
|
'dont_notify',
|
2017-07-01 13:28:12 +00:00
|
|
|
],
|
2016-08-17 17:26:37 +00:00
|
|
|
}));
|
2016-08-18 13:00:14 +00:00
|
|
|
// https://matrix.org/jira/browse/SPEC-400
|
|
|
|
promises.push(cli.setPushRuleEnabled('global', 'room', roomId, true));
|
|
|
|
} else if ('all_messages_loud') {
|
|
|
|
promises.push(cli.addPushRule('global', 'room', roomId, {
|
|
|
|
actions: [
|
|
|
|
'notify',
|
|
|
|
{
|
|
|
|
set_tweak: 'sound',
|
|
|
|
value: 'default',
|
2017-07-01 13:28:12 +00:00
|
|
|
},
|
|
|
|
],
|
2016-08-18 13:00:14 +00:00
|
|
|
}));
|
|
|
|
// https://matrix.org/jira/browse/SPEC-400
|
|
|
|
promises.push(cli.setPushRuleEnabled('global', 'room', roomId, true));
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 13:04:20 +00:00
|
|
|
return Promise.all(promises);
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findOverrideMuteRule(roomId) {
|
|
|
|
for (const rule of MatrixClientPeg.get().pushRules['global'].override) {
|
|
|
|
if (isRuleForRoom(roomId, rule)) {
|
2016-08-18 13:00:14 +00:00
|
|
|
if (isMuteRule(rule) && rule.enabled) {
|
2016-08-17 17:26:37 +00:00
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRuleForRoom(roomId, rule) {
|
|
|
|
if (rule.conditions.length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cond = rule.conditions[0];
|
2017-07-01 13:28:12 +00:00
|
|
|
return (cond.kind === 'event_match' && cond.key === 'room_id' && cond.pattern === roomId);
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isMuteRule(rule) {
|
2017-07-01 13:28:12 +00:00
|
|
|
return (rule.actions.length === 1 && rule.actions[0] === 'dont_notify');
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|