import { plainToClass } from 'class-transformer' ; import { defaults } from 'lodash' ; import { Diff } from 'utility-types' ; import { AttachmentSticker , ChatSticker , CountdownSticker , HashtagSticker , InstaSticker , LocationSticker , MentionSticker , PollSticker , QuestionSticker , QuizSticker , SliderSticker , } from './stickers' ; export type StickerOptions < T extends InstaSticker > = Diff < T , InstaSticker > & Partial < InstaSticker > ; export type StickerConfig = any & { story_sticker_ids : string ; } ; export class StickerBuilder { private stickers : InstaSticker [ ] = [ ] ; public add ( sticker ) { this . stickers . push ( sticker ) ; return this ; } public build ( ) { const map = new Map < string , InstaSticker [ ] > ( ) ; for ( const sticker of this . stickers ) { if ( map . has ( sticker . key ) ) { map . get ( sticker . key ) ?. push ( sticker ) ; } else { map . set ( sticker . key , [ sticker ] ) ; } } const result = { } ; const ids = [ ] ; const additionalProperties = [ ] ; for ( const stickers of map . values ( ) ) { Object . defineProperty ( result , stickers [ 0 ] . key , { value : JSON . stringify ( stickers ) , enumerable : true } ) ; stickers . forEach ( sticker => additionalProperties . push ( sticker . additionalConfigureProperties ) ) ; ids . push ( stickers [ 0 ] . id ) ; } return { ... defaults ( { } , ... additionalProperties ) , ... result , story_sticker_ids : ids . join ( ',' ) , } ; } public static hashtag ( options ) { return plainToClass ( HashtagSticker , options ) ; } public static mention ( options ) { return plainToClass ( MentionSticker , options ) ; } param mediaInfo param additional public static mentionReel ( mediaInfo , additional = { } ) { return StickerBuilder . mention ( { userId : mediaInfo . user . pk . toString ( ) , mediaId : mediaInfo . pk , displayType : 'mention_reshare' , width : 0.7 , height : 0.9 , ... additional , } ) ; } public static location ( options ) { return plainToClass ( LocationSticker , options ) ; } public static countdown ( options ) { return plainToClass ( CountdownSticker , options ) ; } public static chat ( options ) { return plainToClass ( ChatSticker , options ) ; } public static poll ( options ) { return plainToClass ( PollSticker , options ) ; } public static question ( options ) { return plainToClass ( QuestionSticker , options ) ; } public static quiz ( options ) { return plainToClass ( QuizSticker , { width : 0.7291667 , height : 0.11824318 + options . options . length * 0.10304056 , ... options , } ) ; } public static slider ( options ) { return plainToClass ( SliderSticker , options ) ; } param options public static attachment ( options ) { return plainToClass ( AttachmentSticker , options ) ; } public static attachmentFromMedia ( mediaInfo , additional = { } ) { return StickerBuilder . attachment ( { mediaId : mediaInfo . pk , mediaOwnerId : mediaInfo . user . pk . toString ( ) , ... additional , } ) ; } }