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

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