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.

148 lines
4.7 KiB

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