Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
LLS: Remove beacon info illegal replace relation (#8390)
Browse files Browse the repository at this point in the history
* dont apply illegal replace relation to beacon_info event

Signed-off-by: Kerry Archibald <kerrya@element.io>

* only display tiles for beacon infos with live prop

Signed-off-by: Kerry Archibald <kerrya@element.io>

* copyrights

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry authored Apr 22, 2022
1 parent 763edb7 commit a3a7c60
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 37 deletions.
6 changes: 4 additions & 2 deletions src/events/EventTileFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 1 addition & 10 deletions src/stores/OwnBeaconStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
BeaconIdentifier,
BeaconEvent,
MatrixEvent,
RelationType,
Room,
RoomMember,
RoomState,
Expand Down Expand Up @@ -449,20 +448,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
...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);
};
Expand Down
27 changes: 27 additions & 0 deletions src/utils/beacon/timeline.ts
Original file line number Diff line number Diff line change
@@ -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
);
25 changes: 0 additions & 25 deletions test/stores/OwnBeaconStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
BeaconEvent,
getBeaconInfoIdentifier,
MatrixEvent,
RelationType,
RoomStateEvent,
RoomMember,
} from "matrix-js-sdk/src/matrix";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
40 changes: 40 additions & 0 deletions test/utils/beacon/timeline-test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit a3a7c60

Please sign in to comment.