Factor out logic from EnableNotificationsButton(!) and reuse MatrixToolbar.

Added notification logic to Notifier; dispatch notifier_enabled when toggled
so the toolbar can be shown/hidden and the button text can be kept in sync.
Add MatrixToolbar back into MatrixChat for notification nagging.
This commit is contained in:
Kegan Dougal 2015-07-21 15:47:56 +01:00
parent ed738b6398
commit c1de5e9e95
5 changed files with 95 additions and 38 deletions

View file

@ -17,6 +17,15 @@ limitations under the License.
'use strict';
var MatrixClientPeg = require("../../MatrixClientPeg");
var dis = require("../../dispatcher");
/*
* Dispatches:
* {
* action: "notifier_enabled",
* value: boolean
* }
*/
module.exports = {
start: function() {
@ -30,12 +39,60 @@ module.exports = {
}
},
supportsDesktopNotifications: function() {
return !!global.Notification;
},
havePermission: function() {
return global.Notification.permission == 'granted';
},
setEnabled: function(enable, callback) {
console.log("Notifier.setEnabled => %s", enable);
if(enable) {
if (!this.havePermission()) {
var self = this;
global.Notification.requestPermission(function() {
if (callback) {
callback();
}
});
}
if (!global.localStorage) return;
global.localStorage.setItem('notifications_enabled', 'true');
dis.dispatch({
action: "notifier_enabled",
value: true
});
}
else {
if (!global.localStorage) return;
global.localStorage.setItem('notifications_enabled', 'false');
dis.dispatch({
action: "notifier_enabled",
value: false
});
}
},
isEnabled: function() {
if (!this.havePermission()) return false;
if (!global.localStorage) return true;
var enabled = global.localStorage.getItem('notifications_enabled');
if (enabled === null) return true;
return enabled === 'true';
},
onRoomTimeline: function(ev, room, toStartOfTimeline) {
if (toStartOfTimeline) return;
if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return;
var enabled = global.localStorage.getItem('notifications_enabled');
if (enabled === 'false') return;
if (!this.isEnabled()) {
return;
}
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
if (actions && actions.notify) {