闭社主体 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.

73 lines
1.9 KiB

  1. import {
  2. REBLOG_SUCCESS,
  3. UNREBLOG_SUCCESS,
  4. FAVOURITE_SUCCESS,
  5. UNFAVOURITE_SUCCESS
  6. } from '../actions/interactions';
  7. import {
  8. STATUS_FETCH_SUCCESS,
  9. CONTEXT_FETCH_SUCCESS
  10. } from '../actions/statuses';
  11. import {
  12. TIMELINE_REFRESH_SUCCESS,
  13. TIMELINE_UPDATE,
  14. TIMELINE_DELETE,
  15. TIMELINE_EXPAND_SUCCESS
  16. } from '../actions/timelines';
  17. import {
  18. ACCOUNT_TIMELINE_FETCH_SUCCESS,
  19. ACCOUNT_TIMELINE_EXPAND_SUCCESS
  20. } from '../actions/accounts';
  21. import Immutable from 'immutable';
  22. const normalizeStatus = (state, status) => {
  23. status = status.set('account', status.getIn(['account', 'id']));
  24. if (status.getIn(['reblog', 'id'])) {
  25. state = normalizeStatus(state, status.get('reblog'));
  26. status = status.set('reblog', status.getIn(['reblog', 'id']));
  27. }
  28. return state.set(status.get('id'), status);
  29. };
  30. const normalizeStatuses = (state, statuses) => {
  31. statuses.forEach(status => {
  32. state = normalizeStatus(state, status);
  33. });
  34. return state;
  35. };
  36. const deleteStatus = (state, id, references) => {
  37. references.forEach(ref => {
  38. state = deleteStatus(state, ref[0], []);
  39. });
  40. return state.delete(id);
  41. };
  42. const initialState = Immutable.Map();
  43. export default function statuses(state = initialState, action) {
  44. switch(action.type) {
  45. case TIMELINE_UPDATE:
  46. case STATUS_FETCH_SUCCESS:
  47. return normalizeStatus(state, Immutable.fromJS(action.status));
  48. case REBLOG_SUCCESS:
  49. case UNREBLOG_SUCCESS:
  50. case FAVOURITE_SUCCESS:
  51. case UNFAVOURITE_SUCCESS:
  52. return normalizeStatus(state, Immutable.fromJS(action.response));
  53. case TIMELINE_REFRESH_SUCCESS:
  54. case TIMELINE_EXPAND_SUCCESS:
  55. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  56. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  57. case CONTEXT_FETCH_SUCCESS:
  58. return normalizeStatuses(state, Immutable.fromJS(action.statuses));
  59. case TIMELINE_DELETE:
  60. return deleteStatus(state, action.id, action.references);
  61. default:
  62. return state;
  63. }
  64. };