1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-30 04:23:07 +03:00

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

* test case

* handle missing beacon info asset

* default beacon info asset type to self

* make BeaconLocationState.assetType optional
This commit is contained in:
Kerry
2022-08-16 15:33:19 +02:00
committed by GitHub
parent 3f6f5b69c7
commit be3e731499
3 changed files with 23 additions and 4 deletions

View File

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import { MatrixEvent } from "../../../src"; import { MatrixEvent } from "../../../src";
import { M_BEACON_INFO } from "../../../src/@types/beacon";
import { import {
isTimestampInDuration, isTimestampInDuration,
Beacon, Beacon,
@ -129,6 +130,24 @@ describe('Beacon', () => {
expect(beacon.beaconInfo).toBeTruthy(); 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()', () => { describe('isLive()', () => {
it('returns false when beacon is explicitly set to not live', () => { it('returns false when beacon is explicitly set to not live', () => {
const beacon = new Beacon(notLiveBeaconEvent); const beacon = new Beacon(notLiveBeaconEvent);

View File

@ -247,7 +247,7 @@ export const makeBeaconInfoContent: MakeBeaconInfoContent = (
}); });
export type BeaconInfoState = MBeaconInfoContent & { export type BeaconInfoState = MBeaconInfoContent & {
assetType: LocationAssetType; assetType?: LocationAssetType;
timestamp: number; timestamp: number;
}; };
/** /**
@ -255,14 +255,14 @@ export type BeaconInfoState = MBeaconInfoContent & {
*/ */
export const parseBeaconInfoContent = (content: MBeaconInfoEventContent): BeaconInfoState => { export const parseBeaconInfoContent = (content: MBeaconInfoEventContent): BeaconInfoState => {
const { description, timeout, live } = content; const { description, timeout, live } = content;
const { type: assetType } = M_ASSET.findIn<MAssetContent>(content);
const timestamp = M_TIMESTAMP.findIn<number>(content); const timestamp = M_TIMESTAMP.findIn<number>(content);
const asset = M_ASSET.findIn<MAssetContent>(content);
return { return {
description, description,
timeout, timeout,
live, live,
assetType, assetType: asset?.type,
timestamp, timestamp,
}; };
}; };

View File

@ -197,7 +197,7 @@ export class Beacon extends TypedEventEmitter<Exclude<BeaconEvent, BeaconEvent.N
const startTimestamp = this._beaconInfo?.timestamp > Date.now() ? const startTimestamp = this._beaconInfo?.timestamp > Date.now() ?
this._beaconInfo?.timestamp - 360000 /* 6min */ : this._beaconInfo?.timestamp - 360000 /* 6min */ :
this._beaconInfo?.timestamp; this._beaconInfo?.timestamp;
this._isLive = this._beaconInfo?.live && this._isLive = !!this._beaconInfo?.live &&
isTimestampInDuration(startTimestamp, this._beaconInfo?.timeout, Date.now()); isTimestampInDuration(startTimestamp, this._beaconInfo?.timeout, Date.now());
if (prevLiveness !== this.isLive) { if (prevLiveness !== this.isLive) {