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

124 lines
3.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. } from '../actions/interactions';
  11. import {
  12. STATUS_FETCH_SUCCESS,
  13. CONTEXT_FETCH_SUCCESS
  14. } from '../actions/statuses';
  15. import {
  16. TIMELINE_REFRESH_SUCCESS,
  17. TIMELINE_UPDATE,
  18. TIMELINE_DELETE,
  19. TIMELINE_EXPAND_SUCCESS
  20. } from '../actions/timelines';
  21. import {
  22. ACCOUNT_TIMELINE_FETCH_SUCCESS,
  23. ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  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 linebreakComplemented = status.content.replace(/<br \/>/g, '\n').replace(/<\/p><p>/g, '\n\n');
  48. normalStatus.unescaped_content = new DOMParser().parseFromString(linebreakComplemented, '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 TIMELINE_REFRESH_SUCCESS:
  93. case TIMELINE_EXPAND_SUCCESS:
  94. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  95. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  96. case CONTEXT_FETCH_SUCCESS:
  97. case NOTIFICATIONS_REFRESH_SUCCESS:
  98. case NOTIFICATIONS_EXPAND_SUCCESS:
  99. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  100. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  101. case SEARCH_FETCH_SUCCESS:
  102. return normalizeStatuses(state, action.statuses);
  103. case TIMELINE_DELETE:
  104. return deleteStatus(state, action.id, action.references);
  105. case ACCOUNT_BLOCK_SUCCESS:
  106. return filterStatuses(state, action.relationship);
  107. default:
  108. return state;
  109. }
  110. };