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

126 lines
3.6 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_BLOCK_SUCCESS,
  25. } from '../actions/accounts';
  26. import {
  27. NOTIFICATIONS_UPDATE,
  28. NOTIFICATIONS_REFRESH_SUCCESS,
  29. NOTIFICATIONS_EXPAND_SUCCESS,
  30. } from '../actions/notifications';
  31. import {
  32. FAVOURITED_STATUSES_FETCH_SUCCESS,
  33. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  34. } from '../actions/favourites';
  35. import { SEARCH_FETCH_SUCCESS } from '../actions/search';
  36. import Immutable from 'immutable';
  37. const normalizeStatus = (state, status) => {
  38. if (!status) {
  39. return state;
  40. }
  41. const normalStatus = { ...status };
  42. normalStatus.account = status.account.id;
  43. if (status.reblog && status.reblog.id) {
  44. state = normalizeStatus(state, status.reblog);
  45. normalStatus.reblog = status.reblog.id;
  46. }
  47. const searchContent = [status.spoiler_text, status.content].join(' ').replace(/<br \/>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  48. normalStatus.search_index = new DOMParser().parseFromString(searchContent, 'text/html').documentElement.textContent;
  49. return state.update(status.id, Immutable.Map(), map => map.mergeDeep(Immutable.fromJS(normalStatus)));
  50. };
  51. const normalizeStatuses = (state, statuses) => {
  52. statuses.forEach(status => {
  53. state = normalizeStatus(state, status);
  54. });
  55. return state;
  56. };
  57. const deleteStatus = (state, id, references) => {
  58. references.forEach(ref => {
  59. state = deleteStatus(state, ref[0], []);
  60. });
  61. return state.delete(id);
  62. };
  63. const filterStatuses = (state, relationship) => {
  64. state.forEach(status => {
  65. if (status.get('account') !== relationship.id) {
  66. return;
  67. }
  68. state = deleteStatus(state, status.get('id'), state.filter(item => item.get('reblog') === status.get('id')));
  69. });
  70. return state;
  71. };
  72. const initialState = Immutable.Map();
  73. export default function statuses(state = initialState, action) {
  74. switch(action.type) {
  75. case TIMELINE_UPDATE:
  76. case STATUS_FETCH_SUCCESS:
  77. case NOTIFICATIONS_UPDATE:
  78. return normalizeStatus(state, action.status);
  79. case REBLOG_SUCCESS:
  80. case UNREBLOG_SUCCESS:
  81. case FAVOURITE_SUCCESS:
  82. case UNFAVOURITE_SUCCESS:
  83. return normalizeStatus(state, action.response);
  84. case FAVOURITE_REQUEST:
  85. return state.setIn([action.status.get('id'), 'favourited'], true);
  86. case FAVOURITE_FAIL:
  87. return state.setIn([action.status.get('id'), 'favourited'], false);
  88. case REBLOG_REQUEST:
  89. return state.setIn([action.status.get('id'), 'reblogged'], true);
  90. case REBLOG_FAIL:
  91. return state.setIn([action.status.get('id'), 'reblogged'], false);
  92. case STATUS_MUTE_SUCCESS:
  93. return state.setIn([action.id, 'muted'], true);
  94. case STATUS_UNMUTE_SUCCESS:
  95. return state.setIn([action.id, 'muted'], false);
  96. case TIMELINE_REFRESH_SUCCESS:
  97. case TIMELINE_EXPAND_SUCCESS:
  98. case CONTEXT_FETCH_SUCCESS:
  99. case NOTIFICATIONS_REFRESH_SUCCESS:
  100. case NOTIFICATIONS_EXPAND_SUCCESS:
  101. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  102. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  103. case SEARCH_FETCH_SUCCESS:
  104. return normalizeStatuses(state, action.statuses);
  105. case TIMELINE_DELETE:
  106. return deleteStatus(state, action.id, action.references);
  107. case ACCOUNT_BLOCK_SUCCESS:
  108. return filterStatuses(state, action.relationship);
  109. default:
  110. return state;
  111. }
  112. };