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.

149 lines
5.1 KiB

  1. import {
  2. TIMELINE_REFRESH_REQUEST,
  3. TIMELINE_REFRESH_SUCCESS,
  4. TIMELINE_REFRESH_FAIL,
  5. TIMELINE_UPDATE,
  6. TIMELINE_DELETE,
  7. TIMELINE_EXPAND_SUCCESS,
  8. TIMELINE_EXPAND_REQUEST,
  9. TIMELINE_EXPAND_FAIL,
  10. TIMELINE_SCROLL_TOP,
  11. TIMELINE_CONNECT,
  12. TIMELINE_DISCONNECT,
  13. } from '../actions/timelines';
  14. import {
  15. ACCOUNT_BLOCK_SUCCESS,
  16. ACCOUNT_MUTE_SUCCESS,
  17. ACCOUNT_UNFOLLOW_SUCCESS,
  18. } from '../actions/accounts';
  19. import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
  20. const initialState = ImmutableMap();
  21. const initialTimeline = ImmutableMap({
  22. unread: 0,
  23. online: false,
  24. top: true,
  25. loaded: false,
  26. isLoading: false,
  27. next: false,
  28. items: ImmutableList(),
  29. });
  30. const normalizeTimeline = (state, timeline, statuses, next, isPartial) => {
  31. const oldIds = state.getIn([timeline, 'items'], ImmutableList());
  32. const ids = ImmutableList(statuses.map(status => status.get('id'))).filter(newId => !oldIds.includes(newId));
  33. const wasLoaded = state.getIn([timeline, 'loaded']);
  34. const hadNext = state.getIn([timeline, 'next']);
  35. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  36. mMap.set('loaded', true);
  37. mMap.set('isLoading', false);
  38. if (!hadNext) mMap.set('next', next);
  39. mMap.set('items', wasLoaded ? ids.concat(oldIds) : oldIds.concat(ids));
  40. mMap.set('isPartial', isPartial);
  41. }));
  42. };
  43. const appendNormalizedTimeline = (state, timeline, statuses, next) => {
  44. const oldIds = state.getIn([timeline, 'items'], ImmutableList());
  45. const ids = ImmutableList(statuses.map(status => status.get('id'))).filter(newId => !oldIds.includes(newId));
  46. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  47. mMap.set('isLoading', false);
  48. mMap.set('next', next);
  49. mMap.set('items', oldIds.concat(ids));
  50. }));
  51. };
  52. const updateTimeline = (state, timeline, status) => {
  53. const top = state.getIn([timeline, 'top']);
  54. const ids = state.getIn([timeline, 'items'], ImmutableList());
  55. const includesId = ids.includes(status.get('id'));
  56. const unread = state.getIn([timeline, 'unread'], 0);
  57. if (includesId) {
  58. return state;
  59. }
  60. let newIds = ids;
  61. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  62. if (!top) mMap.set('unread', unread + 1);
  63. if (top && ids.size > 40) newIds = newIds.take(20);
  64. mMap.set('items', newIds.unshift(status.get('id')));
  65. }));
  66. };
  67. const deleteStatus = (state, id, accountId, references) => {
  68. state.keySeq().forEach(timeline => {
  69. state = state.updateIn([timeline, 'items'], list => list.filterNot(item => item === id));
  70. });
  71. // Remove reblogs of deleted status
  72. references.forEach(ref => {
  73. state = deleteStatus(state, ref[0], ref[1], []);
  74. });
  75. return state;
  76. };
  77. const filterTimelines = (state, relationship, statuses) => {
  78. let references;
  79. statuses.forEach(status => {
  80. if (status.get('account') !== relationship.id) {
  81. return;
  82. }
  83. references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => [item.get('id'), item.get('account')]);
  84. state = deleteStatus(state, status.get('id'), status.get('account'), references);
  85. });
  86. return state;
  87. };
  88. const filterTimeline = (timeline, state, relationship, statuses) =>
  89. state.updateIn([timeline, 'items'], ImmutableList(), list =>
  90. list.filterNot(statusId =>
  91. statuses.getIn([statusId, 'account']) === relationship.id
  92. ));
  93. const updateTop = (state, timeline, top) => {
  94. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  95. if (top) mMap.set('unread', 0);
  96. mMap.set('top', top);
  97. }));
  98. };
  99. export default function timelines(state = initialState, action) {
  100. switch(action.type) {
  101. case TIMELINE_REFRESH_REQUEST:
  102. case TIMELINE_EXPAND_REQUEST:
  103. return state.update(action.timeline, initialTimeline, map => map.set('isLoading', true));
  104. case TIMELINE_REFRESH_FAIL:
  105. case TIMELINE_EXPAND_FAIL:
  106. return state.update(action.timeline, initialTimeline, map => map.set('isLoading', false));
  107. case TIMELINE_REFRESH_SUCCESS:
  108. return normalizeTimeline(state, action.timeline, fromJS(action.statuses), action.next, action.partial);
  109. case TIMELINE_EXPAND_SUCCESS:
  110. return appendNormalizedTimeline(state, action.timeline, fromJS(action.statuses), action.next);
  111. case TIMELINE_UPDATE:
  112. return updateTimeline(state, action.timeline, fromJS(action.status));
  113. case TIMELINE_DELETE:
  114. return deleteStatus(state, action.id, action.accountId, action.references, action.reblogOf);
  115. case ACCOUNT_BLOCK_SUCCESS:
  116. case ACCOUNT_MUTE_SUCCESS:
  117. return filterTimelines(state, action.relationship, action.statuses);
  118. case ACCOUNT_UNFOLLOW_SUCCESS:
  119. return filterTimeline('home', state, action.relationship, action.statuses);
  120. case TIMELINE_SCROLL_TOP:
  121. return updateTop(state, action.timeline, action.top);
  122. case TIMELINE_CONNECT:
  123. return state.update(action.timeline, initialTimeline, map => map.set('online', true));
  124. case TIMELINE_DISCONNECT:
  125. return state.update(action.timeline, initialTimeline, map => map.set('online', false));
  126. default:
  127. return state;
  128. }
  129. };