The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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