2015-07-03 10:12:54 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-03-23 10:38:00 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-08-25 12:35:04 +00:00
|
|
|
Copyright 2017 New Vector Ltd
|
2020-07-10 18:07:11 +00:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2015-07-03 10:12:54 +00: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.
|
|
|
|
*/
|
|
|
|
|
2023-08-04 07:36:16 +00:00
|
|
|
import {
|
|
|
|
MatrixEvent,
|
|
|
|
MatrixEventEvent,
|
|
|
|
Room,
|
|
|
|
RoomEvent,
|
|
|
|
ClientEvent,
|
|
|
|
MsgType,
|
|
|
|
SyncState,
|
|
|
|
SyncStateData,
|
|
|
|
IRoomTimelineData,
|
2023-08-23 09:04:25 +00:00
|
|
|
M_LOCATION,
|
2023-08-04 07:36:16 +00:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2021-12-09 09:10:23 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-07-29 11:43:29 +00:00
|
|
|
import { PermissionChanged as PermissionChangedEvent } from "@matrix-org/analytics-events/types/typescript/PermissionChanged";
|
2020-08-05 10:07:10 +00:00
|
|
|
|
2020-07-10 18:07:11 +00:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
2022-07-29 11:43:29 +00:00
|
|
|
import { PosthogAnalytics } from "./PosthogAnalytics";
|
2020-07-10 18:07:11 +00:00
|
|
|
import SdkConfig from "./SdkConfig";
|
2017-04-23 05:06:23 +00:00
|
|
|
import PlatformPeg from "./PlatformPeg";
|
2019-12-20 01:19:56 +00:00
|
|
|
import * as TextForEvent from "./TextForEvent";
|
|
|
|
import * as Avatar from "./Avatar";
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2017-05-25 10:39:08 +00:00
|
|
|
import { _t } from "./languageHandler";
|
2017-04-23 05:16:25 +00:00
|
|
|
import Modal from "./Modal";
|
2020-07-28 17:53:43 +00:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2020-08-05 10:07:10 +00:00
|
|
|
import { hideToast as hideNotificationsToast } from "./toasts/DesktopNotificationsToast";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { SettingLevel } from "./settings/SettingLevel";
|
|
|
|
import { isPushNotifyDisabled } from "./settings/controllers/NotificationControllers";
|
2020-10-15 14:58:18 +00:00
|
|
|
import UserActivity from "./UserActivity";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { mediaFromMxc } from "./customisations/Media";
|
2021-07-02 10:12:41 +00:00
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
2022-08-30 19:13:39 +00:00
|
|
|
import LegacyCallHandler from "./LegacyCallHandler";
|
2022-02-03 11:31:32 +00:00
|
|
|
import VoipUserMapper from "./VoipUserMapper";
|
2022-10-19 12:07:03 +00:00
|
|
|
import { SdkContextClass } from "./contexts/SDKContext";
|
2022-11-07 13:45:34 +00:00
|
|
|
import { localNotificationsAreSilenced, createLocalNotificationSettingsIfNeeded } from "./utils/notifications";
|
2022-10-06 14:27:12 +00:00
|
|
|
import { getIncomingCallToastKey, IncomingCallToast } from "./toasts/IncomingCallToast";
|
|
|
|
import ToastStore from "./stores/ToastStore";
|
|
|
|
import { ElementCall } from "./models/Call";
|
2023-01-17 09:04:36 +00:00
|
|
|
import { VoiceBroadcastChunkEventType, VoiceBroadcastInfoEventType } from "./voice-broadcast";
|
|
|
|
import { getSenderName } from "./utils/event/getSenderName";
|
2023-07-17 14:04:09 +00:00
|
|
|
import { stripPlainReply } from "./utils/Reply";
|
2015-09-16 13:48:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatches:
|
|
|
|
* {
|
|
|
|
* action: "notifier_enabled",
|
|
|
|
* value: boolean
|
|
|
|
* }
|
|
|
|
*/
|
2015-07-03 10:12:54 +00:00
|
|
|
|
2017-08-24 13:27:38 +00:00
|
|
|
const MAX_PENDING_ENCRYPTED = 20;
|
|
|
|
|
2020-04-06 15:10:40 +00: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.
|
|
|
|
*/
|
2023-02-15 13:36:22 +00:00
|
|
|
const msgTypeHandlers: Record<string, (event: MatrixEvent) => string | null> = {
|
2022-01-19 00:58:31 +00:00
|
|
|
[MsgType.KeyVerificationRequest]: (event: MatrixEvent) => {
|
2020-04-06 15:10:40 +00:00
|
|
|
const name = (event.sender || {}).name;
|
2023-09-22 15:39:40 +00:00
|
|
|
return _t("notifier|m.key.verification.request", { name });
|
2020-04-06 15:10:40 +00:00
|
|
|
},
|
2022-03-10 18:03:31 +00:00
|
|
|
[M_LOCATION.name]: (event: MatrixEvent) => {
|
2022-01-19 00:58:31 +00:00
|
|
|
return TextForEvent.textForLocationEvent(event)();
|
|
|
|
},
|
2022-03-10 18:03:31 +00:00
|
|
|
[M_LOCATION.altName]: (event: MatrixEvent) => {
|
2022-01-19 00:58:31 +00:00
|
|
|
return TextForEvent.textForLocationEvent(event)();
|
|
|
|
},
|
2023-01-03 09:02:28 +00:00
|
|
|
[MsgType.Audio]: (event: MatrixEvent): string | null => {
|
|
|
|
if (event.getContent()?.[VoiceBroadcastChunkEventType]) {
|
2023-01-17 09:04:36 +00:00
|
|
|
if (event.getContent()?.[VoiceBroadcastChunkEventType]?.sequence === 1) {
|
|
|
|
// Show a notification for the first broadcast chunk.
|
|
|
|
// At this point a user received something to listen to.
|
2023-09-22 15:39:40 +00:00
|
|
|
return _t("notifier|io.element.voice_broadcast_chunk", { senderName: getSenderName(event) });
|
2023-01-17 09:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mute other broadcast chunks
|
2023-01-03 09:02:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-01-17 09:04:36 +00:00
|
|
|
|
2023-06-21 16:29:44 +00:00
|
|
|
return TextForEvent.textForEvent(event, MatrixClientPeg.safeGet());
|
2023-01-03 09:02:28 +00:00
|
|
|
},
|
2020-04-06 15:10:40 +00:00
|
|
|
};
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
class NotifierClass {
|
|
|
|
private notifsByRoom: Record<string, Notification[]> = {};
|
2015-11-30 15:04:24 +00:00
|
|
|
|
2017-08-24 13:27:38 +00: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
|
2023-02-13 11:39:16 +00:00
|
|
|
private pendingEncryptedEventIds: string[] = [];
|
2017-08-24 13:27:38 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private toolbarHidden?: boolean;
|
|
|
|
private isSyncing?: boolean;
|
|
|
|
|
2023-03-07 13:19:18 +00:00
|
|
|
public notificationMessageForEvent(ev: MatrixEvent): string | null {
|
|
|
|
const msgType = ev.getContent().msgtype;
|
|
|
|
if (msgType && msgTypeHandlers.hasOwnProperty(msgType)) {
|
|
|
|
return msgTypeHandlers[msgType](ev);
|
2020-04-06 15:10:40 +00:00
|
|
|
}
|
2023-06-21 16:29:44 +00:00
|
|
|
return TextForEvent.textForEvent(ev, MatrixClientPeg.safeGet());
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-11-30 15:04:24 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
// XXX: exported for tests
|
|
|
|
public displayPopupNotification(ev: MatrixEvent, room: Room): void {
|
2016-11-02 17:35:31 +00:00
|
|
|
const plaf = PlatformPeg.get();
|
2023-06-21 16:29:44 +00:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2016-11-02 17:35:31 +00:00
|
|
|
if (!plaf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) {
|
2015-11-30 15:04:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-29 14:23:02 +00:00
|
|
|
if (localNotificationsAreSilenced(cli)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-23 05:06:23 +00:00
|
|
|
let msg = this.notificationMessageForEvent(ev);
|
2015-11-30 15:04:24 +00:00
|
|
|
if (!msg) return;
|
|
|
|
|
2023-05-16 13:25:43 +00:00
|
|
|
let title: string | undefined;
|
2017-04-23 05:06:23 +00:00
|
|
|
if (!ev.sender || room.name === ev.sender.name) {
|
2015-11-30 15:04:24 +00:00
|
|
|
title = room.name;
|
2023-03-07 13:19:18 +00:00
|
|
|
// notificationMessageForEvent includes sender, but we already have the sender here
|
|
|
|
const msgType = ev.getContent().msgtype;
|
|
|
|
if (ev.getContent().body && (!msgType || !msgTypeHandlers.hasOwnProperty(msgType))) {
|
2023-07-17 14:04:09 +00:00
|
|
|
msg = stripPlainReply(ev.getContent().body);
|
2020-04-06 15:10:40 +00:00
|
|
|
}
|
2017-04-23 05:06:23 +00:00
|
|
|
} else if (ev.getType() === "m.room.member") {
|
2015-11-30 15:04:24 +00: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 + ")";
|
2023-03-07 13:19:18 +00:00
|
|
|
// notificationMessageForEvent includes sender, but we've just out sender in the title
|
|
|
|
const msgType = ev.getContent().msgtype;
|
|
|
|
if (ev.getContent().body && (!msgType || !msgTypeHandlers.hasOwnProperty(msgType))) {
|
2023-07-17 14:04:09 +00:00
|
|
|
msg = stripPlainReply(ev.getContent().body);
|
2020-04-06 15:10:40 +00:00
|
|
|
}
|
2015-11-30 15:04:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 13:25:43 +00:00
|
|
|
if (!title) return;
|
|
|
|
|
2017-09-06 09:56:08 +00:00
|
|
|
if (!this.isBodyEnabled()) {
|
|
|
|
msg = "";
|
|
|
|
}
|
2015-11-30 15:04:24 +00:00
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
let avatarUrl: string | null = null;
|
2019-05-31 02:02:25 +00:00
|
|
|
if (ev.sender && !SettingsStore.getValue("lowBandwidth")) {
|
|
|
|
avatarUrl = Avatar.avatarUrlForMember(ev.sender, 40, 40, "crop");
|
|
|
|
}
|
|
|
|
|
2023-03-07 13:19:18 +00:00
|
|
|
const notif = plaf.displayNotification(title, msg!, avatarUrl, room, ev);
|
2016-03-03 13:18:41 +00:00
|
|
|
|
2016-11-02 17:35:31 +00:00
|
|
|
// if displayNotification returns non-null, the platform supports
|
|
|
|
// clearing notifications later, so keep track of this.
|
|
|
|
if (notif) {
|
2023-02-15 13:36:22 +00:00
|
|
|
if (this.notifsByRoom[ev.getRoomId()!] === undefined) this.notifsByRoom[ev.getRoomId()!] = [];
|
|
|
|
this.notifsByRoom[ev.getRoomId()!].push(notif);
|
2016-11-02 17:35:31 +00:00
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getSoundForRoom(roomId: string): {
|
|
|
|
url: string;
|
|
|
|
name: string;
|
|
|
|
type: string;
|
|
|
|
size: string;
|
|
|
|
} | null {
|
2019-05-12 16:14:21 +00:00
|
|
|
// We do no caching here because the SDK caches setting
|
2019-04-19 11:36:32 +00:00
|
|
|
// and the browser will cache the sound.
|
2019-06-03 16:35:15 +00:00
|
|
|
const content = SettingsStore.getValue("notificationSound", roomId);
|
2019-04-19 21:31:51 +00:00
|
|
|
if (!content) {
|
2019-05-12 16:14:21 +00:00
|
|
|
return null;
|
2019-04-19 11:36:32 +00:00
|
|
|
}
|
2019-04-19 21:31:51 +00:00
|
|
|
|
2022-11-07 11:56:43 +00:00
|
|
|
if (typeof content.url !== "string") {
|
|
|
|
logger.warn(`${roomId} has custom notification sound event, but no url string`);
|
2019-04-19 11:36:32 +00:00
|
|
|
return null;
|
2017-01-20 14:22:27 +00:00
|
|
|
}
|
2019-09-18 08:27:43 +00:00
|
|
|
|
2019-05-12 16:14:21 +00:00
|
|
|
if (!content.url.startsWith("mxc://")) {
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn(`${roomId} has custom notification sound event, but url is not a mxc url`);
|
2019-05-12 16:14:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally in here we could use MSC1310 to detect the type of file, and reject it.
|
2019-04-19 20:42:18 +00:00
|
|
|
|
2023-05-09 17:24:40 +00:00
|
|
|
const url = mediaFromMxc(content.url).srcHttp;
|
|
|
|
if (!url) {
|
|
|
|
logger.warn("Something went wrong when generating src http url for mxc");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-19 13:10:10 +00:00
|
|
|
return {
|
2023-05-09 17:24:40 +00:00
|
|
|
url,
|
2019-04-19 15:27:30 +00:00
|
|
|
name: content.name,
|
2019-04-19 13:10:10 +00:00
|
|
|
type: content.type,
|
2019-04-19 15:27:30 +00:00
|
|
|
size: content.size,
|
2019-04-19 13:10:10 +00:00
|
|
|
};
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2019-04-19 11:36:32 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
// XXX: Exported for tests
|
|
|
|
public async playAudioNotification(ev: MatrixEvent, room: Room): Promise<void> {
|
2023-06-21 16:29:44 +00:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-09-29 14:23:02 +00:00
|
|
|
if (localNotificationsAreSilenced(cli)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 07:43:35 +00:00
|
|
|
const sound = this.getSoundForRoom(room.roomId);
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Got sound ${(sound && sound.name) || "default"} for ${room.roomId}`);
|
2019-05-12 16:14:21 +00:00
|
|
|
|
2019-04-21 17:01:26 +00:00
|
|
|
try {
|
2020-08-05 10:07:10 +00:00
|
|
|
const selector = document.querySelector<HTMLAudioElement>(
|
|
|
|
sound ? `audio[src='${sound.url}']` : "#messageAudio",
|
|
|
|
);
|
2019-04-19 12:21:58 +00:00
|
|
|
let audioElement = selector;
|
2023-02-15 13:36:22 +00:00
|
|
|
if (!audioElement) {
|
2019-04-19 13:10:10 +00:00
|
|
|
if (!sound) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("No audio element or sound to play for notification");
|
2019-04-19 13:10:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
audioElement = new Audio(sound.url);
|
|
|
|
if (sound.type) {
|
|
|
|
audioElement.type = sound.type;
|
2019-04-19 11:36:32 +00:00
|
|
|
}
|
2019-04-19 12:21:58 +00:00
|
|
|
document.body.appendChild(audioElement);
|
2019-04-19 11:36:32 +00:00
|
|
|
}
|
2019-11-21 17:00:35 +00:00
|
|
|
await audioElement.play();
|
2019-04-21 17:01:26 +00:00
|
|
|
} catch (ex) {
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("Caught error when trying to fetch room notification sound:", ex);
|
2019-04-21 17:01:26 +00:00
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-11-30 15:04:24 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public start(): void {
|
2023-06-21 16:29:44 +00:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
|
|
|
cli.on(RoomEvent.Timeline, this.onEvent);
|
|
|
|
cli.on(RoomEvent.Receipt, this.onRoomReceipt);
|
|
|
|
cli.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
|
|
|
cli.on(ClientEvent.Sync, this.onSyncStateChange);
|
2015-11-30 15:04:24 +00:00
|
|
|
this.toolbarHidden = false;
|
2017-03-28 14:02:08 +00:00
|
|
|
this.isSyncing = false;
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-07-03 10:12:54 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public stop(): void {
|
2020-02-20 02:35:30 +00:00
|
|
|
if (MatrixClientPeg.get()) {
|
2023-06-21 16:29:44 +00:00
|
|
|
MatrixClientPeg.get()!.removeListener(RoomEvent.Timeline, this.onEvent);
|
|
|
|
MatrixClientPeg.get()!.removeListener(RoomEvent.Receipt, this.onRoomReceipt);
|
|
|
|
MatrixClientPeg.get()!.removeListener(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
|
|
|
MatrixClientPeg.get()!.removeListener(ClientEvent.Sync, this.onSyncStateChange);
|
2015-07-03 10:12:54 +00:00
|
|
|
}
|
2017-03-28 14:02:08 +00:00
|
|
|
this.isSyncing = false;
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-07-03 10:12:54 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public supportsDesktopNotifications(): boolean {
|
|
|
|
return PlatformPeg.get()?.supportsNotifications() ?? false;
|
|
|
|
}
|
2015-09-16 13:48:49 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public setEnabled(enable: boolean, callback?: () => void): void {
|
2016-11-02 17:35:31 +00:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (!plaf) return;
|
2017-05-29 18:15:52 +00:00
|
|
|
|
2017-12-25 20:27:23 +00: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 10:59:40 +00:00
|
|
|
// make sure that we persist the current setting audio_enabled setting
|
|
|
|
// before changing anything
|
2017-11-05 04:47:18 +00:00
|
|
|
if (SettingsStore.isLevelSupported(SettingLevel.DEVICE)) {
|
|
|
|
SettingsStore.setValue("audioNotificationsEnabled", null, SettingLevel.DEVICE, this.isEnabled());
|
2016-03-10 10:59:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 16:40:33 +00:00
|
|
|
if (enable) {
|
2016-03-23 11:56:41 +00:00
|
|
|
// Attempt to get permission from user
|
2019-11-18 10:03:05 +00:00
|
|
|
plaf.requestNotificationPermission().then((result) => {
|
2016-03-23 11:56:41 +00:00
|
|
|
if (result !== "granted") {
|
|
|
|
// The permission request was dismissed or denied
|
2017-11-05 04:47:18 +00:00
|
|
|
// TODO: Support alternative branding in messaging
|
2020-07-10 18:07:11 +00:00
|
|
|
const brand = SdkConfig.get().brand;
|
2017-04-23 05:16:25 +00:00
|
|
|
const description =
|
|
|
|
result === "denied"
|
2023-09-22 15:39:40 +00:00
|
|
|
? _t("settings|notifications|error_permissions_denied", { brand })
|
|
|
|
: _t("settings|notifications|error_permissions_missing", {
|
2020-07-10 18:07:11 +00:00
|
|
|
brand,
|
|
|
|
});
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-22 15:39:40 +00:00
|
|
|
title: _t("settings|notifications|error_title"),
|
2017-04-23 05:16:25 +00:00
|
|
|
description,
|
|
|
|
});
|
2016-03-23 11:56:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) callback();
|
2022-07-29 11:43:29 +00:00
|
|
|
|
|
|
|
PosthogAnalytics.instance.trackEvent<PermissionChangedEvent>({
|
|
|
|
eventName: "PermissionChanged",
|
|
|
|
permission: "Notification",
|
|
|
|
granted: true,
|
|
|
|
});
|
2016-03-23 11:56:41 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 05:06:23 +00:00
|
|
|
value: true,
|
2015-09-16 13:48:49 +00:00
|
|
|
});
|
2016-03-23 11:56:41 +00:00
|
|
|
});
|
2016-03-21 22:19:46 +00:00
|
|
|
} else {
|
2022-07-29 11:43:29 +00:00
|
|
|
PosthogAnalytics.instance.trackEvent<PermissionChangedEvent>({
|
|
|
|
eventName: "PermissionChanged",
|
|
|
|
permission: "Notification",
|
|
|
|
granted: false,
|
|
|
|
});
|
2015-09-16 13:48:49 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: "notifier_enabled",
|
2017-04-23 05:06:23 +00:00
|
|
|
value: false,
|
2015-09-16 13:48:49 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-01 22:48:00 +00: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 12:58:29 +00:00
|
|
|
this.setPromptHidden(true);
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-09-16 13:48:49 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public isEnabled(): boolean {
|
2017-11-05 04:47:18 +00:00
|
|
|
return this.isPossible() && SettingsStore.getValue("notificationsEnabled");
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2017-11-05 04:47:18 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public isPossible(): boolean {
|
2016-11-02 17:35:31 +00:00
|
|
|
const plaf = PlatformPeg.get();
|
2023-02-13 11:39:16 +00:00
|
|
|
if (!plaf?.supportsNotifications()) return false;
|
2016-11-02 17:35:31 +00:00
|
|
|
if (!plaf.maySendNotifications()) return false;
|
2015-09-16 13:48:49 +00:00
|
|
|
|
2017-11-05 04:47:18 +00:00
|
|
|
return true; // possible, but not necessarily enabled
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2017-09-06 09:56:08 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public isBodyEnabled(): boolean {
|
2017-11-05 04:47:18 +00:00
|
|
|
return this.isEnabled() && SettingsStore.getValue("notificationBodyEnabled");
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2016-03-10 10:59:40 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public isAudioEnabled(): boolean {
|
2020-08-05 10:10:12 +00:00
|
|
|
// We don't route Audio via the HTML Notifications API so it is possible regardless of other things
|
|
|
|
return SettingsStore.getValue("audioNotificationsEnabled");
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2016-03-10 10:59:40 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public setPromptHidden(hidden: boolean, persistent = true): void {
|
2015-11-30 15:04:24 +00:00
|
|
|
this.toolbarHidden = hidden;
|
2016-09-09 01:09:12 +00:00
|
|
|
|
2020-05-22 20:56:25 +00:00
|
|
|
hideNotificationsToast();
|
2016-03-21 22:19:46 +00:00
|
|
|
|
2016-03-23 10:15:54 +00:00
|
|
|
// update the info to localStorage for persistent settings
|
2017-11-09 00:06:36 +00:00
|
|
|
if (persistent && global.localStorage) {
|
2020-08-05 10:07:10 +00:00
|
|
|
global.localStorage.setItem("notifications_hidden", String(hidden));
|
2016-03-21 22:19:46 +00:00
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-09-16 13:48:49 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public shouldShowPrompt(): boolean {
|
2019-03-12 16:29:16 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const isGuest = client.isGuest();
|
2020-09-14 13:35:08 +00:00
|
|
|
return (
|
|
|
|
!isGuest &&
|
|
|
|
this.supportsDesktopNotifications() &&
|
|
|
|
!isPushNotifyDisabled() &&
|
2022-09-30 15:11:04 +00:00
|
|
|
!this.isEnabled() &&
|
2023-02-13 11:39:16 +00:00
|
|
|
!this.isPromptHidden()
|
2022-09-30 15:11:04 +00:00
|
|
|
);
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2019-03-12 16:29:16 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private isPromptHidden(): boolean {
|
2016-03-23 11:56:41 +00:00
|
|
|
// Check localStorage for any such meta data
|
2017-11-09 00:06:36 +00:00
|
|
|
if (global.localStorage) {
|
|
|
|
return global.localStorage.getItem("notifications_hidden") === "true";
|
2016-03-23 11:56:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
return !!this.toolbarHidden;
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2015-09-16 13:48:49 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
// XXX: Exported for tests
|
2023-08-04 07:36:16 +00:00
|
|
|
public onSyncStateChange = (state: SyncState, prevState: SyncState | null, data?: SyncStateData): void => {
|
2022-10-10 14:18:38 +00:00
|
|
|
if (state === SyncState.Syncing) {
|
2017-03-28 14:02:08 +00:00
|
|
|
this.isSyncing = true;
|
2022-10-10 14:18:38 +00:00
|
|
|
} else if (state === SyncState.Stopped || state === SyncState.Error) {
|
2017-03-28 14:02:08 +00:00
|
|
|
this.isSyncing = false;
|
2016-03-17 11:34:20 +00:00
|
|
|
}
|
2022-10-10 14:18:38 +00:00
|
|
|
|
|
|
|
// wait for first non-cached sync to complete
|
|
|
|
if (![SyncState.Stopped, SyncState.Error].includes(state) && !data?.fromCache) {
|
2023-06-21 16:29:44 +00:00
|
|
|
createLocalNotificationSettingsIfNeeded(MatrixClientPeg.safeGet());
|
2022-10-10 14:18:38 +00:00
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
};
|
2015-12-15 17:01:16 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private onEvent = (
|
2022-12-01 12:21:56 +00:00
|
|
|
ev: MatrixEvent,
|
|
|
|
room: Room | undefined,
|
|
|
|
toStartOfTimeline: boolean | undefined,
|
|
|
|
removed: boolean,
|
|
|
|
data: IRoomTimelineData,
|
2023-02-13 11:39:16 +00:00
|
|
|
): void => {
|
2023-06-27 09:39:55 +00:00
|
|
|
if (removed) return; // only notify for new events, not removed ones
|
|
|
|
if (!data.liveEvent || !!toStartOfTimeline) return; // only notify for new things, not old.
|
2017-03-28 14:02:08 +00:00
|
|
|
if (!this.isSyncing) return; // don't alert for any messages initially
|
2023-06-21 16:29:44 +00:00
|
|
|
if (ev.getSender() === MatrixClientPeg.safeGet().getUserId()) return;
|
2023-06-27 09:39:55 +00:00
|
|
|
if (data.timeline.getTimelineSet().threadListType !== null) return; // Ignore events on the thread list generated timelines
|
2015-07-03 10:12:54 +00:00
|
|
|
|
2023-06-21 16:29:44 +00:00
|
|
|
MatrixClientPeg.safeGet().decryptEventIfNeeded(ev);
|
2021-05-18 15:24:38 +00:00
|
|
|
|
2017-08-24 13:27:38 +00: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 13:42:38 +00:00
|
|
|
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
|
2023-02-15 13:36:22 +00:00
|
|
|
this.pendingEncryptedEventIds.push(ev.getId()!);
|
2017-08-24 13:27:38 +00:00
|
|
|
// don't let the list fill up indefinitely
|
|
|
|
while (this.pendingEncryptedEventIds.length > MAX_PENDING_ENCRYPTED) {
|
|
|
|
this.pendingEncryptedEventIds.shift();
|
2016-03-10 10:59:40 +00:00
|
|
|
}
|
2017-08-24 13:27:38 +00:00
|
|
|
return;
|
2015-07-03 10:12:54 +00:00
|
|
|
}
|
2017-08-24 13:27:38 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
this.evaluateEvent(ev);
|
|
|
|
};
|
2017-08-24 13:27:38 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private onEventDecrypted = (ev: MatrixEvent): void => {
|
2018-03-29 17:18:53 +00: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;
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
const idx = this.pendingEncryptedEventIds.indexOf(ev.getId()!);
|
2017-08-24 13:27:38 +00:00
|
|
|
if (idx === -1) return;
|
|
|
|
|
|
|
|
this.pendingEncryptedEventIds.splice(idx, 1);
|
2023-02-13 11:39:16 +00:00
|
|
|
this.evaluateEvent(ev);
|
|
|
|
};
|
2016-11-02 17:35:31 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
private onRoomReceipt = (ev: MatrixEvent, room: Room): void => {
|
2017-04-23 05:06:23 +00:00
|
|
|
if (room.getUnreadNotificationCount() === 0) {
|
2016-11-02 17:35:31 +00: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];
|
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
};
|
2017-08-24 13:27:38 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
// XXX: exported for tests
|
|
|
|
public evaluateEvent(ev: MatrixEvent): void {
|
2023-01-17 09:04:36 +00:00
|
|
|
// Mute notifications for broadcast info events
|
|
|
|
if (ev.getType() === VoiceBroadcastInfoEventType) return;
|
2023-02-15 13:36:22 +00:00
|
|
|
let roomId = ev.getRoomId()!;
|
2022-08-30 19:13:39 +00:00
|
|
|
if (LegacyCallHandler.instance.getSupportsVirtualRooms()) {
|
2022-02-03 11:31:32 +00:00
|
|
|
// Attempt to translate a virtual room to a native one
|
|
|
|
const nativeRoomId = VoipUserMapper.sharedInstance().nativeRoomForVirtualRoom(roomId);
|
|
|
|
if (nativeRoomId) {
|
|
|
|
roomId = nativeRoomId;
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 16:29:44 +00:00
|
|
|
const room = MatrixClientPeg.safeGet().getRoom(roomId);
|
2022-12-01 12:21:56 +00: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 11:31:32 +00:00
|
|
|
|
2023-06-21 16:29:44 +00:00
|
|
|
const actions = MatrixClientPeg.safeGet().getPushActionsForEvent(ev);
|
2022-10-13 07:07:48 +00:00
|
|
|
|
2021-12-15 08:34:52 +00:00
|
|
|
if (actions?.notify) {
|
2023-02-13 11:39:16 +00:00
|
|
|
this.performCustomEventHandling(ev);
|
2022-10-06 14:27:12 +00:00
|
|
|
|
2022-10-25 16:53:31 +00:00
|
|
|
const store = SdkContextClass.instance.roomViewStore;
|
|
|
|
const isViewingRoom = store.getRoomId() === room.roomId;
|
|
|
|
const threadId: string | undefined = ev.getId() !== ev.threadRootId ? ev.threadRootId : undefined;
|
|
|
|
const isViewingThread = store.getThreadId() === threadId;
|
|
|
|
|
|
|
|
const isViewingEventTimeline = isViewingRoom && (!threadId || isViewingThread);
|
|
|
|
|
2021-12-03 11:02:47 +00:00
|
|
|
if (isViewingEventTimeline && UserActivity.sharedInstance().userActiveRecently() && !Modal.hasDialogs()) {
|
2020-10-15 14:58:18 +00:00
|
|
|
// don't bother notifying as user was recently active in this room
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-24 13:27:38 +00:00
|
|
|
if (this.isEnabled()) {
|
2023-02-13 11:39:16 +00:00
|
|
|
this.displayPopupNotification(ev, room);
|
2017-08-24 13:27:38 +00:00
|
|
|
}
|
|
|
|
if (actions.tweaks.sound && this.isAudioEnabled()) {
|
2023-02-15 13:36:22 +00:00
|
|
|
PlatformPeg.get()?.loudNotification(ev, room);
|
2023-02-13 11:39:16 +00:00
|
|
|
this.playAudioNotification(ev, room);
|
2017-08-24 13:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
2022-10-06 14:27:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Some events require special handling such as showing in-app toasts
|
|
|
|
*/
|
2023-02-13 11:39:16 +00:00
|
|
|
private performCustomEventHandling(ev: MatrixEvent): void {
|
2022-10-06 14:27:12 +00:00
|
|
|
if (ElementCall.CALL_EVENT_TYPE.names.includes(ev.getType()) && SettingsStore.getValue("feature_group_calls")) {
|
|
|
|
ToastStore.sharedInstance().addOrReplaceToast({
|
2023-02-15 13:36:22 +00:00
|
|
|
key: getIncomingCallToastKey(ev.getStateKey()!),
|
2022-10-06 14:27:12 +00:00
|
|
|
priority: 100,
|
|
|
|
component: IncomingCallToast,
|
|
|
|
bodyClassName: "mx_IncomingCallToast",
|
|
|
|
props: { callEvent: ev },
|
|
|
|
});
|
|
|
|
}
|
2023-02-13 11:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-03 10:12:54 +00:00
|
|
|
|
2020-08-05 10:07:10 +00:00
|
|
|
if (!window.mxNotifier) {
|
2023-02-13 11:39:16 +00:00
|
|
|
window.mxNotifier = new NotifierClass();
|
2015-12-02 16:35:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 10:07:10 +00:00
|
|
|
export default window.mxNotifier;
|
2023-02-13 11:39:16 +00:00
|
|
|
export const Notifier: NotifierClass = window.mxNotifier;
|