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

Commit b646250

Browse files
authored
Support dynamic room predecessors in OwnBeaconStore (#10339)
* Support dynamic room predecessors in OwnBeaconStore * Fix type of dynamicWatcherRef * Mock a function missing from client
1 parent 50229ab commit b646250

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/stores/OwnBeaconStore.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
} from "../utils/beacon";
4444
import { getCurrentPosition } from "../utils/beacon";
4545
import { doMaybeLocalRoomAction } from "../utils/local-room";
46+
import SettingsStore from "../settings/SettingsStore";
4647

4748
const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconInfoOwner === userId;
4849

@@ -119,6 +120,10 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
119120
* when the target is stationary
120121
*/
121122
private lastPublishedPositionTimestamp?: number;
123+
/**
124+
* Ref returned from watchSetting for the MSC3946 labs flag
125+
*/
126+
private dynamicWatcherRef: string | undefined;
122127

123128
public constructor() {
124129
super(defaultDispatcher);
@@ -142,7 +147,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
142147
this.matrixClient.removeListener(BeaconEvent.Update, this.onUpdateBeacon);
143148
this.matrixClient.removeListener(BeaconEvent.Destroy, this.onDestroyBeacon);
144149
this.matrixClient.removeListener(RoomStateEvent.Members, this.onRoomStateMembers);
150+
SettingsStore.unwatchSetting(this.dynamicWatcherRef ?? "");
151+
152+
this.clearBeacons();
153+
}
145154

155+
private clearBeacons(): void {
146156
this.beacons.forEach((beacon) => beacon.destroy());
147157

148158
this.stopPollingLocation();
@@ -159,6 +169,11 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
159169
this.matrixClient.on(BeaconEvent.Update, this.onUpdateBeacon);
160170
this.matrixClient.on(BeaconEvent.Destroy, this.onDestroyBeacon);
161171
this.matrixClient.on(RoomStateEvent.Members, this.onRoomStateMembers);
172+
this.dynamicWatcherRef = SettingsStore.watchSetting(
173+
"feature_dynamic_room_predecessors",
174+
null,
175+
this.reinitialiseBeaconState,
176+
);
162177

163178
this.initialiseBeaconState();
164179
}
@@ -308,9 +323,19 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
308323
);
309324
}
310325

326+
/**
327+
* @internal public for test only
328+
*/
329+
public reinitialiseBeaconState = (): void => {
330+
this.clearBeacons();
331+
this.initialiseBeaconState();
332+
};
333+
311334
private initialiseBeaconState = (): void => {
312335
const userId = this.matrixClient.getUserId()!;
313-
const visibleRooms = this.matrixClient.getVisibleRooms();
336+
const visibleRooms = this.matrixClient.getVisibleRooms(
337+
SettingsStore.getValue("feature_dynamic_room_predecessors"),
338+
);
314339

315340
visibleRooms.forEach((room) => {
316341
const roomState = room.currentState;

test/components/views/beacon/RoomLiveShareWarning-test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe("<RoomLiveShareWarning />", () => {
4444
getUserId: jest.fn().mockReturnValue(aliceId),
4545
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
4646
sendEvent: jest.fn(),
47+
isGuest: jest.fn().mockReturnValue(false),
4748
});
4849

4950
// 14.03.2022 16:15

test/stores/OwnBeaconStore-test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from "../test-utils";
3939
import { makeBeaconInfoEvent, mockGeolocation, watchPositionMockImplementation } from "../test-utils/beacon";
4040
import { getMockClientWithEventEmitter } from "../test-utils/client";
41+
import SettingsStore from "../../src/settings/SettingsStore";
4142

4243
// modern fake timers and lodash.debounce are a faff
4344
// short circuit it
@@ -62,6 +63,7 @@ describe("OwnBeaconStore", () => {
6263
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
6364
sendEvent: jest.fn().mockResolvedValue({ event_id: "1" }),
6465
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
66+
isGuest: jest.fn().mockReturnValue(false),
6567
});
6668
const room1Id = "$room1:server.org";
6769
const room2Id = "$room2:server.org";
@@ -1204,4 +1206,41 @@ describe("OwnBeaconStore", () => {
12041206
expect(mockClient.unstable_createLiveBeacon).toHaveBeenCalledWith(room1Id, content);
12051207
});
12061208
});
1209+
1210+
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
1211+
beforeEach(() => {
1212+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
1213+
});
1214+
1215+
it("Passes through the dynamic predecessor setting", async () => {
1216+
mockClient.getVisibleRooms.mockReset();
1217+
mockClient.getVisibleRooms.mockReturnValue([]);
1218+
await makeOwnBeaconStore();
1219+
expect(mockClient.getVisibleRooms).toHaveBeenCalledWith(false);
1220+
});
1221+
});
1222+
1223+
describe("If the feature_dynamic_room_predecessors is enabled", () => {
1224+
beforeEach(() => {
1225+
// Turn on feature_dynamic_room_predecessors setting
1226+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
1227+
(settingName) => settingName === "feature_dynamic_room_predecessors",
1228+
);
1229+
});
1230+
1231+
it("Passes through the dynamic predecessor setting", async () => {
1232+
mockClient.getVisibleRooms.mockReset();
1233+
mockClient.getVisibleRooms.mockReturnValue([]);
1234+
await makeOwnBeaconStore();
1235+
expect(mockClient.getVisibleRooms).toHaveBeenCalledWith(true);
1236+
});
1237+
1238+
it("Passes through the dynamic predecessor when reinitialised", async () => {
1239+
const store = await makeOwnBeaconStore();
1240+
mockClient.getVisibleRooms.mockReset();
1241+
mockClient.getVisibleRooms.mockReturnValue([]);
1242+
store.reinitialiseBeaconState();
1243+
expect(mockClient.getVisibleRooms).toHaveBeenCalledWith(true);
1244+
});
1245+
});
12071246
});

0 commit comments

Comments
 (0)