normalizer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import escapeTextContentForBrowser from 'escape-html';
  2. import emojify from '../../features/emoji/emoji';
  3. import { expandSpoilers } from '../../initial_state';
  4. const domParser = new DOMParser();
  5. const makeEmojiMap = emojis => emojis.reduce((obj, emoji) => {
  6. obj[`:${emoji.shortcode}:`] = emoji;
  7. return obj;
  8. }, {});
  9. export function searchTextFromRawStatus (status) {
  10. const spoilerText = status.spoiler_text || '';
  11. const searchContent = ([spoilerText, status.content].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).concat(status.media_attachments.map(att => att.description)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  12. return domParser.parseFromString(searchContent, 'text/html').documentElement.textContent;
  13. }
  14. export function normalizeFilterResult(result) {
  15. const normalResult = { ...result };
  16. normalResult.filter = normalResult.filter.id;
  17. return normalResult;
  18. }
  19. export function normalizeStatus(status, normalOldStatus) {
  20. const normalStatus = { ...status };
  21. normalStatus.account = status.account.id;
  22. if (status.reblog && status.reblog.id) {
  23. normalStatus.reblog = status.reblog.id;
  24. }
  25. if (status.poll && status.poll.id) {
  26. normalStatus.poll = status.poll.id;
  27. }
  28. if (status.filtered) {
  29. normalStatus.filtered = status.filtered.map(normalizeFilterResult);
  30. }
  31. // Only calculate these values when status first encountered and
  32. // when the underlying values change. Otherwise keep the ones
  33. // already in the reducer
  34. if (normalOldStatus && normalOldStatus.get('content') === normalStatus.content && normalOldStatus.get('spoiler_text') === normalStatus.spoiler_text) {
  35. normalStatus.search_index = normalOldStatus.get('search_index');
  36. normalStatus.contentHtml = normalOldStatus.get('contentHtml');
  37. normalStatus.spoilerHtml = normalOldStatus.get('spoilerHtml');
  38. normalStatus.spoiler_text = normalOldStatus.get('spoiler_text');
  39. normalStatus.hidden = normalOldStatus.get('hidden');
  40. if (normalOldStatus.get('translation')) {
  41. normalStatus.translation = normalOldStatus.get('translation');
  42. }
  43. } else {
  44. // If the status has a CW but no contents, treat the CW as if it were the
  45. // status' contents, to avoid having a CW toggle with seemingly no effect.
  46. if (normalStatus.spoiler_text && !normalStatus.content) {
  47. normalStatus.content = normalStatus.spoiler_text;
  48. normalStatus.spoiler_text = '';
  49. }
  50. const spoilerText = normalStatus.spoiler_text || '';
  51. const searchContent = ([spoilerText, status.content].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).concat(status.media_attachments.map(att => att.description)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  52. const emojiMap = makeEmojiMap(normalStatus.emojis);
  53. normalStatus.search_index = domParser.parseFromString(searchContent, 'text/html').documentElement.textContent;
  54. normalStatus.contentHtml = emojify(normalStatus.content, emojiMap);
  55. normalStatus.spoilerHtml = emojify(escapeTextContentForBrowser(spoilerText), emojiMap);
  56. normalStatus.hidden = expandSpoilers ? false : spoilerText.length > 0 || normalStatus.sensitive;
  57. }
  58. if (normalOldStatus) {
  59. const list = normalOldStatus.get('media_attachments');
  60. if (normalStatus.media_attachments && list) {
  61. normalStatus.media_attachments.forEach(item => {
  62. const oldItem = list.find(i => i.get('id') === item.id);
  63. if (oldItem && oldItem.get('description') === item.description) {
  64. item.translation = oldItem.get('translation');
  65. }
  66. });
  67. }
  68. }
  69. return normalStatus;
  70. }
  71. export function normalizeStatusTranslation(translation, status) {
  72. const emojiMap = makeEmojiMap(status.get('emojis').toJS());
  73. const normalTranslation = {
  74. detected_source_language: translation.detected_source_language,
  75. language: translation.language,
  76. provider: translation.provider,
  77. contentHtml: emojify(translation.content, emojiMap),
  78. spoilerHtml: emojify(escapeTextContentForBrowser(translation.spoiler_text), emojiMap),
  79. spoiler_text: translation.spoiler_text,
  80. };
  81. return normalTranslation;
  82. }
  83. export function normalizePoll(poll, normalOldPoll) {
  84. const normalPoll = { ...poll };
  85. const emojiMap = makeEmojiMap(poll.emojis);
  86. normalPoll.options = poll.options.map((option, index) => {
  87. const normalOption = {
  88. ...option,
  89. voted: poll.own_votes && poll.own_votes.includes(index),
  90. titleHtml: emojify(escapeTextContentForBrowser(option.title), emojiMap),
  91. };
  92. if (normalOldPoll && normalOldPoll.getIn(['options', index, 'title']) === option.title) {
  93. normalOption.translation = normalOldPoll.getIn(['options', index, 'translation']);
  94. }
  95. return normalOption;
  96. });
  97. return normalPoll;
  98. }
  99. export function normalizePollOptionTranslation(translation, poll) {
  100. const emojiMap = makeEmojiMap(poll.get('emojis').toJS());
  101. const normalTranslation = {
  102. ...translation,
  103. titleHtml: emojify(escapeTextContentForBrowser(translation.title), emojiMap),
  104. };
  105. return normalTranslation;
  106. }
  107. export function normalizeAnnouncement(announcement) {
  108. const normalAnnouncement = { ...announcement };
  109. const emojiMap = makeEmojiMap(normalAnnouncement.emojis);
  110. normalAnnouncement.contentHtml = emojify(normalAnnouncement.content, emojiMap);
  111. return normalAnnouncement;
  112. }