Skip to content

Commit fbbbd44

Browse files
authored
Merge pull request element-hq#1982 from vector-im/dbkr/make_notif_silder_work
Make the notification slider work
2 parents d3eccc1 + 0bb3eaa commit fbbbd44

2 files changed

Lines changed: 45 additions & 54 deletions

File tree

‎src/components/views/context_menus/NotificationStateContextMenu.js‎

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ limitations under the License.
1919
var q = require("q");
2020
var React = require('react');
2121
var classNames = require('classnames');
22+
var RoomNotifs = require('matrix-react-sdk/lib/RoomNotifs');
2223
var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg');
23-
var dis = require('matrix-react-sdk/lib/dispatcher');
2424

2525
module.exports = React.createClass({
2626
displayName: 'NotificationStateContextMenu',
@@ -31,95 +31,86 @@ module.exports = React.createClass({
3131
onFinished: React.PropTypes.func,
3232
},
3333

34-
getInitialState: function() {
35-
var areNotifsMuted = false;
36-
var cli = MatrixClientPeg.get();
37-
if (!cli.isGuest()) {
38-
var roomPushRule = cli.getRoomPushRule("global", this.props.room.roomId);
39-
if (roomPushRule) {
40-
if (0 <= roomPushRule.actions.indexOf("dont_notify")) {
41-
areNotifsMuted = true;
42-
}
43-
}
34+
getInitialState() {
35+
return {
36+
roomNotifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId),
4437
}
38+
},
4539

46-
return {
47-
areNotifsMuted: areNotifsMuted,
48-
};
40+
componentWillMount: function() {
41+
this._unmounted = false;
4942
},
5043

51-
_save: function( areNotifsMuted ) {
52-
var self = this;
44+
componentWillUnmount: function() {
45+
this._unmounted = true;
46+
},
47+
48+
_save: function(newState) {
49+
const oldState = this.state.roomNotifState;
5350
const roomId = this.props.room.roomId;
5451
var cli = MatrixClientPeg.get();
5552

56-
if (!cli.isGuest()) {
57-
// Wrapping this in a q promise, as setRoomMutePushRule can return
58-
// a promise or a value
59-
q(cli.setRoomMutePushRule("global", roomId, areNotifsMuted))
60-
.then(function() {
61-
self.setState({areNotifsMuted: areNotifsMuted});
62-
63-
// delay slightly so that the user can see their state change
64-
// before closing the menu
65-
return q.delay(500).then(function() {
66-
// tell everyone that wants to know of the change in
67-
// notification state
68-
dis.dispatch({
69-
action: 'notification_change',
70-
roomId: self.props.room.roomId,
71-
areNotifsMuted: areNotifsMuted,
72-
});
73-
74-
// Close the context menu
75-
if (self.props.onFinished) {
76-
self.props.onFinished();
77-
};
78-
});
79-
}).fail(function(error) {
80-
// TODO: some form of error notification to the user
81-
// to inform them that their state change failed.
53+
if (cli.isGuest()) return;
54+
55+
this.setState({
56+
roomNotifState: newState,
57+
});
58+
RoomNotifs.setRoomNotifsState(this.props.room.roomId, newState).done(() => {
59+
// delay slightly so that the user can see their state change
60+
// before closing the menu
61+
return q.delay(500).then(() => {
62+
if (this._unmounted) return;
63+
// Close the context menu
64+
if (this.props.onFinished) {
65+
this.props.onFinished();
66+
};
8267
});
83-
}
68+
}, (error) => {
69+
// TODO: some form of error notification to the user
70+
// to inform them that their state change failed.
71+
// For now we at least set the state back
72+
if (this._unmounted) return;
73+
this.setState({
74+
roomNotifState: oldState,
75+
});
76+
});
8477
},
8578

8679
_onClickAlertMe: function() {
87-
// Placeholder
80+
this._save(RoomNotifs.ALL_MESSAGES_LOUD);
8881
},
8982

9083
_onClickAllNotifs: function() {
91-
this._save(false);
84+
this._save(RoomNotifs.ALL_MESSAGES);
9285
},
9386

9487
_onClickMentions: function() {
95-
this._save(true);
88+
this._save(RoomNotifs.MENTIONS_ONLY);
9689
},
9790

9891
_onClickMute: function() {
99-
// Placeholder
92+
this._save(RoomNotifs.MUTE);
10093
},
10194

10295
render: function() {
103-
var cli = MatrixClientPeg.get();
104-
10596
var alertMeClasses = classNames({
10697
'mx_NotificationStateContextMenu_field': true,
107-
'mx_NotificationStateContextMenu_fieldDisabled': true,
98+
'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == RoomNotifs.ALL_MESSAGES_LOUD,
10899
});
109100

110101
var allNotifsClasses = classNames({
111102
'mx_NotificationStateContextMenu_field': true,
112-
'mx_NotificationStateContextMenu_fieldSet': !this.state.areNotifsMuted,
103+
'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == RoomNotifs.ALL_MESSAGES,
113104
});
114105

115106
var mentionsClasses = classNames({
116107
'mx_NotificationStateContextMenu_field': true,
117-
'mx_NotificationStateContextMenu_fieldSet': this.state.areNotifsMuted,
108+
'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == RoomNotifs.MENTIONS_ONLY,
118109
});
119110

120111
var muteNotifsClasses = classNames({
121112
'mx_NotificationStateContextMenu_field': true,
122-
'mx_NotificationStateContextMenu_fieldDisabled': true,
113+
'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == RoomNotifs.MUTE,
123114
});
124115

125116
return (

‎src/notifications/VectorPushRulesDefinitions.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = {
6565
// Messages containing user's display name
6666
// (skip contains_user_name which is too geeky)
6767
".m.rule.contains_display_name": new VectorPushRuleDefinition({
68-
kind: "underride",
68+
kind: "override",
6969
description: "Messages containing my name",
7070
vectorStateToActions: { // The actions for each vector state, or null to disable the rule.
7171
on: StandardActions.ACTION_NOTIFY,

0 commit comments

Comments
 (0)