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.

118 lines
3.1 KiB

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