| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { ContentLink } from "./content-link";
- import { ContentParams } from "./content-params";
- export class ContentItem {
- public readonly data: ContentParams;
- public readonly hash: string;
- public readonly type: string;
-
- public readonly myTagsWithoutAll: string[];
- public readonly allLinks: ContentLink[];
- public readonly userLinks: ContentLink[];
- public readonly systemLinks: ContentLink[];
- public readonly repliable: boolean;
- public readonly title?: string;
- public readonly text?: 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;
-
- constructor(hash: string, props: ContentParams) {
- this.data = props;
- this.hash = hash;
- this.type = props.type;
- this.title = props.title;
- this.text = props.text;
-
- 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];
- 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;
- }
- }
|