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

Commit f642765

Browse files
authored
Pass a client into RoomNotifs.getRoomNotifsState (#9631)
Pass an explicit client into `RoomNotifs.getRoomNotifsState`, rather than relying on `MatrixClientPeg`. This resolves a race condition where we have a component which thinks it is using a particular component, but `MatrixClientPeg` has been updated.
1 parent 8bd60d0 commit f642765

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

src/RoomNotifs.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
TweakName,
2626
} from "matrix-js-sdk/src/@types/PushRules";
2727
import { EventType } from 'matrix-js-sdk/src/@types/event';
28+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
2829

2930
import { MatrixClientPeg } from './MatrixClientPeg';
3031

@@ -35,8 +36,8 @@ export enum RoomNotifState {
3536
Mute = 'mute',
3637
}
3738

38-
export function getRoomNotifsState(roomId: string): RoomNotifState {
39-
if (MatrixClientPeg.get().isGuest()) return RoomNotifState.AllMessages;
39+
export function getRoomNotifsState(client: MatrixClient, roomId: string): RoomNotifState {
40+
if (client.isGuest()) return RoomNotifState.AllMessages;
4041

4142
// look through the override rules for a rule affecting this room:
4243
// if one exists, it will take precedence.
@@ -48,7 +49,7 @@ export function getRoomNotifsState(roomId: string): RoomNotifState {
4849
// for everything else, look at the room rule.
4950
let roomRule = null;
5051
try {
51-
roomRule = MatrixClientPeg.get().getRoomPushRule('global', roomId);
52+
roomRule = client.getRoomPushRule('global', roomId);
5253
} catch (err) {
5354
// Possible that the client doesn't have pushRules yet. If so, it
5455
// hasn't started either, so indicate that this room is not notifying.

src/hooks/useUnreadNotifications.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const useUnreadNotifications = (room: Room, threadId?: string): {
5555
setSymbol("!");
5656
setCount(1);
5757
setColor(NotificationColor.Red);
58-
} else if (getRoomNotifsState(room.roomId) === RoomNotifState.Mute) {
58+
} else if (getRoomNotifsState(room.client, room.roomId) === RoomNotifState.Mute) {
5959
setSymbol(null);
6060
setCount(0);
6161
setColor(NotificationColor.None);

src/stores/local-echo/RoomEchoChamber.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ export class RoomEchoChamber extends GenericEchoChamber<RoomEchoContext, CachedR
5050
private onAccountData = (event: MatrixEvent) => {
5151
if (event.getType() === EventType.PushRules) {
5252
const currentVolume = this.properties.get(CachedRoomKey.NotificationVolume);
53-
const newVolume = getRoomNotifsState(this.context.room.roomId);
53+
const newVolume = getRoomNotifsState(this.matrixClient, this.context.room.roomId);
5454
if (currentVolume !== newVolume) {
5555
this.updateNotificationVolume();
5656
}
5757
}
5858
};
5959

6060
private updateNotificationVolume() {
61-
this.properties.set(CachedRoomKey.NotificationVolume, getRoomNotifsState(this.context.room.roomId));
61+
this.properties.set(
62+
CachedRoomKey.NotificationVolume,
63+
getRoomNotifsState(this.matrixClient, this.context.room.roomId),
64+
);
6265
this.markEchoReceived(CachedRoomKey.NotificationVolume);
6366
this.emit(PROPERTY_UPDATED, CachedRoomKey.NotificationVolume);
6467
}

src/stores/notifications/RoomNotificationState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ export class RoomNotificationState extends NotificationState implements IDestroy
117117
this._color = NotificationColor.Unsent;
118118
this._symbol = "!";
119119
this._count = 1; // not used, technically
120-
} else if (RoomNotifs.getRoomNotifsState(this.room.roomId) === RoomNotifs.RoomNotifState.Mute) {
120+
} else if (RoomNotifs.getRoomNotifsState(
121+
this.room.client, this.room.roomId,
122+
) === RoomNotifs.RoomNotifState.Mute) {
121123
// When muted we suppress all notification states, even if we have context on them.
122124
this._color = NotificationColor.None;
123125
this._symbol = null;

test/RoomNotifs-test.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ describe("RoomNotifs test", () => {
3232
});
3333

3434
it("getRoomNotifsState handles rules with no conditions", () => {
35-
mocked(MatrixClientPeg.get()).pushRules = {
35+
const cli = MatrixClientPeg.get();
36+
mocked(cli).pushRules = {
3637
global: {
3738
override: [{
3839
rule_id: "!roomId:server",
@@ -42,16 +43,18 @@ describe("RoomNotifs test", () => {
4243
}],
4344
},
4445
};
45-
expect(getRoomNotifsState("!roomId:server")).toBe(null);
46+
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(null);
4647
});
4748

4849
it("getRoomNotifsState handles guest users", () => {
49-
mocked(MatrixClientPeg.get()).isGuest.mockReturnValue(true);
50-
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.AllMessages);
50+
const cli = MatrixClientPeg.get();
51+
mocked(cli).isGuest.mockReturnValue(true);
52+
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.AllMessages);
5153
});
5254

5355
it("getRoomNotifsState handles mute state", () => {
54-
MatrixClientPeg.get().pushRules = {
56+
const cli = MatrixClientPeg.get();
57+
cli.pushRules = {
5558
global: {
5659
override: [{
5760
rule_id: "!roomId:server",
@@ -66,27 +69,29 @@ describe("RoomNotifs test", () => {
6669
}],
6770
},
6871
};
69-
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.Mute);
72+
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.Mute);
7073
});
7174

7275
it("getRoomNotifsState handles mentions only", () => {
73-
MatrixClientPeg.get().getRoomPushRule = () => ({
76+
const cli = MatrixClientPeg.get();
77+
cli.getRoomPushRule = () => ({
7478
rule_id: "!roomId:server",
7579
enabled: true,
7680
default: false,
7781
actions: [PushRuleActionName.DontNotify],
7882
});
79-
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.MentionsOnly);
83+
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.MentionsOnly);
8084
});
8185

8286
it("getRoomNotifsState handles noisy", () => {
83-
MatrixClientPeg.get().getRoomPushRule = () => ({
87+
const cli = MatrixClientPeg.get();
88+
cli.getRoomPushRule = () => ({
8489
rule_id: "!roomId:server",
8590
enabled: true,
8691
default: false,
8792
actions: [{ set_tweak: TweakName.Sound, value: "default" }],
8893
});
89-
expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.AllMessagesLoud);
94+
expect(getRoomNotifsState(cli, "!roomId:server")).toBe(RoomNotifState.AllMessagesLoud);
9095
});
9196

9297
describe("getUnreadNotificationCount", () => {

0 commit comments

Comments
 (0)