import { ClientAction , Confirmation } from '@wireapp/protocol-messaging' ; import UUID from 'uuidjs' ; import { AbortReason , PayloadBundleSource , PayloadBundleState , PayloadBundleType } from '..' ; import { EncryptedAssetUploaded } from '../../cryptography' ; import type { ButtonActionConfirmationContent , ButtonActionContent , CallingContent , FileContent , FileMetaDataContent , ImageContent , KnockContent , LegalHoldStatus , LocationContent , ReactionContent , } from '../content' ; import { CompositeContentBuilder } from './CompositeContentBuilder' ; import type { ButtonActionConfirmationMessage , ButtonActionMessage , CallMessage , ConfirmationMessage , FileAssetAbortMessage , FileAssetMessage , FileAssetMetaDataMessage , ImageAssetMessageOutgoing , LocationMessage , PingMessage , ReactionMessage , ResetSessionMessage , } from './OtrMessage' ; import { TextContentBuilder } from './TextContentBuilder' ; interface BaseOptions { conversationId : string ; from : string ; messageId ? : string ; } interface CreateImageOptions extends BaseOptions { expectsReadConfirmation ? : boolean ; asset : EncryptedAssetUploaded ; image : ImageContent ; legalHoldStatus ? : LegalHoldStatus ; } interface CreateFileOptions extends BaseOptions { expectsReadConfirmation ? : boolean ; asset : EncryptedAssetUploaded ; file : FileContent ; legalHoldStatus ? : LegalHoldStatus ; originalMessageId : string ; } interface CreateEditedTextOptions extends BaseOptions { newMessageText : string ; originalMessageId : string ; } interface CreateFileMetadataOptions extends BaseOptions { expectsReadConfirmation ? : boolean ; legalHoldStatus ? : LegalHoldStatus ; metaData : FileMetaDataContent ; } interface CreateFileAbortOptions { conversationId : string ; expectsReadConfirmation ? : boolean ; from : string ; legalHoldStatus ? : LegalHoldStatus ; originalMessageId : string ; reason : AbortReason ; } interface CreateLocationOptions extends BaseOptions { location : LocationContent ; } interface CreateCallOptions extends BaseOptions { content : CallingContent ; } interface CreateReactionOptions extends BaseOptions { reaction : ReactionContent ; } interface CreateTextOptions extends BaseOptions { text : string ; } interface CreateConfirmationOptions extends BaseOptions { firstMessageId : string ; moreMessageIds ? : string [ ] ; type : Confirmation . Type ; } interface CreatePingOptions extends BaseOptions { ping ? : KnockContent ; } interface CreateButtonActionConfirmationOptions extends BaseOptions { content : ButtonActionConfirmationContent ; } interface CreateActionMessageOptions extends BaseOptions { content : ButtonActionContent ; } function createCommonProperties < T extends BaseOptions > ( options ) { return { id : options . messageId || MessageBuilder . createId ( ) , conversation : options . conversationId , from : options . from , source : PayloadBundleSource . LOCAL , state : PayloadBundleState . OUTGOING_UNSENT , timestamp : Date . now ( ) , } ; } export class MessageBuilder { public static createEditedText ( payload ) { return new TextContentBuilder ( { ... createCommonProperties ( payload ) , content : { originalMessageId : payload . originalMessageId , text : payload . newMessageText , } , type : PayloadBundleType . MESSAGE_EDIT , } ) ; } public static createFileData ( payload ) { const { asset , expectsReadConfirmation , file , legalHoldStatus , originalMessageId } = payload ; return { ... createCommonProperties ( payload ) , content : { asset , expectsReadConfirmation , file , legalHoldStatus , } , id : originalMessageId , type : PayloadBundleType . ASSET , } ; } public static createFileMetadata ( payload ) { const { expectsReadConfirmation , legalHoldStatus , metaData } = payload ; return { ... createCommonProperties ( payload ) , content : { expectsReadConfirmation , legalHoldStatus , metaData , } , type : PayloadBundleType . ASSET_META , } ; } public static createFileAbort ( payload ) { const { expectsReadConfirmation , legalHoldStatus , reason } = payload ; return { ... createCommonProperties ( payload ) , content : { expectsReadConfirmation , legalHoldStatus , reason , } , id : payload . originalMessageId , type : PayloadBundleType . ASSET_ABORT , } ; } public static createImage ( payload ) { const { expectsReadConfirmation , image , asset , legalHoldStatus } = payload ; return { ... createCommonProperties ( payload ) , content : { expectsReadConfirmation , image , asset , legalHoldStatus , } , type : PayloadBundleType . ASSET_IMAGE , } ; } public static createLocation ( payload ) { return { ... createCommonProperties ( payload ) , content : payload . location , type : PayloadBundleType . LOCATION , } ; } public static createCall ( payload ) { return { ... createCommonProperties ( payload ) , content : payload . content , type : PayloadBundleType . CALL , } ; } public static createReaction ( payload ) { return { ... createCommonProperties ( payload ) , content : payload . reaction , type : PayloadBundleType . REACTION , } ; } public static createText ( payload ) { return new TextContentBuilder ( { ... createCommonProperties ( payload ) , content : { text : payload . text } , type : PayloadBundleType . TEXT , } ) ; } public static createConfirmation ( payload ) { const { firstMessageId , moreMessageIds , type } = payload ; return { ... createCommonProperties ( payload ) , content : { firstMessageId , moreMessageIds , type } , type : PayloadBundleType . CONFIRMATION , } ; } public static createButtonActionMessage ( payload ) { return { ... createCommonProperties ( payload ) , content : payload . content , type : PayloadBundleType . BUTTON_ACTION , } ; } public static createButtonActionConfirmationMessage ( payload ) { return { ... createCommonProperties ( payload ) , content : payload . content , type : PayloadBundleType . BUTTON_ACTION_CONFIRMATION , } ; } public static createComposite ( payload ) { return new CompositeContentBuilder ( { ... createCommonProperties ( payload ) , content : { } , type : PayloadBundleType . COMPOSITE , } ) ; } public static createPing ( payload ) { return { ... createCommonProperties ( payload ) , content : payload . ping || { hotKnock : false } , type : PayloadBundleType . PING , } ; } public static createSessionReset ( payload ) { return { ... createCommonProperties ( payload ) , content : { clientAction : ClientAction . RESET_SESSION , } , type : PayloadBundleType . CLIENT_ACTION , } ; } public static createId ( ) { return UUID . genV4 ( ) . toString ( ) ; } }