Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8299abd

Browse files
authored
Fix Mark all as read in settings (#12205)
* Fix `Mark all as read` in settings * Update tests
1 parent 40ee1bb commit 8299abd

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

src/Unread.ts

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export function doesRoomHaveUnreadMessages(room: Room, includeThreads: boolean):
7474
}
7575

7676
function doesTimelineHaveUnreadMessages(room: Room, timeline: Array<MatrixEvent>): boolean {
77+
// The room is a space, let's ignore it
78+
if (room.isSpaceRoom()) return false;
79+
7780
const myUserId = room.client.getSafeUserId();
7881
const latestImportantEventId = findLatestImportantEvent(room.client, timeline)?.getId();
7982
if (latestImportantEventId) {

src/components/views/settings/Notifications.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
import { Caption } from "../typography/Caption";
5858
import { SettingsSubsectionHeading } from "./shared/SettingsSubsectionHeading";
5959
import SettingsSubsection from "./shared/SettingsSubsection";
60+
import { doesRoomHaveUnreadMessages } from "../../../Unread";
6061

6162
// TODO: this "view" component still has far too much application logic in it,
6263
// which should be factored out to other files.
@@ -739,7 +740,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
739740
category === RuleClass.VectorOther &&
740741
MatrixClientPeg.safeGet()
741742
.getRooms()
742-
.some((r) => r.getUnreadNotificationCount() > 0)
743+
.some((r) => doesRoomHaveUnreadMessages(r, true))
743744
) {
744745
clearNotifsButton = (
745746
<AccessibleButton

src/stores/ReadyWatchingStore.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
3434
public async start(): Promise<void> {
3535
this.dispatcherRef = this.dispatcher.register(this.onAction);
3636

37-
const matrixClient = MatrixClientPeg.get();
37+
// MatrixClientPeg can be undefined in tests because of circular dependencies with other stores
38+
const matrixClient = MatrixClientPeg?.get();
3839
if (matrixClient) {
3940
this.matrixClient = matrixClient;
4041
await this.onReady();

src/utils/notifications.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { IndicatorIcon } from "@vector-im/compound-web";
2626

2727
import SettingsStore from "../settings/SettingsStore";
2828
import { NotificationLevel } from "../stores/notifications/NotificationLevel";
29+
import { doesRoomHaveUnreadMessages } from "../Unread";
2930

3031
export const deviceNotificationSettingsKeys = [
3132
"notificationsEnabled",
@@ -105,7 +106,7 @@ export async function clearRoomNotification(room: Room, client: MatrixClient): P
105106
*/
106107
export function clearAllNotifications(client: MatrixClient): Promise<Array<{} | undefined>> {
107108
const receiptPromises = client.getRooms().reduce((promises: Array<Promise<{} | undefined>>, room: Room) => {
108-
if (room.getUnreadNotificationCount() > 0) {
109+
if (doesRoomHaveUnreadMessages(room, true)) {
109110
const promise = clearRoomNotification(room, client);
110111
promises.push(promise);
111112
}

test/Unread-test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ describe("Unread", () => {
421421
},
422422
);
423423
});
424+
425+
it("returns false for space", () => {
426+
jest.spyOn(room, "isSpaceRoom").mockReturnValue(true);
427+
expect(doesRoomHaveUnreadMessages(room, false)).toBe(false);
428+
});
424429
});
425430

426431
describe("doesRoomOrThreadHaveUnreadMessages()", () => {

test/components/views/settings/Notifications-test.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
LOCAL_NOTIFICATION_SETTINGS_PREFIX,
2222
MatrixEvent,
2323
Room,
24-
NotificationCountType,
2524
PushRuleActionName,
2625
TweakName,
2726
ConditionKind,
@@ -31,7 +30,7 @@ import {
3130
ThreepidMedium,
3231
} from "matrix-js-sdk/src/matrix";
3332
import { randomString } from "matrix-js-sdk/src/randomstring";
34-
import { act, fireEvent, getByTestId, render, screen, waitFor, within } from "@testing-library/react";
33+
import { act, fireEvent, getByTestId, render, screen, within } from "@testing-library/react";
3534
import { mocked } from "jest-mock";
3635
import userEvent from "@testing-library/user-event";
3736

@@ -900,8 +899,7 @@ describe("<Notifications />", () => {
900899
user: "@alice:example.org",
901900
ts: 1,
902901
});
903-
room.addLiveEvents([message]);
904-
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
902+
await room.addLiveEvents([message]);
905903

906904
const { container } = await getComponentAndWait();
907905
const clearNotificationEl = getByTestId(container, "clear-notifications");
@@ -910,10 +908,6 @@ describe("<Notifications />", () => {
910908

911909
expect(clearNotificationEl.className).toContain("mx_AccessibleButton_disabled");
912910
expect(mockClient.sendReadReceipt).toHaveBeenCalled();
913-
914-
await waitFor(() => {
915-
expect(clearNotificationEl).not.toBeInTheDocument();
916-
});
917911
});
918912
});
919913
});

0 commit comments

Comments
 (0)