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
|
2017-08-25 20:35:04 +08:00
|
|
|
Copyright 2017 New Vector Ltd
|
2020-07-11 02:07:11 +08:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
2020-07-11 02:07:11 +08:00
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
|
|
|
import SdkConfig from './SdkConfig';
|
2017-04-23 13:06:23 +08:00
|
|
|
import PlatformPeg from './PlatformPeg';
|
2019-12-20 09:19:56 +08:00
|
|
|
import * as TextForEvent from './TextForEvent';
|
2017-05-30 02:15:52 +08:00
|
|
|
import Analytics from './Analytics';
|
2019-12-20 09:19:56 +08:00
|
|
|
import * as Avatar from './Avatar';
|
2020-05-14 10:41:41 +08:00
|
|
|
import dis from './dispatcher/dispatcher';
|
2019-12-20 09:19:56 +08:00
|
|
|
import * as 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';
|
2020-07-29 01:53:43 +08:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2020-08-05 18:07:10 +08:00
|
|
|
import { hideToast as hideNotificationsToast } from "./toasts/DesktopNotificationsToast";
|
2020-07-29 01:53:43 +08:00
|
|
|
import {SettingLevel} from "./settings/SettingLevel";
|
2020-09-14 21:35:08 +08:00
|
|
|
import {isPushNotifyDisabled} from "./settings/controllers/NotificationControllers";
|
2020-10-15 22:58:18 +08:00
|
|
|
import RoomViewStore from "./stores/RoomViewStore";
|
|
|
|
import UserActivity from "./UserActivity";
|
2015-09-16 21:48:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatches:
|
|
|
|
* {
|
|
|
|
* action: "notifier_enabled",
|
|
|
|
* value: boolean
|
|
|
|
* }
|
|
|
|
*/
|
2015-07-03 18:12:54 +08:00
|
|
|
|
2017-08-24 21:27:38 +08:00
|
|
|
const MAX_PENDING_ENCRYPTED = 20;
|
|
|
|
|
2020-04-06 23:10:40 +08:00
|
|
|
/*
|
|
|
|
Override both the content body and the TextForEvent handler for specific msgtypes, in notifications.
|
|
|
|
This is useful when the content body contains fallback text that would explain that the client can't handle a particular
|
|
|
|
type of tile.
|
|
|
|
*/
|
|
|
|
const typehandlers = {
|
|
|
|
"m.key.verification.request": (event) => {
|
|
|
|
const name = (event.sender || {}).name;
|
|
|
|
return _t("%(name)s is requesting verification", { name });
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
export const Notifier = {
|
2016-11-03 01:35:31 +08:00
|
|
|
notifsByRoom: {},
|
2015-11-30 23:04:24 +08:00
|
|
|
|
2017-08-24 21:27:38 +08: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: [],
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
notificationMessageForEvent: function(ev: MatrixEvent) {
|
2020-04-06 23:10:40 +08:00
|
|
|
if (typehandlers.hasOwnProperty(ev.getContent().msgtype)) {
|
|
|
|
return typehandlers[ev.getContent().msgtype](ev);
|
|
|
|
}
|
2015-11-30 23:04:24 +08:00
|
|
|
return TextForEvent.textForEvent(ev);
|
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
_displayPopupNotification: function(ev: MatrixEvent, room: 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
|
2020-04-06 23:10:40 +08:00
|
|
|
if (ev.getContent().body && !typehandlers.hasOwnProperty(ev.getContent().msgtype)) {
|
|
|
|
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
|
2020-04-06 23:10:40 +08:00
|
|
|
if (ev.getContent().body && !typehandlers.hasOwnProperty(ev.getContent().msgtype)) {
|
|
|
|
msg = ev.getContent().body;
|
|
|
|
}
|
2015-11-30 23:04:24 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 17:56:08 +08:00
|
|
|
if (!this.isBodyEnabled()) {
|
|
|
|
msg = '';
|
|
|
|
}
|
2015-11-30 23:04:24 +08:00
|
|
|
|
2019-05-31 10:02:25 +08:00
|
|
|
let avatarUrl = null;
|
|
|
|
if (ev.sender && !SettingsStore.getValue("lowBandwidth")) {
|
|
|
|
avatarUrl = Avatar.avatarUrlForMember(ev.sender, 40, 40, 'crop');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
getSoundForRoom: function(roomId: string) {
|
2019-05-13 00:14:21 +08:00
|
|
|
// We do no caching here because the SDK caches setting
|
2019-04-19 19:36:32 +08:00
|
|
|
// and the browser will cache the sound.
|
2019-06-04 00:35:15 +08:00
|
|
|
const content = SettingsStore.getValue("notificationSound", roomId);
|
2019-04-20 05:31:51 +08:00
|
|
|
if (!content) {
|
2019-05-13 00:14:21 +08:00
|
|
|
return null;
|
2019-04-19 19:36:32 +08:00
|
|
|
}
|
2019-04-20 05:31:51 +08:00
|
|
|
|
2019-04-19 21:10:10 +08:00
|
|
|
if (!content.url) {
|
2019-04-20 05:31:51 +08:00
|
|
|
console.warn(`${roomId} has custom notification sound event, but no url key`);
|
2019-04-19 19:36:32 +08:00
|
|
|
return null;
|
2017-01-20 22:22:27 +08:00
|
|
|
}
|
2019-09-18 16:27:43 +08:00
|
|
|
|
2019-05-13 00:14:21 +08:00
|
|
|
if (!content.url.startsWith("mxc://")) {
|
|
|
|
console.warn(`${roomId} has custom notification sound event, but url is not a mxc url`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally in here we could use MSC1310 to detect the type of file, and reject it.
|
2019-04-20 04:42:18 +08:00
|
|
|
|
2019-04-19 21:10:10 +08:00
|
|
|
return {
|
|
|
|
url: MatrixClientPeg.get().mxcUrlToHttp(content.url),
|
2019-04-19 23:27:30 +08:00
|
|
|
name: content.name,
|
2019-04-19 21:10:10 +08:00
|
|
|
type: content.type,
|
2019-04-19 23:27:30 +08:00
|
|
|
size: content.size,
|
2019-04-19 21:10:10 +08:00
|
|
|
};
|
2019-04-19 19:36:32 +08:00
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
_playAudioNotification: async function(ev: MatrixEvent, room: Room) {
|
2020-06-25 15:43:35 +08:00
|
|
|
const sound = this.getSoundForRoom(room.roomId);
|
2019-04-22 01:05:27 +08:00
|
|
|
console.log(`Got sound ${sound && sound.name || "default"} for ${room.roomId}`);
|
2019-05-13 00:14:21 +08:00
|
|
|
|
2019-04-22 01:01:26 +08:00
|
|
|
try {
|
2020-08-05 18:07:10 +08:00
|
|
|
const selector =
|
|
|
|
document.querySelector<HTMLAudioElement>(sound ? `audio[src='${sound.url}']` : "#messageAudio");
|
2019-04-19 20:21:58 +08:00
|
|
|
let audioElement = selector;
|
2019-04-19 19:36:32 +08:00
|
|
|
if (!selector) {
|
2019-04-19 21:10:10 +08:00
|
|
|
if (!sound) {
|
2019-06-04 04:19:17 +08:00
|
|
|
console.error("No audio element or sound to play for notification");
|
2019-04-19 21:10:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
audioElement = new Audio(sound.url);
|
|
|
|
if (sound.type) {
|
|
|
|
audioElement.type = sound.type;
|
2019-04-19 19:36:32 +08:00
|
|
|
}
|
2019-04-19 20:21:58 +08:00
|
|
|
document.body.appendChild(audioElement);
|
2019-04-19 19:36:32 +08:00
|
|
|
}
|
2019-11-22 01:00:35 +08:00
|
|
|
await audioElement.play();
|
2019-04-22 01:01:26 +08:00
|
|
|
} catch (ex) {
|
2019-04-19 21:10:10 +08:00
|
|
|
console.warn("Caught error when trying to fetch room notification sound:", ex);
|
2019-04-22 01:01:26 +08:00
|
|
|
}
|
2015-11-30 23:04:24 +08:00
|
|
|
},
|
|
|
|
|
2015-07-03 18:12:54 +08:00
|
|
|
start: function() {
|
2020-02-20 10:35:30 +08:00
|
|
|
// do not re-bind in the case of repeated call
|
|
|
|
this.boundOnEvent = this.boundOnEvent || this.onEvent.bind(this);
|
|
|
|
this.boundOnSyncStateChange = this.boundOnSyncStateChange || this.onSyncStateChange.bind(this);
|
|
|
|
this.boundOnRoomReceipt = this.boundOnRoomReceipt || this.onRoomReceipt.bind(this);
|
|
|
|
this.boundOnEventDecrypted = this.boundOnEventDecrypted || this.onEventDecrypted.bind(this);
|
|
|
|
|
2017-08-25 20:18:01 +08:00
|
|
|
MatrixClientPeg.get().on('event', this.boundOnEvent);
|
2017-04-23 13:06:23 +08:00
|
|
|
MatrixClientPeg.get().on('Room.receipt', this.boundOnRoomReceipt);
|
2017-08-24 21:27:38 +08:00
|
|
|
MatrixClientPeg.get().on('Event.decrypted', this.boundOnEventDecrypted);
|
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() {
|
2020-02-20 10:35:30 +08:00
|
|
|
if (MatrixClientPeg.get()) {
|
2017-08-25 20:18:01 +08:00
|
|
|
MatrixClientPeg.get().removeListener('Event', this.boundOnEvent);
|
2017-04-23 13:06:23 +08:00
|
|
|
MatrixClientPeg.get().removeListener('Room.receipt', this.boundOnRoomReceipt);
|
2017-08-24 21:27:38 +08:00
|
|
|
MatrixClientPeg.get().removeListener('Event.decrypted', this.boundOnEventDecrypted);
|
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
|
|
|
},
|
|
|
|
|
2020-08-05 18:22:43 +08:00
|
|
|
setEnabled: function(enable: boolean, callback?: () => void) {
|
2016-11-03 01:35:31 +08:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return;
|
2017-05-30 02:15:52 +08:00
|
|
|
|
2017-12-26 04:27:23 +08: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.
|
|
|
|
|
2020-10-14 05:58:21 +08:00
|
|
|
Analytics.trackEvent('Notifier', 'Set Enabled', String(enable));
|
2017-05-30 02:15:52 +08:00
|
|
|
|
2016-03-10 18:59:40 +08:00
|
|
|
// make sure that we persist the current setting audio_enabled setting
|
|
|
|
// before changing anything
|
2017-11-05 12:47:18 +08:00
|
|
|
if (SettingsStore.isLevelSupported(SettingLevel.DEVICE)) {
|
|
|
|
SettingsStore.setValue("audioNotificationsEnabled", null, SettingLevel.DEVICE, this.isEnabled());
|
2016-03-10 18:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:40:33 +08:00
|
|
|
if (enable) {
|
2016-03-23 19:56:41 +08:00
|
|
|
// Attempt to get permission from user
|
2019-11-18 18:03:05 +08:00
|
|
|
plaf.requestNotificationPermission().then((result) => {
|
2016-03-23 19:56:41 +08:00
|
|
|
if (result !== 'granted') {
|
|
|
|
// The permission request was dismissed or denied
|
2017-11-05 12:47:18 +08:00
|
|
|
// TODO: Support alternative branding in messaging
|
2020-07-11 02:07:11 +08:00
|
|
|
const brand = SdkConfig.get().brand;
|
2017-04-23 13:16:25 +08:00
|
|
|
const description = result === 'denied'
|
2020-07-11 02:07:11 +08:00
|
|
|
? _t('%(brand)s does not have permission to send you notifications - ' +
|
|
|
|
'please check your browser settings', { brand })
|
|
|
|
: _t('%(brand)s was not given permission to send notifications - please try again', { brand });
|
2017-04-23 13:16:25 +08:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
2017-07-28 00:19:18 +08:00
|
|
|
Modal.createTrackedDialog('Unable to enable Notifications', result, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-22 06:19:46 +08:00
|
|
|
} else {
|
2015-09-16 21:48:49 +08:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 13:06:23 +08:00
|
|
|
value: false,
|
2015-09-16 21:48:49 +08:00
|
|
|
});
|
|
|
|
}
|
2018-07-02 06:48:00 +08:00
|
|
|
// set the notifications_hidden flag, as the user has knowingly interacted
|
|
|
|
// with the setting we shouldn't nag them any further
|
2020-09-15 20:58:29 +08:00
|
|
|
this.setPromptHidden(true);
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
isEnabled: function() {
|
2017-11-05 12:47:18 +08:00
|
|
|
return this.isPossible() && SettingsStore.getValue("notificationsEnabled");
|
|
|
|
},
|
|
|
|
|
|
|
|
isPossible: 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
|
|
|
|
2017-11-05 12:47:18 +08:00
|
|
|
return true; // possible, but not necessarily enabled
|
2017-09-06 17:56:08 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
isBodyEnabled: function() {
|
2017-11-05 12:47:18 +08:00
|
|
|
return this.isEnabled() && SettingsStore.getValue("notificationBodyEnabled");
|
2016-03-10 18:59:40 +08:00
|
|
|
},
|
|
|
|
|
2017-11-05 12:47:18 +08:00
|
|
|
isAudioEnabled: function() {
|
2020-08-05 18:10:12 +08:00
|
|
|
// We don't route Audio via the HTML Notifications API so it is possible regardless of other things
|
|
|
|
return SettingsStore.getValue("audioNotificationsEnabled");
|
2016-03-10 18:59:40 +08:00
|
|
|
},
|
|
|
|
|
2020-09-15 20:58:29 +08:00
|
|
|
setPromptHidden: function(hidden: boolean, persistent = true) {
|
2015-11-30 23:04:24 +08:00
|
|
|
this.toolbarHidden = hidden;
|
2016-09-09 09:09:12 +08:00
|
|
|
|
2020-10-14 05:58:21 +08:00
|
|
|
Analytics.trackEvent('Notifier', 'Set Toolbar Hidden', String(hidden));
|
2017-05-30 02:15:52 +08:00
|
|
|
|
2020-05-23 04:56:25 +08:00
|
|
|
hideNotificationsToast();
|
2016-03-22 06:19:46 +08:00
|
|
|
|
2016-03-23 18:15:54 +08:00
|
|
|
// update the info to localStorage for persistent settings
|
2017-11-09 08:06:36 +08:00
|
|
|
if (persistent && global.localStorage) {
|
2020-08-05 18:07:10 +08:00
|
|
|
global.localStorage.setItem("notifications_hidden", String(hidden));
|
2016-03-22 06:19:46 +08:00
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
2020-09-15 20:58:29 +08:00
|
|
|
shouldShowPrompt: function() {
|
2019-03-13 00:29:16 +08:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const isGuest = client.isGuest();
|
2020-09-14 21:35:08 +08:00
|
|
|
return !isGuest && this.supportsDesktopNotifications() && !isPushNotifyDisabled() &&
|
2020-09-15 20:58:29 +08:00
|
|
|
!this.isEnabled() && !this._isPromptHidden();
|
2019-03-13 00:29:16 +08:00
|
|
|
},
|
|
|
|
|
2020-09-15 20:58:29 +08:00
|
|
|
_isPromptHidden: function() {
|
2016-03-23 19:56:41 +08:00
|
|
|
// Check localStorage for any such meta data
|
2017-11-09 08:06:36 +08:00
|
|
|
if (global.localStorage) {
|
|
|
|
return global.localStorage.getItem("notifications_hidden") === "true";
|
2016-03-23 19:56:41 +08:00
|
|
|
}
|
|
|
|
|
2015-11-30 23:04:24 +08:00
|
|
|
return this.toolbarHidden;
|
2015-09-16 21:48:49 +08:00
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
onSyncStateChange: function(state: string) {
|
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
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
onEvent: function(ev: MatrixEvent) {
|
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;
|
2015-07-03 18:12:54 +08:00
|
|
|
|
2017-08-24 21:27:38 +08: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 21:42:38 +08:00
|
|
|
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
|
2017-08-24 21:27:38 +08: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 18:59:40 +08:00
|
|
|
}
|
2017-08-24 21:27:38 +08:00
|
|
|
return;
|
2015-07-03 18:12:54 +08:00
|
|
|
}
|
2017-08-24 21:27:38 +08:00
|
|
|
|
|
|
|
this._evaluateEvent(ev);
|
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
onEventDecrypted: function(ev: MatrixEvent) {
|
2018-03-30 01:18:53 +08: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 21:27:38 +08:00
|
|
|
const idx = this.pendingEncryptedEventIds.indexOf(ev.getId());
|
|
|
|
if (idx === -1) return;
|
|
|
|
|
|
|
|
this.pendingEncryptedEventIds.splice(idx, 1);
|
|
|
|
this._evaluateEvent(ev);
|
2016-11-03 01:35:31 +08:00
|
|
|
},
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
onRoomReceipt: function(ev: MatrixEvent, room: 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
|
|
|
},
|
2017-08-24 21:27:38 +08:00
|
|
|
|
|
|
|
_evaluateEvent: function(ev) {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(ev.getRoomId());
|
|
|
|
const actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
|
|
|
if (actions && actions.notify) {
|
2020-10-15 22:58:18 +08:00
|
|
|
if (RoomViewStore.getRoomId() === room.roomId && UserActivity.sharedInstance().userActiveRecently()) {
|
|
|
|
// don't bother notifying as user was recently active in this room
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:27:38 +08:00
|
|
|
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 17:56:08 +08:00
|
|
|
},
|
2015-07-03 18:12:54 +08:00
|
|
|
};
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
if (!window.mxNotifier) {
|
|
|
|
window.mxNotifier = Notifier;
|
2015-12-03 00:35:16 +08:00
|
|
|
}
|
|
|
|
|
2020-08-05 18:07:10 +08:00
|
|
|
export default window.mxNotifier;
|