Live location share - explicitly stop beacons replaced beacons (PSG-544) (#8933)
* explicitly stop beacons before creating new ones * remove unnecessary optional chainpull/28788/head^2
parent
d439871ea1
commit
4eab0deeb3
|
@ -106,7 +106,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
|
||||||
* ids of live beacons
|
* ids of live beacons
|
||||||
* ordered by creation time descending
|
* ordered by creation time descending
|
||||||
*/
|
*/
|
||||||
private liveBeaconIds = [];
|
private liveBeaconIds: BeaconIdentifier[] = [];
|
||||||
private locationInterval: number;
|
private locationInterval: number;
|
||||||
private geolocationError: GeolocationError | undefined;
|
private geolocationError: GeolocationError | undefined;
|
||||||
private clearPositionWatch: ClearWatchCallback | undefined;
|
private clearPositionWatch: ClearWatchCallback | undefined;
|
||||||
|
@ -392,6 +392,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
|
||||||
roomId: Room['roomId'],
|
roomId: Room['roomId'],
|
||||||
beaconInfoContent: MBeaconInfoEventContent,
|
beaconInfoContent: MBeaconInfoEventContent,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
// explicitly stop any live beacons this user has
|
||||||
|
// to ensure they remain stopped
|
||||||
|
// if the new replacing beacon is redacted
|
||||||
|
const existingLiveBeaconIdsForRoom = this.getLiveBeaconIds(roomId);
|
||||||
|
await Promise.all(existingLiveBeaconIdsForRoom.map(beaconId => this.stopBeacon(beaconId)));
|
||||||
|
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
const { event_id } = await this.matrixClient.unstable_createLiveBeacon(
|
const { event_id } = await this.matrixClient.unstable_createLiveBeacon(
|
||||||
roomId,
|
roomId,
|
||||||
|
|
|
@ -78,6 +78,7 @@ describe('<LocationShareMenu />', () => {
|
||||||
}),
|
}),
|
||||||
sendMessage: jest.fn(),
|
sendMessage: jest.fn(),
|
||||||
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: '1' }),
|
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: '1' }),
|
||||||
|
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: '1' }),
|
||||||
getVisibleRooms: jest.fn().mockReturnValue([]),
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -386,7 +387,7 @@ describe('<LocationShareMenu />', () => {
|
||||||
expect(getShareTypeOption(component, LocationShareType.Live).length).toBeFalsy();
|
expect(getShareTypeOption(component, LocationShareType.Live).length).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates beacon info event on submission', () => {
|
it('creates beacon info event on submission', async () => {
|
||||||
const onFinished = jest.fn();
|
const onFinished = jest.fn();
|
||||||
const component = getComponent({ onFinished });
|
const component = getComponent({ onFinished });
|
||||||
|
|
||||||
|
@ -399,6 +400,9 @@ describe('<LocationShareMenu />', () => {
|
||||||
component.setProps({});
|
component.setProps({});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// flush stopping existing beacons promises
|
||||||
|
await flushPromisesWithFakeTimers();
|
||||||
|
|
||||||
expect(onFinished).toHaveBeenCalled();
|
expect(onFinished).toHaveBeenCalled();
|
||||||
const [eventRoomId, eventContent] = mockClient.unstable_createLiveBeacon.mock.calls[0];
|
const [eventRoomId, eventContent] = mockClient.unstable_createLiveBeacon.mock.calls[0];
|
||||||
expect(eventRoomId).toEqual(defaultProps.roomId);
|
expect(eventRoomId).toEqual(defaultProps.roomId);
|
||||||
|
@ -429,6 +433,7 @@ describe('<LocationShareMenu />', () => {
|
||||||
component.setProps({});
|
component.setProps({});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await flushPromisesWithFakeTimers();
|
||||||
await flushPromisesWithFakeTimers();
|
await flushPromisesWithFakeTimers();
|
||||||
await flushPromisesWithFakeTimers();
|
await flushPromisesWithFakeTimers();
|
||||||
|
|
||||||
|
|
|
@ -1347,5 +1347,29 @@ describe('OwnBeaconStore', () => {
|
||||||
// didn't throw, no error log
|
// didn't throw, no error log
|
||||||
expect(loggerErrorSpy).not.toHaveBeenCalled();
|
expect(loggerErrorSpy).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('stops existing live beacon for room before creates new beacon', async () => {
|
||||||
|
// room1 already has a live beacon for alice
|
||||||
|
makeRoomsWithStateEvents([
|
||||||
|
alicesRoom1BeaconInfo,
|
||||||
|
alicesRoom2BeaconInfo,
|
||||||
|
]);
|
||||||
|
const store = await makeOwnBeaconStore();
|
||||||
|
|
||||||
|
const content = makeBeaconInfoContent(100);
|
||||||
|
await store.createLiveBeacon(room1Id, content);
|
||||||
|
|
||||||
|
// stop alicesRoom1BeaconInfo
|
||||||
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(
|
||||||
|
room1Id, expect.objectContaining({ live: false }),
|
||||||
|
);
|
||||||
|
// only called for beacons in room1, room2 beacon is not stopped
|
||||||
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
// new beacon created
|
||||||
|
expect(mockClient.unstable_createLiveBeacon).toHaveBeenCalledWith(
|
||||||
|
room1Id, content,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue