import type { ConversationOtrMessageAddEvent } from '@wireapp/api-client/src/event/' ; import logdown from 'logdown' ; import { GenericMessageType , PayloadBundle , PayloadBundleSource , PayloadBundleState , PayloadBundleType , } from '../conversation' ; import type { AssetContent , ClearedContent , ConfirmationContent , DeletedContent , EditedTextContent , HiddenContent , KnockContent , LocationContent , ReactionContent , TextContent , } from '../conversation/content' ; export class GenericMessageMapper { private static readonly logger = logdown ( '@wireapp/core/cryptography/GenericMessageMapper' , { logger : console , markdown : false , } ) ; public static mapGenericMessage ( genericMessage , event , source ) { const baseMessage = { conversation : event . conversation , qualifiedConversation : event . qualified_conversation , qualifiedFrom : event . qualified_from , fromClientId : event . data . sender , from : event . from , state : PayloadBundleState . INCOMING , timestamp : new Date ( event . time ) . getTime ( ) , id : genericMessage . messageId , messageTimer : 0 , source , } ; switch ( genericMessage . content ) { case GenericMessageType . TEXT : { const { content : text , expectsReadConfirmation , legalHoldStatus , linkPreview : linkPreviews , mentions , quote , } = genericMessage [ GenericMessageType . TEXT ] ; const content = { expectsReadConfirmation , legalHoldStatus , text } ; if ( linkPreviews ?. length ) { content . linkPreviews = linkPreviews ; } if ( mentions ?. length ) { content . mentions = mentions ; } if ( quote ) { content . quote = quote ; } if ( typeof legalHoldStatus !== 'undefined' ) { content . legalHoldStatus = legalHoldStatus ; } return { ... baseMessage , content , type : PayloadBundleType . TEXT , } ; } case GenericMessageType . BUTTON_ACTION : { return { ... baseMessage , content : genericMessage . buttonAction ! , type : PayloadBundleType . BUTTON_ACTION , } ; } case GenericMessageType . CALLING : { return { ... baseMessage , content : genericMessage . calling . content , type : PayloadBundleType . CALL , } ; } case GenericMessageType . CONFIRMATION : { const { firstMessageId , moreMessageIds , type } = genericMessage [ GenericMessageType . CONFIRMATION ] ; const content = { firstMessageId , moreMessageIds , type } ; return { ... baseMessage , content , type : PayloadBundleType . CONFIRMATION , } ; } case GenericMessageType . CLEARED : { const content = genericMessage [ GenericMessageType . CLEARED ] ; return { ... baseMessage , content , type : PayloadBundleType . CONVERSATION_CLEAR , } ; } case GenericMessageType . DELETED : { const originalMessageId = genericMessage [ GenericMessageType . DELETED ] . messageId ; const content = { messageId : originalMessageId } ; return { ... baseMessage , content , type : PayloadBundleType . MESSAGE_DELETE , } ; } case GenericMessageType . EDITED : { const { expectsReadConfirmation , text : { content : editedText , legalHoldStatus , linkPreview : editedLinkPreviews , mentions : editedMentions , quote : editedQuote , } , replacingMessageId , } = genericMessage [ GenericMessageType . EDITED ] ; const content = { expectsReadConfirmation , legalHoldStatus , originalMessageId : replacingMessageId , text : editedText , } ; if ( editedLinkPreviews ?. length ) { content . linkPreviews = editedLinkPreviews ; } if ( editedMentions ?. length ) { content . mentions = editedMentions ; } if ( editedQuote ) { content . quote = editedQuote ; } return { ... baseMessage , content , type : PayloadBundleType . MESSAGE_EDIT , } ; } case GenericMessageType . HIDDEN : { const { conversationId , messageId } = genericMessage [ GenericMessageType . HIDDEN ] ; const content = { conversationId , messageId , } ; return { ... baseMessage , content , type : PayloadBundleType . MESSAGE_HIDE , } ; } case GenericMessageType . KNOCK : { const { expectsReadConfirmation , legalHoldStatus } = genericMessage [ GenericMessageType . KNOCK ] ; const content = { expectsReadConfirmation , hotKnock : false , legalHoldStatus } ; return { ... baseMessage , content , type : PayloadBundleType . PING , } ; } case GenericMessageType . LOCATION : { const { expectsReadConfirmation , latitude , legalHoldStatus , longitude , name , zoom } = genericMessage [ GenericMessageType . LOCATION ] ; const content = { expectsReadConfirmation , latitude , legalHoldStatus , longitude , name , zoom , } ; return { ... baseMessage , content , type : PayloadBundleType . LOCATION , } ; } case GenericMessageType . ASSET : { const { expectsReadConfirmation , legalHoldStatus , notUploaded , original , preview , status , uploaded } = genericMessage [ GenericMessageType . ASSET ] ; const isImage = ! ! uploaded ?. assetId && ! ! original ?. image ; const content = { abortReason : notUploaded , expectsReadConfirmation , legalHoldStatus , original , preview , status , uploaded , } ; return { ... baseMessage , content , type : isImage ? PayloadBundleType . ASSET_IMAGE : PayloadBundleType . ASSET , } ; } case GenericMessageType . REACTION : { const { emoji , legalHoldStatus , messageId } = genericMessage [ GenericMessageType . REACTION ] ; const content = { legalHoldStatus , originalMessageId : messageId , type : emoji , } ; return { ... baseMessage , content , type : PayloadBundleType . REACTION , } ; } default : { this . logger . warn ( `Unhandled event type "${ genericMessage . content }": ${ JSON . stringify ( genericMessage ) }` ) ; return { ... baseMessage , content : genericMessage . content , type : PayloadBundleType . UNKNOWN , } ; } } } }