import { ContentLink } from "./content-link"; import { ContentParams } from "./content-params"; export class ContentItem { public readonly data: ContentParams; public readonly hash: string; public readonly hashInPlaylist: string; public readonly type: string; public readonly date: Date; public readonly myTagsWithoutAll: string[]; public readonly allLinks: ContentLink[]; public readonly userLinks: ContentLink[]; public readonly systemLinks: ContentLink[]; public readonly repliable: boolean; public readonly creator?: string; public readonly title?: string; public readonly text?: string; public readonly file?: string; public readonly thumb?: string; public readonly price?: number; public readonly priceFormatted?: string; public readonly fromTarget?: string; public readonly toTarget?: string; public readonly fromContactId?: string; public readonly toContactId?: string; public readonly media?: string; public readonly relTarget: any; constructor(hashInPlaylist: string, hash: string, props: ContentParams) { this.data = props; this.hash = hash; this.hashInPlaylist = hashInPlaylist; this.type = props.type; this.title = props.title; this.text = props.text; this.creator = props.creator; this.file = props.file; this.thumb = props.thumb; if (props.date) { this.date = new Date(props.date); } else { this.date = new Date(); } if (props.myTags) { this.myTagsWithoutAll = props.myTags.filter(x => x !== 'all'); } else { this.myTagsWithoutAll = []; } if (props.price != null && !isNaN(Number(props.price))) { this.price = Number(props.price); this.priceFormatted = this.formatCents(this.price); } this.allLinks = props.links || []; this.userLinks = this.allLinks.filter(x => !x.rel.startsWith('.')); this.systemLinks = this.allLinks.filter(x => x.rel.startsWith('.')); this.fromTarget = this.allLinks.filter(x => x.rel === '.from').map(x => x.target)[0]; this.toTarget = this.allLinks.filter(x => x.rel === '.to').map(x => x.target)[0]; this.fromContactId = this.allLinks.filter(x => x.rel === '.from-contact-id').map(x => x.target)[0]; this.toContactId = this.allLinks.filter(x => x.rel === '.to-contact-id').map(x => x.target)[0]; this.media = this.allLinks.filter(x => x.rel === '.media').map(x => x.target)[0]; this.relTarget = this.allLinks.reduce((accum, link) => { accum[link.rel] = link.target; return accum; }, {}); if (this.fromTarget && this.toTarget && this.media) { this.repliable = true; } else { this.repliable = false; } } private formatCents(price: number) { if (price == null || price === 0) { return 'free'; } let dollars = `${price / 100}`; if (dollars.match(/\..$/)) { dollars += '0'; } return '$' + dollars; } }