2015-07-03 12:12:54 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-03-23 11:38:00 +01:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-08-25 14:35:04 +02:00
|
|
|
Copyright 2017 New Vector Ltd
|
2015-07-03 12:12:54 +02: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 07:06:23 +02:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
|
|
|
import PlatformPeg from './PlatformPeg';
|
|
|
|
import TextForEvent from './TextForEvent';
|
2017-05-29 20:15:52 +02:00
|
|
|
import Analytics from './Analytics';
|
2017-04-23 07:06:23 +02:00
|
|
|
import Avatar from './Avatar';
|
|
|
|
import dis from './dispatcher';
|
2017-04-23 07:16:25 +02:00
|
|
|
import sdk from './index';
|
2017-05-25 12:39:08 +02:00
|
|
|
import { _t } from './languageHandler';
|
2017-04-23 07:16:25 +02:00
|
|
|
import Modal from './Modal';
|
2017-11-05 05:47:18 +01:00
|
|
|
import SettingsStore, {SettingLevel} from "./settings/SettingsStore";
|
2015-09-16 15:48:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatches:
|
|
|
|
* {
|
|
|
|
* action: "notifier_enabled",
|
|
|
|
* value: boolean
|
|
|
|
* }
|
|
|
|
*/
|
2015-07-03 12:12:54 +02:00
|
|
|
|
2017-08-24 15:27:38 +02:00
|
|
|
const MAX_PENDING_ENCRYPTED = 20;
|
|
|
|
|
2017-04-23 07:06:23 +02:00
|
|
|
const Notifier = {
|
2016-11-02 18:35:31 +01:00
|
|
|
notifsByRoom: {},
|
2015-11-30 16:04:24 +01:00
|
|
|
|
2017-08-24 15:27:38 +02:00
|
|
|
// A list of event IDs that we've received but need to wait until
|
|
|
|
// they're decrypted until we decide whether to notify for them
|
|
|
|
// or not
|
|
|
|
pendingEncryptedEventIds: [],
|
|
|
|
|
2015-11-30 16:04:24 +01:00
|
|
|
notificationMessageForEvent: function(ev) {
|
|
|
|
return TextForEvent.textForEvent(ev);
|
|
|
|
},
|
|
|
|
|
2016-03-10 11:59:40 +01:00
|
|
|
_displayPopupNotification: function(ev, room) {
|
2016-11-02 18:35:31 +01:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) {
|
2015-11-30 16:04:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (global.document.hasFocus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-23 07:06:23 +02:00
|
|
|
let msg = this.notificationMessageForEvent(ev);
|
2015-11-30 16:04:24 +01:00
|
|
|
if (!msg) return;
|
|
|
|
|
2017-04-23 07:06:23 +02:00
|
|
|
let title;
|
|
|
|
if (!ev.sender || room.name === ev.sender.name) {
|
2015-11-30 16:04:24 +01: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 07:06:23 +02:00
|
|
|
} else if (ev.getType() === 'm.room.member') {
|
2015-11-30 16:04:24 +01: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-09-06 11:56:08 +02:00
|
|
|
if (!this.isBodyEnabled()) {
|
|
|
|
msg = '';
|
|
|
|
}
|
2015-11-30 16:04:24 +01:00
|
|
|
|
2017-09-06 11:56:08 +02:00
|
|
|
const avatarUrl = ev.sender ? Avatar.avatarUrlForMember(ev.sender, 40, 40, 'crop') : null;
|
2016-12-06 14:27:36 +01:00
|
|
|
const notif = plaf.displayNotification(title, msg, avatarUrl, room);
|
2016-03-03 14:18:41 +01:00
|
|
|
|
2016-11-02 18:35:31 +01: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 11:59:40 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
_playAudioNotification: function(ev, room) {
|
2017-04-23 07:06:23 +02:00
|
|
|
const e = document.getElementById("messageAudio");
|
2016-03-10 11:59:40 +01:00
|
|
|
if (e) {
|
|
|
|
e.play();
|
2017-01-20 15:22:27 +01:00
|
|
|
}
|
2015-11-30 16:04:24 +01:00
|
|
|
},
|
|
|
|
|
2015-07-03 12:12:54 +02:00
|
|
|
start: function() {
|
2017-08-25 14:18:01 +02:00
|
|
|
this.boundOnEvent = this.onEvent.bind(this);
|
2015-12-15 18:01:16 +01:00
|
|
|
this.boundOnSyncStateChange = this.onSyncStateChange.bind(this);
|
2016-11-02 18:35:31 +01:00
|
|
|
this.boundOnRoomReceipt = this.onRoomReceipt.bind(this);
|
2017-08-24 15:27:38 +02:00
|
|
|
this.boundOnEventDecrypted = this.onEventDecrypted.bind(this);
|
2017-08-25 14:18:01 +02:00
|
|
|
MatrixClientPeg.get().on('event', this.boundOnEvent);
|
2017-04-23 07:06:23 +02:00
|
|
|
MatrixClientPeg.get().on('Room.receipt', this.boundOnRoomReceipt);
|
2017-08-24 15:27:38 +02:00
|
|
|
MatrixClientPeg.get().on('Event.decrypted', this.boundOnEventDecrypted);
|
2015-12-15 18:01:16 +01:00
|
|
|
MatrixClientPeg.get().on("sync", this.boundOnSyncStateChange);
|
2015-11-30 16:04:24 +01:00
|
|
|
this.toolbarHidden = false;
|
2017-03-28 16:02:08 +02:00
|
|
|
this.isSyncing = false;
|
2015-07-03 12:12:54 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function() {
|
2017-03-23 11:38:00 +01:00
|
|
|
if (MatrixClientPeg.get() && this.boundOnRoomTimeline) {
|
2017-08-25 14:18:01 +02:00
|
|
|
MatrixClientPeg.get().removeListener('Event', this.boundOnEvent);
|
2017-04-23 07:06:23 +02:00
|
|
|
MatrixClientPeg.get().removeListener('Room.receipt', this.boundOnRoomReceipt);
|
2017-08-24 15:27:38 +02:00
|
|
|
MatrixClientPeg.get().removeListener('Event.decrypted', this.boundOnEventDecrypted);
|
2015-12-15 18:01:16 +01:00
|
|
|
MatrixClientPeg.get().removeListener('sync', this.boundOnSyncStateChange);
|
2015-07-03 12:12:54 +02:00
|
|
|
}
|
2017-03-28 16:02:08 +02:00
|
|
|
this.isSyncing = false;
|
2015-07-03 12:12:54 +02:00
|
|
|
},
|
|
|
|
|
2015-09-16 15:48:49 +02:00
|
|
|
supportsDesktopNotifications: function() {
|
2016-11-02 18:35:31 +01:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
return plaf && plaf.supportsNotifications();
|
2015-09-16 15:48:49 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setEnabled: function(enable, callback) {
|
2016-11-02 18:35:31 +01:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return;
|
2017-05-29 20:15:52 +02:00
|
|
|
|
2017-12-25 21:27:23 +01:00
|
|
|
// Dev note: We don't set the "notificationsEnabled" setting to true here because it is a
|
|
|
|
// calculated value. It is determined based upon whether or not the master rule is enabled
|
|
|
|
// and other flags. Setting it here would cause a circular reference.
|
|
|
|
|
2017-05-29 20:15:52 +02:00
|
|
|
Analytics.trackEvent('Notifier', 'Set Enabled', enable);
|
|
|
|
|
2016-03-10 11:59:40 +01:00
|
|
|
// make sure that we persist the current setting audio_enabled setting
|
|
|
|
// before changing anything
|
2017-11-05 05:47:18 +01:00
|
|
|
if (SettingsStore.isLevelSupported(SettingLevel.DEVICE)) {
|
|
|
|
SettingsStore.setValue("audioNotificationsEnabled", null, SettingLevel.DEVICE, this.isEnabled());
|
2016-03-10 11:59:40 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:40:33 +01:00
|
|
|
if (enable) {
|
2016-03-23 12:56:41 +01:00
|
|
|
// Attempt to get permission from user
|
2016-11-02 18:35:31 +01:00
|
|
|
plaf.requestNotificationPermission().done((result) => {
|
2016-03-23 12:56:41 +01:00
|
|
|
if (result !== 'granted') {
|
|
|
|
// The permission request was dismissed or denied
|
2017-11-05 05:47:18 +01:00
|
|
|
// TODO: Support alternative branding in messaging
|
2017-04-23 07:16:25 +02:00
|
|
|
const description = result === 'denied'
|
2017-05-23 16:16:31 +02: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 07:16:25 +02:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
2017-07-27 18:19:18 +02:00
|
|
|
Modal.createTrackedDialog('Unable to enable Notifications', result, ErrorDialog, {
|
2017-05-23 16:16:31 +02:00
|
|
|
title: _t('Unable to enable Notifications'),
|
2017-04-23 07:16:25 +02:00
|
|
|
description,
|
|
|
|
});
|
2016-03-23 12:56:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) callback();
|
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 07:06:23 +02:00
|
|
|
value: true,
|
2015-09-16 15:48:49 +02:00
|
|
|
});
|
2016-03-23 12:56:41 +01:00
|
|
|
});
|
2016-03-21 23:19:46 +01:00
|
|
|
} else {
|
2015-09-16 15:48:49 +02:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 07:06:23 +02:00
|
|
|
value: false,
|
2015-09-16 15:48:49 +02:00
|
|
|
});
|
|
|
|
}
|
2018-07-02 00:48:00 +02:00
|
|
|
// set the notifications_hidden flag, as the user has knowingly interacted
|
|
|
|
// with the setting we shouldn't nag them any further
|
|
|
|
this.setToolbarHidden(true);
|
2015-09-16 15:48:49 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
isEnabled: function() {
|
2017-11-05 05:47:18 +01:00
|
|
|
return this.isPossible() && SettingsStore.getValue("notificationsEnabled");
|
|
|
|
},
|
|
|
|
|
|
|
|
isPossible: function() {
|
2016-11-02 18:35:31 +01:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return false;
|
|
|
|
if (!plaf.supportsNotifications()) return false;
|
|
|
|
if (!plaf.maySendNotifications()) return false;
|
2015-09-16 15:48:49 +02:00
|
|
|
|
2017-11-05 05:47:18 +01:00
|
|
|
return true; // possible, but not necessarily enabled
|
2017-09-06 11:56:08 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
isBodyEnabled: function() {
|
2017-11-05 05:47:18 +01:00
|
|
|
return this.isEnabled() && SettingsStore.getValue("notificationBodyEnabled");
|
2016-03-10 11:59:40 +01:00
|
|
|
},
|
|
|
|
|
2017-11-05 05:47:18 +01:00
|
|
|
isAudioEnabled: function() {
|
|
|
|
return this.isEnabled() && SettingsStore.getValue("audioNotificationsEnabled");
|
2016-03-10 11:59:40 +01:00
|
|
|
},
|
|
|
|
|
2016-03-23 09:20:24 +01:00
|
|
|
setToolbarHidden: function(hidden, persistent = true) {
|
2015-11-30 16:04:24 +01:00
|
|
|
this.toolbarHidden = hidden;
|
2016-09-09 03:09:12 +02:00
|
|
|
|
2017-05-29 20:15:52 +02:00
|
|
|
Analytics.trackEvent('Notifier', 'Set Toolbar Hidden', hidden);
|
|
|
|
|
2016-03-29 11:51:13 +02:00
|
|
|
// XXX: why are we dispatching this here?
|
|
|
|
// this is nothing to do with notifier_enabled
|
2015-09-16 15:48:49 +02:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 07:06:23 +02:00
|
|
|
value: this.isEnabled(),
|
2015-09-16 15:48:49 +02:00
|
|
|
});
|
2016-03-21 23:19:46 +01:00
|
|
|
|
2016-03-23 11:15:54 +01:00
|
|
|
// update the info to localStorage for persistent settings
|
2017-11-09 01:06:36 +01:00
|
|
|
if (persistent && global.localStorage) {
|
|
|
|
global.localStorage.setItem("notifications_hidden", hidden);
|
2016-03-21 23:19:46 +01:00
|
|
|
}
|
2015-09-16 15:48:49 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
isToolbarHidden: function() {
|
2016-03-23 12:56:41 +01:00
|
|
|
// Check localStorage for any such meta data
|
2017-11-09 01:06:36 +01:00
|
|
|
if (global.localStorage) {
|
|
|
|
return global.localStorage.getItem("notifications_hidden") === "true";
|
2016-03-23 12:56:41 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 16:04:24 +01:00
|
|
|
return this.toolbarHidden;
|
2015-09-16 15:48:49 +02:00
|
|
|
},
|
|
|
|
|
2015-12-15 18:01:16 +01:00
|
|
|
onSyncStateChange: function(state) {
|
2017-03-28 16:02:08 +02:00
|
|
|
if (state === "SYNCING") {
|
|
|
|
this.isSyncing = true;
|
2017-04-23 07:06:23 +02:00
|
|
|
} else if (state === "STOPPED" || state === "ERROR") {
|
2017-03-28 16:02:08 +02:00
|
|
|
this.isSyncing = false;
|
2016-03-17 12:34:20 +01:00
|
|
|
}
|
2015-12-15 18:01:16 +01:00
|
|
|
},
|
|
|
|
|
2017-08-25 14:18:01 +02:00
|
|
|
onEvent: function(ev) {
|
2017-03-28 16:02:08 +02:00
|
|
|
if (!this.isSyncing) return; // don't alert for any messages initially
|
2017-04-23 07:06:23 +02:00
|
|
|
if (ev.sender && ev.sender.userId === MatrixClientPeg.get().credentials.userId) return;
|
2015-07-03 12:12:54 +02:00
|
|
|
|
2017-08-24 15:27:38 +02:00
|
|
|
// If it's an encrypted event and the type is still 'm.room.encrypted',
|
|
|
|
// it hasn't yet been decrypted, so wait until it is.
|
2017-08-24 15:42:38 +02:00
|
|
|
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
|
2017-08-24 15:27:38 +02:00
|
|
|
this.pendingEncryptedEventIds.push(ev.getId());
|
|
|
|
// don't let the list fill up indefinitely
|
|
|
|
while (this.pendingEncryptedEventIds.length > MAX_PENDING_ENCRYPTED) {
|
|
|
|
this.pendingEncryptedEventIds.shift();
|
2016-03-10 11:59:40 +01:00
|
|
|
}
|
2017-08-24 15:27:38 +02:00
|
|
|
return;
|
2015-07-03 12:12:54 +02:00
|
|
|
}
|
2017-08-24 15:27:38 +02:00
|
|
|
|
|
|
|
this._evaluateEvent(ev);
|
|
|
|
},
|
|
|
|
|
|
|
|
onEventDecrypted: function(ev) {
|
2018-03-29 19:18:53 +02:00
|
|
|
// 'decrypted' means the decryption process has finished: it may have failed,
|
|
|
|
// in which case it might decrypt soon if the keys arrive
|
|
|
|
if (ev.isDecryptionFailure()) return;
|
|
|
|
|
2017-08-24 15:27:38 +02:00
|
|
|
const idx = this.pendingEncryptedEventIds.indexOf(ev.getId());
|
|
|
|
if (idx === -1) return;
|
|
|
|
|
|
|
|
this.pendingEncryptedEventIds.splice(idx, 1);
|
|
|
|
this._evaluateEvent(ev);
|
2016-11-02 18:35:31 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onRoomReceipt: function(ev, room) {
|
2017-04-23 07:06:23 +02:00
|
|
|
if (room.getUnreadNotificationCount() === 0) {
|
2016-11-02 18:35:31 +01: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 07:06:23 +02:00
|
|
|
},
|
2017-08-24 15:27:38 +02:00
|
|
|
|
|
|
|
_evaluateEvent: function(ev) {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(ev.getRoomId());
|
|
|
|
const actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
|
|
|
if (actions && actions.notify) {
|
|
|
|
if (this.isEnabled()) {
|
|
|
|
this._displayPopupNotification(ev, room);
|
|
|
|
}
|
|
|
|
if (actions.tweaks.sound && this.isAudioEnabled()) {
|
|
|
|
PlatformPeg.get().loudNotification(ev, room);
|
|
|
|
this._playAudioNotification(ev, room);
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 11:56:08 +02:00
|
|
|
},
|
2015-07-03 12:12:54 +02:00
|
|
|
};
|
|
|
|
|
2015-12-02 17:35:16 +01:00
|
|
|
if (!global.mxNotifier) {
|
|
|
|
global.mxNotifier = Notifier;
|
|
|
|
}
|
|
|
|
|
2016-03-04 16:29:33 +01:00
|
|
|
module.exports = global.mxNotifier;
|