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.

148 lines
4.4 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. PIN_SUCCESS,
  11. UNPIN_SUCCESS,
  12. } from 'flavours/glitch/actions/interactions';
  13. import {
  14. STATUS_FETCH_SUCCESS,
  15. CONTEXT_FETCH_SUCCESS,
  16. STATUS_MUTE_SUCCESS,
  17. STATUS_UNMUTE_SUCCESS,
  18. } from 'flavours/glitch/actions/statuses';
  19. import {
  20. TIMELINE_REFRESH_SUCCESS,
  21. TIMELINE_UPDATE,
  22. TIMELINE_DELETE,
  23. TIMELINE_EXPAND_SUCCESS,
  24. } from 'flavours/glitch/actions/timelines';
  25. import {
  26. ACCOUNT_BLOCK_SUCCESS,
  27. ACCOUNT_MUTE_SUCCESS,
  28. } from 'flavours/glitch/actions/accounts';
  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. PINNED_STATUSES_FETCH_SUCCESS,
  40. } from 'flavours/glitch/actions/pin_statuses';
  41. import { SEARCH_FETCH_SUCCESS } from 'flavours/glitch/actions/search';
  42. import emojify from 'flavours/glitch/util/emoji';
  43. import { Map as ImmutableMap, fromJS } from 'immutable';
  44. import escapeTextContentForBrowser from 'escape-html';
  45. const domParser = new DOMParser();
  46. const normalizeStatus = (state, status) => {
  47. if (!status) {
  48. return state;
  49. }
  50. const normalStatus = { ...status };
  51. normalStatus.account = status.account.id;
  52. if (status.reblog && status.reblog.id) {
  53. state = normalizeStatus(state, status.reblog);
  54. normalStatus.reblog = status.reblog.id;
  55. }
  56. const searchContent = [status.spoiler_text, status.content].join('\n\n').replace(/<br \/>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  57. const emojiMap = normalStatus.emojis.reduce((obj, emoji) => {
  58. obj[`:${emoji.shortcode}:`] = emoji;
  59. return obj;
  60. }, {});
  61. normalStatus.search_index = domParser.parseFromString(searchContent, 'text/html').documentElement.textContent;
  62. normalStatus.contentHtml = emojify(normalStatus.content, emojiMap);
  63. normalStatus.spoilerHtml = emojify(escapeTextContentForBrowser(normalStatus.spoiler_text || ''), emojiMap);
  64. return state.update(status.id, ImmutableMap(), map => map.mergeDeep(fromJS(normalStatus)));
  65. };
  66. const normalizeStatuses = (state, statuses) => {
  67. statuses.forEach(status => {
  68. state = normalizeStatus(state, status);
  69. });
  70. return state;
  71. };
  72. const deleteStatus = (state, id, references) => {
  73. references.forEach(ref => {
  74. state = deleteStatus(state, ref[0], []);
  75. });
  76. return state.delete(id);
  77. };
  78. const filterStatuses = (state, relationship) => {
  79. state.forEach(status => {
  80. if (status.get('account') !== relationship.id) {
  81. return;
  82. }
  83. state = deleteStatus(state, status.get('id'), state.filter(item => item.get('reblog') === status.get('id')));
  84. });
  85. return state;
  86. };
  87. const initialState = ImmutableMap();
  88. export default function statuses(state = initialState, action) {
  89. switch(action.type) {
  90. case TIMELINE_UPDATE:
  91. case STATUS_FETCH_SUCCESS:
  92. case NOTIFICATIONS_UPDATE:
  93. return normalizeStatus(state, action.status);
  94. case REBLOG_SUCCESS:
  95. case UNREBLOG_SUCCESS:
  96. case FAVOURITE_SUCCESS:
  97. case UNFAVOURITE_SUCCESS:
  98. case PIN_SUCCESS:
  99. case UNPIN_SUCCESS:
  100. return normalizeStatus(state, action.response);
  101. case FAVOURITE_REQUEST:
  102. return state.setIn([action.status.get('id'), 'favourited'], true);
  103. case FAVOURITE_FAIL:
  104. return state.setIn([action.status.get('id'), 'favourited'], false);
  105. case REBLOG_REQUEST:
  106. return state.setIn([action.status.get('id'), 'reblogged'], true);
  107. case REBLOG_FAIL:
  108. return state.setIn([action.status.get('id'), 'reblogged'], false);
  109. case STATUS_MUTE_SUCCESS:
  110. return state.setIn([action.id, 'muted'], true);
  111. case STATUS_UNMUTE_SUCCESS:
  112. return state.setIn([action.id, 'muted'], false);
  113. case TIMELINE_REFRESH_SUCCESS:
  114. case TIMELINE_EXPAND_SUCCESS:
  115. case CONTEXT_FETCH_SUCCESS:
  116. case NOTIFICATIONS_REFRESH_SUCCESS:
  117. case NOTIFICATIONS_EXPAND_SUCCESS:
  118. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  119. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  120. case PINNED_STATUSES_FETCH_SUCCESS:
  121. case SEARCH_FETCH_SUCCESS:
  122. return normalizeStatuses(state, action.statuses);
  123. case TIMELINE_DELETE:
  124. return deleteStatus(state, action.id, action.references);
  125. case ACCOUNT_BLOCK_SUCCESS:
  126. case ACCOUNT_MUTE_SUCCESS:
  127. return filterStatuses(state, action.relationship);
  128. default:
  129. return state;
  130. }
  131. };