logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

timelines.js (4637B)


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