Factor out audio bings to a separate setting

Some people are sad about bings on the desktop app. Make it turn-off-able.
pull/21833/head
Richard van der Hoff 2016-03-10 10:59:40 +00:00
parent ec2e27a754
commit c5e03913d9
2 changed files with 46 additions and 21 deletions

View File

@ -35,7 +35,7 @@ var Notifier = {
return TextForEvent.textForEvent(ev); return TextForEvent.textForEvent(ev);
}, },
displayNotification: function(ev, room, actions) { _displayPopupNotification: function(ev, room) {
if (!global.Notification || global.Notification.permission != 'granted') { if (!global.Notification || global.Notification.permission != 'granted') {
return; return;
} }
@ -84,24 +84,17 @@ var Notifier = {
global.focus(); global.focus();
}; };
var playAudio = function() { global.setTimeout(function() {
notification.close();
}, 5 * 1000);
},
_playAudioNotification: function(ev, room) {
var e = document.getElementById("messageAudio"); var e = document.getElementById("messageAudio");
if (e) { if (e) {
e.load(); e.load();
e.play(); e.play();
return e;
}
}; };
var audioClip;
if (actions.tweaks.sound) {
audioClip = playAudio();
}
global.setTimeout(function() {
notification.close();
}, 5 * 1000);
}, },
start: function() { start: function() {
@ -129,6 +122,14 @@ var Notifier = {
}, },
setEnabled: function(enable, callback) { setEnabled: function(enable, callback) {
// make sure that we persist the current setting audio_enabled setting
// before changing anything
if (global.localStorage) {
if(global.localStorage.getItem('audio_notifications_enabled') == null) {
this.setAudioEnabled(this.isEnabled());
}
}
if(enable) { if(enable) {
if (!this.havePermission()) { if (!this.havePermission()) {
global.Notification.requestPermission(function() { global.Notification.requestPermission(function() {
@ -174,6 +175,21 @@ var Notifier = {
return enabled === 'true'; return enabled === 'true';
}, },
setAudioEnabled: function(enable) {
if (!global.localStorage) return;
global.localStorage.setItem('audio_notifications_enabled',
enable ? 'true' : 'false');
},
isAudioEnabled: function(enable) {
if (!global.localStorage) return true;
var enabled = global.localStorage.getItem(
'audio_notifications_enabled');
// default to true if the popups are enabled
if (enabled === null) return this.isEnabled();
return enabled === 'true';
},
setToolbarHidden: function(hidden) { setToolbarHidden: function(hidden) {
this.toolbarHidden = hidden; this.toolbarHidden = hidden;
dis.dispatch({ dis.dispatch({
@ -200,13 +216,14 @@ var Notifier = {
if (!this.isPrepared) return; // don't alert for any messages initially if (!this.isPrepared) return; // don't alert for any messages initially
if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return; if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return;
if (!this.isEnabled()) {
return;
}
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev); var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
if (actions && actions.notify) { if (actions && actions.notify) {
this.displayNotification(ev, room, actions); if (this.isEnabled()) {
this._displayPopupNotification(ev, room);
}
if (actions.tweaks.sound && this.isAudioEnabled()) {
this._playAudioNotification(ev, room);
}
} }
} }
}; };

View File

@ -58,6 +58,14 @@ module.exports = {
Notifier.setEnabled(enable); Notifier.setEnabled(enable);
}, },
getEnableAudioNotifications: function() {
return Notifier.isAudioEnabled();
},
setEnableAudioNotifications: function(enable) {
Notifier.setAudioEnabled(enable);
},
changePassword: function(old_password, new_password) { changePassword: function(old_password, new_password) {
var cli = MatrixClientPeg.get(); var cli = MatrixClientPeg.get();