2022-10-22 01:26:33 +08:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2023-04-11 16:23:03 +08:00
|
|
|
import { MsgType } from "matrix-js-sdk/src/matrix";
|
2022-10-22 01:26:33 +08:00
|
|
|
|
2023-11-29 18:11:53 +08:00
|
|
|
import { filterConsole, mkEvent } from "../../../../../test-utils";
|
2022-10-22 01:26:33 +08:00
|
|
|
import { RoomPermalinkCreator } from "../../../../../../src/utils/permalinks/Permalinks";
|
2023-04-11 16:23:03 +08:00
|
|
|
import {
|
|
|
|
createMessageContent,
|
|
|
|
EMOTE_PREFIX,
|
|
|
|
} from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/createMessageContent";
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
describe("createMessageContent", () => {
|
|
|
|
const permalinkCreator = {
|
|
|
|
forEvent(eventId: string): string {
|
|
|
|
return "$$permalink$$";
|
|
|
|
},
|
|
|
|
} as RoomPermalinkCreator;
|
2023-01-04 20:57:09 +08:00
|
|
|
const message = "<em><b>hello</b> world</em>";
|
2022-10-22 01:26:33 +08:00
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
|
|
|
content: { msgtype: "m.text", body: "Replying to this" },
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
describe("Richtext composer input", () => {
|
2023-11-29 18:11:53 +08:00
|
|
|
filterConsole(
|
|
|
|
"WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm`",
|
|
|
|
);
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
// Warm up by creating the component once, with a long timeout.
|
|
|
|
// This prevents tests timing out because of the time spent loading
|
|
|
|
// the WASM component.
|
|
|
|
await createMessageContent(message, true, { permalinkCreator });
|
|
|
|
}, 10000);
|
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
it("Should create html message", async () => {
|
|
|
|
// When
|
|
|
|
const content = await createMessageContent(message, true, { permalinkCreator });
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(content).toEqual({
|
|
|
|
body: "*__hello__ world*",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: message,
|
|
|
|
msgtype: "m.text",
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
it("Should add reply to message content", async () => {
|
|
|
|
// When
|
|
|
|
const content = await createMessageContent(message, true, { permalinkCreator, replyToEvent: mockEvent });
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(content).toEqual({
|
|
|
|
"body": "> <myfakeuser> Replying to this\n\n*__hello__ world*",
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"formatted_body":
|
|
|
|
'<mx-reply><blockquote><a href="$$permalink$$">In reply to</a>' +
|
|
|
|
' <a href="https://matrix.to/#/myfakeuser">myfakeuser</a>' +
|
|
|
|
"<br>Replying to this</blockquote></mx-reply><em><b>hello</b> world</em>",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: mockEvent.getId(),
|
|
|
|
},
|
2022-10-22 01:26:33 +08:00
|
|
|
},
|
2023-06-21 23:02:52 +08:00
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
it("Should add relation to message", async () => {
|
|
|
|
// When
|
|
|
|
const relation = {
|
2022-10-22 01:26:33 +08:00
|
|
|
rel_type: "m.thread",
|
2023-06-21 23:02:52 +08:00
|
|
|
event_id: "myFakeThreadId",
|
|
|
|
};
|
|
|
|
const content = await createMessageContent(message, true, { permalinkCreator, relation });
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(content).toEqual({
|
|
|
|
"body": "*__hello__ world*",
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"formatted_body": message,
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"m.relates_to": {
|
|
|
|
event_id: "myFakeThreadId",
|
|
|
|
rel_type: "m.thread",
|
|
|
|
},
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
it("Should add fields related to edition", async () => {
|
|
|
|
// When
|
|
|
|
const editedEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser2",
|
|
|
|
content: {
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "First message",
|
|
|
|
"formatted_body": "<b>First Message</b>",
|
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: "eventId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
const content = await createMessageContent(message, true, { permalinkCreator, editedEvent });
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(content).toEqual({
|
|
|
|
"body": " * *__hello__ world*",
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"formatted_body": ` * ${message}`,
|
2022-10-22 01:26:33 +08:00
|
|
|
"msgtype": "m.text",
|
2023-06-21 23:02:52 +08:00
|
|
|
"m.new_content": {
|
|
|
|
body: "*__hello__ world*",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: message,
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
2022-10-22 01:26:33 +08:00
|
|
|
"m.relates_to": {
|
2023-06-21 23:02:52 +08:00
|
|
|
event_id: editedEvent.getId(),
|
|
|
|
rel_type: "m.replace",
|
2022-10-22 01:26:33 +08:00
|
|
|
},
|
2023-06-21 23:02:52 +08:00
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
2023-06-21 23:02:52 +08:00
|
|
|
|
|
|
|
it("Should strip the /me prefix from a message", async () => {
|
|
|
|
const textBody = "some body text";
|
|
|
|
const content = await createMessageContent(EMOTE_PREFIX + textBody, true, { permalinkCreator });
|
|
|
|
|
|
|
|
expect(content).toMatchObject({ body: textBody, formatted_body: textBody });
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
2023-04-11 16:23:03 +08:00
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
it("Should strip single / from message prefixed with //", async () => {
|
|
|
|
const content = await createMessageContent("//twoSlashes", true, { permalinkCreator });
|
2023-04-11 16:23:03 +08:00
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
expect(content).toMatchObject({ body: "/twoSlashes", formatted_body: "/twoSlashes" });
|
|
|
|
});
|
2023-04-11 16:23:03 +08:00
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
it("Should set the content type to MsgType.Emote when /me prefix is used", async () => {
|
|
|
|
const textBody = "some body text";
|
|
|
|
const content = await createMessageContent(EMOTE_PREFIX + textBody, true, { permalinkCreator });
|
2023-04-11 16:23:03 +08:00
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
expect(content).toMatchObject({ msgtype: MsgType.Emote });
|
|
|
|
});
|
2023-04-11 16:23:03 +08:00
|
|
|
});
|
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
describe("Plaintext composer input", () => {
|
|
|
|
it("Should replace at-room mentions with `@room` in body", async () => {
|
|
|
|
const messageComposerState = `<a href="#" contenteditable="false" data-mention-type="at-room" style="some styling">@room</a> `;
|
|
|
|
|
|
|
|
const content = await createMessageContent(messageComposerState, false, { permalinkCreator });
|
|
|
|
expect(content).toMatchObject({ body: "@room " });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should replace user mentions with user name in body", async () => {
|
|
|
|
const messageComposerState = `<a href="https://matrix.to/#/@test_user:element.io" contenteditable="false" data-mention-type="user" style="some styling">a test user</a> `;
|
|
|
|
|
|
|
|
const content = await createMessageContent(messageComposerState, false, { permalinkCreator });
|
2023-04-11 16:23:03 +08:00
|
|
|
|
2023-06-21 23:02:52 +08:00
|
|
|
expect(content).toMatchObject({ body: "a test user " });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should replace room mentions with room mxid in body", async () => {
|
|
|
|
const messageComposerState = `<a href="https://matrix.to/#/#test_room:element.io" contenteditable="false" data-mention-type="room" style="some styling">a test room</a> `;
|
|
|
|
|
|
|
|
const content = await createMessageContent(messageComposerState, false, { permalinkCreator });
|
|
|
|
|
|
|
|
expect(content).toMatchObject({
|
|
|
|
body: "#test_room:element.io ",
|
|
|
|
});
|
|
|
|
});
|
2023-04-11 16:23:03 +08:00
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|