@@ -282,6 +282,77 @@ const chatEndpoints = API.v1
282282
283283 return API . v1 . success ( ) ;
284284 } ,
285+ )
286+ . post (
287+ 'chat.update' ,
288+ {
289+ authRequired : true ,
290+ body : isChatUpdateProps ,
291+ response : {
292+ 400 : validateBadRequestErrorResponse ,
293+ 401 : validateUnauthorizedErrorResponse ,
294+ 200 : ajv . compile < { message : IMessage } > ( {
295+ type : 'object' ,
296+ properties : {
297+ message : { $ref : '#/components/schemas/IMessage' } ,
298+ success : {
299+ type : 'boolean' ,
300+ enum : [ true ] ,
301+ } ,
302+ } ,
303+ required : [ 'message' , 'success' ] ,
304+ additionalProperties : false ,
305+ } ) ,
306+ } ,
307+ } ,
308+ async function action ( ) {
309+ const { bodyParams } = this ;
310+
311+ const msg = await Messages . findOneById ( bodyParams . msgId ) ;
312+
313+ // Ensure the message exists
314+ if ( ! msg ) {
315+ return API . v1 . failure ( `No message found with the id of "${ bodyParams . msgId } ".` ) ;
316+ }
317+
318+ if ( bodyParams . roomId !== msg . rid ) {
319+ return API . v1 . failure ( 'The room id provided does not match where the message is from.' ) ;
320+ }
321+
322+ const hasContent = 'content' in bodyParams ;
323+
324+ if ( hasContent && msg . t !== 'e2e' ) {
325+ return API . v1 . failure ( 'Only encrypted messages can have content updated.' ) ;
326+ }
327+
328+ const updateData : Parameters < typeof executeUpdateMessage > = [
329+ this . userId ,
330+ hasContent
331+ ? {
332+ _id : msg . _id ,
333+ rid : msg . rid ,
334+ content : bodyParams . content ,
335+ ...( bodyParams . e2eMentions && { e2eMentions : bodyParams . e2eMentions } ) ,
336+ }
337+ : {
338+ _id : msg . _id ,
339+ rid : msg . rid ,
340+ msg : bodyParams . text ,
341+ ...( bodyParams . customFields && { customFields : bodyParams . customFields } ) ,
342+ } ,
343+ 'previewUrls' in bodyParams ? bodyParams . previewUrls : undefined ,
344+ ] ;
345+
346+ // Permission checks are already done in the updateMessage method, so no need to duplicate them
347+ await applyAirGappedRestrictionsValidation ( ( ) => executeUpdateMessage ( ...updateData ) ) ;
348+
349+ const updatedMessage = await Messages . findOneById ( msg . _id ) ;
350+ const [ message ] = await normalizeMessagesForUser ( updatedMessage ? [ updatedMessage ] : [ ] , this . userId ) ;
351+
352+ return API . v1 . success ( {
353+ message,
354+ } ) ;
355+ } ,
285356 ) ;
286357
287358API . v1 . addRoute (
@@ -421,48 +492,6 @@ API.v1.addRoute(
421492 } ,
422493) ;
423494
424- API . v1 . addRoute (
425- 'chat.update' ,
426- { authRequired : true , validateParams : isChatUpdateProps } ,
427- {
428- async post ( ) {
429- const msg = await Messages . findOneById ( this . bodyParams . msgId ) ;
430-
431- // Ensure the message exists
432- if ( ! msg ) {
433- return API . v1 . failure ( `No message found with the id of "${ this . bodyParams . msgId } ".` ) ;
434- }
435-
436- if ( this . bodyParams . roomId !== msg . rid ) {
437- return API . v1 . failure ( 'The room id provided does not match where the message is from.' ) ;
438- }
439-
440- const msgFromBody = this . bodyParams . text ;
441-
442- // Permission checks are already done in the updateMessage method, so no need to duplicate them
443- await applyAirGappedRestrictionsValidation ( ( ) =>
444- executeUpdateMessage (
445- this . userId ,
446- {
447- _id : msg . _id ,
448- msg : msgFromBody ,
449- rid : msg . rid ,
450- ...( this . bodyParams . customFields && { customFields : this . bodyParams . customFields } ) ,
451- } ,
452- this . bodyParams . previewUrls ,
453- ) ,
454- ) ;
455-
456- const updatedMessage = await Messages . findOneById ( msg . _id ) ;
457- const [ message ] = await normalizeMessagesForUser ( updatedMessage ? [ updatedMessage ] : [ ] , this . userId ) ;
458-
459- return API . v1 . success ( {
460- message,
461- } ) ;
462- } ,
463- } ,
464- ) ;
465-
466495API . v1 . addRoute (
467496 'chat.react' ,
468497 { authRequired : true , validateParams : isChatReactProps } ,
0 commit comments