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

Live location share - forward latest location (PSF-1044) #8860

Merged
merged 8 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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: 18 additions & 1 deletion __mocks__/maplibre-gl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/*
Copyright 2022 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.
*/

const EventEmitter = require("events");
const { LngLat, NavigationControl, LngLatBounds } = require('maplibre-gl');
const { LngLat, NavigationControl, LngLatBounds, AttributionControl } = require('maplibre-gl');

class MockMap extends EventEmitter {
addControl = jest.fn();
Expand Down Expand Up @@ -27,4 +43,5 @@ module.exports = {
LngLat,
LngLatBounds,
NavigationControl,
AttributionControl,
};
12 changes: 7 additions & 5 deletions src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Modal from '../../../Modal';
import Resend from '../../../Resend';
import SettingsStore from '../../../settings/SettingsStore';
import { isUrlPermitted } from '../../../HtmlUtils';
import { canEditContent, canForward, editEvent, isContentActionable, isLocationEvent } from '../../../utils/EventUtils';
import { canEditContent, editEvent, isContentActionable, isLocationEvent } from '../../../utils/EventUtils';
import IconizedContextMenu, { IconizedContextMenuOption, IconizedContextMenuOptionList } from './IconizedContextMenu';
import { ReadPinsEventId } from "../right_panel/types";
import { Action } from "../../../dispatcher/actions";
Expand All @@ -51,6 +51,7 @@ import { GetRelationsForEvent, IEventTileOps } from "../rooms/EventTile";
import { OpenForwardDialogPayload } from "../../../dispatcher/payloads/OpenForwardDialogPayload";
import { OpenReportEventDialogPayload } from "../../../dispatcher/payloads/OpenReportEventDialogPayload";
import { createMapSiteLinkFromEvent } from '../../../utils/location';
import { getForwardableEvent } from '../../../events/forward/getForwardableEvent';

interface IProps extends IPosition {
chevronFace: ChevronFace;
Expand Down Expand Up @@ -188,10 +189,10 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
this.closeMenu();
};

private onForwardClick = (): void => {
private onForwardClick = (forwardableEvent: MatrixEvent) => (): void => {
dis.dispatch<OpenForwardDialogPayload>({
action: Action.OpenForwardDialog,
event: this.props.mxEvent,
event: forwardableEvent,
permalinkCreator: this.props.permalinkCreator,
});
this.closeMenu();
Expand Down Expand Up @@ -379,12 +380,13 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
}

let forwardButton: JSX.Element;
if (contentActionable && canForward(mxEvent)) {
const forwardableEvent = getForwardableEvent(mxEvent, cli);
if (contentActionable && forwardableEvent) {
forwardButton = (
<IconizedContextMenuOption
iconClassName="mx_MessageContextMenu_iconForward"
label={_t("Forward")}
onClick={this.onForwardClick}
onClick={this.onForwardClick(forwardableEvent)}
/>
);
}
Expand Down
37 changes: 24 additions & 13 deletions src/components/views/dialogs/ForwardDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { EventType } from "matrix-js-sdk/src/@types/event";
import { ILocationContent, LocationAssetType, M_TIMESTAMP } from "matrix-js-sdk/src/@types/location";
import { makeLocationContent } from "matrix-js-sdk/src/content-helpers";
import { M_BEACON } from "matrix-js-sdk/src/@types/beacon";

import { _t } from "../../../languageHandler";
import dis from "../../../dispatcher/dispatcher";
Expand Down Expand Up @@ -158,32 +159,42 @@ const Entry: React.FC<IEntryProps> = ({ room, type, content, matrixClient: cli,
</div>;
};

const getStrippedEventContent = (event: MatrixEvent): IContent => {
const transformEvent = (event: MatrixEvent): {type: string, content: IContent } => {
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
"m.relates_to": _, // strip relations - in future we will attach a relation pointing at the original event
// We're taking a shallow copy here to avoid https://github.com/vector-im/element-web/issues/10924
...content
} = event.getContent();

// beacon pulses get transformed into static locations on forward
const type = M_BEACON.matches(event.getType()) ? EventType.RoomMessage : event.getType();

// self location shares should have their description removed
// and become 'pin' share type
if (isLocationEvent(event) && isSelfLocation(content as ILocationContent)) {
if (
(isLocationEvent(event) && isSelfLocation(content as ILocationContent)) ||
// beacon pulses get transformed into static locations on forward
M_BEACON.matches(event.getType())
) {
const timestamp = M_TIMESTAMP.findIn<number>(content);
const geoUri = locationEventGeoUri(event);
return {
...content,
...makeLocationContent(
undefined, // text
geoUri,
timestamp || Date.now(),
undefined, // description
LocationAssetType.Pin,
),
type,
content: {
...content,
...makeLocationContent(
undefined, // text
geoUri,
timestamp || Date.now(),
undefined, // description
LocationAssetType.Pin,
),
},
};
}

return content;
return { type, content };
};

const ForwardDialog: React.FC<IProps> = ({ matrixClient: cli, event, permalinkCreator, onFinished }) => {
Expand All @@ -193,7 +204,7 @@ const ForwardDialog: React.FC<IProps> = ({ matrixClient: cli, event, permalinkCr
cli.getProfileInfo(userId).then(info => setProfileInfo(info));
}, [cli, userId]);

const content = getStrippedEventContent(event);
const { type, content } = transformEvent(event);

// For the message preview we fake the sender as ourselves
const mockEvent = new MatrixEvent({
Expand Down Expand Up @@ -293,7 +304,7 @@ const ForwardDialog: React.FC<IProps> = ({ matrixClient: cli, event, permalinkCr
<Entry
key={room.roomId}
room={room}
type={event.getType()}
type={type}
content={content}
matrixClient={cli}
onFinished={onFinished}
Expand Down
34 changes: 34 additions & 0 deletions src/events/forward/getForwardableBeacon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2022 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 { getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix";

import { ForwardableEventTransformFunction } from "./types";

/**
* Live location beacons should forward their latest location as a static pin location
* If the beacon is not live, or doesn't have a location forwarding is not allowed
*/
export const getForwardableBeaconEvent: ForwardableEventTransformFunction = (event, cli) => {
const room = cli.getRoom(event.getRoomId());
const beacon = room.currentState.beacons?.get(getBeaconInfoIdentifier(event));
const latestLocationEvent = beacon.latestLocationEvent;

if (beacon.isLive && latestLocationEvent) {
return latestLocationEvent;
}
return null;
};
36 changes: 36 additions & 0 deletions src/events/forward/getForwardableEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2022 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 { M_POLL_START } from "matrix-events-sdk";
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";

import { getForwardableBeaconEvent } from "./getForwardableBeacon";

/**
* Get forwardable event for a given event
* If an event is not forwardable return null
*/
export const getForwardableEvent = (event: MatrixEvent, cli: MatrixClient): MatrixEvent | null => {
if (M_POLL_START.matches(event.getType())) {
return null;
}
if (M_BEACON_INFO.matches(event.getType())) {
return getForwardableBeaconEvent(event, cli);
}
return event;
};

19 changes: 19 additions & 0 deletions src/events/forward/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2022 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 { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";

export type ForwardableEventTransformFunction = (event: MatrixEvent, cli: MatrixClient) => MatrixEvent | null;
8 changes: 0 additions & 8 deletions src/utils/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,6 @@ export const isLocationEvent = (event: MatrixEvent): boolean => {
);
};

export function canForward(event: MatrixEvent): boolean {
return !(
M_POLL_START.matches(event.getType()) ||
// disallow forwarding until psf-1044
M_BEACON_INFO.matches(event.getType())
);
}

export function hasThreadSummary(event: MatrixEvent): boolean {
return event.isThreadRoot && event.getThread()?.length && !!event.getThread().replyToEvent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ exports[`<BeaconMarker /> renders marker when beacon has location 1`] = `
},
"_eventsCount": 5,
"_isLive": true,
"_latestLocationState": Object {
"description": undefined,
"timestamp": 1647270879404,
"uri": "geo:51,41",
"_latestLocationEvent": Object {
"content": Object {
"m.relates_to": Object {
"event_id": "$alice-room1-1",
"rel_type": "m.reference",
},
"org.matrix.msc3488.location": Object {
"description": undefined,
"uri": "geo:51,41",
},
"org.matrix.msc3488.ts": 1647270879404,
},
"sender": "@alice:server",
"type": "org.matrix.msc3672.beacon",
},
"_maxListeners": undefined,
"clearLatestLocation": [Function],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`<BeaconStatus /> active state renders without children 1`] = `
"_events": Object {},
"_eventsCount": 0,
"_isLive": undefined,
"_latestLocationState": undefined,
"_latestLocationEvent": undefined,
"_maxListeners": undefined,
"clearLatestLocation": [Function],
"livenessWatchTimeout": undefined,
Expand Down Expand Up @@ -78,7 +78,7 @@ exports[`<BeaconStatus /> active state renders without children 1`] = `
"_events": Object {},
"_eventsCount": 0,
"_isLive": undefined,
"_latestLocationState": undefined,
"_latestLocationEvent": undefined,
"_maxListeners": undefined,
"clearLatestLocation": [Function],
"livenessWatchTimeout": undefined,
Expand Down
Loading