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.

69 lines
2.1 KiB

  1. import { TIMELINE_SET, TIMELINE_UPDATE, TIMELINE_DELETE } 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. function deleteStatus(state, id) {
  38. ['home', 'mentions'].forEach(function (timeline) {
  39. state = state.update(timeline, list => list.filterNot(item => item === id));
  40. });
  41. return state.deleteIn(['statuses', id]);
  42. };
  43. export default function timelines(state = initialState, action) {
  44. switch(action.type) {
  45. case TIMELINE_SET:
  46. return timelineToMaps(state, action.timeline, Immutable.fromJS(action.statuses));
  47. case TIMELINE_UPDATE:
  48. return updateTimelineWithMaps(state, action.timeline, Immutable.fromJS(action.status));
  49. case TIMELINE_DELETE:
  50. return deleteStatus(state, action.id);
  51. case REBLOG_SUCCESS:
  52. case FAVOURITE_SUCCESS:
  53. return statusToMaps(state, Immutable.fromJS(action.response));
  54. default:
  55. return state;
  56. }
  57. }