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.

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