Skip to content

Commit

Permalink
Fix: Handle parsing of a beacon info event without asset (#2591) (#2592)
Browse files Browse the repository at this point in the history
* test case

* handle missing beacon info asset

* default beacon info asset type to self

* make BeaconLocationState.assetType optional

(cherry picked from commit be3e731)

Co-authored-by: Kerry <kerrya@element.io>
  • Loading branch information
RiotRobot and Kerry authored Aug 16, 2022
1 parent a9f2ae6 commit d18aae0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions spec/unit/models/beacon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import { MatrixEvent } from "../../../src";
import { M_BEACON_INFO } from "../../../src/@types/beacon";
import {
isTimestampInDuration,
Beacon,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/content-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,22 @@ export const makeBeaconInfoContent: MakeBeaconInfoContent = (
});

export type BeaconInfoState = MBeaconInfoContent & {
assetType: LocationAssetType;
assetType?: LocationAssetType;
timestamp: number;
};
/**
* Flatten beacon info event content
*/
export const parseBeaconInfoContent = (content: MBeaconInfoEventContent): BeaconInfoState => {
const { description, timeout, live } = content;
const { type: assetType } = M_ASSET.findIn<MAssetContent>(content);
const timestamp = M_TIMESTAMP.findIn<number>(content);
const asset = M_ASSET.findIn<MAssetContent>(content);

return {
description,
timeout,
live,
assetType,
assetType: asset?.type,
timestamp,
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/models/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class Beacon extends TypedEventEmitter<Exclude<BeaconEvent, BeaconEvent.N
const startTimestamp = this._beaconInfo?.timestamp > 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) {
Expand Down

0 comments on commit d18aae0

Please sign in to comment.