@@ -15,7 +15,7 @@ limitations under the License.
15
15
*/
16
16
17
17
import { Composer as ComposerEvent } from "@matrix-org/analytics-events/types/typescript/Composer" ;
18
- import { IContent , IEventRelation , MatrixEvent } from "matrix-js-sdk/src/models/event" ;
18
+ import { IEventRelation , MatrixEvent } from "matrix-js-sdk/src/models/event" ;
19
19
import { ISendEventResponse , MatrixClient } from "matrix-js-sdk/src/matrix" ;
20
20
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread" ;
21
21
@@ -34,7 +34,7 @@ import EditorStateTransfer from "../../../../../utils/EditorStateTransfer";
34
34
import { createMessageContent } from "./createMessageContent" ;
35
35
import { isContentModified } from "./isContentModified" ;
36
36
37
- interface SendMessageParams {
37
+ export interface SendMessageParams {
38
38
mxClient : MatrixClient ;
39
39
relation ?: IEventRelation ;
40
40
replyToEvent ?: MatrixEvent ;
@@ -43,10 +43,18 @@ interface SendMessageParams {
43
43
includeReplyLegacyFallback ?: boolean ;
44
44
}
45
45
46
- export function sendMessage ( message : string , isHTML : boolean , { roomContext, mxClient, ...params } : SendMessageParams ) {
46
+ export async function sendMessage (
47
+ message : string ,
48
+ isHTML : boolean ,
49
+ { roomContext, mxClient, ...params } : SendMessageParams ,
50
+ ) {
47
51
const { relation, replyToEvent } = params ;
48
52
const { room } = roomContext ;
49
- const { roomId } = room ;
53
+ const roomId = room ?. roomId ;
54
+
55
+ if ( ! roomId ) {
56
+ return ;
57
+ }
50
58
51
59
const posthogEvent : ComposerEvent = {
52
60
eventName : "Composer" ,
@@ -63,18 +71,14 @@ export function sendMessage(message: string, isHTML: boolean, { roomContext, mxC
63
71
}*/
64
72
PosthogAnalytics . instance . trackEvent < ComposerEvent > ( posthogEvent ) ;
65
73
66
- let content : IContent ;
74
+ const content = await createMessageContent ( message , isHTML , params ) ;
67
75
68
76
// TODO slash comment
69
77
70
78
// TODO replace emotion end of message ?
71
79
72
80
// TODO quick reaction
73
81
74
- if ( ! content ) {
75
- content = createMessageContent ( message , isHTML , params ) ;
76
- }
77
-
78
82
// don't bother sending an empty message
79
83
if ( ! content . body . trim ( ) ) {
80
84
return ;
@@ -84,7 +88,7 @@ export function sendMessage(message: string, isHTML: boolean, { roomContext, mxC
84
88
decorateStartSendingTime ( content ) ;
85
89
}
86
90
87
- const threadId = relation ?. rel_type === THREAD_RELATION_TYPE . name ? relation . event_id : null ;
91
+ const threadId = relation ?. event_id && relation ?. rel_type === THREAD_RELATION_TYPE . name ? relation . event_id : null ;
88
92
89
93
const prom = doMaybeLocalRoomAction (
90
94
roomId ,
@@ -139,7 +143,7 @@ interface EditMessageParams {
139
143
editorStateTransfer : EditorStateTransfer ;
140
144
}
141
145
142
- export function editMessage ( html : string , { roomContext, mxClient, editorStateTransfer } : EditMessageParams ) {
146
+ export async function editMessage ( html : string , { roomContext, mxClient, editorStateTransfer } : EditMessageParams ) {
143
147
const editedEvent = editorStateTransfer . getEvent ( ) ;
144
148
145
149
PosthogAnalytics . instance . trackEvent < ComposerEvent > ( {
@@ -156,7 +160,7 @@ export function editMessage(html: string, { roomContext, mxClient, editorStateTr
156
160
const position = this.model.positionForOffset(caret.offset, caret.atNodeEnd);
157
161
this.editorRef.current?.replaceEmoticon(position, REGEX_EMOTICON);
158
162
}*/
159
- const editContent = createMessageContent ( html , true , { editedEvent } ) ;
163
+ const editContent = await createMessageContent ( html , true , { editedEvent } ) ;
160
164
const newContent = editContent [ "m.new_content" ] ;
161
165
162
166
const shouldSend = true ;
@@ -174,10 +178,10 @@ export function editMessage(html: string, { roomContext, mxClient, editorStateTr
174
178
175
179
let response : Promise < ISendEventResponse > | undefined ;
176
180
177
- // If content is modified then send an updated event into the room
178
- if ( isContentModified ( newContent , editorStateTransfer ) ) {
179
- const roomId = editedEvent . getRoomId ( ) ;
181
+ const roomId = editedEvent . getRoomId ( ) ;
180
182
183
+ // If content is modified then send an updated event into the room
184
+ if ( isContentModified ( newContent , editorStateTransfer ) && roomId ) {
181
185
// TODO Slash Commands
182
186
183
187
if ( shouldSend ) {
0 commit comments