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
5.2 KiB

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