2022-03-16 19:35:09 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
import { debounce } from "lodash";
|
2022-03-16 19:35:09 +03:00
|
|
|
import {
|
|
|
|
Beacon,
|
2022-04-08 11:53:06 +03:00
|
|
|
BeaconIdentifier,
|
2022-03-16 19:35:09 +03:00
|
|
|
BeaconEvent,
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
2022-03-29 19:18:34 +03:00
|
|
|
RoomMember,
|
|
|
|
RoomState,
|
|
|
|
RoomStateEvent,
|
2022-03-16 19:35:09 +03:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-03-28 13:48:38 +03:00
|
|
|
import { BeaconInfoState, makeBeaconContent, makeBeaconInfoContent } from "matrix-js-sdk/src/content-helpers";
|
2022-04-22 15:05:36 +03:00
|
|
|
import { MBeaconInfoEventContent, M_BEACON } from "matrix-js-sdk/src/@types/beacon";
|
2022-03-28 13:48:38 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
|
|
|
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
|
2022-03-28 13:48:38 +03:00
|
|
|
import { arrayDiff } from "../utils/arrays";
|
|
|
|
import {
|
|
|
|
ClearWatchCallback,
|
|
|
|
GeolocationError,
|
|
|
|
mapGeolocationPositionToTimedGeo,
|
2022-03-31 14:51:44 +03:00
|
|
|
sortBeaconsByLatestCreation,
|
2022-03-28 13:48:38 +03:00
|
|
|
TimedGeoUri,
|
|
|
|
watchPosition,
|
2023-08-03 15:56:30 +03:00
|
|
|
getCurrentPosition,
|
2022-03-28 13:48:38 +03:00
|
|
|
} from "../utils/beacon";
|
2022-07-13 08:56:36 +03:00
|
|
|
import { doMaybeLocalRoomAction } from "../utils/local-room";
|
2023-03-10 12:15:54 +03:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconInfoOwner === userId;
|
|
|
|
|
|
|
|
export enum OwnBeaconStoreEvent {
|
2022-03-22 16:57:12 +03:00
|
|
|
LivenessChange = "OwnBeaconStore.LivenessChange",
|
2022-03-28 19:46:39 +03:00
|
|
|
MonitoringLivePosition = "OwnBeaconStore.MonitoringLivePosition",
|
2022-04-25 15:44:18 +03:00
|
|
|
LocationPublishError = "LocationPublishError",
|
2022-04-28 15:03:51 +03:00
|
|
|
BeaconUpdateError = "BeaconUpdateError",
|
2022-03-16 19:35:09 +03:00
|
|
|
}
|
|
|
|
|
2022-08-08 19:40:46 +03:00
|
|
|
const MOVING_UPDATE_INTERVAL = 5000;
|
2022-03-28 13:48:38 +03:00
|
|
|
const STATIC_UPDATE_INTERVAL = 30000;
|
|
|
|
|
2022-03-30 17:01:44 +03:00
|
|
|
const BAIL_AFTER_CONSECUTIVE_ERROR_COUNT = 2;
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
type OwnBeaconStoreState = {
|
2022-04-08 11:53:06 +03:00
|
|
|
beacons: Map<BeaconIdentifier, Beacon>;
|
2022-04-25 15:44:18 +03:00
|
|
|
beaconLocationPublishErrorCounts: Map<BeaconIdentifier, number>;
|
2022-04-28 15:03:51 +03:00
|
|
|
beaconUpdateErrors: Map<BeaconIdentifier, Error>;
|
2022-04-08 11:53:06 +03:00
|
|
|
beaconsByRoomId: Map<Room["roomId"], Set<BeaconIdentifier>>;
|
|
|
|
liveBeaconIds: BeaconIdentifier[];
|
2022-03-16 19:35:09 +03:00
|
|
|
};
|
2022-04-22 15:05:36 +03:00
|
|
|
|
|
|
|
const CREATED_BEACONS_KEY = "mx_live_beacon_created_id";
|
|
|
|
const removeLocallyCreateBeaconEventId = (eventId: string): void => {
|
|
|
|
const ids = getLocallyCreatedBeaconEventIds();
|
|
|
|
window.localStorage.setItem(CREATED_BEACONS_KEY, JSON.stringify(ids.filter((id) => id !== eventId)));
|
|
|
|
};
|
|
|
|
const storeLocallyCreateBeaconEventId = (eventId: string): void => {
|
|
|
|
const ids = getLocallyCreatedBeaconEventIds();
|
|
|
|
window.localStorage.setItem(CREATED_BEACONS_KEY, JSON.stringify([...ids, eventId]));
|
|
|
|
};
|
|
|
|
|
|
|
|
const getLocallyCreatedBeaconEventIds = (): string[] => {
|
|
|
|
let ids: string[];
|
|
|
|
try {
|
|
|
|
ids = JSON.parse(window.localStorage.getItem(CREATED_BEACONS_KEY) ?? "[]");
|
|
|
|
if (!Array.isArray(ids)) {
|
|
|
|
throw new Error("Invalid stored value");
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error("Failed to retrieve locally created beacon event ids", error);
|
|
|
|
ids = [];
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
};
|
2022-03-16 19:35:09 +03:00
|
|
|
export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
|
2022-08-30 22:13:39 +03:00
|
|
|
private static readonly internalInstance = (() => {
|
|
|
|
const instance = new OwnBeaconStore();
|
|
|
|
instance.start();
|
|
|
|
return instance;
|
|
|
|
})();
|
2022-03-22 16:57:12 +03:00
|
|
|
// users beacons, keyed by event type
|
2022-04-08 11:53:06 +03:00
|
|
|
public readonly beacons = new Map<BeaconIdentifier, Beacon>();
|
|
|
|
public readonly beaconsByRoomId = new Map<Room["roomId"], Set<BeaconIdentifier>>();
|
2022-03-30 15:31:19 +03:00
|
|
|
/**
|
2022-03-30 17:01:44 +03:00
|
|
|
* Track over the wire errors for published positions
|
|
|
|
* Counts consecutive wire errors per beacon
|
|
|
|
* Reset on successful publish of location
|
2022-03-30 15:31:19 +03:00
|
|
|
*/
|
2022-04-25 15:44:18 +03:00
|
|
|
public readonly beaconLocationPublishErrorCounts = new Map<BeaconIdentifier, number>();
|
2023-07-07 16:46:12 +03:00
|
|
|
public readonly beaconUpdateErrors = new Map<BeaconIdentifier, unknown>();
|
2022-03-31 14:51:44 +03:00
|
|
|
/**
|
|
|
|
* ids of live beacons
|
|
|
|
* ordered by creation time descending
|
|
|
|
*/
|
2022-06-30 10:33:51 +03:00
|
|
|
private liveBeaconIds: BeaconIdentifier[] = [];
|
2023-02-15 16:36:22 +03:00
|
|
|
private locationInterval?: number;
|
|
|
|
private clearPositionWatch?: ClearWatchCallback;
|
2022-03-28 13:48:38 +03:00
|
|
|
/**
|
|
|
|
* Track when the last position was published
|
|
|
|
* So we can manually get position on slow interval
|
|
|
|
* when the target is stationary
|
|
|
|
*/
|
2023-02-15 16:36:22 +03:00
|
|
|
private lastPublishedPositionTimestamp?: number;
|
2023-03-10 12:15:54 +03:00
|
|
|
/**
|
|
|
|
* Ref returned from watchSetting for the MSC3946 labs flag
|
|
|
|
*/
|
|
|
|
private dynamicWatcherRef: string | undefined;
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
super(defaultDispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get instance(): OwnBeaconStore {
|
|
|
|
return OwnBeaconStore.internalInstance;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
/**
|
|
|
|
* True when we have live beacons
|
|
|
|
* and geolocation.watchPosition is active
|
|
|
|
*/
|
|
|
|
public get isMonitoringLiveLocation(): boolean {
|
|
|
|
return !!this.clearPositionWatch;
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
protected async onNotReady(): Promise<void> {
|
2023-04-06 13:10:14 +03:00
|
|
|
if (this.matrixClient) {
|
|
|
|
this.matrixClient.removeListener(BeaconEvent.LivenessChange, this.onBeaconLiveness);
|
|
|
|
this.matrixClient.removeListener(BeaconEvent.New, this.onNewBeacon);
|
|
|
|
this.matrixClient.removeListener(BeaconEvent.Update, this.onUpdateBeacon);
|
|
|
|
this.matrixClient.removeListener(BeaconEvent.Destroy, this.onDestroyBeacon);
|
|
|
|
this.matrixClient.removeListener(RoomStateEvent.Members, this.onRoomStateMembers);
|
|
|
|
}
|
2023-03-10 12:15:54 +03:00
|
|
|
SettingsStore.unwatchSetting(this.dynamicWatcherRef ?? "");
|
|
|
|
|
|
|
|
this.clearBeacons();
|
|
|
|
}
|
2022-03-16 19:35:09 +03:00
|
|
|
|
2023-03-10 12:15:54 +03:00
|
|
|
private clearBeacons(): void {
|
2022-03-16 19:35:09 +03:00
|
|
|
this.beacons.forEach((beacon) => beacon.destroy());
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
this.stopPollingLocation();
|
2022-03-16 19:35:09 +03:00
|
|
|
this.beacons.clear();
|
|
|
|
this.beaconsByRoomId.clear();
|
|
|
|
this.liveBeaconIds = [];
|
2022-04-25 15:44:18 +03:00
|
|
|
this.beaconLocationPublishErrorCounts.clear();
|
2022-04-28 15:03:51 +03:00
|
|
|
this.beaconUpdateErrors.clear();
|
2022-03-16 19:35:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async onReady(): Promise<void> {
|
2023-04-06 13:10:14 +03:00
|
|
|
if (this.matrixClient) {
|
|
|
|
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness);
|
|
|
|
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon);
|
|
|
|
this.matrixClient.on(BeaconEvent.Update, this.onUpdateBeacon);
|
|
|
|
this.matrixClient.on(BeaconEvent.Destroy, this.onDestroyBeacon);
|
|
|
|
this.matrixClient.on(RoomStateEvent.Members, this.onRoomStateMembers);
|
|
|
|
}
|
2023-03-10 12:15:54 +03:00
|
|
|
this.dynamicWatcherRef = SettingsStore.watchSetting(
|
|
|
|
"feature_dynamic_room_predecessors",
|
|
|
|
null,
|
|
|
|
this.reinitialiseBeaconState,
|
|
|
|
);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
this.initialiseBeaconState();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onAction(payload: ActionPayload): Promise<void> {
|
|
|
|
// we don't actually do anything here
|
|
|
|
}
|
|
|
|
|
2022-03-31 14:51:44 +03:00
|
|
|
public hasLiveBeacons = (roomId?: string): boolean => {
|
2022-03-16 19:35:09 +03:00
|
|
|
return !!this.getLiveBeaconIds(roomId).length;
|
2022-03-31 14:51:44 +03:00
|
|
|
};
|
2022-03-16 19:35:09 +03:00
|
|
|
|
2022-03-31 11:57:12 +03:00
|
|
|
/**
|
|
|
|
* Some live beacon has a wire error
|
|
|
|
* Optionally filter by room
|
|
|
|
*/
|
2022-04-25 15:44:18 +03:00
|
|
|
public hasLocationPublishErrors = (roomId?: string): boolean => {
|
|
|
|
return this.getLiveBeaconIds(roomId).some(this.beaconHasLocationPublishError);
|
2022-03-31 14:51:44 +03:00
|
|
|
};
|
2022-03-31 11:57:12 +03:00
|
|
|
|
2022-03-30 17:01:44 +03:00
|
|
|
/**
|
|
|
|
* If a beacon has failed to publish position
|
|
|
|
* past the allowed consecutive failure count (BAIL_AFTER_CONSECUTIVE_ERROR_COUNT)
|
|
|
|
* Then consider it to have an error
|
|
|
|
*/
|
2022-04-25 15:44:18 +03:00
|
|
|
public beaconHasLocationPublishError = (beaconId: string): boolean => {
|
2023-04-06 13:10:14 +03:00
|
|
|
const counts = this.beaconLocationPublishErrorCounts.get(beaconId);
|
|
|
|
return counts !== undefined && counts >= BAIL_AFTER_CONSECUTIVE_ERROR_COUNT;
|
2022-03-31 11:57:12 +03:00
|
|
|
};
|
2022-03-30 17:01:44 +03:00
|
|
|
|
2022-04-25 15:44:18 +03:00
|
|
|
public resetLocationPublishError = (beaconId: string): void => {
|
|
|
|
this.incrementBeaconLocationPublishErrorCount(beaconId, false);
|
2022-03-30 17:01:44 +03:00
|
|
|
|
|
|
|
// always publish to all live beacons together
|
|
|
|
// instead of just one that was changed
|
|
|
|
// to keep lastPublishedTimestamp simple
|
|
|
|
// and extra published locations don't hurt
|
|
|
|
this.publishCurrentLocationToBeacons();
|
2022-03-31 11:57:12 +03:00
|
|
|
};
|
2022-03-30 17:01:44 +03:00
|
|
|
|
2022-03-31 14:51:44 +03:00
|
|
|
public getLiveBeaconIds = (roomId?: string): string[] => {
|
2022-03-16 19:35:09 +03:00
|
|
|
if (!roomId) {
|
|
|
|
return this.liveBeaconIds;
|
|
|
|
}
|
|
|
|
return this.liveBeaconIds.filter((beaconId) => this.beaconsByRoomId.get(roomId)?.has(beaconId));
|
2022-03-31 14:51:44 +03:00
|
|
|
};
|
|
|
|
|
2022-04-25 15:44:18 +03:00
|
|
|
public getLiveBeaconIdsWithLocationPublishError = (roomId?: string): string[] => {
|
|
|
|
return this.getLiveBeaconIds(roomId).filter(this.beaconHasLocationPublishError);
|
2022-03-31 14:51:44 +03:00
|
|
|
};
|
2022-03-16 19:35:09 +03:00
|
|
|
|
2022-03-31 14:51:44 +03:00
|
|
|
public getBeaconById = (beaconId: string): Beacon | undefined => {
|
2022-03-22 16:57:12 +03:00
|
|
|
return this.beacons.get(beaconId);
|
2022-03-31 14:51:44 +03:00
|
|
|
};
|
2022-03-22 16:57:12 +03:00
|
|
|
|
2022-04-08 11:53:06 +03:00
|
|
|
public stopBeacon = async (beaconIdentifier: string): Promise<void> => {
|
|
|
|
const beacon = this.beacons.get(beaconIdentifier);
|
2022-03-18 16:38:41 +03:00
|
|
|
// if no beacon, or beacon is already explicitly set isLive: false
|
|
|
|
// do nothing
|
|
|
|
if (!beacon?.beaconInfo?.live) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:05:36 +03:00
|
|
|
await this.updateBeaconEvent(beacon, { live: false });
|
|
|
|
// prune from local store
|
|
|
|
removeLocallyCreateBeaconEventId(beacon.beaconInfoId);
|
2022-03-18 16:38:41 +03:00
|
|
|
};
|
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
/**
|
|
|
|
* Listeners
|
|
|
|
*/
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
private onNewBeacon = (_event: MatrixEvent, beacon: Beacon): void => {
|
2023-04-06 13:10:14 +03:00
|
|
|
if (!this.matrixClient || !isOwnBeacon(beacon, this.matrixClient.getUserId()!)) {
|
2022-03-16 19:35:09 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.addBeacon(beacon);
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
2022-04-08 11:53:06 +03:00
|
|
|
/**
|
|
|
|
* This will be called when a beacon is replaced
|
|
|
|
*/
|
|
|
|
private onUpdateBeacon = (_event: MatrixEvent, beacon: Beacon): void => {
|
2023-04-06 13:10:14 +03:00
|
|
|
if (!this.matrixClient || !isOwnBeacon(beacon, this.matrixClient.getUserId()!)) {
|
2022-04-08 11:53:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkLiveness();
|
2022-04-11 12:16:32 +03:00
|
|
|
beacon.monitorLiveness();
|
2022-04-08 11:53:06 +03:00
|
|
|
};
|
|
|
|
|
2022-04-22 15:05:36 +03:00
|
|
|
private onDestroyBeacon = (beaconIdentifier: BeaconIdentifier): void => {
|
|
|
|
// check if we care about this beacon
|
|
|
|
if (!this.beacons.has(beaconIdentifier)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
private onBeaconLiveness = (isLive: boolean, beacon: Beacon): void => {
|
|
|
|
// check if we care about this beacon
|
2022-03-22 16:57:12 +03:00
|
|
|
if (!this.beacons.has(beacon.identifier)) {
|
2022-03-16 19:35:09 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:38:41 +03:00
|
|
|
// beacon expired, update beacon to un-alive state
|
|
|
|
if (!isLive) {
|
2022-03-22 16:57:12 +03:00
|
|
|
this.stopBeacon(beacon.identifier);
|
2022-03-18 16:38:41 +03:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
this.checkLiveness();
|
2022-03-18 16:38:41 +03:00
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
this.emit(OwnBeaconStoreEvent.LivenessChange, this.getLiveBeaconIds());
|
2022-03-16 19:35:09 +03:00
|
|
|
};
|
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
/**
|
|
|
|
* Check for changes in membership in rooms with beacons
|
|
|
|
* and stop monitoring beacons in rooms user is no longer member of
|
|
|
|
*/
|
|
|
|
private onRoomStateMembers = (_event: MatrixEvent, roomState: RoomState, member: RoomMember): void => {
|
|
|
|
// no beacons for this room, ignore
|
2023-04-06 13:10:14 +03:00
|
|
|
if (
|
|
|
|
!this.matrixClient ||
|
|
|
|
!this.beaconsByRoomId.has(roomState.roomId) ||
|
|
|
|
member.userId !== this.matrixClient.getUserId()
|
|
|
|
) {
|
2022-03-29 19:18:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check powerlevels here
|
|
|
|
// in PSF-797
|
|
|
|
|
|
|
|
// stop watching beacons in rooms where user is no longer a member
|
|
|
|
if (member.membership === "leave" || member.membership === "ban") {
|
2022-04-22 15:05:36 +03:00
|
|
|
this.beaconsByRoomId.get(roomState.roomId)?.forEach(this.removeBeacon);
|
2022-03-29 19:18:34 +03:00
|
|
|
this.beaconsByRoomId.delete(roomState.roomId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* State management
|
|
|
|
*/
|
|
|
|
|
2022-03-30 17:01:44 +03:00
|
|
|
/**
|
|
|
|
* Live beacon ids that do not have wire errors
|
|
|
|
*/
|
2023-01-12 16:25:14 +03:00
|
|
|
private get healthyLiveBeaconIds(): string[] {
|
2022-04-28 15:03:51 +03:00
|
|
|
return this.liveBeaconIds.filter(
|
|
|
|
(beaconId) => !this.beaconHasLocationPublishError(beaconId) && !this.beaconUpdateErrors.has(beaconId),
|
|
|
|
);
|
2022-03-30 17:01:44 +03:00
|
|
|
}
|
|
|
|
|
2023-03-10 12:15:54 +03:00
|
|
|
/**
|
|
|
|
* @internal public for test only
|
|
|
|
*/
|
|
|
|
public reinitialiseBeaconState = (): void => {
|
|
|
|
this.clearBeacons();
|
|
|
|
this.initialiseBeaconState();
|
|
|
|
};
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
private initialiseBeaconState = (): void => {
|
2023-04-06 13:10:14 +03:00
|
|
|
if (!this.matrixClient) return;
|
|
|
|
const userId = this.matrixClient.getSafeUserId();
|
2023-03-10 12:15:54 +03:00
|
|
|
const visibleRooms = this.matrixClient.getVisibleRooms(
|
|
|
|
SettingsStore.getValue("feature_dynamic_room_predecessors"),
|
|
|
|
);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
visibleRooms.forEach((room) => {
|
|
|
|
const roomState = room.currentState;
|
|
|
|
const beacons = roomState.beacons;
|
|
|
|
const ownBeaconsArray = [...beacons.values()].filter((beacon) => isOwnBeacon(beacon, userId));
|
|
|
|
ownBeaconsArray.forEach((beacon) => this.addBeacon(beacon));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
|
|
|
private addBeacon = (beacon: Beacon): void => {
|
2022-03-22 16:57:12 +03:00
|
|
|
this.beacons.set(beacon.identifier, beacon);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
if (!this.beaconsByRoomId.has(beacon.roomId)) {
|
|
|
|
this.beaconsByRoomId.set(beacon.roomId, new Set<string>());
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
this.beaconsByRoomId.get(beacon.roomId)!.add(beacon.identifier);
|
2022-03-18 16:38:41 +03:00
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
beacon.monitorLiveness();
|
|
|
|
};
|
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
/**
|
|
|
|
* Remove listeners for a given beacon
|
|
|
|
* remove from state
|
|
|
|
* and update liveness if changed
|
|
|
|
*/
|
|
|
|
private removeBeacon = (beaconId: string): void => {
|
|
|
|
if (!this.beacons.has(beaconId)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-15 16:36:22 +03:00
|
|
|
this.beacons.get(beaconId)!.destroy();
|
2022-03-29 19:18:34 +03:00
|
|
|
this.beacons.delete(beaconId);
|
|
|
|
|
|
|
|
this.checkLiveness();
|
|
|
|
};
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
private checkLiveness = (): void => {
|
2022-04-22 15:05:36 +03:00
|
|
|
const locallyCreatedBeaconEventIds = getLocallyCreatedBeaconEventIds();
|
2022-03-22 16:57:12 +03:00
|
|
|
const prevLiveBeaconIds = this.getLiveBeaconIds();
|
2022-03-16 19:35:09 +03:00
|
|
|
this.liveBeaconIds = [...this.beacons.values()]
|
2022-04-22 15:05:36 +03:00
|
|
|
.filter(
|
|
|
|
(beacon) =>
|
|
|
|
beacon.isLive &&
|
|
|
|
// only beacons created on this device should be shared to
|
|
|
|
locallyCreatedBeaconEventIds.includes(beacon.beaconInfoId),
|
|
|
|
)
|
2022-03-31 14:51:44 +03:00
|
|
|
.sort(sortBeaconsByLatestCreation)
|
2022-03-22 16:57:12 +03:00
|
|
|
.map((beacon) => beacon.identifier);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
const diff = arrayDiff(prevLiveBeaconIds, this.liveBeaconIds);
|
|
|
|
|
|
|
|
if (diff.added.length || diff.removed.length) {
|
2022-03-22 16:57:12 +03:00
|
|
|
this.emit(OwnBeaconStoreEvent.LivenessChange, this.liveBeaconIds);
|
2022-03-16 19:35:09 +03:00
|
|
|
}
|
2022-03-28 13:48:38 +03:00
|
|
|
|
|
|
|
// publish current location immediately
|
|
|
|
// when there are new live beacons
|
|
|
|
// and we already have a live monitor
|
|
|
|
// so first position is published quickly
|
|
|
|
// even when target is stationary
|
|
|
|
//
|
|
|
|
// when there is no existing live monitor
|
|
|
|
// it will be created below by togglePollingLocation
|
|
|
|
// and publish first position quickly
|
|
|
|
if (diff.added.length && this.isMonitoringLiveLocation) {
|
|
|
|
this.publishCurrentLocationToBeacons();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if overall liveness changed
|
|
|
|
if (!!prevLiveBeaconIds?.length !== !!this.liveBeaconIds.length) {
|
|
|
|
this.togglePollingLocation();
|
|
|
|
}
|
2022-03-16 19:35:09 +03:00
|
|
|
};
|
2022-03-18 16:38:41 +03:00
|
|
|
|
2022-04-22 15:05:36 +03:00
|
|
|
public createLiveBeacon = async (
|
|
|
|
roomId: Room["roomId"],
|
|
|
|
beaconInfoContent: MBeaconInfoEventContent,
|
|
|
|
): Promise<void> => {
|
2023-04-21 13:50:42 +03:00
|
|
|
if (!this.matrixClient) return;
|
2022-06-30 10:33:51 +03:00
|
|
|
// 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)));
|
|
|
|
|
2022-04-22 15:05:36 +03:00
|
|
|
// eslint-disable-next-line camelcase
|
2022-07-13 08:56:36 +03:00
|
|
|
const { event_id } = await doMaybeLocalRoomAction(
|
2022-04-22 15:05:36 +03:00
|
|
|
roomId,
|
2023-04-21 13:50:42 +03:00
|
|
|
(actualRoomId: string) => this.matrixClient!.unstable_createLiveBeacon(actualRoomId, beaconInfoContent),
|
2022-07-13 08:56:36 +03:00
|
|
|
this.matrixClient,
|
2022-04-22 15:05:36 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
storeLocallyCreateBeaconEventId(event_id);
|
|
|
|
};
|
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
/**
|
|
|
|
* Geolocation
|
|
|
|
*/
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
private togglePollingLocation = (): void => {
|
2022-03-28 13:48:38 +03:00
|
|
|
if (!!this.liveBeaconIds.length) {
|
2022-03-28 19:46:39 +03:00
|
|
|
this.startPollingLocation();
|
|
|
|
} else {
|
|
|
|
this.stopPollingLocation();
|
2022-03-28 13:48:38 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
private startPollingLocation = async (): Promise<void> => {
|
2022-03-28 13:48:38 +03:00
|
|
|
// clear any existing interval
|
|
|
|
this.stopPollingLocation();
|
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
try {
|
2022-05-04 00:04:37 +03:00
|
|
|
this.clearPositionWatch = watchPosition(this.onWatchedPosition, this.onGeolocationError);
|
2022-03-28 19:46:39 +03:00
|
|
|
} catch (error) {
|
2023-07-07 16:46:12 +03:00
|
|
|
if (error instanceof Error) {
|
|
|
|
this.onGeolocationError(error.message as GeolocationError);
|
|
|
|
} else {
|
|
|
|
console.error("Unexpected error", error);
|
|
|
|
}
|
2022-03-28 19:46:39 +03:00
|
|
|
// don't set locationInterval if geolocation failed to setup
|
|
|
|
return;
|
|
|
|
}
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2022-11-30 14:32:56 +03:00
|
|
|
this.locationInterval = window.setInterval(() => {
|
2022-03-28 13:48:38 +03:00
|
|
|
if (!this.lastPublishedPositionTimestamp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if position was last updated STATIC_UPDATE_INTERVAL ms ago or more
|
|
|
|
// get our position and publish it
|
|
|
|
if (this.lastPublishedPositionTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
|
|
|
|
this.publishCurrentLocationToBeacons();
|
|
|
|
}
|
|
|
|
}, STATIC_UPDATE_INTERVAL);
|
2022-03-28 19:46:39 +03:00
|
|
|
|
|
|
|
this.emit(OwnBeaconStoreEvent.MonitoringLivePosition);
|
2022-03-28 13:48:38 +03:00
|
|
|
};
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
private stopPollingLocation = (): void => {
|
2022-03-28 13:48:38 +03:00
|
|
|
clearInterval(this.locationInterval);
|
|
|
|
this.locationInterval = undefined;
|
|
|
|
this.lastPublishedPositionTimestamp = undefined;
|
|
|
|
|
|
|
|
if (this.clearPositionWatch) {
|
|
|
|
this.clearPositionWatch();
|
|
|
|
this.clearPositionWatch = undefined;
|
|
|
|
}
|
2022-03-28 19:46:39 +03:00
|
|
|
|
|
|
|
this.emit(OwnBeaconStoreEvent.MonitoringLivePosition);
|
2022-03-28 13:48:38 +03:00
|
|
|
};
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
private onWatchedPosition = (position: GeolocationPosition): void => {
|
2022-03-29 19:18:34 +03:00
|
|
|
const timedGeoPosition = mapGeolocationPositionToTimedGeo(position);
|
|
|
|
|
2022-05-10 01:52:05 +03:00
|
|
|
// if this is our first position, publish immediately
|
2022-03-29 19:18:34 +03:00
|
|
|
if (!this.lastPublishedPositionTimestamp) {
|
|
|
|
this.publishLocationToBeacons(timedGeoPosition);
|
|
|
|
} else {
|
|
|
|
this.debouncedPublishLocationToBeacons(timedGeoPosition);
|
|
|
|
}
|
2022-03-28 13:48:38 +03:00
|
|
|
};
|
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
private onGeolocationError = async (error: GeolocationError): Promise<void> => {
|
2023-07-07 16:46:12 +03:00
|
|
|
logger.error("Geolocation failed", error);
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
// other errors are considered non-fatal
|
|
|
|
// and self recovering
|
|
|
|
if (![GeolocationError.Unavailable, GeolocationError.PermissionDenied].includes(error)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stopPollingLocation();
|
|
|
|
// kill live beacons when location permissions are revoked
|
|
|
|
await Promise.all(this.liveBeaconIds.map(this.stopBeacon));
|
2022-03-28 13:48:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current location
|
|
|
|
* (as opposed to using watched location)
|
|
|
|
* and publishes it to all live beacons
|
|
|
|
*/
|
2023-01-12 16:25:14 +03:00
|
|
|
private publishCurrentLocationToBeacons = async (): Promise<void> => {
|
2022-03-28 19:46:39 +03:00
|
|
|
try {
|
|
|
|
const position = await getCurrentPosition();
|
|
|
|
this.publishLocationToBeacons(mapGeolocationPositionToTimedGeo(position));
|
|
|
|
} catch (error) {
|
2023-07-07 16:46:12 +03:00
|
|
|
if (error instanceof Error) {
|
|
|
|
this.onGeolocationError(error.message as GeolocationError);
|
|
|
|
} else {
|
|
|
|
console.error("Unexpected error", error);
|
|
|
|
}
|
2022-03-28 19:46:39 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-29 19:18:34 +03:00
|
|
|
/**
|
|
|
|
* MatrixClient api
|
|
|
|
*/
|
2022-03-28 19:46:39 +03:00
|
|
|
|
2022-04-28 15:03:51 +03:00
|
|
|
/**
|
|
|
|
* Updates beacon with provided content update
|
|
|
|
* Records error in beaconUpdateErrors
|
|
|
|
* rethrows
|
|
|
|
*/
|
2022-03-29 19:18:34 +03:00
|
|
|
private updateBeaconEvent = async (beacon: Beacon, update: Partial<BeaconInfoState>): Promise<void> => {
|
|
|
|
const { description, timeout, timestamp, live, assetType } = {
|
|
|
|
...beacon.beaconInfo,
|
|
|
|
...update,
|
|
|
|
};
|
2022-03-28 19:46:39 +03:00
|
|
|
|
2022-04-22 14:38:27 +03:00
|
|
|
const updateContent = makeBeaconInfoContent(timeout, live, description, assetType, timestamp);
|
2022-03-29 19:18:34 +03:00
|
|
|
|
2022-04-28 15:03:51 +03:00
|
|
|
try {
|
2023-04-21 13:50:42 +03:00
|
|
|
await this.matrixClient!.unstable_setLiveBeacon(beacon.roomId, updateContent);
|
2022-04-28 15:03:51 +03:00
|
|
|
// cleanup any errors
|
|
|
|
const hadError = this.beaconUpdateErrors.has(beacon.identifier);
|
|
|
|
if (hadError) {
|
|
|
|
this.beaconUpdateErrors.delete(beacon.identifier);
|
|
|
|
this.emit(OwnBeaconStoreEvent.BeaconUpdateError, beacon.identifier, false);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error("Failed to update beacon", error);
|
|
|
|
this.beaconUpdateErrors.set(beacon.identifier, error);
|
|
|
|
this.emit(OwnBeaconStoreEvent.BeaconUpdateError, beacon.identifier, true);
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2022-03-29 19:18:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends m.location events to all live beacons
|
|
|
|
* Sets last published beacon
|
|
|
|
*/
|
2023-01-12 16:25:14 +03:00
|
|
|
private publishLocationToBeacons = async (position: TimedGeoUri): Promise<void> => {
|
2022-03-29 19:18:34 +03:00
|
|
|
this.lastPublishedPositionTimestamp = Date.now();
|
2022-03-30 17:01:44 +03:00
|
|
|
await Promise.all(
|
|
|
|
this.healthyLiveBeaconIds.map((beaconId) =>
|
2023-04-21 13:50:42 +03:00
|
|
|
this.beacons.has(beaconId) ? this.sendLocationToBeacon(this.beacons.get(beaconId)!, position) : null,
|
2022-12-12 14:24:14 +03:00
|
|
|
),
|
2022-03-29 19:18:34 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private debouncedPublishLocationToBeacons = debounce(this.publishLocationToBeacons, MOVING_UPDATE_INTERVAL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends m.location event to referencing given beacon
|
|
|
|
*/
|
2023-01-12 16:25:14 +03:00
|
|
|
private sendLocationToBeacon = async (beacon: Beacon, { geoUri, timestamp }: TimedGeoUri): Promise<void> => {
|
2022-03-29 19:18:34 +03:00
|
|
|
const content = makeBeaconContent(geoUri, timestamp, beacon.beaconInfoId);
|
2022-03-30 15:31:19 +03:00
|
|
|
try {
|
2023-04-21 13:50:42 +03:00
|
|
|
await this.matrixClient!.sendEvent(beacon.roomId, M_BEACON.name, content);
|
2022-04-25 15:44:18 +03:00
|
|
|
this.incrementBeaconLocationPublishErrorCount(beacon.identifier, false);
|
2022-03-30 15:31:19 +03:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2022-04-25 15:44:18 +03:00
|
|
|
this.incrementBeaconLocationPublishErrorCount(beacon.identifier, true);
|
2022-03-30 17:01:44 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage beacon wire error count
|
|
|
|
* - clear count for beacon when not error
|
|
|
|
* - increment count for beacon when is error
|
|
|
|
* - emit if beacon error count crossed threshold
|
|
|
|
*/
|
2022-04-25 15:44:18 +03:00
|
|
|
private incrementBeaconLocationPublishErrorCount = (beaconId: string, isError: boolean): void => {
|
|
|
|
const hadError = this.beaconHasLocationPublishError(beaconId);
|
2022-03-30 17:01:44 +03:00
|
|
|
|
|
|
|
if (isError) {
|
|
|
|
// increment error count
|
2022-04-25 15:44:18 +03:00
|
|
|
this.beaconLocationPublishErrorCounts.set(
|
2022-03-30 17:01:44 +03:00
|
|
|
beaconId,
|
2022-04-25 15:44:18 +03:00
|
|
|
(this.beaconLocationPublishErrorCounts.get(beaconId) ?? 0) + 1,
|
2022-03-30 17:01:44 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// clear any error count
|
2022-04-25 15:44:18 +03:00
|
|
|
this.beaconLocationPublishErrorCounts.delete(beaconId);
|
2022-03-30 17:01:44 +03:00
|
|
|
}
|
|
|
|
|
2022-04-25 15:44:18 +03:00
|
|
|
if (this.beaconHasLocationPublishError(beaconId) !== hadError) {
|
|
|
|
this.emit(OwnBeaconStoreEvent.LocationPublishError, beaconId);
|
2022-03-30 15:31:19 +03:00
|
|
|
}
|
2022-03-28 13:48:38 +03:00
|
|
|
};
|
2022-03-16 19:35:09 +03:00
|
|
|
}
|