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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
import { MatrixEventEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
import { RoomEvent } from "matrix-js-sdk/src/models/room";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { ClientEvent } from "matrix-js-sdk/src/client";
|
2022-10-24 07:50:21 +01:00
|
|
|
import { Feature, ServerSupport } from "matrix-js-sdk/src/feature";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
import type { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
import type { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
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";
|
2022-03-31 14:48:23 +01:00
|
|
|
import { NotificationState, NotificationStateEvents } from "./NotificationState";
|
2023-01-31 09:58:17 +00:00
|
|
|
import type { ThreadsRoomNotificationState } from "./ThreadsRoomNotificationState";
|
2020-06-30 13:34:44 -06:00
|
|
|
|
2020-07-08 19:26:25 -06:00
|
|
|
export class RoomNotificationState extends NotificationState implements IDestroyable {
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(public readonly room: Room, private readonly threadsState?: ThreadsRoomNotificationState) {
|
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);
|
|
|
|
|
|
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
|
|
|
if (cli.canSupport.get(Feature.ThreadUnreadNotifications) === ServerSupport.Unsupported) {
|
|
|
|
|
this.threadsState?.on(NotificationStateEvents.Update, this.handleThreadsUpdate);
|
2022-03-31 14:48:23 +01:00
|
|
|
}
|
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);
|
2022-10-24 07:50:21 +01:00
|
|
|
if (cli.canSupport.get(Feature.ThreadUnreadNotifications) === ServerSupport.Unsupported) {
|
|
|
|
|
this.room.removeListener(RoomEvent.UnreadNotifications, this.handleNotificationCountUpdate);
|
|
|
|
|
} else if (this.threadsState) {
|
2022-03-31 14:48:23 +01:00
|
|
|
this.threadsState.removeListener(NotificationStateEvents.Update, this.handleThreadsUpdate);
|
|
|
|
|
}
|
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 handleThreadsUpdate = (): void => {
|
2022-03-31 14:48:23 +01:00
|
|
|
this.updateNotificationState();
|
|
|
|
|
};
|
|
|
|
|
|
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 => {
|
2020-06-30 13:34:44 -06:00
|
|
|
if (!readReceiptChangeIsFor(event, MatrixClientPeg.get())) return; // not our own - ignore
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
const { color, symbol, count } = RoomNotifs.determineUnreadState(this.room);
|
|
|
|
|
this._color = color;
|
|
|
|
|
this._symbol = symbol;
|
|
|
|
|
this._count = count;
|
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
|
|
|
}
|
|
|
|
|
}
|