mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-11-24 18:59:17 +00:00
Re-evaluate the unread state of a room when one of its events is decrypted (#34515)
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
@@ -9,8 +9,16 @@ Please see LICENSE files in the repository root for full details.
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { renderHook } from "test-utils-rtl";
|
||||
import { type MatrixClient, NotificationCountType, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { act, renderHook } from "test-utils-rtl";
|
||||
import {
|
||||
EventType,
|
||||
type MatrixClient,
|
||||
MatrixEventEvent,
|
||||
NotificationCountType,
|
||||
RelationType,
|
||||
Room,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { decryptExistingEvent, mkMatrixEvent } from "matrix-js-sdk/src/testing";
|
||||
import { stubClient } from "test-utils";
|
||||
import { populateThread } from "test-utils/threads";
|
||||
|
||||
@@ -72,4 +80,46 @@ describe("useRoomThreadNotifications", () => {
|
||||
|
||||
expect(result.current).toBe(NotificationLevel.Activity);
|
||||
});
|
||||
|
||||
it("returns activity once a thread reply which arrived undecryptable gets decrypted", async () => {
|
||||
// A thread whose latest reply we sent ourselves, so it starts out read.
|
||||
const { rootEvent } = await populateThread({
|
||||
room,
|
||||
client: cli,
|
||||
authorId: cli.getSafeUserId(),
|
||||
participantUserIds: [cli.getSafeUserId()],
|
||||
});
|
||||
|
||||
const { result } = render(room);
|
||||
expect(result.current).toBe(NotificationLevel.None);
|
||||
|
||||
// Somebody else replies in the thread, but the room key has not arrived yet, so the
|
||||
// event cannot be decrypted. An undecryptable event has no renderer, so it does not
|
||||
// count towards the unread state.
|
||||
const reply = mkMatrixEvent({
|
||||
type: EventType.RoomMessageEncrypted,
|
||||
roomId: room.roomId,
|
||||
sender: "@alice:server.org",
|
||||
ts: 10,
|
||||
content: {
|
||||
"algorithm": "m.megolm.v1.aes-sha2",
|
||||
"m.relates_to": { rel_type: RelationType.Thread, event_id: rootEvent.getId()! },
|
||||
},
|
||||
});
|
||||
await act(async () => {
|
||||
await room.addLiveEvents([reply], { addToState: false });
|
||||
});
|
||||
expect(result.current).toBe(NotificationLevel.None);
|
||||
|
||||
// The room key turns up and the reply is decrypted: it now counts as unread.
|
||||
await decryptExistingEvent(reply, {
|
||||
plainType: EventType.RoomMessage,
|
||||
plainContent: { msgtype: "m.text", body: "hello" },
|
||||
});
|
||||
act(() => {
|
||||
cli.emit(MatrixEventEvent.Decrypted, reply);
|
||||
});
|
||||
|
||||
expect(result.current).toBe(NotificationLevel.Activity);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { NotificationCountType, type Room, RoomEvent, ThreadEvent } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
type MatrixEvent,
|
||||
MatrixEventEvent,
|
||||
NotificationCountType,
|
||||
type Room,
|
||||
RoomEvent,
|
||||
ThreadEvent,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { NotificationLevel } from "../../stores/notifications/NotificationLevel";
|
||||
@@ -41,6 +48,17 @@ export const useRoomThreadNotifications = (room: Room): NotificationLevel => {
|
||||
setNotificationLevel(NotificationLevel.None);
|
||||
}, [room]);
|
||||
|
||||
// An event which has not been decrypted yet has no renderer, so it does not count towards the
|
||||
// unread state. The room key often only turns up after the event itself, so re-evaluate the
|
||||
// notifications once an event of this room is decrypted.
|
||||
const onEventDecrypted = useCallback(
|
||||
(event: MatrixEvent) => {
|
||||
if (event.getRoomId() !== room.roomId) return;
|
||||
updateNotification();
|
||||
},
|
||||
[room.roomId, updateNotification],
|
||||
);
|
||||
|
||||
useEventEmitter(room, RoomEvent.UnreadNotifications, updateNotification);
|
||||
useEventEmitter(room, RoomEvent.Receipt, updateNotification);
|
||||
useEventEmitter(room, RoomEvent.Timeline, updateNotification);
|
||||
@@ -49,6 +67,7 @@ export const useRoomThreadNotifications = (room: Room): NotificationLevel => {
|
||||
useEventEmitter(room, RoomEvent.MyMembership, updateNotification);
|
||||
useEventEmitter(room, ThreadEvent.New, updateNotification);
|
||||
useEventEmitter(room, ThreadEvent.Update, updateNotification);
|
||||
useEventEmitter(room.client, MatrixEventEvent.Decrypted, onEventDecrypted);
|
||||
|
||||
// Compute the notification once when mouting a room
|
||||
useEffect(() => {
|
||||
|
||||
@@ -8,9 +8,15 @@ Please see LICENSE files in the repository root for full details.
|
||||
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { renderHook } from "test-utils-rtl";
|
||||
import { EventStatus, NotificationCountType, PendingEventOrdering, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { act, renderHook } from "test-utils-rtl";
|
||||
import {
|
||||
EventStatus,
|
||||
MatrixEventEvent,
|
||||
NotificationCountType,
|
||||
PendingEventOrdering,
|
||||
Room,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { KnownMembership } from "matrix-js-sdk/src/types";
|
||||
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { mkEvent, muteRoom, stubClient } from "test-utils";
|
||||
@@ -102,4 +108,27 @@ describe("useUnreadNotifications", () => {
|
||||
expect(level).toBe(NotificationLevel.Highlight);
|
||||
expect(count).toBe(888);
|
||||
});
|
||||
|
||||
it("re-evaluates the unread state when an event of the room is decrypted", async () => {
|
||||
const mkDecryptedEvent = (roomId: string) =>
|
||||
mkEvent({ event: true, type: "m.room.message", user: "@alice:example.org", room: roomId, content: {} });
|
||||
|
||||
const { result } = renderHook(() => useUnreadNotifications(room));
|
||||
expect(result.current.level).toBe(NotificationLevel.None);
|
||||
|
||||
room.getRoomUnreadNotificationCount = vi
|
||||
.fn()
|
||||
.mockImplementation((type: NotificationCountType) => (type === NotificationCountType.Total ? 1 : 0));
|
||||
|
||||
// A decryption in another room must not disturb this one
|
||||
act(() => {
|
||||
client.emit(MatrixEventEvent.Decrypted, mkDecryptedEvent("!other:example.org"));
|
||||
});
|
||||
expect(result.current.level).toBe(NotificationLevel.None);
|
||||
|
||||
act(() => {
|
||||
client.emit(MatrixEventEvent.Decrypted, mkDecryptedEvent(room.roomId));
|
||||
});
|
||||
expect(result.current.level).toBe(NotificationLevel.Notification);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,10 +6,10 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { RoomEvent } from "matrix-js-sdk/src/matrix";
|
||||
import { MatrixEventEvent, RoomEvent } from "matrix-js-sdk/src/matrix";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import type { NotificationCount, Room } from "matrix-js-sdk/src/matrix";
|
||||
import type { MatrixEvent, NotificationCount, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { determineUnreadState } from "../RoomNotifs";
|
||||
import { NotificationLevel } from "../stores/notifications/NotificationLevel";
|
||||
import { useEventEmitter } from "./useEventEmitter";
|
||||
@@ -40,6 +40,13 @@ export const useUnreadNotifications = (
|
||||
useEventEmitter(room, RoomEvent.Redaction, () => updateNotificationState());
|
||||
useEventEmitter(room, RoomEvent.LocalEchoUpdated, () => updateNotificationState());
|
||||
useEventEmitter(room, RoomEvent.MyMembership, () => updateNotificationState());
|
||||
// An event which has not been decrypted yet has no renderer, so it does not count towards the
|
||||
// unread state. The room key often only turns up after the event itself, so re-evaluate the
|
||||
// unread state once an event of this room is decrypted.
|
||||
useEventEmitter(room?.client, MatrixEventEvent.Decrypted, (event: MatrixEvent) => {
|
||||
if (event.getRoomId() !== room?.roomId) return;
|
||||
updateNotificationState();
|
||||
});
|
||||
|
||||
const updateNotificationState = useCallback(() => {
|
||||
const { symbol, count, level } = determineUnreadState(room, threadId, false);
|
||||
|
||||
+29
-2
@@ -7,9 +7,15 @@
|
||||
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { NotificationCountType, PendingEventOrdering, Room, RoomEvent } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
MatrixEventEvent,
|
||||
NotificationCountType,
|
||||
PendingEventOrdering,
|
||||
Room,
|
||||
RoomEvent,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { vi, describe, it, expect, beforeEach } from "vitest";
|
||||
import { stubClient } from "test-utils";
|
||||
import { mkEvent, stubClient } from "test-utils";
|
||||
|
||||
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { UnreadNotificationBadgeViewModel } from "./UnreadNotificationBadgeViewModel";
|
||||
@@ -71,6 +77,27 @@ describe("UnreadNotificationBadgeViewModel", () => {
|
||||
vm.dispose();
|
||||
});
|
||||
|
||||
it("re-evaluates the unread state when an event of the room is decrypted", () => {
|
||||
const mkDecryptedEvent = (roomId: string) =>
|
||||
mkEvent({ event: true, type: "m.room.message", user: "@alice:example.org", room: roomId, content: {} });
|
||||
const vm = new UnreadNotificationBadgeViewModel({ room });
|
||||
const listener = vi.fn();
|
||||
vm.subscribe(listener);
|
||||
|
||||
client.emit(MatrixEventEvent.Decrypted, mkDecryptedEvent("!other:example.org"));
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
|
||||
room.getRoomUnreadNotificationCount = vi
|
||||
.fn()
|
||||
.mockImplementation((type: NotificationCountType) => (type === NotificationCountType.Total ? 3 : 0));
|
||||
client.emit(MatrixEventEvent.Decrypted, mkDecryptedEvent(room.roomId));
|
||||
|
||||
expect(vm.getSnapshot()).toMatchObject({ isNotification: true, symbol: "3" });
|
||||
expect(listener).toHaveBeenCalled();
|
||||
|
||||
vm.dispose();
|
||||
});
|
||||
|
||||
it("skips unchanged force-dot setter updates", () => {
|
||||
setUnreads(1, 0);
|
||||
const vm = new UnreadNotificationBadgeViewModel({ room, forceDot: false });
|
||||
|
||||
+19
-1
@@ -5,7 +5,13 @@
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type NotificationCount, type Room, RoomEvent } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
type MatrixEvent,
|
||||
MatrixEventEvent,
|
||||
type NotificationCount,
|
||||
type Room,
|
||||
RoomEvent,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
BaseViewModel,
|
||||
type NotificationBadgeType,
|
||||
@@ -152,12 +158,14 @@ export class UnreadNotificationBadgeViewModel
|
||||
for (const eventName of notificationChangeEvents) {
|
||||
room.on(eventName, this.onNotificationChanged);
|
||||
}
|
||||
room.client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
||||
|
||||
this.listenerCleanups.push(() => {
|
||||
room.off(RoomEvent.UnreadNotifications, this.onRoomUnreadNotifications);
|
||||
for (const eventName of notificationChangeEvents) {
|
||||
room.off(eventName, this.onNotificationChanged);
|
||||
}
|
||||
room.client.off(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -180,6 +188,16 @@ export class UnreadNotificationBadgeViewModel
|
||||
this.updateSnapshotFromProps();
|
||||
};
|
||||
|
||||
/**
|
||||
* An event which has not been decrypted yet has no renderer, so it does not count towards the
|
||||
* unread state. The room key often only turns up after the event itself, so re-evaluate the
|
||||
* unread state once an event of this room is decrypted.
|
||||
*/
|
||||
private readonly onEventDecrypted = (event: MatrixEvent): void => {
|
||||
if (event.getRoomId() !== this.props.room?.roomId) return;
|
||||
this.updateSnapshotFromProps();
|
||||
};
|
||||
|
||||
private readonly onHideBoldSettingChanged = (): void => {
|
||||
this.setHideBold(SettingsStore.getValue("feature_hidebold"));
|
||||
};
|
||||
|
||||
@@ -73,6 +73,7 @@ const clientMxcUrlToHttpMocks = new WeakMap<MatrixClient, Mock>();
|
||||
class TestRoom extends EventEmitter {
|
||||
public roomId = roomId;
|
||||
public getMember = vi.fn();
|
||||
public client = new EventEmitter() as unknown as MatrixClient;
|
||||
}
|
||||
|
||||
class TestThread extends EventEmitter {
|
||||
@@ -340,6 +341,27 @@ describe("ThreadSummaryViewModel", () => {
|
||||
expect(vm.getSnapshot().notificationIndicator).toBe("success");
|
||||
});
|
||||
|
||||
it("refreshes the notification indicator when an event of the room is decrypted", () => {
|
||||
const { vm, room } = makeSummaryVm();
|
||||
vi.mocked(determineUnreadState).mockClear();
|
||||
vi.mocked(determineUnreadState).mockReturnValue({
|
||||
symbol: null,
|
||||
count: 1,
|
||||
level: NotificationLevel.Notification,
|
||||
invited: false,
|
||||
});
|
||||
|
||||
room.client.emit(MatrixEventEvent.Decrypted, { getRoomId: () => "!other:example.org" } as MatrixEvent);
|
||||
|
||||
expect(determineUnreadState).not.toHaveBeenCalled();
|
||||
expect(vm.getSnapshot().notificationIndicator).toBeUndefined();
|
||||
|
||||
room.client.emit(MatrixEventEvent.Decrypted, { getRoomId: () => roomId } as MatrixEvent);
|
||||
|
||||
expect(determineUnreadState).toHaveBeenCalledWith(room, "$root", false);
|
||||
expect(vm.getSnapshot().notificationIndicator).toBe("success");
|
||||
});
|
||||
|
||||
it("rebinds thread listeners when the thread changes and removes them on dispose", () => {
|
||||
const { vm, thread, room } = makeSummaryVm();
|
||||
const nextRoom = new TestRoom() as TestRoom & Room;
|
||||
|
||||
@@ -11,6 +11,7 @@ import { type MouseEvent } from "react";
|
||||
import {
|
||||
type MatrixClient,
|
||||
type MatrixEvent,
|
||||
MatrixEventEvent,
|
||||
type NotificationCount,
|
||||
RoomEvent,
|
||||
type Room,
|
||||
@@ -527,6 +528,7 @@ export class ThreadSummaryViewModel
|
||||
room.on(RoomEvent.Redaction, this.onNotificationChanged);
|
||||
room.on(RoomEvent.LocalEchoUpdated, this.onNotificationChanged);
|
||||
room.on(RoomEvent.MyMembership, this.onNotificationChanged);
|
||||
room.client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
||||
this.listenerCleanups.push(() => {
|
||||
room.off(RoomEvent.UnreadNotifications, this.onRoomUnreadNotifications);
|
||||
room.off(RoomEvent.Receipt, this.onNotificationChanged);
|
||||
@@ -534,6 +536,7 @@ export class ThreadSummaryViewModel
|
||||
room.off(RoomEvent.Redaction, this.onNotificationChanged);
|
||||
room.off(RoomEvent.LocalEchoUpdated, this.onNotificationChanged);
|
||||
room.off(RoomEvent.MyMembership, this.onNotificationChanged);
|
||||
room.client.off(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -560,6 +563,16 @@ export class ThreadSummaryViewModel
|
||||
this.updateNotificationSnapshot();
|
||||
};
|
||||
|
||||
/**
|
||||
* An event which has not been decrypted yet has no renderer, so it does not count towards the
|
||||
* unread state. The room key often only turns up after the event itself, so re-evaluate the
|
||||
* unread state once an event of this room is decrypted.
|
||||
*/
|
||||
private readonly onEventDecrypted = (event: MatrixEvent): void => {
|
||||
if (event.getRoomId() !== this.props.thread.room.roomId) return;
|
||||
this.updateNotificationSnapshot();
|
||||
};
|
||||
|
||||
private updateSnapshotFromProps(): void {
|
||||
this.snapshot.merge({
|
||||
...ThreadSummaryViewModel.computeLayoutSnapshot(this.props),
|
||||
|
||||
Reference in New Issue
Block a user