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

* handle beacon location events in ForwardDialog

* add transformer for forwarded events in MessageContextMenu

* remove canForward

* update snapshots for beacon model change

* add comments

* fix bad copy pasted test

* add test for beacon locations
This commit is contained in:
Kerry 2022-06-17 15:27:08 +02:00 committed by GitHub
parent 0a90674e89
commit b51ef246ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 292 additions and 81 deletions

View file

@ -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],

View file

@ -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,
@ -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,

View file

@ -18,18 +18,26 @@ import React from 'react';
import { mount, ReactWrapper } from 'enzyme';
import { EventStatus, MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { Room } from 'matrix-js-sdk/src/models/room';
import { PendingEventOrdering } from 'matrix-js-sdk/src/matrix';
import {
PendingEventOrdering,
BeaconIdentifier,
Beacon,
getBeaconInfoIdentifier,
} from 'matrix-js-sdk/src/matrix';
import { ExtensibleEvent, MessageEvent, M_POLL_KIND_DISCLOSED, PollStartEvent } from 'matrix-events-sdk';
import { Thread } from "matrix-js-sdk/src/models/thread";
import { mocked } from "jest-mock";
import { act } from '@testing-library/react';
import * as TestUtils from '../../../test-utils';
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
import { IRoomState } from "../../../../src/components/structures/RoomView";
import { canEditContent, canForward, isContentActionable } from "../../../../src/utils/EventUtils";
import { canEditContent, isContentActionable } from "../../../../src/utils/EventUtils";
import { copyPlaintext, getSelectedText } from "../../../../src/utils/strings";
import MessageContextMenu from "../../../../src/components/views/context_menus/MessageContextMenu";
import { makeBeaconEvent, makeBeaconInfoEvent } from '../../../test-utils';
import dispatcher from '../../../../src/dispatcher/dispatcher';
jest.mock("../../../../src/utils/strings", () => ({
copyPlaintext: jest.fn(),
@ -37,33 +45,17 @@ jest.mock("../../../../src/utils/strings", () => ({
}));
jest.mock("../../../../src/utils/EventUtils", () => ({
canEditContent: jest.fn(),
canForward: jest.fn(),
isContentActionable: jest.fn(),
isLocationEvent: jest.fn(),
}));
const roomId = 'roomid';
describe('MessageContextMenu', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('allows forwarding a room message', () => {
mocked(canForward).mockReturnValue(true);
mocked(isContentActionable).mockReturnValue(true);
const eventContent = MessageEvent.from("hello");
const menu = createMenuWithContent(eventContent);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(1);
});
it('does not allow forwarding a poll', () => {
mocked(canForward).mockReturnValue(false);
const eventContent = PollStartEvent.from("why?", ["42"], M_POLL_KIND_DISCLOSED);
const menu = createMenuWithContent(eventContent);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(0);
});
it('does show copy link button when supplied a link', () => {
const eventContent = MessageEvent.from("hello");
const props = {
@ -82,6 +74,99 @@ describe('MessageContextMenu', () => {
expect(copyLinkButton).toHaveLength(0);
});
describe('message forwarding', () => {
it('allows forwarding a room message', () => {
mocked(isContentActionable).mockReturnValue(true);
const eventContent = MessageEvent.from("hello");
const menu = createMenuWithContent(eventContent);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(1);
});
it('does not allow forwarding a poll', () => {
const eventContent = PollStartEvent.from("why?", ["42"], M_POLL_KIND_DISCLOSED);
const menu = createMenuWithContent(eventContent);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(0);
});
describe('forwarding beacons', () => {
const aliceId = "@alice:server.org";
beforeEach(() => {
mocked(isContentActionable).mockReturnValue(true);
});
it('does not allow forwarding a beacon that is not live', () => {
const deadBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false });
const beacon = new Beacon(deadBeaconEvent);
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(deadBeaconEvent), beacon);
const menu = createMenu(deadBeaconEvent, {}, {}, beacons);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(0);
});
it('does not allow forwarding a beacon that is not live but has a latestLocation', () => {
const deadBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false });
const beaconLocation = makeBeaconEvent(
aliceId, { beaconInfoId: deadBeaconEvent.getId(), geoUri: 'geo:51,41' },
);
const beacon = new Beacon(deadBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(deadBeaconEvent), beacon);
const menu = createMenu(deadBeaconEvent, {}, {}, beacons);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(0);
});
it('does not allow forwarding a live beacon that does not have a latestLocation', () => {
const beaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true });
const beacon = new Beacon(beaconEvent);
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(beaconEvent), beacon);
const menu = createMenu(beaconEvent, {}, {}, beacons);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(0);
});
it('allows forwarding a live beacon that has a location', () => {
const liveBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true });
const beaconLocation = makeBeaconEvent(
aliceId, { beaconInfoId: liveBeaconEvent.getId(), geoUri: 'geo:51,41' },
);
const beacon = new Beacon(liveBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(liveBeaconEvent), beacon);
const menu = createMenu(liveBeaconEvent, {}, {}, beacons);
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(1);
});
it('opens forward dialog with correct event', () => {
const dispatchSpy = jest.spyOn(dispatcher, 'dispatch');
const liveBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true });
const beaconLocation = makeBeaconEvent(
aliceId, { beaconInfoId: liveBeaconEvent.getId(), geoUri: 'geo:51,41' },
);
const beacon = new Beacon(liveBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(liveBeaconEvent), beacon);
const menu = createMenu(liveBeaconEvent, {}, {}, beacons);
act(() => {
menu.find('div[aria-label="Forward"]').simulate('click');
});
// called with forwardableEvent, not beaconInfo event
expect(dispatchSpy).toHaveBeenCalledWith(expect.objectContaining({
event: beaconLocation,
}));
});
});
});
describe("right click", () => {
it('copy button does work as expected', () => {
const text = "hello";
@ -215,12 +300,13 @@ function createMenu(
mxEvent: MatrixEvent,
props?: Partial<React.ComponentProps<typeof MessageContextMenu>>,
context: Partial<IRoomState> = {},
beacons: Map<BeaconIdentifier, Beacon> = new Map(),
): ReactWrapper {
TestUtils.stubClient();
const client = MatrixClientPeg.get();
const room = new Room(
"roomid",
roomId,
client,
"@user:example.com",
{
@ -228,6 +314,9 @@ function createMenu(
},
);
// @ts-ignore illegally set private prop
room.currentState.beacons = beacons;
mxEvent.setStatus(EventStatus.SENT);
client.getUserId = jest.fn().mockReturnValue("@user:example.com");

View file

@ -28,6 +28,7 @@ import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
import {
getMockClientWithEventEmitter,
makeBeaconEvent,
makeLegacyLocationEvent,
makeLocationEvent,
mkEvent,
@ -285,6 +286,33 @@ describe("ForwardDialog", () => {
);
});
it('forwards beacon location as a pin drop event', async () => {
const timestamp = 123456;
const beaconEvent = makeBeaconEvent('@alice:server.org', { geoUri, timestamp });
const text = `Location ${geoUri} at ${new Date(timestamp).toISOString()}`;
const expectedContent = {
msgtype: "m.location",
body: text,
[TEXT_NODE_TYPE.name]: text,
[M_ASSET.name]: { type: LocationAssetType.Pin },
[M_LOCATION.name]: {
uri: geoUri,
description: undefined,
},
geo_uri: geoUri,
[M_TIMESTAMP.name]: timestamp,
};
const wrapper = await mountForwardDialog(beaconEvent);
expect(wrapper.find('MLocationBody').length).toBeTruthy();
sendToFirstRoom(wrapper);
expect(mockClient.sendEvent).toHaveBeenCalledWith(
roomId, EventType.RoomMessage, expectedContent,
);
});
it('forwards pin drop event', async () => {
const wrapper = await mountForwardDialog(pinDropLocationEvent);