闭社主体 forked from https://github.com/tootsuite/mastodon
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.

71 lines
2.2 KiB

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