diff --git a/src/components/views/rooms/wysiwyg_composer/message.ts b/src/components/views/rooms/wysiwyg_composer/message.ts index e39f6b48c8..9f0f47d128 100644 --- a/src/components/views/rooms/wysiwyg_composer/message.ts +++ b/src/components/views/rooms/wysiwyg_composer/message.ts @@ -23,7 +23,6 @@ import { PosthogAnalytics } from "../../../../PosthogAnalytics"; import SettingsStore from "../../../../settings/SettingsStore"; import { decorateStartSendingTime, sendRoundTripMetric } from "../../../../sendTimePerformanceMetrics"; import { attachRelation } from "../SendMessageComposer"; -import { addReplyToMessageContent } from "../../../../utils/Reply"; import { RoomPermalinkCreator } from "../../../../utils/permalinks/Permalinks"; import { doMaybeLocalRoomAction } from "../../../../utils/local-room"; import { CHAT_EFFECTS } from "../../../../effects"; @@ -33,7 +32,7 @@ import dis from '../../../../dispatcher/dispatcher'; interface SendMessageParams { mxClient: MatrixClient; - relation: IEventRelation; + relation?: IEventRelation; replyToEvent?: MatrixEvent; roomContext: IRoomState; permalinkCreator: RoomPermalinkCreator; @@ -81,12 +80,14 @@ export function createMessageContent( } attachRelation(content, relation); - if (replyToEvent) { + + // TODO reply + /*if (replyToEvent) { addReplyToMessageContent(content, replyToEvent, { permalinkCreator, includeLegacyFallback: includeReplyLegacyFallback, }); - } + }*/ return content; } @@ -106,7 +107,7 @@ export function sendMessage( inThread: relation?.rel_type === THREAD_RELATION_TYPE.name, }; if (posthogEvent.inThread) { - const threadRoot = room.findEventById(relation.event_id); + const threadRoot = room.findEventById(relation?.event_id); posthogEvent.startsThread = threadRoot?.getThread()?.events.length === 1; } PosthogAnalytics.instance.trackEvent(posthogEvent); @@ -144,7 +145,9 @@ export function sendMessage( (actualRoomId: string) => mxClient.sendMessage(actualRoomId, threadId, content), mxClient, ); - if (replyToEvent) { + + // TODO reply + /*if (replyToEvent) { // Clear reply_to_event as we put the message into the queue // if the send fails, retry will handle resending. dis.dispatch({ @@ -152,7 +155,7 @@ export function sendMessage( event: null, context: roomContext.timelineRenderingType, }); - } + }*/ dis.dispatch({ action: "message_sent" }); CHAT_EFFECTS.forEach((effect) => { if (containsEmoji(content, effect.emojis)) { @@ -180,4 +183,6 @@ export function sendMessage( timelineRenderingType: roomContext.timelineRenderingType, }); } + + return prom; } diff --git a/test/components/views/rooms/wysisyg_composer/message-test.ts b/test/components/views/rooms/wysisyg_composer/message-test.ts new file mode 100644 index 0000000000..fe4183f6d0 --- /dev/null +++ b/test/components/views/rooms/wysisyg_composer/message-test.ts @@ -0,0 +1,148 @@ +/* +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. +*/ + +import { IRoomState } from "../../../../../src/components/structures/RoomView"; +import { createMessageContent, sendMessage } from "../../../../../src/components/views/rooms/wysiwyg_composer/message"; +import { TimelineRenderingType } from "../../../../../src/contexts/RoomContext"; +import { Layout } from "../../../../../src/settings/enums/Layout"; +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"; + +describe('message', () => { + const permalinkCreator = jest.fn() as any; + const message = 'hello world'; + const mockEvent = mkEvent({ + type: "m.room.message", + room: 'myfakeroom', + user: 'myfakeuser', + content: { "msgtype": "m.text", "body": "Replying to this" }, + event: true, + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + describe('createMessageContent', () => { + it("Should create html message", () => { + // When + const content = createMessageContent(message, { permalinkCreator }); + + // Then + expect(content).toEqual({ + body: message, + format: "org.matrix.custom.html", + formatted_body: message, + msgtype: "m.text", + }); + }); + }); + + describe('sendMessage', () => { + const mockClient = createTestClient(); + const mockRoom = mkStubRoom('myfakeroom', 'myfakeroom', mockClient) as any; + mockRoom.findEventById = jest.fn(eventId => { + return eventId === mockEvent.getId() ? mockEvent : null; + }); + + const defaultRoomContext: IRoomState = { + room: mockRoom, + roomLoading: true, + peekLoading: false, + shouldPeek: true, + membersLoaded: false, + numUnreadMessages: 0, + canPeek: false, + showApps: false, + isPeeking: false, + showRightPanel: true, + joining: false, + atEndOfLiveTimeline: true, + showTopUnreadMessagesBar: false, + statusBarVisible: false, + canReact: false, + canSendMessages: false, + canSendVoiceBroadcasts: false, + layout: Layout.Group, + lowBandwidth: false, + alwaysShowTimestamps: false, + showTwelveHourTimestamps: false, + readMarkerInViewThresholdMs: 3000, + readMarkerOutOfViewThresholdMs: 30000, + showHiddenEvents: false, + showReadReceipts: true, + showRedactions: true, + showJoinLeaves: true, + showAvatarChanges: true, + showDisplaynameChanges: true, + matrixClientIsReady: false, + timelineRenderingType: TimelineRenderingType.Room, + liveTimeline: undefined, + canSelfRedact: false, + resizing: false, + narrow: false, + }; + + const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch"); + + it('Should not send empty html message', async () => { + // When + await sendMessage(message, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); + + // Then + const expectedContent = { + "body": "hello world", + "format": "org.matrix.custom.html", + "formatted_body": "hello world", + "msgtype": "m.text", + }; + expect(mockClient.sendMessage).toBeCalledWith('myfakeroom', null, expectedContent); + expect(spyDispatcher).toBeCalledWith({ action: 'message_sent' }); + }); + + it('Should send html message', async () => { + // When + await sendMessage('', { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); + + // Then + expect(mockClient.sendMessage).toBeCalledTimes(0); + expect(spyDispatcher).toBeCalledTimes(0); + }); + + it('Should scroll to bottom after sending a html message', async () => { + // When + SettingsStore.setValue("scrollToBottomOnMessageSent", null, SettingLevel.DEVICE, true); + await sendMessage(message, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); + + // Then + expect(spyDispatcher).toBeCalledWith( + { action: 'scroll_to_bottom', timelineRenderingType: defaultRoomContext.timelineRenderingType }, + ); + }); + + it('Should handle emojis', async () => { + // When + await sendMessage('🎉', { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator }); + + // Then + expect(spyDispatcher).toBeCalledWith( + { action: 'effects.confetti' }, + ); + }); + }); +});