Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle parsing of a beacon info event without asset #2591

Merged
merged 4 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion src/content-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ export type BeaconInfoState = MBeaconInfoContent & {
*/
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);
// default asset type to self, as with location events
const assetType = asset?.type ?? LocationAssetType.Self;

return {
description,
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