闭社主体 forked from https://github.com/tootsuite/mastodon
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.

158 lines
4.5 KiB

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