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

Commit

Permalink
update location event types (#8022)
Browse files Browse the repository at this point in the history
* update types

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

* fix tests

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

* lint

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

* trigger

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry committed Mar 10, 2022
1 parent a4b2e0c commit e96d915
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/Notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)();
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/TextForEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
)
);
}
3 changes: 1 addition & 2 deletions src/components/views/location/shareLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions src/components/views/messages/MLocationBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -129,7 +129,7 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
}

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;
}
Expand Down Expand Up @@ -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'];
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/messages/MessageEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -132,7 +132,7 @@ export default class MessageEvent extends React.Component<IProps> 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');
Expand Down
6 changes: 3 additions & 3 deletions src/utils/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
)
);

Expand Down
6 changes: 3 additions & 3 deletions test/components/views/location/LocationShareMenu-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('<LocationShareMenu />', () => {
expect(messageRoomId).toEqual(defaultProps.roomId);
expect(relation).toEqual(null);
expect(messageBody).toEqual(expect.objectContaining({
[ASSET_NODE_TYPE.name]: {
[M_ASSET.name]: {
type: LocationAssetType.Self,
},
}));
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('<LocationShareMenu />', () => {
expect(messageRoomId).toEqual(defaultProps.roomId);
expect(relation).toEqual(null);
expect(messageBody).toEqual(expect.objectContaining({
[ASSET_NODE_TYPE.name]: {
[M_ASSET.name]: {
type: LocationAssetType.Pin,
},
}));
Expand Down
29 changes: 17 additions & 12 deletions test/components/views/messages/MLocationBody-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
Expand All @@ -225,18 +225,23 @@ 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!
},
};
expect(isSelfLocation(content as ILocationContent)).toBe(true);
});

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);
});
});
Expand All @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit e96d915

Please sign in to comment.