Skip to content

Commit fd20e82

Browse files
committed
Add desktop notifications, overridable in the same way as other components (although it's not a react component). Also extend the flux dispatcher a little to be less dumb about dispatching while something else is already dispatching.
1 parent 947f389 commit fd20e82

9 files changed

Lines changed: 288 additions & 3 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
var EnableNotificationsButtonController = require("../../../../src/controllers/atoms/EnableNotificationsButton");
22+
23+
module.exports = React.createClass({
24+
displayName: 'EnableNotificationsButton',
25+
mixins: [EnableNotificationsButtonController],
26+
27+
render: function() {
28+
if (this.enabled()) {
29+
return (
30+
<button className="mx_EnableNotificationsButton" onClick={this.onClick}>Disable Notifications</button>
31+
);
32+
} else {
33+
return (
34+
<button className="mx_EnableNotificationsButton" onClick={this.onClick}>Enable Notifications</button>
35+
);
36+
}
37+
}
38+
});

‎skins/base/views/molecules/MatrixToolbar.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var React = require('react');
2121
var ComponentBroker = require('../../../../src/ComponentBroker');
2222

2323
var LogoutButton = ComponentBroker.get("atoms/LogoutButton");
24+
var EnableNotificationsButton = ComponentBroker.get("atoms/EnableNotificationsButton");
2425

2526
var MatrixToolbarController = require("../../../../src/controllers/molecules/MatrixToolbar");
2627

@@ -32,6 +33,7 @@ module.exports = React.createClass({
3233
return (
3334
<div className="mx_MatrixToolbar">
3435
<LogoutButton />
36+
<EnableNotificationsButton />
3537
</div>
3638
);
3739
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var NotifierController = require("../../../../src/controllers/organisms/Notifier");
20+
21+
var MatrixClientPeg = require("../../../../src/MatrixClientPeg");
22+
var extend = require("../../../../src/extend");
23+
var dis = require("../../../../src/dispatcher");
24+
25+
26+
var NotifierView = {
27+
notificationMessageForEvent: function(ev) {
28+
var senderDisplayName = ev.sender.name;
29+
var message = null;
30+
31+
if (ev.event.type === "m.room.message") {
32+
message = ev.getContent().body;
33+
if (ev.getContent().msgtype === "m.emote") {
34+
message = "* " + senderDisplayName + " " + message;
35+
} else if (ev.getContent().msgtype === "m.image") {
36+
message = senderDisplayName + " sent an image.";
37+
}
38+
} else if (ev.event.type == "m.room.member") {
39+
if (ev.event.state_key !== MatrixClientPeg.get().credentials.userId && "join" === ev.getContent().membership) {
40+
// Notify when another user joins
41+
message = ev.target.name + " joined";
42+
} else if (ev.event.state_key === MatrixClientPeg.get().credentials.userId && "invite" === ev.getContent().membership) {
43+
// notify when you are invited
44+
message = senderDisplayName + " invited you to a room";
45+
}
46+
}
47+
return message;
48+
},
49+
50+
displayNotification: function(ev, room) {
51+
if (!global.Notification || global.Notification.permission != 'granted') {
52+
return;
53+
}
54+
if (global.document.hasFocus()) {
55+
return;
56+
}
57+
58+
var msg = this.notificationMessageForEvent(ev);
59+
if (!msg) return;
60+
61+
var title;
62+
if (room.name == ev.sender.name) {
63+
title = room.name;
64+
} else {
65+
title = ev.sender.name + " (" + room.name + ")";
66+
}
67+
68+
var notification = new global.Notification(
69+
title,
70+
{
71+
"body": msg,
72+
"icon": MatrixClientPeg.get().getAvatarUrlForMember(ev.sender)
73+
}
74+
);
75+
76+
notification.onclick = function() {
77+
dis.dispatch({
78+
action: 'view_room',
79+
room_id: room.roomId
80+
});
81+
global.focus();
82+
};
83+
84+
/*var audioClip;
85+
86+
if (audioNotification) {
87+
audioClip = playAudio(audioNotification);
88+
}*/
89+
90+
global.setTimeout(function() {
91+
notification.close();
92+
}, 5 * 1000);
93+
94+
}
95+
};
96+
97+
var NotifierClass = function() {};
98+
extend(NotifierClass.prototype, NotifierController);
99+
extend(NotifierClass.prototype, NotifierView);
100+
101+
module.exports = new NotifierClass();
102+

‎src/ComponentBroker.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = {
4242
// Must be in this file (because the require is file-specific) and
4343
// must be at the end because the components include this file.
4444
require('../skins/base/views/atoms/LogoutButton');
45+
require('../skins/base/views/atoms/EnableNotificationsButton');
4546
require('../skins/base/views/atoms/MessageTimestamp');
4647
require('../skins/base/views/molecules/MatrixToolbar');
4748
require('../skins/base/views/molecules/RoomTile');
@@ -60,3 +61,4 @@ require('../skins/base/views/molecules/MemberTile');
6061
require('../skins/base/views/organisms/RoomList');
6162
require('../skins/base/views/organisms/RoomView');
6263
require('../skins/base/views/templates/Login');
64+
require('../skins/base/views/organisms/Notifier');
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
module.exports = {
20+
notificationsAvailable: function() {
21+
return !!global.Notification;
22+
},
23+
24+
havePermission: function() {
25+
return global.Notification.permission == 'granted';
26+
},
27+
28+
enabled: function() {
29+
if (!this.havePermission()) return false;
30+
31+
if (!global.localStorage) return true;
32+
33+
var enabled = global.localStorage.getItem('notifications_enabled');
34+
if (enabled === null) return true;
35+
return enabled === 'true';
36+
},
37+
38+
disable: function() {
39+
if (!global.localStorage) return;
40+
global.localStorage.setItem('notifications_enabled', 'false');
41+
this.forceUpdate();
42+
},
43+
44+
enable: function() {
45+
if (!this.havePermission()) {
46+
global.Notification.requestPermission();
47+
}
48+
49+
if (!global.localStorage) return;
50+
global.localStorage.setItem('notifications_enabled', 'true');
51+
this.forceUpdate();
52+
},
53+
54+
onClick: function() {
55+
if (!this.notificationsAvailable()) {
56+
return;
57+
}
58+
if (!this.enabled()) {
59+
this.enable();
60+
} else {
61+
this.disable();
62+
}
63+
},
64+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var MatrixClientPeg = require("../../MatrixClientPeg");
20+
21+
module.exports = {
22+
start: function() {
23+
this.boundOnRoomTimeline = this.onRoomTimeline.bind(this);
24+
MatrixClientPeg.get().on('Room.timeline', this.boundOnRoomTimeline);
25+
},
26+
27+
stop: function() {
28+
if (MatrixClientPeg.get()) {
29+
MatrixClientPeg.get().removeListener('Room.timeline', this.boundOnRoomTimeline);
30+
}
31+
},
32+
33+
onRoomTimeline: function(ev, room, toStartOfTimeline) {
34+
if (toStartOfTimeline) return;
35+
if (ev.sender.userId == MatrixClientPeg.get().credentials.userId) return;
36+
37+
var enabled = global.localStorage.getItem('notifications_enabled');
38+
if (enabled === 'false') return;
39+
40+
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
41+
if (actions && actions.notify) {
42+
this.displayNotification(ev, room);
43+
}
44+
}
45+
};
46+

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ var MatrixClientPeg = require("../../MatrixClientPeg");
2323

2424
var dis = require("../../dispatcher");
2525

26+
var ComponentBroker = require('../../ComponentBroker');
27+
28+
var Notifier = ComponentBroker.get('organisms/Notifier');
29+
2630
module.exports = {
2731
getInitialState: function() {
2832
return {
29-
logged_in: !!(MatrixClientPeg.get() && mxCliPeg.get().credentials),
33+
logged_in: !!(MatrixClientPeg.get() && MatrixClientPeg.get().credentials),
3034
ready: false
3135
};
3236
},
@@ -61,14 +65,15 @@ module.exports = {
6165
logged_in: false,
6266
ready: false
6367
});
68+
Notifier.stop();
6469
MatrixClientPeg.get().removeAllListeners();
6570
MatrixClientPeg.replace(null);
6671
break;
6772
case 'view_room':
73+
this.focusComposer = true;
6874
this.setState({
6975
currentRoom: payload.room_id
7076
});
71-
this.focusComposer = true;
7277
break;
7378
case 'view_prev_room':
7479
roomIndexDelta = -1;
@@ -105,6 +110,7 @@ module.exports = {
105110
that.setState({ready: true, currentRoom: firstRoom});
106111
dis.dispatch({action: 'focus_composer'});
107112
});
113+
Notifier.start();
108114
cli.startClient();
109115
},
110116

‎src/dispatcher.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,21 @@ limitations under the License.
1717
'use strict';
1818

1919
var flux = require("flux");
20-
module.exports = new flux.Dispatcher();
20+
var extend = require("./extend");
21+
22+
var MatrixDispatcher = function() {
23+
flux.Dispatcher.call(this);
24+
};
25+
26+
extend(MatrixDispatcher.prototype, flux.Dispatcher.prototype);
27+
MatrixDispatcher.prototype.dispatch = function(payload) {
28+
if (this.dispatching) {
29+
setTimeout(flux.Dispatcher.prototype.dispatch.bind(this, payload), 0);
30+
} else {
31+
this.dispatching = true;
32+
flux.Dispatcher.prototype.dispatch.call(this, payload);
33+
this.dispatching = false;
34+
}
35+
}
36+
37+
module.exports = new MatrixDispatcher();

‎src/extend.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function(dest, src) {
2+
for (var i in src) {
3+
if (src.hasOwnProperty(i)) {
4+
dest[i] = src[i];
5+
}
6+
}
7+
return dest;
8+
}

0 commit comments

Comments
 (0)