2015-07-03 18:12:54 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-07-03 18:12:54 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-11-30 23:04:24 +08:00
|
|
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
|
|
|
var TextForEvent = require('./TextForEvent');
|
|
|
|
var Avatar = require('./Avatar');
|
|
|
|
var dis = require("./dispatcher");
|
2015-09-16 21:48:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatches:
|
|
|
|
* {
|
|
|
|
* action: "notifier_enabled",
|
|
|
|
* value: boolean
|
|
|
|
* }
|
|
|
|
*/
|
2015-07-03 18:12:54 +08:00
|
|
|
|
2015-12-03 00:35:16 +08:00
|
|
|
var Notifier = {
|
2015-11-30 23:04:24 +08:00
|
|
|
|
|
|
|
notificationMessageForEvent: function(ev) {
|
|
|
|
return TextForEvent.textForEvent(ev);
|
|
|
|
},
|
|
|
|
|
2016-03-10 18:59:40 +08:00
|
|
|
_displayPopupNotification: function(ev, room) {
|
2015-11-30 23:04:24 +08:00
|
|
|
if (!global.Notification || global.Notification.permission != 'granted') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (global.document.hasFocus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = this.notificationMessageForEvent(ev);
|
|
|
|
if (!msg) return;
|
|
|
|
|
|
|
|
var title;
|
|
|
|
if (!ev.sender || room.name == ev.sender.name) {
|
|
|
|
title = room.name;
|
|
|
|
// notificationMessageForEvent includes sender,
|
|
|
|
// but we already have the sender here
|
|
|
|
if (ev.getContent().body) msg = ev.getContent().body;
|
|
|
|
} else if (ev.getType() == 'm.room.member') {
|
|
|
|
// context is all in the message here, we don't need
|
|
|
|
// to display sender info
|
|
|
|
title = room.name;
|
|
|
|
} else if (ev.sender) {
|
|
|
|
title = ev.sender.name + " (" + room.name + ")";
|
|
|
|
// notificationMessageForEvent includes sender,
|
|
|
|
// but we've just out sender in the title
|
|
|
|
if (ev.getContent().body) msg = ev.getContent().body;
|
|
|
|
}
|
|
|
|
|
|
|
|
var avatarUrl = ev.sender ? Avatar.avatarUrlForMember(
|
|
|
|
ev.sender, 40, 40, 'crop'
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
var notification = new global.Notification(
|
|
|
|
title,
|
|
|
|
{
|
|
|
|
"body": msg,
|
|
|
|
"icon": avatarUrl,
|
2015-12-01 02:11:04 +08:00
|
|
|
"tag": "matrixreactsdk"
|
2015-11-30 23:04:24 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
notification.onclick = function() {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: room.roomId
|
|
|
|
});
|
|
|
|
global.focus();
|
|
|
|
};
|
2016-03-03 21:18:41 +08:00
|
|
|
|
2015-11-30 23:04:24 +08:00
|
|
|
global.setTimeout(function() {
|
|
|
|
notification.close();
|
|
|
|
}, 5 * 1000);
|
2016-03-10 18:59:40 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_playAudioNotification: function(ev, room) {
|
|
|
|
var e = document.getElementById("messageAudio");
|
|
|
|
if (e) {
|
|
|
|
e.load();
|
|
|
|
e.play();
|
|
|
|
};
|
2015-11-30 23:04:24 +08:00
|
|
|
},
|
|
|
|
|
2015-07-03 18:12:54 +08:00
|
|
|
start: function() {
|
|
|
|
this.boundOnRoomTimeline = this.onRoomTimeline.bind(this);
|
2015-12-16 01:01:16 +08:00
|
|
|
this.boundOnSyncStateChange = this.onSyncStateChange.bind(this);
|
2015-07-03 18:12:54 +08:00
|
|
|
MatrixClientPeg.get().on('Room.timeline', this.boundOnRoomTimeline);
|
2015-12-16 01:01:16 +08:00
|
|
|
MatrixClientPeg.get().on("sync", this.boundOnSyncStateChange);
|
2015-11-30 23:04:24 +08:00
|
|
|
this.toolbarHidden = false;
|
2016-03-17 10:06:47 +08:00
|
|
|
this.isPrepared = false;
|
2015-07-03 18:12:54 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function() {
|
|
|
|
if (MatrixClientPeg.get()) {
|
|
|
|
MatrixClientPeg.get().removeListener('Room.timeline', this.boundOnRoomTimeline);
|
2015-12-16 01:01:16 +08:00
|
|
|
MatrixClientPeg.get().removeListener('sync', this.boundOnSyncStateChange);
|
2015-07-03 18:12:54 +08:00
|
|
|
}
|
2016-03-17 10:06:47 +08:00
|
|
|
this.isPrepared = false;
|
2015-07-03 18:12:54 +08:00
|
|
|
},
|
|
|
|
|
2015-09-16 21:48:49 +08:00
|
|
|
supportsDesktopNotifications: function() {
|
|
|
|
return !!global.Notification;
|
|
|
|
},
|
|
|
|
|
|
|
|
havePermission: function() {
|
|
|
|
if (!this.supportsDesktopNotifications()) return false;
|
|
|
|
return global.Notification.permission == 'granted';
|
|
|
|
},
|
|
|
|
|
2016-03-22 06:19:46 +08:00
|
|
|
isPermissionDefault: function() {
|
|
|
|
if (!this.supportsDesktopNotifications()) return false;
|
|
|
|
return global.Notification.permission == 'default';
|
|
|
|
},
|
|
|
|
|
2016-03-23 17:41:09 +08:00
|
|
|
// Function to be used by clients to check weather or not to
|
|
|
|
// show the toolbar.
|
|
|
|
shouldShowToolbar: function() {
|
2016-03-22 06:19:46 +08:00
|
|
|
// Check localStorage for any such meta data
|
|
|
|
if (global.localStorage) {
|
|
|
|
if (global.localStorage.getItem('notifications_hidden') === 'true')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if permission is granted by any chance.
|
|
|
|
if (this.havePermission()) return false;
|
|
|
|
|
|
|
|
// means the permission is blocked
|
|
|
|
if (!this.isPermissionDefault()) return false;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2015-09-16 21:48:49 +08:00
|
|
|
setEnabled: function(enable, callback) {
|
2016-03-10 18:59:40 +08:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 21:48:49 +08:00
|
|
|
if(enable) {
|
2016-03-22 06:19:46 +08:00
|
|
|
// Case when we do not have the permission as 'granted'
|
|
|
|
if (this.isPermissionDefault()) {
|
|
|
|
// Attempt to get permission from user
|
2016-03-22 20:21:17 +08:00
|
|
|
var self = this;
|
2016-03-23 18:15:54 +08:00
|
|
|
global.Notification.requestPermission(function(result) {
|
2016-03-22 06:19:46 +08:00
|
|
|
if (result === 'denied') {
|
2015-09-16 21:48:49 +08:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2016-03-22 06:19:46 +08:00
|
|
|
value: false
|
2015-09-16 21:48:49 +08:00
|
|
|
});
|
2016-03-23 16:20:24 +08:00
|
|
|
self.setToolbarHidden(true, false);
|
2016-03-22 06:19:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result === 'default') {
|
|
|
|
// The permission request was dismissed
|
|
|
|
return;
|
2015-09-16 21:48:49 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 06:19:46 +08:00
|
|
|
if (callback) callback();
|
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
|
|
|
value: true
|
|
|
|
});
|
2015-09-16 21:48:49 +08:00
|
|
|
|
2016-03-22 06:19:46 +08:00
|
|
|
if (!global.localStorage) return;
|
|
|
|
global.localStorage.setItem('notifications_enabled', 'true');
|
2015-09-16 21:48:49 +08:00
|
|
|
});
|
|
|
|
}
|
2016-03-22 06:19:46 +08:00
|
|
|
} else {
|
2015-09-16 21:48:49 +08:00
|
|
|
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';
|
|
|
|
},
|
|
|
|
|
2016-03-10 18:59:40 +08:00
|
|
|
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';
|
|
|
|
},
|
|
|
|
|
2016-03-23 16:20:24 +08:00
|
|
|
setToolbarHidden: function(hidden, persistent = true) {
|
2015-11-30 23:04:24 +08:00
|
|
|
this.toolbarHidden = hidden;
|
2015-09-16 21:48:49 +08:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
|
|
|
value: this.isEnabled()
|
|
|
|
});
|
2016-03-22 06:19:46 +08:00
|
|
|
|
2016-03-23 18:15:54 +08:00
|
|
|
// update the info to localStorage for persistent settings
|
|
|
|
if (persistent && global.localStorage) {
|
2016-03-22 06:19:46 +08:00
|
|
|
global.localStorage.setItem('notifications_hidden', 'true');
|
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
isToolbarHidden: function() {
|
2015-11-30 23:04:24 +08:00
|
|
|
return this.toolbarHidden;
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
2015-12-16 01:01:16 +08:00
|
|
|
onSyncStateChange: function(state) {
|
|
|
|
if (state === "PREPARED" || state === "SYNCING") {
|
|
|
|
this.isPrepared = true;
|
|
|
|
}
|
2016-03-17 19:34:20 +08:00
|
|
|
else if (state === "STOPPED" || state === "ERROR") {
|
|
|
|
this.isPrepared = false;
|
|
|
|
}
|
2015-12-16 01:01:16 +08:00
|
|
|
},
|
|
|
|
|
2015-07-03 18:12:54 +08:00
|
|
|
onRoomTimeline: function(ev, room, toStartOfTimeline) {
|
|
|
|
if (toStartOfTimeline) return;
|
2015-12-16 01:01:16 +08:00
|
|
|
if (!this.isPrepared) return; // don't alert for any messages initially
|
2015-07-03 23:46:30 +08:00
|
|
|
if (ev.sender && ev.sender.userId == MatrixClientPeg.get().credentials.userId) return;
|
2015-07-03 18:12:54 +08:00
|
|
|
|
|
|
|
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
|
|
|
if (actions && actions.notify) {
|
2016-03-10 18:59:40 +08:00
|
|
|
if (this.isEnabled()) {
|
|
|
|
this._displayPopupNotification(ev, room);
|
|
|
|
}
|
|
|
|
if (actions.tweaks.sound && this.isAudioEnabled()) {
|
|
|
|
this._playAudioNotification(ev, room);
|
|
|
|
}
|
2015-07-03 18:12:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-03 00:35:16 +08:00
|
|
|
if (!global.mxNotifier) {
|
|
|
|
global.mxNotifier = Notifier;
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:29:33 +08:00
|
|
|
module.exports = global.mxNotifier;
|