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.

81 lines
2.8 KiB

  1. import {
  2. REBLOG_REQUEST,
  3. REBLOG_FAIL,
  4. FAVOURITE_REQUEST,
  5. FAVOURITE_FAIL,
  6. UNFAVOURITE_SUCCESS,
  7. BOOKMARK_REQUEST,
  8. BOOKMARK_FAIL,
  9. } from '../actions/interactions';
  10. import {
  11. STATUS_MUTE_SUCCESS,
  12. STATUS_UNMUTE_SUCCESS,
  13. STATUS_REVEAL,
  14. STATUS_HIDE,
  15. } from '../actions/statuses';
  16. import { TIMELINE_DELETE } from '../actions/timelines';
  17. import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
  18. import { Map as ImmutableMap, fromJS } from 'immutable';
  19. const importStatus = (state, status) => state.set(status.id, fromJS(status));
  20. const importStatuses = (state, statuses) =>
  21. state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
  22. const deleteStatus = (state, id, references) => {
  23. references.forEach(ref => {
  24. state = deleteStatus(state, ref[0], []);
  25. });
  26. return state.delete(id);
  27. };
  28. const initialState = ImmutableMap();
  29. export default function statuses(state = initialState, action) {
  30. switch(action.type) {
  31. case STATUS_IMPORT:
  32. return importStatus(state, action.status);
  33. case STATUSES_IMPORT:
  34. return importStatuses(state, action.statuses);
  35. case FAVOURITE_REQUEST:
  36. return state.setIn([action.status.get('id'), 'favourited'], true);
  37. case UNFAVOURITE_SUCCESS:
  38. const favouritesCount = action.status.get('favourites_count');
  39. return state.setIn([action.status.get('id'), 'favourites_count'], favouritesCount - 1);
  40. case FAVOURITE_FAIL:
  41. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false);
  42. case BOOKMARK_REQUEST:
  43. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], true);
  44. case BOOKMARK_FAIL:
  45. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], false);
  46. case REBLOG_REQUEST:
  47. return state.setIn([action.status.get('id'), 'reblogged'], true);
  48. case REBLOG_FAIL:
  49. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], false);
  50. case STATUS_MUTE_SUCCESS:
  51. return state.setIn([action.id, 'muted'], true);
  52. case STATUS_UNMUTE_SUCCESS:
  53. return state.setIn([action.id, 'muted'], false);
  54. case STATUS_REVEAL:
  55. return state.withMutations(map => {
  56. action.ids.forEach(id => {
  57. if (!(state.get(id) === undefined)) {
  58. map.setIn([id, 'hidden'], false);
  59. }
  60. });
  61. });
  62. case STATUS_HIDE:
  63. return state.withMutations(map => {
  64. action.ids.forEach(id => {
  65. if (!(state.get(id) === undefined)) {
  66. map.setIn([id, 'hidden'], true);
  67. }
  68. });
  69. });
  70. case TIMELINE_DELETE:
  71. return deleteStatus(state, action.id, action.references);
  72. default:
  73. return state;
  74. }
  75. };