闭社主体 forked from https://github.com/tootsuite/mastodon
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.

243 lines
8.1 KiB

  1. import {
  2. TIMELINE_REFRESH_SUCCESS,
  3. TIMELINE_UPDATE,
  4. TIMELINE_DELETE,
  5. TIMELINE_EXPAND_SUCCESS
  6. } from '../actions/timelines';
  7. import {
  8. REBLOG_SUCCESS,
  9. UNREBLOG_SUCCESS,
  10. FAVOURITE_SUCCESS,
  11. UNFAVOURITE_SUCCESS
  12. } from '../actions/interactions';
  13. import {
  14. ACCOUNT_SET_SELF,
  15. ACCOUNT_FETCH_SUCCESS,
  16. ACCOUNT_FOLLOW_SUCCESS,
  17. ACCOUNT_UNFOLLOW_SUCCESS,
  18. ACCOUNT_BLOCK_SUCCESS,
  19. ACCOUNT_UNBLOCK_SUCCESS,
  20. ACCOUNT_TIMELINE_FETCH_SUCCESS,
  21. ACCOUNT_TIMELINE_EXPAND_SUCCESS
  22. } from '../actions/accounts';
  23. import {
  24. STATUS_FETCH_SUCCESS,
  25. STATUS_DELETE_SUCCESS
  26. } from '../actions/statuses';
  27. import { FOLLOW_SUBMIT_SUCCESS } from '../actions/follow';
  28. import { SUGGESTIONS_FETCH_SUCCESS } from '../actions/suggestions';
  29. import Immutable from 'immutable';
  30. const initialState = Immutable.Map({
  31. home: Immutable.List([]),
  32. mentions: Immutable.List([]),
  33. public: Immutable.List([]),
  34. statuses: Immutable.Map(),
  35. accounts: Immutable.Map(),
  36. accounts_timelines: Immutable.Map(),
  37. me: null,
  38. ancestors: Immutable.Map(),
  39. descendants: Immutable.Map(),
  40. relationships: Immutable.Map(),
  41. suggestions: Immutable.List([])
  42. });
  43. function normalizeStatus(state, status) {
  44. // Separate account
  45. let account = status.get('account');
  46. status = status.set('account', account.get('id'));
  47. // Separate reblog, repeat for reblog
  48. let reblog = status.get('reblog');
  49. if (reblog !== null) {
  50. status = status.set('reblog', reblog.get('id'));
  51. state = normalizeStatus(state, reblog);
  52. }
  53. // Replies
  54. if (status.get('in_reply_to_id')) {
  55. state = state.updateIn(['descendants', status.get('in_reply_to_id')], set => {
  56. if (!Immutable.OrderedSet.isOrderedSet(set)) {
  57. return Immutable.OrderedSet([status.get('id')]);
  58. } else {
  59. return set.add(status.get('id'));
  60. }
  61. });
  62. }
  63. return state.withMutations(map => {
  64. if (status.get('in_reply_to_id')) {
  65. map.updateIn(['descendants', status.get('in_reply_to_id')], Immutable.OrderedSet(), set => set.add(status.get('id')));
  66. map.updateIn(['ancestors', status.get('id')], Immutable.OrderedSet(), set => set.add(status.get('in_reply_to_id')));
  67. }
  68. map.setIn(['accounts', account.get('id')], account);
  69. map.setIn(['statuses', status.get('id')], status);
  70. });
  71. };
  72. function normalizeTimeline(state, timeline, statuses) {
  73. statuses.forEach((status, i) => {
  74. state = normalizeStatus(state, status);
  75. state = state.setIn([timeline, i], status.get('id'));
  76. });
  77. return state;
  78. };
  79. function appendNormalizedTimeline(state, timeline, statuses) {
  80. let moreIds = Immutable.List([]);
  81. statuses.forEach((status, i) => {
  82. state = normalizeStatus(state, status);
  83. moreIds = moreIds.set(i, status.get('id'));
  84. });
  85. return state.update(timeline, list => list.push(...moreIds));
  86. };
  87. function normalizeAccountTimeline(state, accountId, statuses) {
  88. state = state.updateIn(['accounts_timelines', accountId], Immutable.List([]), list => {
  89. return (list.size > 0) ? list.clear() : list;
  90. });
  91. statuses.forEach((status, i) => {
  92. state = normalizeStatus(state, status);
  93. state = state.updateIn(['accounts_timelines', accountId], Immutable.List([]), list => list.set(i, status.get('id')));
  94. });
  95. return state;
  96. };
  97. function appendNormalizedAccountTimeline(state, accountId, statuses) {
  98. let moreIds = Immutable.List([]);
  99. statuses.forEach((status, i) => {
  100. state = normalizeStatus(state, status);
  101. moreIds = moreIds.set(i, status.get('id'));
  102. });
  103. return state.updateIn(['accounts_timelines', accountId], Immutable.List([]), list => list.push(...moreIds));
  104. };
  105. function updateTimeline(state, timeline, status) {
  106. state = normalizeStatus(state, status);
  107. state = state.update(timeline, list => list.unshift(status.get('id')));
  108. state = state.updateIn(['accounts_timelines', status.getIn(['account', 'id'])], Immutable.List([]), list => (list.includes(status.get('id')) ? list : list.unshift(status.get('id'))));
  109. return state;
  110. };
  111. function deleteStatus(state, id) {
  112. const status = state.getIn(['statuses', id]);
  113. if (!status) {
  114. return state;
  115. }
  116. // Remove references from timelines
  117. ['home', 'mentions'].forEach(function (timeline) {
  118. state = state.update(timeline, list => list.filterNot(item => item === id));
  119. });
  120. // Remove references from account timelines
  121. state = state.updateIn(['accounts_timelines', status.get('account')], Immutable.List([]), list => list.filterNot(item => item === id));
  122. // Remove reblogs of deleted status
  123. const references = state.get('statuses').filter(item => item.get('reblog') === id);
  124. references.forEach(referencingId => {
  125. state = deleteStatus(state, referencingId);
  126. });
  127. // Remove normalized status
  128. return state.deleteIn(['statuses', id]);
  129. };
  130. function normalizeAccount(state, account, relationship) {
  131. if (relationship) {
  132. state = normalizeRelationship(state, relationship);
  133. }
  134. return state.setIn(['accounts', account.get('id')], account);
  135. };
  136. function normalizeRelationship(state, relationship) {
  137. if (state.get('suggestions').includes(relationship.get('id')) && (relationship.get('following') || relationship.get('blocking'))) {
  138. state = state.update('suggestions', list => list.filterNot(id => id === relationship.get('id')));
  139. }
  140. return state.setIn(['relationships', relationship.get('id')], relationship);
  141. };
  142. function setSelf(state, account) {
  143. state = normalizeAccount(state, account);
  144. return state.set('me', account.get('id'));
  145. };
  146. function normalizeContext(state, status, ancestors, descendants) {
  147. state = normalizeStatus(state, status);
  148. let ancestorsIds = ancestors.map(ancestor => {
  149. state = normalizeStatus(state, ancestor);
  150. return ancestor.get('id');
  151. }).toOrderedSet();
  152. let descendantsIds = descendants.map(descendant => {
  153. state = normalizeStatus(state, descendant);
  154. return descendant.get('id');
  155. }).toOrderedSet();
  156. return state.withMutations(map => {
  157. map.setIn(['ancestors', status.get('id')], ancestorsIds);
  158. map.setIn(['descendants', status.get('id')], descendantsIds);
  159. });
  160. };
  161. function normalizeSuggestions(state, accounts) {
  162. accounts.forEach(account => {
  163. state = state.setIn(['accounts', account.get('id')], account);
  164. });
  165. return state.set('suggestions', accounts.map(account => account.get('id')));
  166. };
  167. export default function timelines(state = initialState, action) {
  168. switch(action.type) {
  169. case TIMELINE_REFRESH_SUCCESS:
  170. return normalizeTimeline(state, action.timeline, Immutable.fromJS(action.statuses));
  171. case TIMELINE_EXPAND_SUCCESS:
  172. return appendNormalizedTimeline(state, action.timeline, Immutable.fromJS(action.statuses));
  173. case TIMELINE_UPDATE:
  174. return updateTimeline(state, action.timeline, Immutable.fromJS(action.status));
  175. case TIMELINE_DELETE:
  176. case STATUS_DELETE_SUCCESS:
  177. return deleteStatus(state, action.id);
  178. case REBLOG_SUCCESS:
  179. case FAVOURITE_SUCCESS:
  180. case UNREBLOG_SUCCESS:
  181. case UNFAVOURITE_SUCCESS:
  182. return normalizeStatus(state, Immutable.fromJS(action.response));
  183. case ACCOUNT_SET_SELF:
  184. return setSelf(state, Immutable.fromJS(action.account));
  185. case ACCOUNT_FETCH_SUCCESS:
  186. case FOLLOW_SUBMIT_SUCCESS:
  187. return normalizeAccount(state, Immutable.fromJS(action.account), Immutable.fromJS(action.relationship));
  188. case ACCOUNT_FOLLOW_SUCCESS:
  189. case ACCOUNT_UNFOLLOW_SUCCESS:
  190. case ACCOUNT_UNBLOCK_SUCCESS:
  191. case ACCOUNT_BLOCK_SUCCESS:
  192. return normalizeRelationship(state, Immutable.fromJS(action.relationship));
  193. case STATUS_FETCH_SUCCESS:
  194. return normalizeContext(state, Immutable.fromJS(action.status), Immutable.fromJS(action.context.ancestors), Immutable.fromJS(action.context.descendants));
  195. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  196. return normalizeAccountTimeline(state, action.id, Immutable.fromJS(action.statuses));
  197. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  198. return appendNormalizedAccountTimeline(state, action.id, Immutable.fromJS(action.statuses));
  199. case SUGGESTIONS_FETCH_SUCCESS:
  200. return normalizeSuggestions(state, Immutable.fromJS(action.suggestions));
  201. default:
  202. return state;
  203. }
  204. };