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

Commit a2f31fd

Browse files
authored
Location (live) share replies now provide a fallback content (#8949)
* Implement location share reply fallback content * Replace single condition switch with if
1 parent 924865b commit a2f31fd

File tree

3 files changed

+95
-14
lines changed

3 files changed

+95
-14
lines changed

src/utils/Reply.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import sanitizeHtml from "sanitize-html";
1919
import escapeHtml from "escape-html";
2020
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
2121
import { MsgType } from "matrix-js-sdk/src/@types/event";
22+
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
2223

2324
import { PERMITTED_URL_SCHEMES } from "../HtmlUtils";
2425
import { makeUserPermalink, RoomPermalinkCreator } from "./permalinks/Permalinks";
2526
import SettingsStore from "../settings/SettingsStore";
27+
import { isSelfLocation } from "./location";
2628

2729
export function getParentEventId(ev?: MatrixEvent): string | undefined {
2830
if (!ev || ev.isRedacted()) return;
@@ -93,6 +95,15 @@ export function getNestedReplyText(
9395
const userLink = makeUserPermalink(ev.getSender());
9496
const mxid = ev.getSender();
9597

98+
if (M_BEACON_INFO.matches(ev.getType())) {
99+
const aTheir = isSelfLocation(ev.getContent()) ? "their" : "a";
100+
return {
101+
html: `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
102+
+ `<br>shared ${aTheir} live location.</blockquote></mx-reply>`,
103+
body: `> <${mxid}> shared ${aTheir} live location.\n\n`,
104+
};
105+
}
106+
96107
// This fallback contains text that is explicitly EN.
97108
switch (msgtype) {
98109
case MsgType.Text:
@@ -126,6 +137,13 @@ export function getNestedReplyText(
126137
+ `<br>sent a file.</blockquote></mx-reply>`;
127138
body = `> <${mxid}> sent a file.\n\n`;
128139
break;
140+
case MsgType.Location: {
141+
const aTheir = isSelfLocation(ev.getContent()) ? "their" : "a";
142+
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> <a href="${userLink}">${mxid}</a>`
143+
+ `<br>shared ${aTheir} location.</blockquote></mx-reply>`;
144+
body = `> <${mxid}> shared ${aTheir} location.\n\n`;
145+
break;
146+
}
129147
case MsgType.Emote: {
130148
html = `<mx-reply><blockquote><a href="${evLink}">In reply to</a> * `
131149
+ `<a href="${userLink}">${mxid}</a><br>${html}</blockquote></mx-reply>`;

test/Reply-test.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
import { IContent, MatrixEvent, MsgType } from "matrix-js-sdk/src/matrix";
18+
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
19+
import { LocationAssetType, M_ASSET } from "matrix-js-sdk/src/@types/location";
20+
1721
import {
1822
getNestedReplyText,
1923
getParentEventId,
@@ -24,6 +28,22 @@ import {
2428
import { mkEvent } from "./test-utils";
2529
import { RoomPermalinkCreator } from "../src/utils/permalinks/Permalinks";
2630

31+
function makeTestEvent(type: string, content: IContent): MatrixEvent {
32+
return mkEvent({
33+
event: true,
34+
type: type,
35+
user: "@user1:server",
36+
room: "!room1:server",
37+
content,
38+
});
39+
}
40+
41+
const mockPermalinkGenerator = {
42+
forEvent(eventId: string): string {
43+
return "$$permalink$$";
44+
},
45+
} as RoomPermalinkCreator;
46+
2747
// don't litter test console with logs
2848
jest.mock("matrix-js-sdk/src/logger");
2949

@@ -99,22 +119,29 @@ But this is not
99119

100120
describe("getNestedReplyText", () => {
101121
it("Returns valid reply fallback text for m.text msgtypes", () => {
102-
const event = mkEvent({
103-
event: true,
104-
type: "m.room.message",
105-
user: "@user1:server",
106-
room: "!room1:server",
107-
content: {
108-
body: "body",
109-
msgtype: "m.text",
110-
},
122+
const event = makeTestEvent(MsgType.Text, {
123+
body: "body",
124+
msgtype: "m.text",
111125
});
112126

113-
expect(getNestedReplyText(event, {
114-
forEvent(eventId: string): string {
115-
return "$$permalink$$";
116-
},
117-
} as RoomPermalinkCreator)).toMatchSnapshot();
127+
expect(getNestedReplyText(event, mockPermalinkGenerator)).toMatchSnapshot();
128+
});
129+
130+
[
131+
["m.room.message", MsgType.Location, LocationAssetType.Pin],
132+
["m.room.message", MsgType.Location, LocationAssetType.Self],
133+
[M_BEACON_INFO.name, undefined, LocationAssetType.Pin],
134+
[M_BEACON_INFO.name, undefined, LocationAssetType.Self],
135+
].forEach(([type, msgType, assetType]) => {
136+
it(`should create the expected fallback text for ${assetType} ${type}/${msgType}`, () => {
137+
const event = makeTestEvent(type, {
138+
body: "body",
139+
msgtype: msgType,
140+
[M_ASSET.name]: { type: assetType },
141+
});
142+
143+
expect(getNestedReplyText(event, mockPermalinkGenerator)).toMatchSnapshot();
144+
});
118145
});
119146
});
120147

test/__snapshots__/Reply-test.ts.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,39 @@ Object {
88
"html": "<mx-reply><blockquote><a href=\\"$$permalink$$\\">In reply to</a> <a href=\\"https://matrix.to/#/@user1:server\\">@user1:server</a><br>body</blockquote></mx-reply>",
99
}
1010
`;
11+
12+
exports[`Reply getNestedReplyText should create the expected fallback text for m.pin m.room.message/m.location 1`] = `
13+
Object {
14+
"body": "> <@user1:server> shared a location.
15+
16+
",
17+
"html": "<mx-reply><blockquote><a href=\\"$$permalink$$\\">In reply to</a> <a href=\\"https://matrix.to/#/@user1:server\\">@user1:server</a><br>shared a location.</blockquote></mx-reply>",
18+
}
19+
`;
20+
21+
exports[`Reply getNestedReplyText should create the expected fallback text for m.pin org.matrix.msc3672.beacon_info/undefined 1`] = `
22+
Object {
23+
"body": "> <@user1:server> shared a live location.
24+
25+
",
26+
"html": "<mx-reply><blockquote><a href=\\"$$permalink$$\\">In reply to</a> <a href=\\"https://matrix.to/#/@user1:server\\">@user1:server</a><br>shared a live location.</blockquote></mx-reply>",
27+
}
28+
`;
29+
30+
exports[`Reply getNestedReplyText should create the expected fallback text for m.self m.room.message/m.location 1`] = `
31+
Object {
32+
"body": "> <@user1:server> shared their location.
33+
34+
",
35+
"html": "<mx-reply><blockquote><a href=\\"$$permalink$$\\">In reply to</a> <a href=\\"https://matrix.to/#/@user1:server\\">@user1:server</a><br>shared their location.</blockquote></mx-reply>",
36+
}
37+
`;
38+
39+
exports[`Reply getNestedReplyText should create the expected fallback text for m.self org.matrix.msc3672.beacon_info/undefined 1`] = `
40+
Object {
41+
"body": "> <@user1:server> shared their live location.
42+
43+
",
44+
"html": "<mx-reply><blockquote><a href=\\"$$permalink$$\\">In reply to</a> <a href=\\"https://matrix.to/#/@user1:server\\">@user1:server</a><br>shared their live location.</blockquote></mx-reply>",
45+
}
46+
`;

0 commit comments

Comments
 (0)