2022-03-24 12:17:42 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-24 12:17:42 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-03-24 12:17:42 +00:00
|
|
|
*/
|
|
|
|
|
2023-08-23 09:04:25 +00:00
|
|
|
import {
|
|
|
|
IContent,
|
|
|
|
MatrixEvent,
|
|
|
|
MsgType,
|
|
|
|
M_BEACON_INFO,
|
|
|
|
LocationAssetType,
|
|
|
|
M_ASSET,
|
|
|
|
M_POLL_END,
|
2024-03-25 12:21:02 +00:00
|
|
|
Room,
|
2023-08-23 09:04:25 +00:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-07-01 06:01:22 +00:00
|
|
|
|
2022-03-24 12:17:42 +00:00
|
|
|
import {
|
|
|
|
getNestedReplyText,
|
|
|
|
getParentEventId,
|
|
|
|
shouldDisplayReply,
|
|
|
|
stripHTMLReply,
|
|
|
|
stripPlainReply,
|
2024-10-15 13:57:26 +00:00
|
|
|
} from "../../src/utils/Reply";
|
|
|
|
import { makePollStartEvent, mkEvent, stubClient } from "../test-utils";
|
|
|
|
import { RoomPermalinkCreator } from "../../src/utils/permalinks/Permalinks";
|
2022-03-24 12:17:42 +00:00
|
|
|
|
2022-07-01 06:01:22 +00:00
|
|
|
function makeTestEvent(type: string, content: IContent): MatrixEvent {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: type,
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!room1:server",
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const mockPermalinkGenerator = {
|
|
|
|
forEvent(eventId: string): string {
|
|
|
|
return "$$permalink$$";
|
|
|
|
},
|
|
|
|
} as RoomPermalinkCreator;
|
|
|
|
|
2022-03-24 12:17:42 +00:00
|
|
|
// don't litter test console with logs
|
|
|
|
jest.mock("matrix-js-sdk/src/logger");
|
|
|
|
|
|
|
|
describe("Reply", () => {
|
|
|
|
describe("getParentEventId", () => {
|
|
|
|
it("returns undefined if given a falsey value", async () => {
|
|
|
|
expect(getParentEventId()).toBeUndefined();
|
|
|
|
});
|
|
|
|
it("returns undefined if given a redacted event", async () => {
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!room1:server",
|
|
|
|
content: {},
|
|
|
|
});
|
2024-03-25 12:21:02 +00:00
|
|
|
event.makeRedacted(event, new Room(event.getRoomId()!, stubClient(), event.getSender()!));
|
2022-03-24 12:17:42 +00:00
|
|
|
|
|
|
|
expect(getParentEventId(event)).toBeUndefined();
|
|
|
|
});
|
|
|
|
it("returns undefined if the given event is not a reply", async () => {
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!room1:server",
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getParentEventId(event)).toBeUndefined();
|
|
|
|
});
|
|
|
|
it("returns id of the event being replied to", async () => {
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!room1:server",
|
|
|
|
content: {
|
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
2022-12-12 11:24:14 +00:00
|
|
|
event_id: "$event1",
|
2022-03-24 12:17:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getParentEventId(event)).toBe("$event1");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("stripPlainReply", () => {
|
|
|
|
it("Removes leading quotes until the first blank line", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(
|
|
|
|
stripPlainReply(
|
|
|
|
`
|
2022-03-24 12:17:42 +00:00
|
|
|
> This is part
|
|
|
|
> of the quote
|
|
|
|
|
|
|
|
But this is not
|
2022-12-12 11:24:14 +00:00
|
|
|
`.trim(),
|
|
|
|
),
|
|
|
|
).toBe("But this is not");
|
2022-03-24 12:17:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("stripHTMLReply", () => {
|
|
|
|
it("Removes <mx-reply> from the input", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(
|
|
|
|
stripHTMLReply(`
|
2022-03-24 12:17:42 +00:00
|
|
|
<mx-reply>
|
|
|
|
This is part
|
|
|
|
of the quote
|
|
|
|
</mx-reply>
|
|
|
|
But this is not
|
2022-12-12 11:24:14 +00:00
|
|
|
`).trim(),
|
|
|
|
).toBe("But this is not");
|
2022-03-24 12:17:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getNestedReplyText", () => {
|
|
|
|
it("Returns valid reply fallback text for m.text msgtypes", () => {
|
2022-07-01 06:01:22 +00:00
|
|
|
const event = makeTestEvent(MsgType.Text, {
|
|
|
|
body: "body",
|
|
|
|
msgtype: "m.text",
|
2022-03-24 12:17:42 +00:00
|
|
|
});
|
|
|
|
|
2022-07-01 06:01:22 +00:00
|
|
|
expect(getNestedReplyText(event, mockPermalinkGenerator)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
(
|
|
|
|
[
|
|
|
|
["m.room.message", MsgType.Location, LocationAssetType.Pin],
|
|
|
|
["m.room.message", MsgType.Location, LocationAssetType.Self],
|
|
|
|
[M_BEACON_INFO.name, undefined, LocationAssetType.Pin],
|
|
|
|
[M_BEACON_INFO.name, undefined, LocationAssetType.Self],
|
|
|
|
] as const
|
|
|
|
).forEach(([type, msgType, assetType]) => {
|
2022-07-01 06:01:22 +00:00
|
|
|
it(`should create the expected fallback text for ${assetType} ${type}/${msgType}`, () => {
|
|
|
|
const event = makeTestEvent(type, {
|
|
|
|
body: "body",
|
|
|
|
msgtype: msgType,
|
|
|
|
[M_ASSET.name]: { type: assetType },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getNestedReplyText(event, mockPermalinkGenerator)).toMatchSnapshot();
|
|
|
|
});
|
2022-03-24 12:17:42 +00:00
|
|
|
});
|
2023-02-13 22:34:47 +00:00
|
|
|
|
|
|
|
it("should create the expected fallback text for poll end events", () => {
|
|
|
|
const event = makeTestEvent(M_POLL_END.name, {
|
|
|
|
body: "body",
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getNestedReplyText(event, mockPermalinkGenerator)).toMatchSnapshot();
|
|
|
|
});
|
2023-03-16 21:27:56 +00:00
|
|
|
|
|
|
|
it("should create the expected fallback text for poll start events", () => {
|
|
|
|
const event = makePollStartEvent("Will this test pass?", "@user:server.org");
|
|
|
|
|
|
|
|
expect(getNestedReplyText(event, mockPermalinkGenerator)).toMatchSnapshot();
|
|
|
|
});
|
2022-03-24 12:17:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("shouldDisplayReply", () => {
|
|
|
|
it("Returns false for redacted events", () => {
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!room1:server",
|
|
|
|
content: {},
|
|
|
|
});
|
2024-03-25 12:21:02 +00:00
|
|
|
event.makeRedacted(event, new Room(event.getRoomId()!, stubClient(), event.getSender()!));
|
2022-03-24 12:17:42 +00:00
|
|
|
|
|
|
|
expect(shouldDisplayReply(event)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Returns false for non-reply events", () => {
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!room1:server",
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(shouldDisplayReply(event)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|