Support dynamic room predecessors in OwnBeaconStore (#10339)

* Support dynamic room predecessors in OwnBeaconStore

* Fix type of dynamicWatcherRef

* Mock a function missing from client
This commit is contained in:
Andy Balaam 2023-03-10 09:15:54 +00:00 committed by GitHub
parent 50229abf1e
commit b646250c90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 1 deletions

View file

@ -43,6 +43,7 @@ import {
} from "../utils/beacon";
import { getCurrentPosition } from "../utils/beacon";
import { doMaybeLocalRoomAction } from "../utils/local-room";
import SettingsStore from "../settings/SettingsStore";
const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconInfoOwner === userId;
@ -119,6 +120,10 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
* when the target is stationary
*/
private lastPublishedPositionTimestamp?: number;
/**
* Ref returned from watchSetting for the MSC3946 labs flag
*/
private dynamicWatcherRef: string | undefined;
public constructor() {
super(defaultDispatcher);
@ -142,7 +147,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
this.matrixClient.removeListener(BeaconEvent.Update, this.onUpdateBeacon);
this.matrixClient.removeListener(BeaconEvent.Destroy, this.onDestroyBeacon);
this.matrixClient.removeListener(RoomStateEvent.Members, this.onRoomStateMembers);
SettingsStore.unwatchSetting(this.dynamicWatcherRef ?? "");
this.clearBeacons();
}
private clearBeacons(): void {
this.beacons.forEach((beacon) => beacon.destroy());
this.stopPollingLocation();
@ -159,6 +169,11 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
this.matrixClient.on(BeaconEvent.Update, this.onUpdateBeacon);
this.matrixClient.on(BeaconEvent.Destroy, this.onDestroyBeacon);
this.matrixClient.on(RoomStateEvent.Members, this.onRoomStateMembers);
this.dynamicWatcherRef = SettingsStore.watchSetting(
"feature_dynamic_room_predecessors",
null,
this.reinitialiseBeaconState,
);
this.initialiseBeaconState();
}
@ -308,9 +323,19 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
);
}
/**
* @internal public for test only
*/
public reinitialiseBeaconState = (): void => {
this.clearBeacons();
this.initialiseBeaconState();
};
private initialiseBeaconState = (): void => {
const userId = this.matrixClient.getUserId()!;
const visibleRooms = this.matrixClient.getVisibleRooms();
const visibleRooms = this.matrixClient.getVisibleRooms(
SettingsStore.getValue("feature_dynamic_room_predecessors"),
);
visibleRooms.forEach((room) => {
const roomState = room.currentState;

View file

@ -44,6 +44,7 @@ describe("<RoomLiveShareWarning />", () => {
getUserId: jest.fn().mockReturnValue(aliceId),
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
sendEvent: jest.fn(),
isGuest: jest.fn().mockReturnValue(false),
});
// 14.03.2022 16:15

View file

@ -38,6 +38,7 @@ import {
} from "../test-utils";
import { makeBeaconInfoEvent, mockGeolocation, watchPositionMockImplementation } from "../test-utils/beacon";
import { getMockClientWithEventEmitter } from "../test-utils/client";
import SettingsStore from "../../src/settings/SettingsStore";
// modern fake timers and lodash.debounce are a faff
// short circuit it
@ -62,6 +63,7 @@ describe("OwnBeaconStore", () => {
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
sendEvent: jest.fn().mockResolvedValue({ event_id: "1" }),
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
isGuest: jest.fn().mockReturnValue(false),
});
const room1Id = "$room1:server.org";
const room2Id = "$room2:server.org";
@ -1204,4 +1206,41 @@ describe("OwnBeaconStore", () => {
expect(mockClient.unstable_createLiveBeacon).toHaveBeenCalledWith(room1Id, content);
});
});
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
beforeEach(() => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
});
it("Passes through the dynamic predecessor setting", async () => {
mockClient.getVisibleRooms.mockReset();
mockClient.getVisibleRooms.mockReturnValue([]);
await makeOwnBeaconStore();
expect(mockClient.getVisibleRooms).toHaveBeenCalledWith(false);
});
});
describe("If the feature_dynamic_room_predecessors is enabled", () => {
beforeEach(() => {
// Turn on feature_dynamic_room_predecessors setting
jest.spyOn(SettingsStore, "getValue").mockImplementation(
(settingName) => settingName === "feature_dynamic_room_predecessors",
);
});
it("Passes through the dynamic predecessor setting", async () => {
mockClient.getVisibleRooms.mockReset();
mockClient.getVisibleRooms.mockReturnValue([]);
await makeOwnBeaconStore();
expect(mockClient.getVisibleRooms).toHaveBeenCalledWith(true);
});
it("Passes through the dynamic predecessor when reinitialised", async () => {
const store = await makeOwnBeaconStore();
mockClient.getVisibleRooms.mockReset();
mockClient.getVisibleRooms.mockReturnValue([]);
store.reinitialiseBeaconState();
expect(mockClient.getVisibleRooms).toHaveBeenCalledWith(true);
});
});
});