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.

59 lines
1.7 KiB

  1. import { TIMELINE_SET, TIMELINE_UPDATE } from '../actions/timelines';
  2. import { REBLOG_SUCCESS, FAVOURITE_SUCCESS } from '../actions/interactions';
  3. import Immutable from 'immutable';
  4. const initialState = Immutable.Map({
  5. home: Immutable.List(),
  6. mentions: Immutable.List(),
  7. statuses: Immutable.Map(),
  8. accounts: Immutable.Map()
  9. });
  10. function statusToMaps(state, status) {
  11. // Separate account
  12. let account = status.get('account');
  13. status = status.set('account', account.get('id'));
  14. // Separate reblog, repeat for reblog
  15. let reblog = status.get('reblog');
  16. if (reblog !== null) {
  17. status = status.set('reblog', reblog.get('id'));
  18. state = statusToMaps(state, reblog);
  19. }
  20. return state.withMutations(map => {
  21. map.setIn(['accounts', account.get('id')], account);
  22. map.setIn(['statuses', status.get('id')], status);
  23. });
  24. };
  25. function timelineToMaps(state, timeline, statuses) {
  26. statuses.forEach((status, i) => {
  27. state = statusToMaps(state, status);
  28. state = state.setIn([timeline, i], status.get('id'));
  29. });
  30. return state;
  31. };
  32. function updateTimelineWithMaps(state, timeline, status) {
  33. state = statusToMaps(state, status);
  34. state = state.update(timeline, list => list.unshift(status.get('id')));
  35. return state;
  36. };
  37. export default function timelines(state = initialState, action) {
  38. switch(action.type) {
  39. case TIMELINE_SET:
  40. return timelineToMaps(state, action.timeline, Immutable.fromJS(action.statuses));
  41. case TIMELINE_UPDATE:
  42. return updateTimelineWithMaps(state, action.timeline,Immutable.fromJS(action.status));
  43. case REBLOG_SUCCESS:
  44. case FAVOURITE_SUCCESS:
  45. return statusToMaps(state, Immutable.fromJS(action.response));
  46. default:
  47. return state;
  48. }
  49. }