闭社主体 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.

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