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.

168 lines
5.3 KiB

  1. import {
  2. TIMELINE_SET,
  3. TIMELINE_UPDATE,
  4. TIMELINE_DELETE
  5. } from '../actions/timelines';
  6. import {
  7. REBLOG_SUCCESS,
  8. FAVOURITE_SUCCESS
  9. } from '../actions/interactions';
  10. import {
  11. ACCOUNT_SET_SELF,
  12. ACCOUNT_FETCH_SUCCESS,
  13. ACCOUNT_FOLLOW_SUCCESS,
  14. ACCOUNT_UNFOLLOW_SUCCESS,
  15. ACCOUNT_TIMELINE_FETCH_SUCCESS
  16. } from '../actions/accounts';
  17. import { STATUS_FETCH_SUCCESS } from '../actions/statuses';
  18. import { FOLLOW_SUBMIT_SUCCESS } from '../actions/follow';
  19. import Immutable from 'immutable';
  20. const initialState = Immutable.Map({
  21. home: Immutable.List([]),
  22. mentions: Immutable.List([]),
  23. statuses: Immutable.Map(),
  24. accounts: Immutable.Map(),
  25. accounts_timelines: Immutable.Map(),
  26. me: null,
  27. ancestors: Immutable.Map(),
  28. descendants: Immutable.Map()
  29. });
  30. export function selectStatus(state, id) {
  31. let status = state.getIn(['timelines', 'statuses', id], null);
  32. if (status === null) {
  33. return null;
  34. }
  35. status = status.set('account', state.getIn(['timelines', 'accounts', status.get('account')]));
  36. if (status.get('reblog') !== null) {
  37. status = status.set('reblog', selectStatus(state, status.get('reblog')));
  38. }
  39. return status;
  40. };
  41. function normalizeStatus(state, status) {
  42. // Separate account
  43. let account = status.get('account');
  44. status = status.set('account', account.get('id'));
  45. // Separate reblog, repeat for reblog
  46. let reblog = status.get('reblog');
  47. if (reblog !== null) {
  48. status = status.set('reblog', reblog.get('id'));
  49. state = normalizeStatus(state, reblog);
  50. }
  51. // Replies
  52. if (status.get('in_reply_to_id')) {
  53. state = state.updateIn(['descendants', status.get('in_reply_to_id')], set => {
  54. if (!Immutable.OrderedSet.isOrderedSet(set)) {
  55. return Immutable.OrderedSet([status.get('id')]);
  56. } else {
  57. return set.add(status.get('id'));
  58. }
  59. });
  60. }
  61. return state.withMutations(map => {
  62. if (status.get('in_reply_to_id')) {
  63. map.updateIn(['descendants', status.get('in_reply_to_id')], Immutable.OrderedSet(), set => set.add(status.get('id')));
  64. map.updateIn(['ancestors', status.get('id')], Immutable.OrderedSet(), set => set.add(status.get('in_reply_to_id')));
  65. }
  66. map.setIn(['accounts', account.get('id')], account);
  67. map.setIn(['statuses', status.get('id')], status);
  68. });
  69. };
  70. function normalizeTimeline(state, timeline, statuses) {
  71. statuses.forEach((status, i) => {
  72. state = normalizeStatus(state, status);
  73. state = state.setIn([timeline, i], status.get('id'));
  74. });
  75. return state;
  76. };
  77. function normalizeAccountTimeline(state, accountId, statuses) {
  78. statuses.forEach((status, i) => {
  79. state = normalizeStatus(state, status);
  80. state = state.updateIn(['accounts_timelines', accountId], Immutable.List(), list => list.set(i, status.get('id')));
  81. });
  82. return state;
  83. };
  84. function updateTimeline(state, timeline, status) {
  85. state = normalizeStatus(state, status);
  86. state = state.update(timeline, list => list.unshift(status.get('id')));
  87. state = state.updateIn(['accounts_timelines', status.getIn(['account', 'id'])], Immutable.List(), list => list.unshift(status.get('id')));
  88. return state;
  89. };
  90. function deleteStatus(state, id) {
  91. ['home', 'mentions'].forEach(function (timeline) {
  92. state = state.update(timeline, list => list.filterNot(item => item === id));
  93. });
  94. return state.deleteIn(['statuses', id]);
  95. };
  96. function normalizeAccount(state, account) {
  97. return state.setIn(['accounts', account.get('id')], account);
  98. };
  99. function normalizeContext(state, status, ancestors, descendants) {
  100. state = normalizeStatus(state, status);
  101. let ancestorsIds = ancestors.map(ancestor => {
  102. state = normalizeStatus(state, ancestor);
  103. return ancestor.get('id');
  104. }).toOrderedSet();
  105. let descendantsIds = descendants.map(descendant => {
  106. state = normalizeStatus(state, descendant);
  107. return descendant.get('id');
  108. }).toOrderedSet();
  109. return state.withMutations(map => {
  110. map.setIn(['ancestors', status.get('id')], ancestorsIds);
  111. map.setIn(['descendants', status.get('id')], descendantsIds);
  112. });
  113. };
  114. export default function timelines(state = initialState, action) {
  115. switch(action.type) {
  116. case TIMELINE_SET:
  117. return normalizeTimeline(state, action.timeline, Immutable.fromJS(action.statuses));
  118. case TIMELINE_UPDATE:
  119. return updateTimeline(state, action.timeline, Immutable.fromJS(action.status));
  120. case TIMELINE_DELETE:
  121. return deleteStatus(state, action.id);
  122. case REBLOG_SUCCESS:
  123. case FAVOURITE_SUCCESS:
  124. return normalizeStatus(state, Immutable.fromJS(action.response));
  125. case ACCOUNT_SET_SELF:
  126. return state.withMutations(map => {
  127. map.setIn(['accounts', action.account.id], Immutable.fromJS(action.account));
  128. map.set('me', action.account.id);
  129. });
  130. case ACCOUNT_FETCH_SUCCESS:
  131. case FOLLOW_SUBMIT_SUCCESS:
  132. case ACCOUNT_FOLLOW_SUCCESS:
  133. case ACCOUNT_UNFOLLOW_SUCCESS:
  134. return normalizeAccount(state, Immutable.fromJS(action.account));
  135. case STATUS_FETCH_SUCCESS:
  136. return normalizeContext(state, Immutable.fromJS(action.status), Immutable.fromJS(action.context.ancestors), Immutable.fromJS(action.context.descendants));
  137. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  138. return normalizeAccountTimeline(state, action.id, Immutable.fromJS(action.statuses));
  139. default:
  140. return state;
  141. }
  142. };