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
|
2020-07-10 20:07:11 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-02-22 13:18:08 +01:00
|
|
|
import { MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { Room, RoomEvent } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { ClientEvent } from "matrix-js-sdk/src/client";
|
2021-12-09 10:10:23 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
import { MsgType } from "matrix-js-sdk/src/@types/event";
|
2022-03-10 19:03:31 +01:00
|
|
|
import { M_LOCATION } from "matrix-js-sdk/src/@types/location";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { PermissionChanged as PermissionChangedEvent } from "@matrix-org/analytics-events/types/typescript/PermissionChanged";
|
2022-10-10 16:18:38 +02:00
|
|
|
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
|
2022-12-01 13:21:56 +01:00
|
|
|
import { IRoomTimelineData } from "matrix-js-sdk/src/matrix";
|
2020-08-05 12:07:10 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2022-07-29 13:43:29 +02:00
|
|
|
import { PosthogAnalytics } from "./PosthogAnalytics";
|
2022-12-12 12:24:14 +01:00
|
|
|
import SdkConfig from "./SdkConfig";
|
|
|
|
import PlatformPeg from "./PlatformPeg";
|
|
|
|
import * as TextForEvent from "./TextForEvent";
|
|
|
|
import * as Avatar from "./Avatar";
|
|
|
|
import dis from "./dispatcher/dispatcher";
|
|
|
|
import { _t } from "./languageHandler";
|
|
|
|
import Modal from "./Modal";
|
2020-07-28 19:53:43 +02:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2020-08-05 12:07:10 +02:00
|
|
|
import { hideToast as hideNotificationsToast } from "./toasts/DesktopNotificationsToast";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { SettingLevel } from "./settings/SettingLevel";
|
|
|
|
import { isPushNotifyDisabled } from "./settings/controllers/NotificationControllers";
|
2020-10-15 16:58:18 +02:00
|
|
|
import UserActivity from "./UserActivity";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { mediaFromMxc } from "./customisations/Media";
|
2021-07-02 12:12:41 +02:00
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
2022-08-30 21:13:39 +02:00
|
|
|
import LegacyCallHandler from "./LegacyCallHandler";
|
2022-02-03 12:31:32 +01:00
|
|
|
import VoipUserMapper from "./VoipUserMapper";
|
2022-10-19 14:07:03 +02:00
|
|
|
import { SdkContextClass } from "./contexts/SDKContext";
|
2022-11-07 14:45:34 +01:00
|
|
|
import { localNotificationsAreSilenced, createLocalNotificationSettingsIfNeeded } from "./utils/notifications";
|
2022-10-06 16:27:12 +02:00
|
|
|
import { getIncomingCallToastKey, IncomingCallToast } from "./toasts/IncomingCallToast";
|
|
|
|
import ToastStore from "./stores/ToastStore";
|
|
|
|
import { ElementCall } from "./models/Call";
|
2023-01-03 10:02:28 +01:00
|
|
|
import { VoiceBroadcastChunkEventType } from "./voice-broadcast";
|
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;
|
|
|
|
|
2020-04-06 17:10:40 +02: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.
|
|
|
|
*/
|
2021-11-22 18:17:05 +01:00
|
|
|
const msgTypeHandlers = {
|
2022-01-19 01:58:31 +01:00
|
|
|
[MsgType.KeyVerificationRequest]: (event: MatrixEvent) => {
|
2020-04-06 17:10:40 +02:00
|
|
|
const name = (event.sender || {}).name;
|
|
|
|
return _t("%(name)s is requesting verification", { name });
|
|
|
|
},
|
2022-03-10 19:03:31 +01:00
|
|
|
[M_LOCATION.name]: (event: MatrixEvent) => {
|
2022-01-19 01:58:31 +01:00
|
|
|
return TextForEvent.textForLocationEvent(event)();
|
|
|
|
},
|
2022-03-10 19:03:31 +01:00
|
|
|
[M_LOCATION.altName]: (event: MatrixEvent) => {
|
2022-01-19 01:58:31 +01:00
|
|
|
return TextForEvent.textForLocationEvent(event)();
|
|
|
|
},
|
2023-01-03 10:02:28 +01:00
|
|
|
[MsgType.Audio]: (event: MatrixEvent): string | null => {
|
|
|
|
if (event.getContent()?.[VoiceBroadcastChunkEventType]) {
|
|
|
|
// mute broadcast chunks
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return TextForEvent.textForEvent(event);
|
|
|
|
},
|
2020-04-06 17:10:40 +02:00
|
|
|
};
|
|
|
|
|
2020-08-05 12:07:10 +02:00
|
|
|
export 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: [],
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
notificationMessageForEvent: function (ev: MatrixEvent): string {
|
2021-11-22 18:17:05 +01:00
|
|
|
if (msgTypeHandlers.hasOwnProperty(ev.getContent().msgtype)) {
|
|
|
|
return msgTypeHandlers[ev.getContent().msgtype](ev);
|
2020-04-06 17:10:40 +02:00
|
|
|
}
|
2015-11-30 16:04:24 +01:00
|
|
|
return TextForEvent.textForEvent(ev);
|
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
_displayPopupNotification: function (ev: MatrixEvent, room: Room): void {
|
2016-11-02 18:35:31 +01:00
|
|
|
const plaf = PlatformPeg.get();
|
2022-09-29 16:23:02 +02:00
|
|
|
const cli = MatrixClientPeg.get();
|
2016-11-02 18:35:31 +01:00
|
|
|
if (!plaf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) {
|
2015-11-30 16:04:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-29 16:23:02 +02:00
|
|
|
if (localNotificationsAreSilenced(cli)) {
|
|
|
|
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
|
2021-11-22 18:17:05 +01:00
|
|
|
if (ev.getContent().body && !msgTypeHandlers.hasOwnProperty(ev.getContent().msgtype)) {
|
2020-04-06 17:10:40 +02:00
|
|
|
msg = ev.getContent().body;
|
|
|
|
}
|
2022-12-12 12:24:14 +01: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
|
2021-11-22 18:17:05 +01:00
|
|
|
if (ev.getContent().body && !msgTypeHandlers.hasOwnProperty(ev.getContent().msgtype)) {
|
2020-04-06 17:10:40 +02:00
|
|
|
msg = ev.getContent().body;
|
|
|
|
}
|
2015-11-30 16:04:24 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 11:56:08 +02:00
|
|
|
if (!this.isBodyEnabled()) {
|
2022-12-12 12:24:14 +01:00
|
|
|
msg = "";
|
2017-09-06 11:56:08 +02:00
|
|
|
}
|
2015-11-30 16:04:24 +01:00
|
|
|
|
2019-05-31 04:02:25 +02:00
|
|
|
let avatarUrl = null;
|
|
|
|
if (ev.sender && !SettingsStore.getValue("lowBandwidth")) {
|
2022-12-12 12:24:14 +01:00
|
|
|
avatarUrl = Avatar.avatarUrlForMember(ev.sender, 40, 40, "crop");
|
2019-05-31 04:02:25 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 09:34:52 +01:00
|
|
|
const notif = plaf.displayNotification(title, msg, avatarUrl, room, ev);
|
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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
getSoundForRoom: function (roomId: string) {
|
2019-05-12 18:14:21 +02:00
|
|
|
// We do no caching here because the SDK caches setting
|
2019-04-19 13:36:32 +02:00
|
|
|
// and the browser will cache the sound.
|
2019-06-03 18:35:15 +02:00
|
|
|
const content = SettingsStore.getValue("notificationSound", roomId);
|
2019-04-19 23:31:51 +02:00
|
|
|
if (!content) {
|
2019-05-12 18:14:21 +02:00
|
|
|
return null;
|
2019-04-19 13:36:32 +02:00
|
|
|
}
|
2019-04-19 23:31:51 +02:00
|
|
|
|
2022-11-07 12:56:43 +01:00
|
|
|
if (typeof content.url !== "string") {
|
|
|
|
logger.warn(`${roomId} has custom notification sound event, but no url string`);
|
2019-04-19 13:36:32 +02:00
|
|
|
return null;
|
2017-01-20 15:22:27 +01:00
|
|
|
}
|
2019-09-18 10:27:43 +02:00
|
|
|
|
2019-05-12 18:14:21 +02:00
|
|
|
if (!content.url.startsWith("mxc://")) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn(`${roomId} has custom notification sound event, but url is not a mxc url`);
|
2019-05-12 18:14:21 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally in here we could use MSC1310 to detect the type of file, and reject it.
|
2019-04-19 22:42:18 +02:00
|
|
|
|
2019-04-19 15:10:10 +02:00
|
|
|
return {
|
2021-03-04 03:06:46 +01:00
|
|
|
url: mediaFromMxc(content.url).srcHttp,
|
2019-04-19 17:27:30 +02:00
|
|
|
name: content.name,
|
2019-04-19 15:10:10 +02:00
|
|
|
type: content.type,
|
2019-04-19 17:27:30 +02:00
|
|
|
size: content.size,
|
2019-04-19 15:10:10 +02:00
|
|
|
};
|
2019-04-19 13:36:32 +02:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
_playAudioNotification: async function (ev: MatrixEvent, room: Room): Promise<void> {
|
2022-09-29 16:23:02 +02:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
if (localNotificationsAreSilenced(cli)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:43:35 +02:00
|
|
|
const sound = this.getSoundForRoom(room.roomId);
|
2022-12-12 12:24:14 +01:00
|
|
|
logger.log(`Got sound ${(sound && sound.name) || "default"} for ${room.roomId}`);
|
2019-05-12 18:14:21 +02:00
|
|
|
|
2019-04-21 19:01:26 +02:00
|
|
|
try {
|
2022-12-12 12:24:14 +01:00
|
|
|
const selector = document.querySelector<HTMLAudioElement>(
|
|
|
|
sound ? `audio[src='${sound.url}']` : "#messageAudio",
|
|
|
|
);
|
2019-04-19 14:21:58 +02:00
|
|
|
let audioElement = selector;
|
2019-04-19 13:36:32 +02:00
|
|
|
if (!selector) {
|
2019-04-19 15:10:10 +02:00
|
|
|
if (!sound) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("No audio element or sound to play for notification");
|
2019-04-19 15:10:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
audioElement = new Audio(sound.url);
|
|
|
|
if (sound.type) {
|
|
|
|
audioElement.type = sound.type;
|
2019-04-19 13:36:32 +02:00
|
|
|
}
|
2019-04-19 14:21:58 +02:00
|
|
|
document.body.appendChild(audioElement);
|
2019-04-19 13:36:32 +02:00
|
|
|
}
|
2019-11-21 18:00:35 +01:00
|
|
|
await audioElement.play();
|
2019-04-21 19:01:26 +02:00
|
|
|
} catch (ex) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("Caught error when trying to fetch room notification sound:", ex);
|
2019-04-21 19:01:26 +02:00
|
|
|
}
|
2015-11-30 16:04:24 +01:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
start: function (this: typeof Notifier) {
|
2020-02-20 03:35:30 +01: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);
|
|
|
|
|
2022-12-01 13:21:56 +01:00
|
|
|
MatrixClientPeg.get().on(RoomEvent.Timeline, this.boundOnEvent);
|
2022-02-22 13:18:08 +01:00
|
|
|
MatrixClientPeg.get().on(RoomEvent.Receipt, this.boundOnRoomReceipt);
|
|
|
|
MatrixClientPeg.get().on(MatrixEventEvent.Decrypted, this.boundOnEventDecrypted);
|
|
|
|
MatrixClientPeg.get().on(ClientEvent.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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
stop: function (this: typeof Notifier) {
|
2020-02-20 03:35:30 +01:00
|
|
|
if (MatrixClientPeg.get()) {
|
2022-12-01 13:21:56 +01:00
|
|
|
MatrixClientPeg.get().removeListener(RoomEvent.Timeline, this.boundOnEvent);
|
2022-02-22 13:18:08 +01:00
|
|
|
MatrixClientPeg.get().removeListener(RoomEvent.Receipt, this.boundOnRoomReceipt);
|
|
|
|
MatrixClientPeg.get().removeListener(MatrixEventEvent.Decrypted, this.boundOnEventDecrypted);
|
|
|
|
MatrixClientPeg.get().removeListener(ClientEvent.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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01: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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
setEnabled: function (enable: boolean, callback?: () => void) {
|
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.
|
|
|
|
|
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
|
2019-11-18 11:03:05 +01:00
|
|
|
plaf.requestNotificationPermission().then((result) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
if (result !== "granted") {
|
2016-03-23 12:56:41 +01:00
|
|
|
// The permission request was dismissed or denied
|
2017-11-05 05:47:18 +01:00
|
|
|
// TODO: Support alternative branding in messaging
|
2020-07-10 20:07:11 +02:00
|
|
|
const brand = SdkConfig.get().brand;
|
2022-12-12 12:24:14 +01:00
|
|
|
const description =
|
|
|
|
result === "denied"
|
|
|
|
? _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,
|
|
|
|
});
|
2022-06-14 18:51:51 +02:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2022-12-12 12:24:14 +01: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();
|
2022-07-29 13:43:29 +02:00
|
|
|
|
|
|
|
PosthogAnalytics.instance.trackEvent<PermissionChangedEvent>({
|
|
|
|
eventName: "PermissionChanged",
|
|
|
|
permission: "Notification",
|
|
|
|
granted: true,
|
|
|
|
});
|
2016-03-23 12:56:41 +01:00
|
|
|
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 {
|
2022-07-29 13:43:29 +02:00
|
|
|
PosthogAnalytics.instance.trackEvent<PermissionChangedEvent>({
|
|
|
|
eventName: "PermissionChanged",
|
|
|
|
permission: "Notification",
|
|
|
|
granted: false,
|
|
|
|
});
|
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
|
2020-09-15 14:58:29 +02:00
|
|
|
this.setPromptHidden(true);
|
2015-09-16 15:48:49 +02:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
isEnabled: function () {
|
2017-11-05 05:47:18 +01:00
|
|
|
return this.isPossible() && SettingsStore.getValue("notificationsEnabled");
|
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01: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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
isAudioEnabled: function () {
|
2020-08-05 12:10:12 +02: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 11:59:40 +01:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
setPromptHidden: function (this: typeof Notifier, hidden: boolean, persistent = true) {
|
2015-11-30 16:04:24 +01:00
|
|
|
this.toolbarHidden = hidden;
|
2016-09-09 03:09:12 +02:00
|
|
|
|
2020-05-22 22:56:25 +02:00
|
|
|
hideNotificationsToast();
|
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) {
|
2020-08-05 12:07:10 +02:00
|
|
|
global.localStorage.setItem("notifications_hidden", String(hidden));
|
2016-03-21 23:19:46 +01:00
|
|
|
}
|
2015-09-16 15:48:49 +02:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
shouldShowPrompt: function () {
|
2019-03-12 17:29:16 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const isGuest = client.isGuest();
|
2022-12-12 12:24:14 +01:00
|
|
|
return (
|
|
|
|
!isGuest &&
|
|
|
|
this.supportsDesktopNotifications() &&
|
|
|
|
!isPushNotifyDisabled() &&
|
|
|
|
!this.isEnabled() &&
|
|
|
|
!this._isPromptHidden()
|
|
|
|
);
|
2019-03-12 17:29:16 +01:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
_isPromptHidden: function (this: typeof Notifier) {
|
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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
onSyncStateChange: function (
|
|
|
|
this: typeof Notifier,
|
|
|
|
state: SyncState,
|
|
|
|
prevState?: SyncState,
|
|
|
|
data?: ISyncStateData,
|
|
|
|
) {
|
2022-10-10 16:18:38 +02:00
|
|
|
if (state === SyncState.Syncing) {
|
2017-03-28 16:02:08 +02:00
|
|
|
this.isSyncing = true;
|
2022-10-10 16:18:38 +02:00
|
|
|
} else if (state === SyncState.Stopped || state === SyncState.Error) {
|
2017-03-28 16:02:08 +02:00
|
|
|
this.isSyncing = false;
|
2016-03-17 12:34:20 +01:00
|
|
|
}
|
2022-10-10 16:18:38 +02:00
|
|
|
|
|
|
|
// wait for first non-cached sync to complete
|
2022-12-12 12:24:14 +01:00
|
|
|
if (![SyncState.Stopped, SyncState.Error].includes(state) && !data?.fromCache) {
|
2022-10-10 16:18:38 +02:00
|
|
|
createLocalNotificationSettingsIfNeeded(MatrixClientPeg.get());
|
|
|
|
}
|
2015-12-15 18:01:16 +01:00
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
onEvent: function (
|
2022-12-01 13:21:56 +01:00
|
|
|
this: typeof Notifier,
|
|
|
|
ev: MatrixEvent,
|
|
|
|
room: Room | undefined,
|
|
|
|
toStartOfTimeline: boolean | undefined,
|
|
|
|
removed: boolean,
|
|
|
|
data: IRoomTimelineData,
|
|
|
|
) {
|
|
|
|
if (!data.liveEvent) return; // only notify for new things, not old.
|
2017-03-28 16:02:08 +02:00
|
|
|
if (!this.isSyncing) return; // don't alert for any messages initially
|
2022-10-06 16:27:12 +02:00
|
|
|
if (ev.getSender() === MatrixClientPeg.get().getUserId()) return;
|
2015-07-03 12:12:54 +02:00
|
|
|
|
2021-05-18 17:24:38 +02:00
|
|
|
MatrixClientPeg.get().decryptEventIfNeeded(ev);
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
onEventDecrypted: function (ev: MatrixEvent) {
|
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
|
|
|
},
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
onRoomReceipt: function (ev: MatrixEvent, room: 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
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
_evaluateEvent: function (ev: MatrixEvent) {
|
2022-02-03 12:31:32 +01:00
|
|
|
let roomId = ev.getRoomId();
|
2022-08-30 21:13:39 +02:00
|
|
|
if (LegacyCallHandler.instance.getSupportsVirtualRooms()) {
|
2022-02-03 12:31:32 +01:00
|
|
|
// Attempt to translate a virtual room to a native one
|
|
|
|
const nativeRoomId = VoipUserMapper.sharedInstance().nativeRoomForVirtualRoom(roomId);
|
|
|
|
if (nativeRoomId) {
|
|
|
|
roomId = nativeRoomId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
2022-12-01 13:21:56 +01:00
|
|
|
if (!room) {
|
|
|
|
// e.g we are in the process of joining a room.
|
|
|
|
// Seen in the cypress lazy-loading test.
|
|
|
|
return;
|
|
|
|
}
|
2022-02-03 12:31:32 +01:00
|
|
|
|
2017-08-24 15:27:38 +02:00
|
|
|
const actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
2022-10-13 09:07:48 +02:00
|
|
|
|
2021-12-15 09:34:52 +01:00
|
|
|
if (actions?.notify) {
|
2022-10-06 16:27:12 +02:00
|
|
|
this._performCustomEventHandling(ev);
|
|
|
|
|
2022-10-25 18:53:31 +02:00
|
|
|
const store = SdkContextClass.instance.roomViewStore;
|
|
|
|
const isViewingRoom = store.getRoomId() === room.roomId;
|
2022-12-12 12:24:14 +01:00
|
|
|
const threadId: string | undefined = ev.getId() !== ev.threadRootId ? ev.threadRootId : undefined;
|
2022-10-25 18:53:31 +02:00
|
|
|
const isViewingThread = store.getThreadId() === threadId;
|
|
|
|
|
|
|
|
const isViewingEventTimeline = isViewingRoom && (!threadId || isViewingThread);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
if (isViewingEventTimeline && UserActivity.sharedInstance().userActiveRecently() && !Modal.hasDialogs()) {
|
2020-10-15 16:58:18 +02:00
|
|
|
// don't bother notifying as user was recently active in this room
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-24 15:27:38 +02: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 11:56:08 +02:00
|
|
|
},
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Some events require special handling such as showing in-app toasts
|
|
|
|
*/
|
2022-12-12 12:24:14 +01:00
|
|
|
_performCustomEventHandling: function (ev: MatrixEvent) {
|
|
|
|
if (ElementCall.CALL_EVENT_TYPE.names.includes(ev.getType()) && SettingsStore.getValue("feature_group_calls")) {
|
2022-10-06 16:27:12 +02:00
|
|
|
ToastStore.sharedInstance().addOrReplaceToast({
|
|
|
|
key: getIncomingCallToastKey(ev.getStateKey()),
|
|
|
|
priority: 100,
|
|
|
|
component: IncomingCallToast,
|
|
|
|
bodyClassName: "mx_IncomingCallToast",
|
|
|
|
props: { callEvent: ev },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2015-07-03 12:12:54 +02:00
|
|
|
};
|
|
|
|
|
2020-08-05 12:07:10 +02:00
|
|
|
if (!window.mxNotifier) {
|
|
|
|
window.mxNotifier = Notifier;
|
2015-12-02 17:35:16 +01:00
|
|
|
}
|
|
|
|
|
2020-08-05 12:07:10 +02:00
|
|
|
export default window.mxNotifier;
|