@@ -398,8 +398,8 @@ export class LabChatModel
398398 }
399399
400400 private _onchange = async ( _ : YChat , changes : IChatChanges ) => {
401- if ( changes . messageChanges ) {
402- const msgDelta = changes . messageChanges ;
401+ if ( changes . messageListChanges ) {
402+ const msgDelta = changes . messageListChanges ;
403403 let index = 0 ;
404404 for ( const delta of msgDelta ) {
405405 if ( delta . retain ) {
@@ -411,7 +411,7 @@ export class LabChatModel
411411 attachments : attachmentIds ,
412412 mentions : mentionsIds ,
413413 ...baseMessage
414- } = ymessage ;
414+ } = ymessage . toJSON ( ) as IYmessage ;
415415
416416 // Build the base message with sender.
417417 const msg : IChatMessage = {
@@ -457,6 +457,53 @@ export class LabChatModel
457457 }
458458 }
459459
460+ if ( changes . messageChanges ) {
461+ // Update change in the message.
462+ changes . messageChanges . forEach ( change => {
463+ const message = this . messages [ change . index ] ;
464+ if ( change . type === 'remove' ) {
465+ delete message [ change . key as keyof IChatMessage ] ;
466+ } else if ( change . newValue !== undefined ) {
467+ const key = change . key ;
468+ const value = change . newValue ;
469+ if ( key === 'attachments' ) {
470+ const attachments : IAttachment [ ] = [ ] ;
471+ ( value as string [ ] ) . forEach ( attachmentId => {
472+ const attachment = this . sharedModel . getAttachment ( attachmentId ) ;
473+ if ( attachment ) {
474+ attachments . push ( attachment ) ;
475+ }
476+ } ) ;
477+ if ( attachments . length ) {
478+ message . attachments = attachments ;
479+ } else {
480+ delete message . attachments ;
481+ }
482+ } else if ( key === 'mentions' ) {
483+ const mentions : IUser [ ] = ( value as string [ ] ) . map (
484+ user =>
485+ this . sharedModel . getUser ( user ) || {
486+ username : 'User undefined' ,
487+ mention_name : 'User-undefined'
488+ }
489+ ) ;
490+ if ( mentions ?. length ) {
491+ message . mentions = mentions ;
492+ }
493+ } else if (
494+ [ 'body' , 'time' , 'raw_time' , 'deleted' , 'edited' ] . includes ( key )
495+ ) {
496+ ( message as any ) [ key ] = value ;
497+ } else {
498+ console . error (
499+ `The attribute '${ key } ' of message cannot be updated`
500+ ) ;
501+ }
502+ }
503+ this . messageUpdated ( change . index , message ) ;
504+ } ) ;
505+ }
506+
460507 if ( changes . metadataChanges ) {
461508 changes . metadataChanges . forEach ( change => {
462509 // no need to search for update or add, if the new value contains ID, let's
0 commit comments