timelines.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {
  2. TIMELINE_UPDATE,
  3. TIMELINE_DELETE,
  4. TIMELINE_CLEAR,
  5. TIMELINE_EXPAND_SUCCESS,
  6. TIMELINE_EXPAND_REQUEST,
  7. TIMELINE_EXPAND_FAIL,
  8. TIMELINE_SCROLL_TOP,
  9. TIMELINE_CONNECT,
  10. TIMELINE_DISCONNECT,
  11. TIMELINE_LOAD_PENDING,
  12. TIMELINE_MARK_AS_PARTIAL,
  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, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
  20. import compareId from '../compare_id';
  21. const initialState = ImmutableMap();
  22. const initialTimeline = ImmutableMap({
  23. unread: 0,
  24. online: false,
  25. top: true,
  26. isLoading: false,
  27. hasMore: true,
  28. pendingItems: ImmutableList(),
  29. items: ImmutableList(),
  30. });
  31. const expandNormalizedTimeline = (state, timeline, statuses, next, isPartial, isLoadingRecent, usePendingItems) => {
  32. // This method is pretty tricky because:
  33. // - existing items in the timeline might be out of order
  34. // - the existing timeline may have gaps, most often explicitly noted with a `null` item
  35. // - ideally, we don't want it to reorder existing items of the timeline
  36. // - `statuses` may include items that are already included in the timeline
  37. // - this function can be called either to fill in a gap, or load newer items
  38. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  39. mMap.set('isLoading', false);
  40. mMap.set('isPartial', isPartial);
  41. if (!next && !isLoadingRecent) mMap.set('hasMore', false);
  42. if (timeline.endsWith(':pinned')) {
  43. mMap.set('items', statuses.map(status => status.get('id')));
  44. } else if (!statuses.isEmpty()) {
  45. usePendingItems = isLoadingRecent && (usePendingItems || !mMap.get('pendingItems').isEmpty());
  46. mMap.update(usePendingItems ? 'pendingItems' : 'items', ImmutableList(), oldIds => {
  47. const newIds = statuses.map(status => status.get('id'));
  48. // Now this gets tricky, as we don't necessarily know for sure where the gap to fill is
  49. // and some items in the timeline may not be properly ordered.
  50. // However, we know that `newIds.last()` is the oldest item that was requested and that
  51. // there is no “hole” between `newIds.last()` and `newIds.first()`.
  52. // First, find the furthest (if properly sorted, oldest) item in the timeline that is
  53. // newer than the oldest fetched one, as it's most likely that it delimits the gap.
  54. // Start the gap *after* that item.
  55. const lastIndex = oldIds.findLastIndex(id => id !== null && compareId(id, newIds.last()) >= 0) + 1;
  56. // Then, try to find the furthest (if properly sorted, oldest) item in the timeline that
  57. // is newer than the most recent fetched one, as it delimits a section comprised of only
  58. // items older or within `newIds` (or that were deleted from the server, so should be removed
  59. // anyway).
  60. // Stop the gap *after* that item.
  61. const firstIndex = oldIds.take(lastIndex).findLastIndex(id => id !== null && compareId(id, newIds.first()) > 0) + 1;
  62. let insertedIds = ImmutableOrderedSet(newIds).withMutations(insertedIds => {
  63. // It is possible, though unlikely, that the slice we are replacing contains items older
  64. // than the elements we got from the API. Get them and add them back at the back of the
  65. // slice.
  66. const olderIds = oldIds.slice(firstIndex, lastIndex).filter(id => id !== null && compareId(id, newIds.last()) < 0);
  67. insertedIds.union(olderIds);
  68. // Make sure we aren't inserting duplicates
  69. insertedIds.subtract(oldIds.take(firstIndex), oldIds.skip(lastIndex));
  70. }).toList();
  71. // Finally, insert a gap marker if the data is marked as partial by the server
  72. if (isPartial && (firstIndex === 0 || oldIds.get(firstIndex - 1) !== null)) {
  73. insertedIds = insertedIds.unshift(null);
  74. }
  75. return oldIds.take(firstIndex).concat(
  76. insertedIds,
  77. oldIds.skip(lastIndex),
  78. );
  79. });
  80. }
  81. }));
  82. };
  83. const updateTimeline = (state, timeline, status, usePendingItems) => {
  84. const top = state.getIn([timeline, 'top']);
  85. if (usePendingItems || !state.getIn([timeline, 'pendingItems']).isEmpty()) {
  86. if (state.getIn([timeline, 'pendingItems'], ImmutableList()).includes(status.get('id')) || state.getIn([timeline, 'items'], ImmutableList()).includes(status.get('id'))) {
  87. return state;
  88. }
  89. return state.update(timeline, initialTimeline, map => map.update('pendingItems', list => list.unshift(status.get('id'))).update('unread', unread => unread + 1));
  90. }
  91. const ids = state.getIn([timeline, 'items'], ImmutableList());
  92. const includesId = ids.includes(status.get('id'));
  93. const unread = state.getIn([timeline, 'unread'], 0);
  94. if (includesId) {
  95. return state;
  96. }
  97. let newIds = ids;
  98. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  99. if (!top) mMap.set('unread', unread + 1);
  100. if (top && ids.size > 40) newIds = newIds.take(20);
  101. mMap.set('items', newIds.unshift(status.get('id')));
  102. }));
  103. };
  104. const deleteStatus = (state, id, references, exclude_account = null) => {
  105. state.keySeq().forEach(timeline => {
  106. if (exclude_account === null || (timeline !== `account:${exclude_account}` && !timeline.startsWith(`account:${exclude_account}:`))) {
  107. const helper = list => list.filterNot(item => item === id);
  108. state = state.updateIn([timeline, 'items'], helper).updateIn([timeline, 'pendingItems'], helper);
  109. }
  110. });
  111. // Remove reblogs of deleted status
  112. references.forEach(ref => {
  113. state = deleteStatus(state, ref, [], exclude_account);
  114. });
  115. return state;
  116. };
  117. const clearTimeline = (state, timeline) => {
  118. return state.set(timeline, initialTimeline);
  119. };
  120. const filterTimelines = (state, relationship, statuses) => {
  121. let references;
  122. statuses.forEach(status => {
  123. if (status.get('account') !== relationship.id) {
  124. return;
  125. }
  126. references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => item.get('id'));
  127. state = deleteStatus(state, status.get('id'), references, relationship.id);
  128. });
  129. return state;
  130. };
  131. const filterTimeline = (timeline, state, relationship, statuses) => {
  132. const helper = list => list.filterNot(statusId => statuses.getIn([statusId, 'account']) === relationship.id);
  133. return state.updateIn([timeline, 'items'], ImmutableList(), helper).updateIn([timeline, 'pendingItems'], ImmutableList(), helper);
  134. };
  135. const updateTop = (state, timeline, top) => {
  136. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  137. if (top) mMap.set('unread', mMap.get('pendingItems').size);
  138. mMap.set('top', top);
  139. }));
  140. };
  141. const reconnectTimeline = (state, usePendingItems) => {
  142. if (state.get('online')) {
  143. return state;
  144. }
  145. return state.withMutations(mMap => {
  146. mMap.update(usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items);
  147. mMap.set('online', true);
  148. });
  149. };
  150. export default function timelines(state = initialState, action) {
  151. switch(action.type) {
  152. case TIMELINE_LOAD_PENDING:
  153. return state.update(action.timeline, initialTimeline, map =>
  154. map.update('items', list => map.get('pendingItems').concat(list.take(40))).set('pendingItems', ImmutableList()).set('unread', 0));
  155. case TIMELINE_EXPAND_REQUEST:
  156. return state.update(action.timeline, initialTimeline, map => map.set('isLoading', true));
  157. case TIMELINE_EXPAND_FAIL:
  158. return state.update(action.timeline, initialTimeline, map => map.set('isLoading', false));
  159. case TIMELINE_EXPAND_SUCCESS:
  160. return expandNormalizedTimeline(state, action.timeline, fromJS(action.statuses), action.next, action.partial, action.isLoadingRecent, action.usePendingItems);
  161. case TIMELINE_UPDATE:
  162. return updateTimeline(state, action.timeline, fromJS(action.status), action.usePendingItems);
  163. case TIMELINE_DELETE:
  164. return deleteStatus(state, action.id, action.references, action.reblogOf);
  165. case TIMELINE_CLEAR:
  166. return clearTimeline(state, action.timeline);
  167. case ACCOUNT_BLOCK_SUCCESS:
  168. case ACCOUNT_MUTE_SUCCESS:
  169. return filterTimelines(state, action.relationship, action.statuses);
  170. case ACCOUNT_UNFOLLOW_SUCCESS:
  171. return filterTimeline('home', state, action.relationship, action.statuses);
  172. case TIMELINE_SCROLL_TOP:
  173. return updateTop(state, action.timeline, action.top);
  174. case TIMELINE_CONNECT:
  175. return state.update(action.timeline, initialTimeline, map => reconnectTimeline(map, action.usePendingItems));
  176. case TIMELINE_DISCONNECT:
  177. return state.update(
  178. action.timeline,
  179. initialTimeline,
  180. map => map.set('online', false).update(action.usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items),
  181. );
  182. case TIMELINE_MARK_AS_PARTIAL:
  183. return state.update(
  184. action.timeline,
  185. initialTimeline,
  186. map => map.set('isPartial', true).set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('unread', 0),
  187. );
  188. default:
  189. return state;
  190. }
  191. };