From d18aae09c8575daf98c671b2c403563a1ca30bab Mon Sep 17 00:00:00 2001 From: ElementRobot Date: Tue, 16 Aug 2022 14:50:49 +0100 Subject: [PATCH] Fix: Handle parsing of a beacon info event without asset (#2591) (#2592) * test case * handle missing beacon info asset * default beacon info asset type to self * make BeaconLocationState.assetType optional (cherry picked from commit be3e731499fb2458edb88be60f7f172b78ebba2c) Co-authored-by: Kerry --- spec/unit/models/beacon.spec.ts | 19 +++++++++++++++++++ src/content-helpers.ts | 6 +++--- src/models/beacon.ts | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/spec/unit/models/beacon.spec.ts b/spec/unit/models/beacon.spec.ts index 73b6bc552d2..c0de3591b5b 100644 --- a/spec/unit/models/beacon.spec.ts +++ b/spec/unit/models/beacon.spec.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { MatrixEvent } from "../../../src"; +import { M_BEACON_INFO } from "../../../src/@types/beacon"; import { isTimestampInDuration, Beacon, @@ -129,6 +130,24 @@ describe('Beacon', () => { expect(beacon.beaconInfo).toBeTruthy(); }); + it('creates beacon without error from a malformed event', () => { + const event = new MatrixEvent({ + type: M_BEACON_INFO.name, + room_id: roomId, + state_key: userId, + content: {}, + }); + const beacon = new Beacon(event); + + expect(beacon.beaconInfoId).toEqual(event.getId()); + expect(beacon.roomId).toEqual(roomId); + expect(beacon.isLive).toEqual(false); + expect(beacon.beaconInfoOwner).toEqual(userId); + expect(beacon.beaconInfoEventType).toEqual(liveBeaconEvent.getType()); + expect(beacon.identifier).toEqual(`${roomId}_${userId}`); + expect(beacon.beaconInfo).toBeTruthy(); + }); + describe('isLive()', () => { it('returns false when beacon is explicitly set to not live', () => { const beacon = new Beacon(notLiveBeaconEvent); diff --git a/src/content-helpers.ts b/src/content-helpers.ts index 8d417d0a912..82404ea6e30 100644 --- a/src/content-helpers.ts +++ b/src/content-helpers.ts @@ -247,7 +247,7 @@ export const makeBeaconInfoContent: MakeBeaconInfoContent = ( }); export type BeaconInfoState = MBeaconInfoContent & { - assetType: LocationAssetType; + assetType?: LocationAssetType; timestamp: number; }; /** @@ -255,14 +255,14 @@ export type BeaconInfoState = MBeaconInfoContent & { */ export const parseBeaconInfoContent = (content: MBeaconInfoEventContent): BeaconInfoState => { const { description, timeout, live } = content; - const { type: assetType } = M_ASSET.findIn(content); const timestamp = M_TIMESTAMP.findIn(content); + const asset = M_ASSET.findIn(content); return { description, timeout, live, - assetType, + assetType: asset?.type, timestamp, }; }; diff --git a/src/models/beacon.ts b/src/models/beacon.ts index 9df62bbe2b1..25abf2842ef 100644 --- a/src/models/beacon.ts +++ b/src/models/beacon.ts @@ -197,7 +197,7 @@ export class Beacon extends TypedEventEmitter Date.now() ? this._beaconInfo?.timestamp - 360000 /* 6min */ : this._beaconInfo?.timestamp; - this._isLive = this._beaconInfo?.live && + this._isLive = !!this._beaconInfo?.live && isTimestampInDuration(startTimestamp, this._beaconInfo?.timeout, Date.now()); if (prevLiveness !== this.isLive) {