You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6 KiB

7 years ago
  1. import {
  2. REBLOG_REQUEST,
  3. REBLOG_SUCCESS,
  4. REBLOG_FAIL,
  5. UNREBLOG_SUCCESS,
  6. FAVOURITE_REQUEST,
  7. FAVOURITE_SUCCESS,
  8. FAVOURITE_FAIL,
  9. UNFAVOURITE_SUCCESS,
  10. BOOKMARK_REQUEST,
  11. BOOKMARK_SUCCESS,
  12. BOOKMARK_FAIL,
  13. UNBOOKMARK_SUCCESS,
  14. PIN_SUCCESS,
  15. UNPIN_SUCCESS,
  16. } from 'flavours/glitch/actions/interactions';
  17. import {
  18. STATUS_FETCH_SUCCESS,
  19. CONTEXT_FETCH_SUCCESS,
  20. STATUS_MUTE_SUCCESS,
  21. STATUS_UNMUTE_SUCCESS,
  22. } from 'flavours/glitch/actions/statuses';
  23. import {
  24. TIMELINE_REFRESH_SUCCESS,
  25. TIMELINE_UPDATE,
  26. TIMELINE_DELETE,
  27. TIMELINE_EXPAND_SUCCESS,
  28. } from 'flavours/glitch/actions/timelines';
  29. import {
  30. NOTIFICATIONS_UPDATE,
  31. NOTIFICATIONS_REFRESH_SUCCESS,
  32. NOTIFICATIONS_EXPAND_SUCCESS,
  33. } from 'flavours/glitch/actions/notifications';
  34. import {
  35. FAVOURITED_STATUSES_FETCH_SUCCESS,
  36. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  37. } from 'flavours/glitch/actions/favourites';
  38. import {
  39. BOOKMARKED_STATUSES_FETCH_SUCCESS,
  40. BOOKMARKED_STATUSES_EXPAND_SUCCESS,
  41. } from 'flavours/glitch/actions/bookmarks';
  42. import {
  43. PINNED_STATUSES_FETCH_SUCCESS,
  44. } from 'flavours/glitch/actions/pin_statuses';
  45. import { SEARCH_FETCH_SUCCESS } from 'flavours/glitch/actions/search';
  46. import emojify from 'flavours/glitch/util/emoji';
  47. import { Map as ImmutableMap, fromJS } from 'immutable';
  48. import escapeTextContentForBrowser from 'escape-html';
  49. const domParser = new DOMParser();
  50. const normalizeStatus = (state, status) => {
  51. if (!status) {
  52. return state;
  53. }
  54. const normalStatus = { ...status };
  55. normalStatus.account = status.account.id;
  56. if (status.reblog && status.reblog.id) {
  57. state = normalizeStatus(state, status.reblog);
  58. normalStatus.reblog = status.reblog.id;
  59. }
  60. // Only calculate these values when status first encountered
  61. // Otherwise keep the ones already in the reducer
  62. if (!state.has(status.id)) {
  63. const searchContent = [status.spoiler_text, status.content].join('\n\n').replace(/<br \/>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  64. const emojiMap = normalStatus.emojis.reduce((obj, emoji) => {
  65. obj[`:${emoji.shortcode}:`] = emoji;
  66. return obj;
  67. }, {});
  68. normalStatus.search_index = domParser.parseFromString(searchContent, 'text/html').documentElement.textContent;
  69. normalStatus.contentHtml = emojify(normalStatus.content, emojiMap);
  70. normalStatus.spoilerHtml = emojify(escapeTextContentForBrowser(normalStatus.spoiler_text || ''), emojiMap);
  71. }
  72. return state.update(status.id, ImmutableMap(), map => map.mergeDeep(fromJS(normalStatus)));
  73. };
  74. const normalizeStatuses = (state, statuses) => {
  75. statuses.forEach(status => {
  76. state = normalizeStatus(state, status);
  77. });
  78. return state;
  79. };
  80. const deleteStatus = (state, id, references) => {
  81. references.forEach(ref => {
  82. state = deleteStatus(state, ref[0], []);
  83. });
  84. return state.delete(id);
  85. };
  86. const initialState = ImmutableMap();
  87. export default function statuses(state = initialState, action) {
  88. switch(action.type) {
  89. case TIMELINE_UPDATE:
  90. case STATUS_FETCH_SUCCESS:
  91. case NOTIFICATIONS_UPDATE:
  92. return normalizeStatus(state, action.status);
  93. case REBLOG_SUCCESS:
  94. case UNREBLOG_SUCCESS:
  95. case FAVOURITE_SUCCESS:
  96. case UNFAVOURITE_SUCCESS:
  97. case BOOKMARK_SUCCESS:
  98. case UNBOOKMARK_SUCCESS:
  99. case PIN_SUCCESS:
  100. case UNPIN_SUCCESS:
  101. return normalizeStatus(state, action.response);
  102. case FAVOURITE_REQUEST:
  103. return state.setIn([action.status.get('id'), 'favourited'], true);
  104. case FAVOURITE_FAIL:
  105. return state.setIn([action.status.get('id'), 'favourited'], false);
  106. case BOOKMARK_REQUEST:
  107. return state.setIn([action.status.get('id'), 'bookmarked'], true);
  108. case BOOKMARK_FAIL:
  109. return state.setIn([action.status.get('id'), 'bookmarked'], false);
  110. case REBLOG_REQUEST:
  111. return state.setIn([action.status.get('id'), 'reblogged'], true);
  112. case REBLOG_FAIL:
  113. return state.setIn([action.status.get('id'), 'reblogged'], false);
  114. case STATUS_MUTE_SUCCESS:
  115. return state.setIn([action.id, 'muted'], true);
  116. case STATUS_UNMUTE_SUCCESS:
  117. return state.setIn([action.id, 'muted'], false);
  118. case TIMELINE_REFRESH_SUCCESS:
  119. case TIMELINE_EXPAND_SUCCESS:
  120. case CONTEXT_FETCH_SUCCESS:
  121. case NOTIFICATIONS_REFRESH_SUCCESS:
  122. case NOTIFICATIONS_EXPAND_SUCCESS:
  123. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  124. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  125. case BOOKMARKED_STATUSES_FETCH_SUCCESS:
  126. case BOOKMARKED_STATUSES_EXPAND_SUCCESS:
  127. case PINNED_STATUSES_FETCH_SUCCESS:
  128. case SEARCH_FETCH_SUCCESS:
  129. return normalizeStatuses(state, action.statuses);
  130. case TIMELINE_DELETE:
  131. return deleteStatus(state, action.id, action.references);
  132. default:
  133. return state;
  134. }
  135. };