diff --git a/src/events/EventTileFactory.tsx b/src/events/EventTileFactory.tsx index ecda002f59..f122329ee5 100644 --- a/src/events/EventTileFactory.tsx +++ b/src/events/EventTileFactory.tsx @@ -19,7 +19,6 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { EventType, MsgType, RelationType } from "matrix-js-sdk/src/@types/event"; import { M_POLL_START, Optional } from "matrix-events-sdk"; import { MatrixClient } from "matrix-js-sdk/src/client"; -import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon"; import EditorStateTransfer from "../utils/EditorStateTransfer"; import { RoomPermalinkCreator } from "../utils/permalinks/Permalinks"; @@ -44,6 +43,7 @@ import { getMessageModerationState, MessageModerationState } from "../utils/Even import HiddenBody from "../components/views/messages/HiddenBody"; import SettingsStore from "../settings/SettingsStore"; import ViewSourceEvent from "../components/views/messages/ViewSourceEvent"; +import { shouldDisplayAsBeaconTile } from "../utils/beacon/timeline"; // Subset of EventTile's IProps plus some mixins export interface EventTileTypeProps { @@ -219,7 +219,9 @@ export function pickFactory( // Try and pick a state event factory, if we can. if (mxEvent.isState()) { if ( - M_BEACON_INFO.matches(evType) && + shouldDisplayAsBeaconTile(mxEvent) && + // settings store access here temporarily during labs + // only hit when a beacon_info event is hit SettingsStore.getValue("feature_location_share_live") ) { return MessageEventFactory; diff --git a/src/stores/OwnBeaconStore.ts b/src/stores/OwnBeaconStore.ts index eabe9ea083..1bef120ae7 100644 --- a/src/stores/OwnBeaconStore.ts +++ b/src/stores/OwnBeaconStore.ts @@ -20,7 +20,6 @@ import { BeaconIdentifier, BeaconEvent, MatrixEvent, - RelationType, Room, RoomMember, RoomState, @@ -449,20 +448,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient { ...update, }; - const newContent = makeBeaconInfoContent(timeout, + const updateContent = makeBeaconInfoContent(timeout, live, description, assetType, timestamp, ); - const updateContent = { - ...newContent, - "m.new_content": newContent, - "m.relates_to": { - "rel_type": RelationType.Replace, - "event_id": beacon.beaconInfoId, - }, - }; await this.matrixClient.unstable_setLiveBeacon(beacon.roomId, updateContent); }; diff --git a/src/utils/beacon/timeline.ts b/src/utils/beacon/timeline.ts new file mode 100644 index 0000000000..e6e8f2311d --- /dev/null +++ b/src/utils/beacon/timeline.ts @@ -0,0 +1,27 @@ +/* +Copyright 2020 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. +*/ + +import { MatrixEvent } from "matrix-js-sdk/src/matrix"; +import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon"; + +/** + * beacon_info events without live property set to true + * should be displayed in the timeline + */ +export const shouldDisplayAsBeaconTile = (event: MatrixEvent): boolean => ( + M_BEACON_INFO.matches(event.getType()) && + !!event.getContent()?.live +); diff --git a/test/stores/OwnBeaconStore-test.ts b/test/stores/OwnBeaconStore-test.ts index b5713f6348..6a95c82ad0 100644 --- a/test/stores/OwnBeaconStore-test.ts +++ b/test/stores/OwnBeaconStore-test.ts @@ -20,7 +20,6 @@ import { BeaconEvent, getBeaconInfoIdentifier, MatrixEvent, - RelationType, RoomStateEvent, RoomMember, } from "matrix-js-sdk/src/matrix"; @@ -473,14 +472,6 @@ describe('OwnBeaconStore', () => { const expectedUpdateContent = { ...prevEventContent, live: false, - ["m.new_content"]: { - ...prevEventContent, - live: false, - }, - ["m.relates_to"]: { - event_id: alicesRoom1BeaconInfo.getId(), - rel_type: RelationType.Replace, - }, }; expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith( room1Id, @@ -650,14 +641,6 @@ describe('OwnBeaconStore', () => { const expectedUpdateContent = { ...prevEventContent, live: false, - ["m.new_content"]: { - ...prevEventContent, - live: false, - }, - ["m.relates_to"]: { - event_id: alicesRoom1BeaconInfo.getId(), - rel_type: RelationType.Replace, - }, }; expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith( room1Id, @@ -683,14 +666,6 @@ describe('OwnBeaconStore', () => { const expectedUpdateContent = { ...prevEventContent, live: false, - ["m.new_content"]: { - ...prevEventContent, - live: false, - }, - ["m.relates_to"]: { - event_id: alicesRoom1BeaconInfo.getId(), - rel_type: RelationType.Replace, - }, }; expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith( room1Id, diff --git a/test/utils/beacon/timeline-test.ts b/test/utils/beacon/timeline-test.ts new file mode 100644 index 0000000000..96be565de7 --- /dev/null +++ b/test/utils/beacon/timeline-test.ts @@ -0,0 +1,40 @@ +/* +Copyright 2020 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. +*/ + +import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix"; + +import { shouldDisplayAsBeaconTile } from "../../../src/utils/beacon/timeline"; +import { makeBeaconInfoEvent } from "../../test-utils"; + +describe('shouldDisplayAsBeaconTile', () => { + const userId = '@user:server'; + const roomId = '!room:server'; + const liveBeacon = makeBeaconInfoEvent(userId, roomId, { isLive: true }); + const notLiveBeacon = makeBeaconInfoEvent(userId, roomId, { isLive: false }); + const memberEvent = new MatrixEvent({ type: EventType.RoomMember }); + + it('returns true for a beacon with live property set to true', () => { + expect(shouldDisplayAsBeaconTile(liveBeacon)).toBe(true); + }); + + it('returns false for a beacon with live property set to false', () => { + expect(shouldDisplayAsBeaconTile(notLiveBeacon)).toBe(false); + }); + + it('returns false for a non beacon event', () => { + expect(shouldDisplayAsBeaconTile(memberEvent)).toBe(false); + }); +});