@@ -75,30 +75,42 @@ export class RepliesUpdaterMiddleware implements EventMiddleware {
7575 return event ;
7676 }
7777
78+ /**
79+ * Updates replies to point to the new message ID after an edit
80+ */
81+ private async updateRepliesToEditedMessage (
82+ replies : StoredEvent < MessageAddEvent | MultipartMessageAddEvent > [ ] ,
83+ newMessageId : string ,
84+ ) : Promise < void > {
85+ await Promise . all (
86+ replies . map ( async reply => {
87+ if ( reply . type === ClientEvent . CONVERSATION . MESSAGE_ADD ) {
88+ const quote = reply . data . quote ;
89+ if ( quote && typeof quote !== 'string' && 'message_id' in quote ) {
90+ quote . message_id = newMessageId ;
91+ }
92+ } else if ( reply . type === ClientEvent . CONVERSATION . MULTIPART_MESSAGE_ADD && reply . data . text ?. quote ) {
93+ const quote = reply . data . text . quote ;
94+ if ( quote && typeof quote !== 'string' && 'message_id' in quote ) {
95+ quote . message_id = newMessageId ;
96+ }
97+ }
98+ await this . eventService . replaceEvent ( reply ) ;
99+ } ) ,
100+ ) ;
101+ }
102+
78103 /**
79104 * will update the message ID of all the replies to an edited message
80105 */
81106 private async handleEditEvent ( event : MessageAddEvent , originalMessageId : string ) {
82107 const { originalEvent, replies} = await this . findRepliesToMessage ( event . conversation , originalMessageId , event . id ) ;
83- if ( ! originalEvent ) {
108+ if ( ! originalEvent || ! event . id ) {
84109 return event ;
85110 }
86111
87112 this . logger . info ( `Updating '${ replies . length } ' replies to updated message '${ originalMessageId } '` ) ;
88- replies . forEach ( async reply => {
89- if ( reply . type === ClientEvent . CONVERSATION . MESSAGE_ADD ) {
90- const quote = reply . data . quote ;
91- if ( quote && typeof quote !== 'string' && 'message_id' in quote && 'id' in event ) {
92- quote . message_id = event . id as string ;
93- }
94- } else if ( reply . type === ClientEvent . CONVERSATION . MULTIPART_MESSAGE_ADD && reply . data . text ?. quote ) {
95- const quote = reply . data . text . quote ;
96- if ( quote && typeof quote !== 'string' && 'message_id' in quote && 'id' in event ) {
97- quote . message_id = event . id as string ;
98- }
99- }
100- await this . eventService . replaceEvent ( reply ) ;
101- } ) ;
113+ await this . updateRepliesToEditedMessage ( replies , event . id ) ;
102114 return event ;
103115 }
104116
@@ -107,25 +119,12 @@ export class RepliesUpdaterMiddleware implements EventMiddleware {
107119 */
108120 private async handleMultipartEditEvent ( event : MultipartMessageAddEvent , originalMessageId : string ) {
109121 const { originalEvent, replies} = await this . findRepliesToMessage ( event . conversation , originalMessageId , event . id ) ;
110- if ( ! originalEvent ) {
122+ if ( ! originalEvent || ! event . id ) {
111123 return event ;
112124 }
113125
114126 this . logger . info ( `Updating '${ replies . length } ' replies to updated multipart message '${ originalMessageId } '` ) ;
115- replies . forEach ( async reply => {
116- if ( reply . type === ClientEvent . CONVERSATION . MESSAGE_ADD ) {
117- const quote = reply . data . quote ;
118- if ( quote && typeof quote !== 'string' && 'message_id' in quote && 'id' in event ) {
119- quote . message_id = event . id as string ;
120- }
121- } else if ( reply . type === ClientEvent . CONVERSATION . MULTIPART_MESSAGE_ADD && reply . data . text ?. quote ) {
122- const quote = reply . data . text . quote ;
123- if ( quote && typeof quote !== 'string' && 'message_id' in quote && 'id' in event ) {
124- quote . message_id = event . id as string ;
125- }
126- }
127- await this . eventService . replaceEvent ( reply ) ;
128- } ) ;
127+ await this . updateRepliesToEditedMessage ( replies , event . id ) ;
129128 return event ;
130129 }
131130
0 commit comments