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.

75 lines
2.4 KiB

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