From e96d9157a9467df6c217bc9327b365b93b649f40 Mon Sep 17 00:00:00 2001 From: Kerry Date: Thu, 10 Mar 2022 19:03:31 +0100 Subject: [PATCH] update location event types (#8022) * update types Signed-off-by: Kerry Archibald * fix tests Signed-off-by: Kerry Archibald * lint Signed-off-by: Kerry Archibald * trigger Signed-off-by: Kerry Archibald --- src/Notifier.ts | 6 ++-- src/TextForEvent.tsx | 4 +-- .../context_menus/MessageContextMenu.tsx | 6 ++-- .../views/location/shareLocation.ts | 3 +- .../views/messages/MLocationBody.tsx | 10 +++---- .../views/messages/MessageEvent.tsx | 4 +-- src/utils/EventUtils.ts | 6 ++-- .../views/location/LocationShareMenu-test.tsx | 6 ++-- .../views/messages/MLocationBody-test.tsx | 29 +++++++++++-------- 9 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Notifier.ts b/src/Notifier.ts index c5383488f2..3f17ae0091 100644 --- a/src/Notifier.ts +++ b/src/Notifier.ts @@ -22,7 +22,7 @@ import { Room, RoomEvent } from "matrix-js-sdk/src/models/room"; import { ClientEvent } from "matrix-js-sdk/src/client"; import { logger } from "matrix-js-sdk/src/logger"; import { MsgType } from "matrix-js-sdk/src/@types/event"; -import { LOCATION_EVENT_TYPE } from "matrix-js-sdk/src/@types/location"; +import { M_LOCATION } from "matrix-js-sdk/src/@types/location"; import { MatrixClientPeg } from './MatrixClientPeg'; import SdkConfig from './SdkConfig'; @@ -64,10 +64,10 @@ const msgTypeHandlers = { const name = (event.sender || {}).name; return _t("%(name)s is requesting verification", { name }); }, - [LOCATION_EVENT_TYPE.name]: (event: MatrixEvent) => { + [M_LOCATION.name]: (event: MatrixEvent) => { return TextForEvent.textForLocationEvent(event)(); }, - [LOCATION_EVENT_TYPE.altName]: (event: MatrixEvent) => { + [M_LOCATION.altName]: (event: MatrixEvent) => { return TextForEvent.textForLocationEvent(event)(); }, }; diff --git a/src/TextForEvent.tsx b/src/TextForEvent.tsx index 49da76b8b8..5b76eb6f20 100644 --- a/src/TextForEvent.tsx +++ b/src/TextForEvent.tsx @@ -29,7 +29,7 @@ import { M_POLL_END, PollStartEvent, } from "matrix-events-sdk"; -import { LOCATION_EVENT_TYPE } from "matrix-js-sdk/src/@types/location"; +import { M_LOCATION } from "matrix-js-sdk/src/@types/location"; import { _t } from './languageHandler'; import * as Roles from './Roles'; @@ -339,7 +339,7 @@ function textForMessageEvent(ev: MatrixEvent): () => string | null { const content = ev.getContent(); const msgtype = content.msgtype; - if (LOCATION_EVENT_TYPE.matches(type) || LOCATION_EVENT_TYPE.matches(msgtype)) { + if (M_LOCATION.matches(type) || M_LOCATION.matches(msgtype)) { return textForLocationEvent(ev); } diff --git a/src/components/views/context_menus/MessageContextMenu.tsx b/src/components/views/context_menus/MessageContextMenu.tsx index 28089fb70f..300cf42554 100644 --- a/src/components/views/context_menus/MessageContextMenu.tsx +++ b/src/components/views/context_menus/MessageContextMenu.tsx @@ -20,7 +20,7 @@ import { EventStatus, MatrixEvent } from 'matrix-js-sdk/src/models/event'; import { EventType, RelationType } from "matrix-js-sdk/src/@types/event"; import { Relations } from 'matrix-js-sdk/src/models/relations'; import { RoomMemberEvent } from "matrix-js-sdk/src/models/room-member"; -import { LOCATION_EVENT_TYPE } from 'matrix-js-sdk/src/@types/location'; +import { M_LOCATION } from 'matrix-js-sdk/src/@types/location'; import { M_POLL_START } from "matrix-events-sdk"; import { MatrixClientPeg } from '../../../MatrixClientPeg'; @@ -526,10 +526,10 @@ function canForward(event: MatrixEvent): boolean { function isLocationEvent(event: MatrixEvent): boolean { const eventType = event.getType(); return ( - LOCATION_EVENT_TYPE.matches(eventType) || + M_LOCATION.matches(eventType) || ( eventType === EventType.RoomMessage && - LOCATION_EVENT_TYPE.matches(event.getContent().msgtype) + M_LOCATION.matches(event.getContent().msgtype) ) ); } diff --git a/src/components/views/location/shareLocation.ts b/src/components/views/location/shareLocation.ts index 4ab8e169d0..c7ea31bb26 100644 --- a/src/components/views/location/shareLocation.ts +++ b/src/components/views/location/shareLocation.ts @@ -41,10 +41,9 @@ export const shareLocation = ( ) => async (uri: string, ts: number) => { if (!uri) return false; try { - const text = textForLocation(uri, ts, null); const threadId = relation?.rel_type === RelationType.Thread ? relation.event_id : null; const assetType = shareType === LocationShareType.Pin ? LocationAssetType.Pin : LocationAssetType.Self; - await client.sendMessage(roomId, threadId, makeLocationContent(text, uri, ts, null, assetType)); + await client.sendMessage(roomId, threadId, makeLocationContent(undefined, uri, ts, undefined, assetType)); } catch (e) { logger.error("We couldn't send your location", e); diff --git a/src/components/views/messages/MLocationBody.tsx b/src/components/views/messages/MLocationBody.tsx index 0d3ce4eebd..c466d10737 100644 --- a/src/components/views/messages/MLocationBody.tsx +++ b/src/components/views/messages/MLocationBody.tsx @@ -19,10 +19,10 @@ import maplibregl from 'maplibre-gl'; import { logger } from "matrix-js-sdk/src/logger"; import { MatrixEvent } from 'matrix-js-sdk/src/models/event'; import { - ASSET_NODE_TYPE, + M_ASSET, LocationAssetType, ILocationContent, - LOCATION_EVENT_TYPE, + M_LOCATION, } from 'matrix-js-sdk/src/@types/location'; import { ClientEvent, IClientWellKnown } from 'matrix-js-sdk/src/client'; @@ -129,7 +129,7 @@ export default class MLocationBody extends React.Component { } export function isSelfLocation(locationContent: ILocationContent): boolean { - const asset = ASSET_NODE_TYPE.findIn(locationContent) as { type: string }; + const asset = M_ASSET.findIn(locationContent) as { type: string }; const assetType = asset?.type ?? LocationAssetType.Self; return assetType == LocationAssetType.Self; } @@ -298,7 +298,7 @@ export function locationEventGeoUri(mxEvent: MatrixEvent): string { // events - so folks can read their old chat history correctly. // https://github.com/matrix-org/matrix-doc/issues/3516 const content = mxEvent.getContent(); - const loc = LOCATION_EVENT_TYPE.findIn(content) as { uri?: string }; + const loc = M_LOCATION.findIn(content) as { uri?: string }; return loc ? loc.uri : content['geo_uri']; } @@ -343,7 +343,7 @@ function makeLink(coords: GeolocationCoordinates): string { export function createMapSiteLink(event: MatrixEvent): string { const content: Object = event.getContent(); - const mLocation = content[LOCATION_EVENT_TYPE.name]; + const mLocation = content[M_LOCATION.name]; if (mLocation !== undefined) { const uri = mLocation["uri"]; if (uri !== undefined) { diff --git a/src/components/views/messages/MessageEvent.tsx b/src/components/views/messages/MessageEvent.tsx index 578b887420..68281beaee 100644 --- a/src/components/views/messages/MessageEvent.tsx +++ b/src/components/views/messages/MessageEvent.tsx @@ -17,7 +17,7 @@ limitations under the License. import React, { createRef } from 'react'; import { EventType, MsgType } from "matrix-js-sdk/src/@types/event"; import { Relations } from 'matrix-js-sdk/src/models/relations'; -import { LOCATION_EVENT_TYPE } from 'matrix-js-sdk/src/@types/location'; +import { M_LOCATION } from 'matrix-js-sdk/src/@types/location'; import { M_POLL_START } from "matrix-events-sdk"; import * as sdk from '../../../index'; @@ -132,7 +132,7 @@ export default class MessageEvent extends React.Component implements IMe // TODO: move to eventTypes when location sharing spec stabilises if ( - LOCATION_EVENT_TYPE.matches(type) || + M_LOCATION.matches(type) || (type === EventType.RoomMessage && msgtype === MsgType.Location) ) { BodyType = sdk.getComponent('messages.MLocationBody'); diff --git a/src/utils/EventUtils.ts b/src/utils/EventUtils.ts index f91fadc96b..a8d27d3f79 100644 --- a/src/utils/EventUtils.ts +++ b/src/utils/EventUtils.ts @@ -18,7 +18,7 @@ import { EventStatus, MatrixEvent } from 'matrix-js-sdk/src/models/event'; import { EventType, EVENT_VISIBILITY_CHANGE_TYPE, MsgType, RelationType } from "matrix-js-sdk/src/@types/event"; import { MatrixClient } from 'matrix-js-sdk/src/client'; import { logger } from 'matrix-js-sdk/src/logger'; -import { LOCATION_EVENT_TYPE } from 'matrix-js-sdk/src/@types/location'; +import { M_LOCATION } from 'matrix-js-sdk/src/@types/location'; import { M_POLL_START } from "matrix-events-sdk"; import { MatrixClientPeg } from '../MatrixClientPeg'; @@ -252,10 +252,10 @@ export function getEventDisplayInfo(mxEvent: MatrixEvent, hideEvent?: boolean): const noBubbleEvent = ( (eventType === EventType.RoomMessage && msgtype === MsgType.Emote) || M_POLL_START.matches(eventType) || - LOCATION_EVENT_TYPE.matches(eventType) || + M_LOCATION.matches(eventType) || ( eventType === EventType.RoomMessage && - LOCATION_EVENT_TYPE.matches(msgtype) + M_LOCATION.matches(msgtype) ) ); diff --git a/test/components/views/location/LocationShareMenu-test.tsx b/test/components/views/location/LocationShareMenu-test.tsx index c7a840e33a..267c8aa6f1 100644 --- a/test/components/views/location/LocationShareMenu-test.tsx +++ b/test/components/views/location/LocationShareMenu-test.tsx @@ -20,7 +20,7 @@ import { RoomMember } from 'matrix-js-sdk/src/models/room-member'; import { MatrixClient } from 'matrix-js-sdk/src/client'; import { mocked } from 'jest-mock'; import { act } from 'react-dom/test-utils'; -import { ASSET_NODE_TYPE, LocationAssetType } from 'matrix-js-sdk/src/@types/location'; +import { M_ASSET, LocationAssetType } from 'matrix-js-sdk/src/@types/location'; import '../../../skinned-sdk'; import LocationShareMenu from '../../../../src/components/views/location/LocationShareMenu'; @@ -162,7 +162,7 @@ describe('', () => { expect(messageRoomId).toEqual(defaultProps.roomId); expect(relation).toEqual(null); expect(messageBody).toEqual(expect.objectContaining({ - [ASSET_NODE_TYPE.name]: { + [M_ASSET.name]: { type: LocationAssetType.Self, }, })); @@ -243,7 +243,7 @@ describe('', () => { expect(messageRoomId).toEqual(defaultProps.roomId); expect(relation).toEqual(null); expect(messageBody).toEqual(expect.objectContaining({ - [ASSET_NODE_TYPE.name]: { + [M_ASSET.name]: { type: LocationAssetType.Pin, }, })); diff --git a/test/components/views/messages/MLocationBody-test.tsx b/test/components/views/messages/MLocationBody-test.tsx index d5b9665f3a..62bdc82a54 100644 --- a/test/components/views/messages/MLocationBody-test.tsx +++ b/test/components/views/messages/MLocationBody-test.tsx @@ -16,11 +16,11 @@ limitations under the License. import { makeLocationContent } from "matrix-js-sdk/src/content-helpers"; import { - ASSET_NODE_TYPE, + M_ASSET, LocationAssetType, ILocationContent, - LOCATION_EVENT_TYPE, - TIMESTAMP_NODE_TYPE, + M_LOCATION, + M_TIMESTAMP, } from "matrix-js-sdk/src/@types/location"; import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; @@ -203,7 +203,7 @@ describe("MLocationBody", () => { describe("isSelfLocation", () => { it("Returns true for a full m.asset event", () => { - const content = makeLocationContent("", "", 0); + const content = makeLocationContent("", 0); expect(isSelfLocation(content)).toBe(true); }); @@ -212,9 +212,9 @@ describe("MLocationBody", () => { body: "", msgtype: "m.location", geo_uri: "", - [LOCATION_EVENT_TYPE.name]: { uri: "" }, + [M_LOCATION.name]: { uri: "" }, [TEXT_NODE_TYPE.name]: "", - [TIMESTAMP_NODE_TYPE.name]: 0, + [M_TIMESTAMP.name]: 0, // Note: no m.asset! }; expect(isSelfLocation(content as ILocationContent)).toBe(true); @@ -225,10 +225,10 @@ describe("MLocationBody", () => { body: "", msgtype: "m.location", geo_uri: "", - [LOCATION_EVENT_TYPE.name]: { uri: "" }, + [M_LOCATION.name]: { uri: "" }, [TEXT_NODE_TYPE.name]: "", - [TIMESTAMP_NODE_TYPE.name]: 0, - [ASSET_NODE_TYPE.name]: { + [M_TIMESTAMP.name]: 0, + [M_ASSET.name]: { // Note: no type! }, }; @@ -236,7 +236,12 @@ describe("MLocationBody", () => { }); it("Returns false for an unknown asset type", () => { - const content = makeLocationContent("", "", 0, "", "org.example.unknown" as unknown as LocationAssetType); + const content = makeLocationContent( + undefined, /* text */ + "geo:foo", + 0, + undefined, /* description */ + "org.example.unknown" as unknown as LocationAssetType); expect(isSelfLocation(content)).toBe(false); }); }); @@ -246,7 +251,7 @@ function oldLocationEvent(geoUri: string): MatrixEvent { return new MatrixEvent( { "event_id": nextId(), - "type": LOCATION_EVENT_TYPE.name, + "type": M_LOCATION.name, "content": { "body": "Something about where I am", "msgtype": "m.location", @@ -260,7 +265,7 @@ function modernLocationEvent(geoUri: string): MatrixEvent { return new MatrixEvent( { "event_id": nextId(), - "type": LOCATION_EVENT_TYPE.name, + "type": M_LOCATION.name, "content": makeLocationContent( `Found at ${geoUri} at 2021-12-21T12:22+0000`, geoUri,