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.

181 lines
6.7 KiB

  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, 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. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  33. mMap.set('isLoading', false);
  34. mMap.set('isPartial', isPartial);
  35. if (!next && !isLoadingRecent) mMap.set('hasMore', false);
  36. if (timeline.endsWith(':pinned')) {
  37. mMap.set('items', statuses.map(status => status.get('id')));
  38. } else if (!statuses.isEmpty()) {
  39. usePendingItems = isLoadingRecent && (usePendingItems || !mMap.get('pendingItems').isEmpty());
  40. mMap.update(usePendingItems ? 'pendingItems' : 'items', ImmutableList(), oldIds => {
  41. const newIds = statuses.map(status => status.get('id'));
  42. const lastIndex = oldIds.findLastIndex(id => id !== null && compareId(id, newIds.last()) >= 0) + 1;
  43. const firstIndex = oldIds.take(lastIndex).findLastIndex(id => id !== null && compareId(id, newIds.first()) > 0);
  44. if (firstIndex < 0) {
  45. return (isPartial ? newIds.unshift(null) : newIds).concat(oldIds.skip(lastIndex));
  46. }
  47. return oldIds.take(firstIndex + 1).concat(
  48. isPartial && oldIds.get(firstIndex) !== null ? newIds.unshift(null) : newIds,
  49. oldIds.skip(lastIndex),
  50. );
  51. });
  52. }
  53. }));
  54. };
  55. const updateTimeline = (state, timeline, status, usePendingItems) => {
  56. const top = state.getIn([timeline, 'top']);
  57. if (usePendingItems || !state.getIn([timeline, 'pendingItems']).isEmpty()) {
  58. if (state.getIn([timeline, 'pendingItems'], ImmutableList()).includes(status.get('id')) || state.getIn([timeline, 'items'], ImmutableList()).includes(status.get('id'))) {
  59. return state;
  60. }
  61. return state.update(timeline, initialTimeline, map => map.update('pendingItems', list => list.unshift(status.get('id'))).update('unread', unread => unread + 1));
  62. }
  63. const ids = state.getIn([timeline, 'items'], ImmutableList());
  64. const includesId = ids.includes(status.get('id'));
  65. const unread = state.getIn([timeline, 'unread'], 0);
  66. if (includesId) {
  67. return state;
  68. }
  69. let newIds = ids;
  70. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  71. if (!top) mMap.set('unread', unread + 1);
  72. if (top && ids.size > 40) newIds = newIds.take(20);
  73. mMap.set('items', newIds.unshift(status.get('id')));
  74. }));
  75. };
  76. const deleteStatus = (state, id, references, exclude_account = null) => {
  77. state.keySeq().forEach(timeline => {
  78. if (exclude_account === null || (timeline !== `account:${exclude_account}` && !timeline.startsWith(`account:${exclude_account}:`))) {
  79. const helper = list => list.filterNot(item => item === id);
  80. state = state.updateIn([timeline, 'items'], helper).updateIn([timeline, 'pendingItems'], helper);
  81. }
  82. });
  83. // Remove reblogs of deleted status
  84. references.forEach(ref => {
  85. state = deleteStatus(state, ref, [], exclude_account);
  86. });
  87. return state;
  88. };
  89. const clearTimeline = (state, timeline) => {
  90. return state.set(timeline, initialTimeline);
  91. };
  92. const filterTimelines = (state, relationship, statuses) => {
  93. let references;
  94. statuses.forEach(status => {
  95. if (status.get('account') !== relationship.id) {
  96. return;
  97. }
  98. references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => item.get('id'));
  99. state = deleteStatus(state, status.get('id'), references, relationship.id);
  100. });
  101. return state;
  102. };
  103. const filterTimeline = (timeline, state, relationship, statuses) => {
  104. const helper = list => list.filterNot(statusId => statuses.getIn([statusId, 'account']) === relationship.id);
  105. return state.updateIn([timeline, 'items'], ImmutableList(), helper).updateIn([timeline, 'pendingItems'], ImmutableList(), helper);
  106. };
  107. const updateTop = (state, timeline, top) => {
  108. return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
  109. if (top) mMap.set('unread', mMap.get('pendingItems').size);
  110. mMap.set('top', top);
  111. }));
  112. };
  113. export default function timelines(state = initialState, action) {
  114. switch(action.type) {
  115. case TIMELINE_LOAD_PENDING:
  116. return state.update(action.timeline, initialTimeline, map =>
  117. map.update('items', list => map.get('pendingItems').concat(list.take(40))).set('pendingItems', ImmutableList()).set('unread', 0));
  118. case TIMELINE_EXPAND_REQUEST:
  119. return state.update(action.timeline, initialTimeline, map => map.set('isLoading', true));
  120. case TIMELINE_EXPAND_FAIL:
  121. return state.update(action.timeline, initialTimeline, map => map.set('isLoading', false));
  122. case TIMELINE_EXPAND_SUCCESS:
  123. return expandNormalizedTimeline(state, action.timeline, fromJS(action.statuses), action.next, action.partial, action.isLoadingRecent, action.usePendingItems);
  124. case TIMELINE_UPDATE:
  125. return updateTimeline(state, action.timeline, fromJS(action.status), action.usePendingItems);
  126. case TIMELINE_DELETE:
  127. return deleteStatus(state, action.id, action.references, action.reblogOf);
  128. case TIMELINE_CLEAR:
  129. return clearTimeline(state, action.timeline);
  130. case ACCOUNT_BLOCK_SUCCESS:
  131. case ACCOUNT_MUTE_SUCCESS:
  132. return filterTimelines(state, action.relationship, action.statuses);
  133. case ACCOUNT_UNFOLLOW_SUCCESS:
  134. return filterTimeline('home', state, action.relationship, action.statuses);
  135. case TIMELINE_SCROLL_TOP:
  136. return updateTop(state, action.timeline, action.top);
  137. case TIMELINE_CONNECT:
  138. return state.update(action.timeline, initialTimeline, map => map.set('online', true));
  139. case TIMELINE_DISCONNECT:
  140. return state.update(
  141. action.timeline,
  142. initialTimeline,
  143. map => map.set('online', false).update(action.usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items),
  144. );
  145. case TIMELINE_MARK_AS_PARTIAL:
  146. return state.update(
  147. action.timeline,
  148. initialTimeline,
  149. map => map.set('isPartial', true).set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('unread', 0),
  150. );
  151. default:
  152. return state;
  153. }
  154. };