2021-12-07 13:51:34 +01:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-12-09 10:10:23 +01:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { Thread, ThreadEvent } from "matrix-js-sdk/src/models/thread";
|
|
|
|
|
2021-12-07 13:51:34 +01:00
|
|
|
import { NotificationColor } from "./NotificationColor";
|
|
|
|
import { IDestroyable } from "../../utils/IDestroyable";
|
|
|
|
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
|
|
|
import { NotificationState } from "./NotificationState";
|
|
|
|
|
|
|
|
export class ThreadNotificationState extends NotificationState implements IDestroyable {
|
|
|
|
protected _symbol = null;
|
|
|
|
protected _count = 0;
|
|
|
|
protected _color = NotificationColor.None;
|
|
|
|
|
2021-12-15 13:40:56 +01:00
|
|
|
constructor(public readonly thread: Thread) {
|
2021-12-07 13:51:34 +01:00
|
|
|
super();
|
|
|
|
this.thread.on(ThreadEvent.NewReply, this.handleNewThreadReply);
|
|
|
|
this.thread.on(ThreadEvent.ViewThread, this.resetThreadNotification);
|
|
|
|
}
|
|
|
|
|
|
|
|
public destroy(): void {
|
|
|
|
super.destroy();
|
|
|
|
this.thread.off(ThreadEvent.NewReply, this.handleNewThreadReply);
|
|
|
|
this.thread.off(ThreadEvent.ViewThread, this.resetThreadNotification);
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:05:42 +01:00
|
|
|
private handleNewThreadReply = (thread: Thread, event: MatrixEvent) => {
|
2021-12-07 13:51:34 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2021-12-13 15:05:42 +01:00
|
|
|
const myUserId = client.getUserId();
|
|
|
|
|
|
|
|
const isOwn = myUserId === event.getSender();
|
2021-12-15 13:40:56 +01:00
|
|
|
const readReceipt = this.thread.room.getReadReceiptForUserId(myUserId);
|
2021-12-13 15:05:42 +01:00
|
|
|
|
|
|
|
if (!isOwn && !readReceipt || event.getTs() >= readReceipt.data.ts) {
|
2021-12-07 13:51:34 +01:00
|
|
|
const actions = client.getPushActionsForEvent(event, true);
|
|
|
|
|
2021-12-13 15:05:42 +01:00
|
|
|
if (actions?.tweaks) {
|
|
|
|
const color = !!actions.tweaks.highlight
|
|
|
|
? NotificationColor.Red
|
|
|
|
: NotificationColor.Grey;
|
2021-12-07 13:51:34 +01:00
|
|
|
|
2021-12-13 15:05:42 +01:00
|
|
|
this.updateNotificationState(color);
|
|
|
|
}
|
2021-12-07 13:51:34 +01:00
|
|
|
}
|
2021-12-13 15:05:42 +01:00
|
|
|
};
|
2021-12-07 13:51:34 +01:00
|
|
|
|
|
|
|
private resetThreadNotification = (): void => {
|
|
|
|
this.updateNotificationState(NotificationColor.None);
|
|
|
|
};
|
|
|
|
|
|
|
|
private updateNotificationState(color: NotificationColor) {
|
|
|
|
const snapshot = this.snapshot();
|
|
|
|
|
|
|
|
this._color = color;
|
|
|
|
|
|
|
|
// finally, publish an update if needed
|
|
|
|
this.emitIfUpdated(snapshot);
|
|
|
|
}
|
|
|
|
}
|