From af48b8920ec8d192d82869176b7839b4216fd6e5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 18 Aug 2016 14:00:14 +0100 Subject: [PATCH] Various PR feedback --- src/RoomNotifs.js | 107 ++++++++++++++----------- src/components/views/rooms/RoomTile.js | 16 ++-- 2 files changed, 66 insertions(+), 57 deletions(-) diff --git a/src/RoomNotifs.js b/src/RoomNotifs.js index 6f792df26b..563491b695 100644 --- a/src/RoomNotifs.js +++ b/src/RoomNotifs.js @@ -24,10 +24,12 @@ export const MENTIONS_ONLY = 'mentions_only'; export const MUTE = 'mute'; export function getRoomNotifsState(roomId) { + if (MatrixClientPeg.get().isGuest()) return RoomNotifs.ALL_MESSAGES; + // look through the override rules for a rule affecting this room: // if one exists, it will take precedence. const muteRule = findOverrideMuteRule(roomId); - if (muteRule && muteRule.enabled) { + if (muteRule) { return MUTE; } @@ -50,58 +52,71 @@ export function getRoomNotifsState(roomId) { } export function setRoomNotifsState(roomId, newState) { + if (newState == 'mute') { + return setRoomNotifsStateMuted(roomId); + } else { + return setRoomNotifsStateUnmuted(roomId, newState); + } +} + +function setRoomNotifsStateMuted(roomId) { 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)); - } + // delete the room rule + const roomRule = cli.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, - } - ], + // 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', + ] + })); + + return q.all(promises); +} + +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)); + } + + 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', ] })); - } 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)); - } + // 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); @@ -110,7 +125,7 @@ export function setRoomNotifsState(roomId, newState) { function findOverrideMuteRule(roomId) { for (const rule of MatrixClientPeg.get().pushRules['global'].override) { if (isRuleForRoom(roomId, rule)) { - if (isMuteRule(rule)) { + if (isMuteRule(rule) && rule.enabled) { return rule; } } diff --git a/src/components/views/rooms/RoomTile.js b/src/components/views/rooms/RoomTile.js index 5b508b6ad0..e520b0bb00 100644 --- a/src/components/views/rooms/RoomTile.js +++ b/src/components/views/rooms/RoomTile.js @@ -49,29 +49,23 @@ module.exports = React.createClass({ badgeHover : false, notificationTagMenu: false, roomTagMenu: false, - notifState: this._getNotifState(), + notifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId), }); }, - _getNotifState: function() { - if (MatrixClientPeg.get().isGuest()) return RoomNotifs.ALL_MESSAGES; - return RoomNotifs.getRoomNotifsState(this.props.room.roomId); - }, - _shouldShowNotifBadge: function() { const showBadgeInStates = [RoomNotifs.ALL_MESSAGES, RoomNotifs.ALL_MESSAGES_LOUD]; - const currentState = this._getNotifState(); - return showBadgeInStates.indexOf(currentState) > -1; + return showBadgeInStates.indexOf(this.state.notifState) > -1; }, _shouldShowMentionBadge: function() { - return this._getNotifState() != RoomNotifs.MUTE; + return this.state.notifState != RoomNotifs.MUTE; }, onAccountData: function(accountDataEvent) { if (accountDataEvent.getType() == 'm.push_rules') { this.setState({ - notifState: this._getNotifState(), + notifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId), }); } }, @@ -193,7 +187,7 @@ module.exports = React.createClass({ 'mx_RoomTile_selected': this.props.selected, 'mx_RoomTile_unread': this.props.unread, 'mx_RoomTile_unreadNotify': notificationCount > 0 && this._shouldShowNotifBadge(), - 'mx_RoomTile_highlight': this.props.highlight && badges, + 'mx_RoomTile_highlight': this.props.highlight && this._shouldShowMentionBadge(), 'mx_RoomTile_invited': (me && me.membership == 'invite'), 'mx_RoomTile_notificationTagMenu': this.state.notificationTagMenu, 'mx_RoomTile_noBadges': !badges,