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.

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