content-item.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 hashInPlaylist: string;
  7. public readonly type: string;
  8. public readonly date: Date;
  9. public readonly myTagsWithoutAll: string[];
  10. public readonly allLinks: ContentLink[];
  11. public readonly userLinks: ContentLink[];
  12. public readonly systemLinks: ContentLink[];
  13. public readonly repliable: boolean;
  14. public readonly creator?: string;
  15. public readonly title?: string;
  16. public readonly text?: string;
  17. public readonly file?: string;
  18. public readonly thumb?: string;
  19. public readonly price?: number;
  20. public readonly priceFormatted?: string;
  21. public readonly fromTarget?: string;
  22. public readonly toTarget?: string;
  23. public readonly fromContactId?: string;
  24. public readonly toContactId?: string;
  25. public readonly media?: string;
  26. public readonly relTarget: any;
  27. constructor(hashInPlaylist: string, hash: string, props: ContentParams) {
  28. this.data = props;
  29. this.hash = hash;
  30. this.hashInPlaylist = hashInPlaylist;
  31. this.type = props.type;
  32. this.title = props.title;
  33. this.text = props.text;
  34. this.creator = props.creator;
  35. this.file = props.file;
  36. this.thumb = props.thumb;
  37. if (props.date) {
  38. this.date = new Date(props.date);
  39. } else {
  40. this.date = new Date();
  41. }
  42. if (props.myTags) {
  43. this.myTagsWithoutAll = props.myTags.filter(x => x !== 'all');
  44. } else {
  45. this.myTagsWithoutAll = [];
  46. }
  47. if (props.price != null && !isNaN(Number(props.price))) {
  48. this.price = Number(props.price);
  49. this.priceFormatted = this.formatCents(this.price);
  50. }
  51. this.allLinks = props.links || [];
  52. this.userLinks = this.allLinks.filter(x => !x.rel.startsWith('.'));
  53. this.systemLinks = this.allLinks.filter(x => x.rel.startsWith('.'));
  54. this.fromTarget = this.allLinks.filter(x => x.rel === '.from').map(x => x.target)[0];
  55. this.toTarget = this.allLinks.filter(x => x.rel === '.to').map(x => x.target)[0];
  56. this.fromContactId = this.allLinks.filter(x => x.rel === '.from-contact-id').map(x => x.target)[0];
  57. this.toContactId = this.allLinks.filter(x => x.rel === '.to-contact-id').map(x => x.target)[0];
  58. this.media = this.allLinks.filter(x => x.rel === '.media').map(x => x.target)[0];
  59. this.relTarget = this.allLinks.reduce((accum, link) => {
  60. accum[link.rel] = link.target;
  61. return accum;
  62. }, {});
  63. if (this.fromTarget && this.toTarget && this.media) {
  64. this.repliable = true;
  65. } else {
  66. this.repliable = false;
  67. }
  68. }
  69. private formatCents(price: number) {
  70. if (price == null || price === 0) {
  71. return 'free';
  72. }
  73. let dollars = `${price / 100}`;
  74. if (dollars.match(/\..$/)) {
  75. dollars += '0';
  76. }
  77. return '$' + dollars;
  78. }
  79. }