2020-06-30 13:34:44 -06:00
|
|
|
/*
|
2023-01-31 09:58:17 +00:00
|
|
|
Copyright 2020, 2023 The Matrix.org Foundation C.I.C.
|
2020-06-30 13:34:44 -06: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-03-18 14:40:52 +00:00
|
|
|
import { MatrixEventEvent, RoomEvent, ClientEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2023-08-07 09:24:58 +01:00
|
|
|
import type { Room, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2023-01-31 09:58:17 +00:00
|
|
|
import type { IDestroyable } from "../../utils/IDestroyable";
|
2020-06-30 13:34:44 -06:00
|
|
|
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
|
|
|
|
import { readReceiptChangeIsFor } from "../../utils/read-receipts";
|
|
|
|
|
import * as RoomNotifs from "../../RoomNotifs";
|
2023-01-31 16:59:24 +00:00
|
|
|
import { NotificationState } from "./NotificationState";
|
2023-09-19 14:24:35 +03:00
|
|
|
import SettingsStore from "../../settings/SettingsStore";
|
2024-03-19 13:28:20 +00:00
|
|
|
import { MARKED_UNREAD_TYPE_STABLE, MARKED_UNREAD_TYPE_UNSTABLE } from "../../utils/notifications";
|
2020-06-30 13:34:44 -06:00
|
|
|
|
2020-07-08 19:26:25 -06:00
|
|
|
export class RoomNotificationState extends NotificationState implements IDestroyable {
|
2024-01-29 18:52:48 +01:00
|
|
|
public constructor(
|
|
|
|
|
public readonly room: Room,
|
|
|
|
|
private includeThreads: boolean,
|
|
|
|
|
) {
|
2020-06-30 13:34:44 -06:00
|
|
|
super();
|
2022-10-24 07:50:21 +01:00
|
|
|
const cli = this.room.client;
|
|
|
|
|
this.room.on(RoomEvent.Receipt, this.handleReadReceipt);
|
|
|
|
|
this.room.on(RoomEvent.MyMembership, this.handleMembershipUpdate);
|
|
|
|
|
this.room.on(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated);
|
2022-10-26 10:33:06 +01:00
|
|
|
this.room.on(RoomEvent.Timeline, this.handleRoomEventUpdate);
|
|
|
|
|
this.room.on(RoomEvent.Redaction, this.handleRoomEventUpdate);
|
2024-03-19 13:28:20 +00:00
|
|
|
this.room.on(RoomEvent.AccountData, this.handleRoomAccountDataUpdate);
|
2022-10-26 10:33:06 +01:00
|
|
|
|
2022-10-18 13:44:45 +01:00
|
|
|
this.room.on(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate); // for server-sent counts
|
2022-10-24 07:50:21 +01:00
|
|
|
cli.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
|
|
|
|
cli.on(ClientEvent.AccountData, this.handleAccountDataUpdate);
|
2020-06-30 13:34:44 -06:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public destroy(): void {
|
2020-07-08 19:26:25 -06:00
|
|
|
super.destroy();
|
2022-10-24 07:50:21 +01:00
|
|
|
const cli = this.room.client;
|
2022-02-22 12:18:08 +00:00
|
|
|
this.room.removeListener(RoomEvent.Receipt, this.handleReadReceipt);
|
|
|
|
|
this.room.removeListener(RoomEvent.MyMembership, this.handleMembershipUpdate);
|
|
|
|
|
this.room.removeListener(RoomEvent.LocalEchoUpdated, this.handleLocalEchoUpdated);
|
2022-10-26 10:33:06 +01:00
|
|
|
this.room.removeListener(RoomEvent.Timeline, this.handleRoomEventUpdate);
|
|
|
|
|
this.room.removeListener(RoomEvent.Redaction, this.handleRoomEventUpdate);
|
2024-03-19 13:28:20 +00:00
|
|
|
this.room.removeListener(RoomEvent.AccountData, this.handleRoomAccountDataUpdate);
|
2022-10-24 07:50:21 +01:00
|
|
|
cli.removeListener(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
|
|
|
|
cli.removeListener(ClientEvent.AccountData, this.handleAccountDataUpdate);
|
2020-06-30 13:34:44 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private handleLocalEchoUpdated = (): void => {
|
2021-09-09 13:14:05 +01:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private handleReadReceipt = (event: MatrixEvent, room: Room): void => {
|
2023-06-15 15:11:49 +01:00
|
|
|
if (!readReceiptChangeIsFor(event, MatrixClientPeg.safeGet())) return; // not our own - ignore
|
2020-06-30 13:34:44 -06:00
|
|
|
if (room.roomId !== this.room.roomId) return; // not for us - ignore
|
|
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private handleMembershipUpdate = (): void => {
|
2020-07-30 14:33:38 -06:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private handleNotificationCountUpdate = (): void => {
|
2022-02-15 20:06:29 +00:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onEventDecrypted = (event: MatrixEvent): void => {
|
2022-10-18 13:44:45 +01:00
|
|
|
if (event.getRoomId() !== this.room.roomId) return; // ignore - not for us or notifications timeline
|
2022-02-15 20:06:29 +00:00
|
|
|
|
2020-06-30 13:34:44 -06:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private handleRoomEventUpdate = (event: MatrixEvent): void => {
|
2022-12-06 09:59:17 +00:00
|
|
|
if (event?.getRoomId() !== this.room.roomId) return; // ignore - not for us or notifications timeline
|
2022-10-26 10:33:06 +01:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private handleAccountDataUpdate = (ev: MatrixEvent): void => {
|
2020-07-02 15:05:01 -06:00
|
|
|
if (ev.getType() === "m.push_rules") {
|
|
|
|
|
this.updateNotificationState();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-19 13:28:20 +00:00
|
|
|
private handleRoomAccountDataUpdate = (ev: MatrixEvent): void => {
|
|
|
|
|
if ([MARKED_UNREAD_TYPE_STABLE, MARKED_UNREAD_TYPE_UNSTABLE].includes(ev.getType())) {
|
|
|
|
|
this.updateNotificationState();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private updateNotificationState(): void {
|
2020-07-08 19:26:25 -06:00
|
|
|
const snapshot = this.snapshot();
|
2020-06-30 13:34:44 -06:00
|
|
|
|
2024-01-29 18:52:48 +01:00
|
|
|
const { level, symbol, count } = RoomNotifs.determineUnreadState(this.room, undefined, this.includeThreads);
|
2023-05-05 13:53:26 +12:00
|
|
|
const muted =
|
|
|
|
|
RoomNotifs.getRoomNotifsState(this.room.client, this.room.roomId) === RoomNotifs.RoomNotifState.Mute;
|
2024-03-11 17:16:53 +00:00
|
|
|
const knocked =
|
2024-03-12 14:52:54 +00:00
|
|
|
SettingsStore.getValue("feature_ask_to_join") && this.room.getMyMembership() === KnownMembership.Knock;
|
2024-01-15 15:25:48 +00:00
|
|
|
this._level = level;
|
2023-01-31 09:58:17 +00:00
|
|
|
this._symbol = symbol;
|
|
|
|
|
this._count = count;
|
2023-05-05 13:53:26 +12:00
|
|
|
this._muted = muted;
|
2023-09-19 14:24:35 +03:00
|
|
|
this._knocked = knocked;
|
2020-06-30 13:34:44 -06:00
|
|
|
|
|
|
|
|
// finally, publish an update if needed
|
2020-07-08 19:26:25 -06:00
|
|
|
this.emitIfUpdated(snapshot);
|
2020-06-30 13:34:44 -06:00
|
|
|
}
|
|
|
|
|
}
|