Make the slider work

Still WIP though: need to make vector work with the 'contains display name' rule being an override
This commit is contained in:
David Baker 2016-08-16 15:54:28 +01:00
parent a17df609f3
commit cd0ed879e3
3 changed files with 105 additions and 30 deletions

View file

@ -16,16 +16,14 @@ limitations under the License.
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
import PushProcessor from 'matrix-js-sdk/lib/pushprocessor';
import q from 'q';
export function getVectorRoomNotifsState(roomId) {
// look through the override rules for a rule affecting this room:
// if one exists, it will take precedence.
for (const rule of MatrixClientPeg.get().pushRules['global'].override) {
if (isRuleForRoom(roomId, rule)) {
if (isMuteRule(rule)) {
return 'mute';
}
}
const muteRule = findOverrideMuteRule(roomId);
if (muteRule && muteRule.enabled) {
return 'mute';
}
// for everything else, look at the room rule.
@ -34,7 +32,7 @@ export function getVectorRoomNotifsState(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)
if (!roomRule) return 'all_messages';
if (!roomRule || !roomRule.enabled) return 'all_messages';
// a mute at the room level will still allow mentions
// to notify
@ -46,6 +44,75 @@ export function getVectorRoomNotifsState(roomId) {
return null;
}
export function setVectorRoomNotifsState(roomId, newState) {
const cli = MatrixClientPeg.get();
const promises = [];
if (newState == 'mute') {
// delete the room rule
const roomRule = MatrixClientPeg.get().getRoomPushRule('global', roomId);
if (roomRule) {
promises.push(cli.deletePushRule('global', 'room', roomRule.rule_id));
}
// add an override rule to squelch everything in this room
promises.push(cli.addPushRule('global', 'override', roomId, {
conditions: [
{
kind: 'event_match',
key: 'room_id',
pattern: roomId,
}
],
actions: [
'dont_notify',
]
}));
} else {
const overrideMuteRule = findOverrideMuteRule(roomId);
if (overrideMuteRule) {
promises.push(cli.deletePushRule('global', 'override', overrideMuteRule.rule_id));
}
if (newState == 'all_messages') {
promises.push(cli.deletePushRule('global', 'room', roomId));
} else if (newState == 'mentions_only') {
promises.push(cli.addPushRule('global', 'room', roomId, {
actions: [
'dont_notify',
]
}));
// 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',
}
]
}));
// https://matrix.org/jira/browse/SPEC-400
promises.push(cli.setPushRuleEnabled('global', 'room', roomId, true));
}
}
return q.all(promises);
}
function findOverrideMuteRule(roomId) {
for (const rule of MatrixClientPeg.get().pushRules['global'].override) {
if (isRuleForRoom(roomId, rule)) {
if (isMuteRule(rule)) {
return rule;
}
}
}
return null;
}
function isRuleForRoom(roomId, rule) {
if (rule.conditions.length !== 1) {
return false;

View file

@ -65,7 +65,7 @@ module.exports = {
// Messages containing user's display name
// (skip contains_user_name which is too geeky)
".m.rule.contains_display_name": new VectorPushRuleDefinition({
kind: "underride",
kind: "override",
description: "Messages containing my name",
vectorStateToActions: { // The actions for each vector state, or null to disable the rule.
on: StandardActions.ACTION_NOTIFY,