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.

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