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.

33 lines
1.1 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. function updateMatchingStatuses(state, needle, callback) {
  6. return state.map(function (list) {
  7. return list.map(function (status) {
  8. if (status.get('id') === needle.get('id')) {
  9. return callback(status);
  10. } else if (status.getIn(['reblog', 'id'], null) === needle.get('id')) {
  11. return status.set('reblog', callback(status.get('reblog')));
  12. }
  13. return status;
  14. });
  15. });
  16. };
  17. export default function timelines(state = initialState, action) {
  18. switch(action.type) {
  19. case TIMELINE_SET:
  20. return state.set(action.timeline, Immutable.fromJS(action.statuses));
  21. case TIMELINE_UPDATE:
  22. return state.update(action.timeline, list => list.unshift(Immutable.fromJS(action.status)));
  23. case REBLOG_SUCCESS:
  24. case FAVOURITE_SUCCESS:
  25. return updateMatchingStatuses(state, action.status, () => Immutable.fromJS(action.response));
  26. default:
  27. return state;
  28. }
  29. }