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.

121 lines
3.3 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. return state.update(status.id, Immutable.Map(), map => map.mergeDeep(Immutable.fromJS(normalStatus)));
  48. };
  49. const normalizeStatuses = (state, statuses) => {
  50. statuses.forEach(status => {
  51. state = normalizeStatus(state, status);
  52. });
  53. return state;
  54. };
  55. const deleteStatus = (state, id, references) => {
  56. references.forEach(ref => {
  57. state = deleteStatus(state, ref[0], []);
  58. });
  59. return state.delete(id);
  60. };
  61. const filterStatuses = (state, relationship) => {
  62. state.forEach(status => {
  63. if (status.get('account') !== relationship.id) {
  64. return;
  65. }
  66. state = deleteStatus(state, status.get('id'), state.filter(item => item.get('reblog') === status.get('id')));
  67. });
  68. return state;
  69. };
  70. const initialState = Immutable.Map();
  71. export default function statuses(state = initialState, action) {
  72. switch(action.type) {
  73. case TIMELINE_UPDATE:
  74. case STATUS_FETCH_SUCCESS:
  75. case NOTIFICATIONS_UPDATE:
  76. return normalizeStatus(state, action.status);
  77. case REBLOG_SUCCESS:
  78. case UNREBLOG_SUCCESS:
  79. case FAVOURITE_SUCCESS:
  80. case UNFAVOURITE_SUCCESS:
  81. return normalizeStatus(state, action.response);
  82. case FAVOURITE_REQUEST:
  83. return state.setIn([action.status.get('id'), 'favourited'], true);
  84. case FAVOURITE_FAIL:
  85. return state.setIn([action.status.get('id'), 'favourited'], false);
  86. case REBLOG_REQUEST:
  87. return state.setIn([action.status.get('id'), 'reblogged'], true);
  88. case REBLOG_FAIL:
  89. return state.setIn([action.status.get('id'), 'reblogged'], false);
  90. case TIMELINE_REFRESH_SUCCESS:
  91. case TIMELINE_EXPAND_SUCCESS:
  92. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  93. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  94. case CONTEXT_FETCH_SUCCESS:
  95. case NOTIFICATIONS_REFRESH_SUCCESS:
  96. case NOTIFICATIONS_EXPAND_SUCCESS:
  97. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  98. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  99. case SEARCH_FETCH_SUCCESS:
  100. return normalizeStatuses(state, action.statuses);
  101. case TIMELINE_DELETE:
  102. return deleteStatus(state, action.id, action.references);
  103. case ACCOUNT_BLOCK_SUCCESS:
  104. return filterStatuses(state, action.relationship);
  105. default:
  106. return state;
  107. }
  108. };