content-item.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { ContentLink } from "./content-link";
  2. import { ContentParams } from "./content-params";
  3. export class ContentItem {
  4. public readonly data: ContentParams;
  5. public readonly hash: string;
  6. public readonly type: string;
  7. public readonly myTagsWithoutAll: string[];
  8. public readonly allLinks: ContentLink[];
  9. public readonly userLinks: ContentLink[];
  10. public readonly systemLinks: ContentLink[];
  11. public readonly repliable: boolean;
  12. public readonly title?: string;
  13. public readonly text?: string;
  14. public readonly price?: number;
  15. public readonly priceFormatted?: string;
  16. public readonly fromTarget?: string;
  17. public readonly toTarget?: string;
  18. public readonly fromContactId?: string;
  19. public readonly toContactId?: string;
  20. public readonly media?: string;
  21. constructor(hash: string, props: ContentParams) {
  22. this.data = props;
  23. this.hash = hash;
  24. this.type = props.type;
  25. this.title = props.title;
  26. this.text = props.text;
  27. if (props.myTags) {
  28. this.myTagsWithoutAll = props.myTags.filter(x => x !== 'all');
  29. } else {
  30. this.myTagsWithoutAll = [];
  31. }
  32. if (props.price != null && !isNaN(Number(props.price))) {
  33. this.price = Number(props.price);
  34. this.priceFormatted = this.formatCents(this.price);
  35. }
  36. this.allLinks = props.links || [];
  37. this.userLinks = this.allLinks.filter(x => !x.rel.startsWith('.'));
  38. this.systemLinks = this.allLinks.filter(x => x.rel.startsWith('.'));
  39. this.fromTarget = this.allLinks.filter(x => x.rel === '.from').map(x => x.target)[0];
  40. this.toTarget = this.allLinks.filter(x => x.rel === '.to').map(x => x.target)[0];
  41. this.fromContactId = this.allLinks.filter(x => x.rel === '.from-contact-id').map(x => x.target)[0];
  42. this.toContactId = this.allLinks.filter(x => x.rel === '.to-contact-id').map(x => x.target)[0];
  43. this.media = this.allLinks.filter(x => x.rel === '.media').map(x => x.target)[0];
  44. if (this.fromTarget && this.toTarget && this.media) {
  45. this.repliable = true;
  46. } else {
  47. this.repliable = false;
  48. }
  49. }
  50. private formatCents(price: number) {
  51. if (price == null || price === 0) {
  52. return 'free';
  53. }
  54. let dollars = `${price / 100}`;
  55. if (dollars.match(/\..$/)) {
  56. dollars += '0';
  57. }
  58. return '$' + dollars;
  59. }
  60. }