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

Commit b93c139

Browse files
author
Kerry Archibald
committed
extract isSelfLocation into utils
Signed-off-by: Kerry Archibald <[email protected]>
1 parent aecd71a commit b93c139

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/utils/location/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
export * from './findMapStyleUrl';
18+
export * from './isSelfLocation';
1819
export * from './locationEventGeoUri';
1920
export * from './LocationShareErrors';
2021
export * from './map';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { ILocationContent, LocationAssetType, M_ASSET } from "matrix-js-sdk/src/@types/location";
18+
19+
export const isSelfLocation = (locationContent: ILocationContent): boolean => {
20+
const asset = M_ASSET.findIn(locationContent) as { type: string };
21+
const assetType = asset?.type ?? LocationAssetType.Self;
22+
return assetType == LocationAssetType.Self;
23+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events";
18+
import {
19+
ILocationContent,
20+
LocationAssetType,
21+
M_ASSET,
22+
M_LOCATION,
23+
M_TIMESTAMP,
24+
} from "matrix-js-sdk/src/@types/location";
25+
import { makeLocationContent } from "matrix-js-sdk/src/content-helpers";
26+
27+
import { isSelfLocation } from "../../../src/utils/location";
28+
29+
describe("isSelfLocation", () => {
30+
it("Returns true for a full m.asset event", () => {
31+
const content = makeLocationContent("", '0');
32+
expect(isSelfLocation(content)).toBe(true);
33+
});
34+
35+
it("Returns true for a missing m.asset", () => {
36+
const content = {
37+
body: "",
38+
msgtype: "m.location",
39+
geo_uri: "",
40+
[M_LOCATION.name]: { uri: "" },
41+
[TEXT_NODE_TYPE.name]: "",
42+
[M_TIMESTAMP.name]: 0,
43+
// Note: no m.asset!
44+
};
45+
expect(isSelfLocation(content as ILocationContent)).toBe(true);
46+
});
47+
48+
it("Returns true for a missing m.asset type", () => {
49+
const content = {
50+
body: "",
51+
msgtype: "m.location",
52+
geo_uri: "",
53+
[M_LOCATION.name]: { uri: "" },
54+
[TEXT_NODE_TYPE.name]: "",
55+
[M_TIMESTAMP.name]: 0,
56+
[M_ASSET.name]: {
57+
// Note: no type!
58+
},
59+
};
60+
expect(isSelfLocation(content as ILocationContent)).toBe(true);
61+
});
62+
63+
it("Returns false for an unknown asset type", () => {
64+
const content = makeLocationContent(
65+
undefined, /* text */
66+
"geo:foo",
67+
0,
68+
undefined, /* description */
69+
"org.example.unknown" as unknown as LocationAssetType);
70+
expect(isSelfLocation(content)).toBe(false);
71+
});
72+
});

0 commit comments

Comments
 (0)