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.

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