From 4ba3f99489ed755b77190f593c538f18048166aa Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Fri, 14 Oct 2022 15:52:05 +0200 Subject: [PATCH 1/3] Add reply support to WysiwygComposer --- .../views/rooms/wysiwyg_composer/message.ts | 23 ++++-- .../rooms/wysiwyg_composer/message-test.ts | 79 ++++++++++++++++--- 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/components/views/rooms/wysiwyg_composer/message.ts b/src/components/views/rooms/wysiwyg_composer/message.ts index c1569324ef1..5569af02a95 100644 --- a/src/components/views/rooms/wysiwyg_composer/message.ts +++ b/src/components/views/rooms/wysiwyg_composer/message.ts @@ -22,13 +22,23 @@ import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread"; import { PosthogAnalytics } from "../../../../PosthogAnalytics"; import SettingsStore from "../../../../settings/SettingsStore"; import { decorateStartSendingTime, sendRoundTripMetric } from "../../../../sendTimePerformanceMetrics"; -import { attachRelation } from "../SendMessageComposer"; import { RoomPermalinkCreator } from "../../../../utils/permalinks/Permalinks"; import { doMaybeLocalRoomAction } from "../../../../utils/local-room"; import { CHAT_EFFECTS } from "../../../../effects"; import { containsEmoji } from "../../../../effects/utils"; import { IRoomState } from "../../../structures/RoomView"; import dis from '../../../../dispatcher/dispatcher'; +import { addReplyToMessageContent } from "../../../../utils/Reply"; + +// Merges favouring the given relation +function attachRelation(content: IContent, relation?: IEventRelation): void { + if (relation) { + content['m.relates_to'] = { + ...(content['m.relates_to'] || {}), + ...relation, + }; + } +} interface SendMessageParams { mxClient: MatrixClient; @@ -81,13 +91,12 @@ export function createMessageContent( attachRelation(content, relation); - // TODO reply - /*if (replyToEvent) { + if (replyToEvent) { addReplyToMessageContent(content, replyToEvent, { permalinkCreator, includeLegacyFallback: includeReplyLegacyFallback, }); - }*/ + } return content; } @@ -148,8 +157,7 @@ export function sendMessage( mxClient, ); - // TODO reply - /*if (replyToEvent) { + if (replyToEvent) { // Clear reply_to_event as we put the message into the queue // if the send fails, retry will handle resending. dis.dispatch({ @@ -157,7 +165,8 @@ export function sendMessage( event: null, context: roomContext.timelineRenderingType, }); - }*/ + } + dis.dispatch({ action: "message_sent" }); CHAT_EFFECTS.forEach((effect) => { if (containsEmoji(content, effect.emojis)) { diff --git a/test/components/views/rooms/wysiwyg_composer/message-test.ts b/test/components/views/rooms/wysiwyg_composer/message-test.ts index 605b3e35a7e..2f7532fb8f1 100644 --- a/test/components/views/rooms/wysiwyg_composer/message-test.ts +++ b/test/components/views/rooms/wysiwyg_composer/message-test.ts @@ -22,9 +22,14 @@ import { createTestClient, mkEvent, mkStubRoom } from "../../../../test-utils"; import defaultDispatcher from "../../../../../src/dispatcher/dispatcher"; import SettingsStore from "../../../../../src/settings/SettingsStore"; import { SettingLevel } from "../../../../../src/settings/SettingLevel"; +import { RoomPermalinkCreator } from "../../../../../src/utils/permalinks/Permalinks"; describe('message', () => { - const permalinkCreator = jest.fn() as any; + const permalinkCreator = { + forEvent(eventId: string): string { + return "$$permalink$$"; + }, + } as RoomPermalinkCreator; const message = 'hello world'; const mockEvent = mkEvent({ type: "m.room.message", @@ -45,10 +50,28 @@ describe('message', () => { // Then expect(content).toEqual({ - body: message, - format: "org.matrix.custom.html", - formatted_body: message, - msgtype: "m.text", + "body": message, + "format": "org.matrix.custom.html", + "formatted_body": message, + "msgtype": "m.text", + }); + }); + + it('Should add reply to message content', () => { + // When + const content = createMessageContent(message, { permalinkCreator, replyToEvent: mockEvent }); + + // Then + expect(content).toEqual({ + "body": "> Replying to this\n\nhello world", + "format": "org.matrix.custom.html", + "formatted_body": "
In reply to myfakeuser
Replying to this
hello world", + "msgtype": "m.text", + "m.relates_to": { + "m.in_reply_to": { + "event_id": mockEvent.getId(), + }, + }, }); }); }); @@ -102,6 +125,15 @@ describe('message', () => { const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch"); it('Should not send empty html message', async () => { + // When + await sendMessage('', { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); + + // Then + expect(mockClient.sendMessage).toBeCalledTimes(0); + expect(spyDispatcher).toBeCalledTimes(0); + }); + + it('Should send html message', async () => { // When await sendMessage(message, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); @@ -116,13 +148,42 @@ describe('message', () => { expect(spyDispatcher).toBeCalledWith({ action: 'message_sent' }); }); - it('Should send html message', async () => { + it('Should send reply to html message', async () => { + const mockReplyEvent = mkEvent({ + type: "m.room.message", + room: 'myfakeroom', + user: 'myfakeuser2', + content: { "msgtype": "m.text", "body": "My reply" }, + event: true, + }); + // When - await sendMessage('', { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); + await sendMessage(message, { + roomContext: defaultRoomContext, + mxClient: mockClient, + permalinkCreator, + replyToEvent: mockReplyEvent, + }); // Then - expect(mockClient.sendMessage).toBeCalledTimes(0); - expect(spyDispatcher).toBeCalledTimes(0); + expect(spyDispatcher).toBeCalledWith({ + action: 'reply_to_event', + event: null, + context: defaultRoomContext.timelineRenderingType, + }); + + const expectedContent = { + "body": "> My reply\n\nhello world", + "format": "org.matrix.custom.html", + "formatted_body": "
In reply to myfakeuser2
My reply
hello world", + "msgtype": "m.text", + "m.relates_to": { + "m.in_reply_to": { + "event_id": mockReplyEvent.getId(), + }, + }, + }; + expect(mockClient.sendMessage).toBeCalledWith('myfakeroom', null, expectedContent); }); it('Should scroll to bottom after sending a html message', async () => { From e8d0fb0ab03fa7b90632f4df750db3643173dcce Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Fri, 14 Oct 2022 15:59:40 +0200 Subject: [PATCH 2/3] Fix linting in test --- .../views/rooms/wysiwyg_composer/message-test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/components/views/rooms/wysiwyg_composer/message-test.ts b/test/components/views/rooms/wysiwyg_composer/message-test.ts index 2f7532fb8f1..a762c7696e5 100644 --- a/test/components/views/rooms/wysiwyg_composer/message-test.ts +++ b/test/components/views/rooms/wysiwyg_composer/message-test.ts @@ -65,7 +65,9 @@ describe('message', () => { expect(content).toEqual({ "body": "> Replying to this\n\nhello world", "format": "org.matrix.custom.html", - "formatted_body": "
In reply to myfakeuser
Replying to this
hello world", + "formatted_body": "
In reply to" + + " myfakeuser"+ + "
Replying to this
hello world", "msgtype": "m.text", "m.relates_to": { "m.in_reply_to": { @@ -175,7 +177,9 @@ describe('message', () => { const expectedContent = { "body": "> My reply\n\nhello world", "format": "org.matrix.custom.html", - "formatted_body": "
In reply to myfakeuser2
My reply
hello world", + "formatted_body": "
In reply to" + + " myfakeuser2" + + "
My reply
hello world", "msgtype": "m.text", "m.relates_to": { "m.in_reply_to": { From d1a98b632e336134c9d3fbbced6dca0bd732c6b7 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Fri, 14 Oct 2022 16:42:45 +0200 Subject: [PATCH 3/3] Add test to attachRelation --- .../rooms/wysiwyg_composer/message-test.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/components/views/rooms/wysiwyg_composer/message-test.ts b/test/components/views/rooms/wysiwyg_composer/message-test.ts index a762c7696e5..712b671c9f7 100644 --- a/test/components/views/rooms/wysiwyg_composer/message-test.ts +++ b/test/components/views/rooms/wysiwyg_composer/message-test.ts @@ -76,6 +76,27 @@ describe('message', () => { }, }); }); + + it("Should add relation to message", () => { + // When + const relation = { + rel_type: "m.thread", + event_id: "myFakeThreadId", + }; + const content = createMessageContent(message, { permalinkCreator, relation }); + + // Then + expect(content).toEqual({ + "body": message, + "format": "org.matrix.custom.html", + "formatted_body": message, + "msgtype": "m.text", + "m.relates_to": { + "event_id": "myFakeThreadId", + "rel_type": "m.thread", + }, + }); + }); }); describe('sendMessage', () => {