statuses.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. STATUS_COLLAPSE,
  16. } from '../actions/statuses';
  17. import { TIMELINE_DELETE } from '../actions/timelines';
  18. import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
  19. import { Map as ImmutableMap, fromJS } from 'immutable';
  20. const importStatus = (state, status) => state.set(status.id, fromJS(status));
  21. const importStatuses = (state, statuses) =>
  22. state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
  23. const deleteStatus = (state, id, references) => {
  24. references.forEach(ref => {
  25. state = deleteStatus(state, ref, []);
  26. });
  27. return state.delete(id);
  28. };
  29. const initialState = ImmutableMap();
  30. export default function statuses(state = initialState, action) {
  31. switch(action.type) {
  32. case STATUS_IMPORT:
  33. return importStatus(state, action.status);
  34. case STATUSES_IMPORT:
  35. return importStatuses(state, action.statuses);
  36. case FAVOURITE_REQUEST:
  37. return state.setIn([action.status.get('id'), 'favourited'], true);
  38. case UNFAVOURITE_SUCCESS:
  39. return state.updateIn([action.status.get('id'), 'favourites_count'], x => Math.max(0, x - 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 STATUS_COLLAPSE:
  71. return state.setIn([action.id, 'collapsed'], action.isCollapsed);
  72. case TIMELINE_DELETE:
  73. return deleteStatus(state, action.id, action.references);
  74. default:
  75. return state;
  76. }
  77. };