2015-07-03 18:12:54 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-03-23 18:38:00 +08:00
|
|
|
Copyright 2017 Vector Creations 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.
|
|
|
|
*/
|
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
|
|
|
import PlatformPeg from './PlatformPeg';
|
|
|
|
import TextForEvent from './TextForEvent';
|
|
|
|
import Avatar from './Avatar';
|
|
|
|
import dis from './dispatcher';
|
2017-04-23 13:16:25 +08:00
|
|
|
import sdk from './index';
|
2017-05-25 18:39:08 +08:00
|
|
|
import { _t } from './languageHandler';
|
2017-04-23 13:16:25 +08:00
|
|
|
import Modal from './Modal';
|
2015-09-16 21:48:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatches:
|
|
|
|
* {
|
|
|
|
* action: "notifier_enabled",
|
|
|
|
* value: boolean
|
|
|
|
* }
|
|
|
|
*/
|
2015-07-03 18:12:54 +08:00
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
const Notifier = {
|
2016-11-03 01:35:31 +08:00
|
|
|
notifsByRoom: {},
|
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) {
|
2016-11-03 01:35:31 +08:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) {
|
2015-11-30 23:04:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (global.document.hasFocus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
let msg = this.notificationMessageForEvent(ev);
|
2015-11-30 23:04:24 +08:00
|
|
|
if (!msg) return;
|
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
let title;
|
|
|
|
if (!ev.sender || room.name === ev.sender.name) {
|
2015-11-30 23:04:24 +08:00
|
|
|
title = room.name;
|
|
|
|
// notificationMessageForEvent includes sender,
|
|
|
|
// but we already have the sender here
|
|
|
|
if (ev.getContent().body) msg = ev.getContent().body;
|
2017-04-23 13:06:23 +08:00
|
|
|
} else if (ev.getType() === 'm.room.member') {
|
2015-11-30 23:04:24 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
const avatarUrl = ev.sender ? Avatar.avatarUrlForMember(
|
2015-11-30 23:04:24 +08:00
|
|
|
ev.sender, 40, 40, 'crop'
|
|
|
|
) : null;
|
|
|
|
|
2016-12-06 21:27:36 +08:00
|
|
|
const notif = plaf.displayNotification(title, msg, avatarUrl, room);
|
2016-03-03 21:18:41 +08:00
|
|
|
|
2016-11-03 01:35:31 +08:00
|
|
|
// if displayNotification returns non-null, the platform supports
|
|
|
|
// clearing notifications later, so keep track of this.
|
|
|
|
if (notif) {
|
|
|
|
if (this.notifsByRoom[ev.getRoomId()] === undefined) this.notifsByRoom[ev.getRoomId()] = [];
|
|
|
|
this.notifsByRoom[ev.getRoomId()].push(notif);
|
|
|
|
}
|
2016-03-10 18:59:40 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_playAudioNotification: function(ev, room) {
|
2017-04-23 13:06:23 +08:00
|
|
|
const e = document.getElementById("messageAudio");
|
2016-03-10 18:59:40 +08:00
|
|
|
if (e) {
|
|
|
|
e.load();
|
|
|
|
e.play();
|
2017-01-20 22:22:27 +08:00
|
|
|
}
|
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);
|
2016-11-03 01:35:31 +08:00
|
|
|
this.boundOnRoomReceipt = this.onRoomReceipt.bind(this);
|
2015-07-03 18:12:54 +08:00
|
|
|
MatrixClientPeg.get().on('Room.timeline', this.boundOnRoomTimeline);
|
2017-04-23 13:06:23 +08:00
|
|
|
MatrixClientPeg.get().on('Room.receipt', this.boundOnRoomReceipt);
|
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;
|
2017-03-28 22:02:08 +08:00
|
|
|
this.isSyncing = false;
|
2015-07-03 18:12:54 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function() {
|
2017-03-23 18:38:00 +08:00
|
|
|
if (MatrixClientPeg.get() && this.boundOnRoomTimeline) {
|
2015-07-03 18:12:54 +08:00
|
|
|
MatrixClientPeg.get().removeListener('Room.timeline', this.boundOnRoomTimeline);
|
2017-04-23 13:06:23 +08:00
|
|
|
MatrixClientPeg.get().removeListener('Room.receipt', this.boundOnRoomReceipt);
|
2015-12-16 01:01:16 +08:00
|
|
|
MatrixClientPeg.get().removeListener('sync', this.boundOnSyncStateChange);
|
2015-07-03 18:12:54 +08:00
|
|
|
}
|
2017-03-28 22:02:08 +08:00
|
|
|
this.isSyncing = false;
|
2015-07-03 18:12:54 +08:00
|
|
|
},
|
|
|
|
|
2015-09-16 21:48:49 +08:00
|
|
|
supportsDesktopNotifications: function() {
|
2016-11-03 01:35:31 +08:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
return plaf && plaf.supportsNotifications();
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
setEnabled: function(enable, callback) {
|
2016-11-03 01:35:31 +08:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return;
|
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) {
|
2017-04-23 13:06:23 +08:00
|
|
|
if (global.localStorage.getItem('audio_notifications_enabled') === null) {
|
2016-03-10 18:59:40 +08:00
|
|
|
this.setAudioEnabled(this.isEnabled());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:40:33 +08:00
|
|
|
if (enable) {
|
2016-03-23 19:56:41 +08:00
|
|
|
// Attempt to get permission from user
|
2016-11-03 01:35:31 +08:00
|
|
|
plaf.requestNotificationPermission().done((result) => {
|
2016-03-23 19:56:41 +08:00
|
|
|
if (result !== 'granted') {
|
|
|
|
// The permission request was dismissed or denied
|
2017-04-23 13:16:25 +08:00
|
|
|
const description = result === 'denied'
|
2017-05-23 22:16:31 +08:00
|
|
|
? _t('Riot does not have permission to send you notifications - please check your browser settings')
|
|
|
|
: _t('Riot was not given permission to send notifications - please try again');
|
2017-04-23 13:16:25 +08:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2017-05-23 22:16:31 +08:00
|
|
|
title: _t('Unable to enable Notifications'),
|
2017-04-23 13:16:25 +08:00
|
|
|
description,
|
|
|
|
});
|
2016-03-23 19:56:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:40:33 +08:00
|
|
|
if (global.localStorage) {
|
|
|
|
global.localStorage.setItem('notifications_enabled', 'true');
|
|
|
|
}
|
|
|
|
|
2016-03-23 19:56:41 +08:00
|
|
|
if (callback) callback();
|
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 13:06:23 +08:00
|
|
|
value: true,
|
2015-09-16 21:48:49 +08:00
|
|
|
});
|
2016-03-23 19:56:41 +08:00
|
|
|
});
|
2016-03-29 17:51:13 +08:00
|
|
|
// clear the notifications_hidden flag, so that if notifications are
|
|
|
|
// disabled again in the future, we will show the banner again.
|
2016-03-24 08:16:52 +08:00
|
|
|
this.setToolbarHidden(false);
|
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",
|
2017-04-23 13:06:23 +08:00
|
|
|
value: false,
|
2015-09-16 21:48:49 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
isEnabled: function() {
|
2016-11-03 01:35:31 +08:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return false;
|
|
|
|
if (!plaf.supportsNotifications()) return false;
|
|
|
|
if (!plaf.maySendNotifications()) return false;
|
2015-09-16 21:48:49 +08:00
|
|
|
|
|
|
|
if (!global.localStorage) return true;
|
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
const enabled = global.localStorage.getItem('notifications_enabled');
|
2015-09-16 21:48:49 +08:00
|
|
|
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',
|
2017-04-23 13:06:23 +08:00
|
|
|
enable ? 'true' : 'false');
|
2016-03-10 18:59:40 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
isAudioEnabled: function(enable) {
|
|
|
|
if (!global.localStorage) return true;
|
2017-04-23 13:06:23 +08:00
|
|
|
const enabled = global.localStorage.getItem(
|
2016-03-10 18:59:40 +08:00
|
|
|
'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;
|
2016-09-09 09:09:12 +08:00
|
|
|
|
2016-03-29 17:51:13 +08:00
|
|
|
// XXX: why are we dispatching this here?
|
|
|
|
// this is nothing to do with notifier_enabled
|
2015-09-16 21:48:49 +08:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 13:06:23 +08:00
|
|
|
value: this.isEnabled(),
|
2015-09-16 21:48:49 +08:00
|
|
|
});
|
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-24 08:16:52 +08:00
|
|
|
global.localStorage.setItem('notifications_hidden', hidden);
|
2016-03-22 06:19:46 +08:00
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
isToolbarHidden: function() {
|
2016-03-23 19:56:41 +08:00
|
|
|
// Check localStorage for any such meta data
|
|
|
|
if (global.localStorage) {
|
|
|
|
if (global.localStorage.getItem('notifications_hidden') === 'true') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-03-28 22:02:08 +08:00
|
|
|
if (state === "SYNCING") {
|
|
|
|
this.isSyncing = true;
|
2017-04-23 13:06:23 +08:00
|
|
|
} else if (state === "STOPPED" || state === "ERROR") {
|
2017-03-28 22:02:08 +08:00
|
|
|
this.isSyncing = false;
|
2016-03-17 19:34:20 +08:00
|
|
|
}
|
2015-12-16 01:01:16 +08:00
|
|
|
},
|
|
|
|
|
2016-09-09 05:48:44 +08:00
|
|
|
onRoomTimeline: function(ev, room, toStartOfTimeline, removed, data) {
|
2015-07-03 18:12:54 +08:00
|
|
|
if (toStartOfTimeline) return;
|
2016-09-09 09:09:12 +08:00
|
|
|
if (!room) return;
|
2017-03-28 22:02:08 +08:00
|
|
|
if (!this.isSyncing) return; // don't alert for any messages initially
|
2017-04-23 13:06:23 +08:00
|
|
|
if (ev.sender && ev.sender.userId === MatrixClientPeg.get().credentials.userId) return;
|
2016-09-09 05:48:44 +08:00
|
|
|
if (data.timeline.getTimelineSet() !== room.getUnfilteredTimelineSet()) return;
|
2015-07-03 18:12:54 +08:00
|
|
|
|
2017-04-23 13:06:23 +08:00
|
|
|
const actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
2015-07-03 18:12:54 +08:00
|
|
|
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
|
|
|
}
|
2016-11-03 01:35:31 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
onRoomReceipt: function(ev, room) {
|
2017-04-23 13:06:23 +08:00
|
|
|
if (room.getUnreadNotificationCount() === 0) {
|
2016-11-03 01:35:31 +08:00
|
|
|
// ideally we would clear each notification when it was read,
|
|
|
|
// but we have no way, given a read receipt, to know whether
|
|
|
|
// the receipt comes before or after an event, so we can't
|
|
|
|
// do this. Instead, clear all notifications for a room once
|
|
|
|
// there are no notifs left in that room., which is not quite
|
|
|
|
// as good but it's something.
|
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return;
|
|
|
|
if (this.notifsByRoom[room.roomId] === undefined) return;
|
|
|
|
for (const notif of this.notifsByRoom[room.roomId]) {
|
|
|
|
plaf.clearNotification(notif);
|
|
|
|
}
|
|
|
|
delete this.notifsByRoom[room.roomId];
|
|
|
|
}
|
2017-04-23 13:06:23 +08:00
|
|
|
},
|
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;
|