import { StreamContentResponse , StreamContentsResponse } from "../../../infra/api/StreamContentsResponse" ; import { SubscriptionContent , SubscriptionContentIdentifier } from "./SubscriptionContent" ; import { SubscriptionContentBody } from "./SubscriptionContentBody" ; import { SubscriptionContents } from "./SubscriptionContents" ; import { TimeStamp } from "../TimeStamp" ; const he = require ( "he" ) ; export const createSubscriptionContentsFromResponse = ( streamContentResponse ) : SubscriptionContents => { const contentList = streamContentResponse . items . map ( ( item ) => { return createSubscriptionContentFromResponse ( streamContentResponse . id , item ) ; } ) ; return new SubscriptionContents ( { contents : contentList , continuationKey : streamContentResponse . continuation , lastUpdatedTimestamp : TimeStamp . createTimeStampFromSecond ( streamContentResponse . updated ) } ) ; } ; export const createSubscriptionContentFromResponse = ( streamId , streamContentResponse ) : SubscriptionContent => { const hasUpdate = streamContentResponse . updated !== undefined && streamContentResponse . updated !== 0 ; const canonicalHref = streamContentResponse . canonical . map ( ( canonical ) => canonical . href ) . join ( "," ) ; const identifier = `${ streamId }--${ streamContentResponse . id }--${ canonicalHref }` . replace ( /\s+/ , "" ) ; return new SubscriptionContent ( { id : new SubscriptionContentIdentifier ( identifier ) , url : streamContentResponse . canonical [ 0 ] . href , title : he . decode ( streamContentResponse . title ) as string , author : streamContentResponse . author , body : new SubscriptionContentBody ( streamContentResponse . summary . content ) , publishedDate : TimeStamp . createTimeStampFromSecond ( streamContentResponse . published ) , updatedDate : hasUpdate ? TimeStamp . createTimeStampFromSecond ( streamContentResponse . updated ) : TimeStamp . createTimeStampFromSecond ( streamContentResponse . published ) } ) ; } ;