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.

154 lines
4.7 KiB

7 years ago
  1. import {
  2. ACCOUNT_FETCH_SUCCESS,
  3. FOLLOWERS_FETCH_SUCCESS,
  4. FOLLOWERS_EXPAND_SUCCESS,
  5. FOLLOWING_FETCH_SUCCESS,
  6. FOLLOWING_EXPAND_SUCCESS,
  7. FOLLOW_REQUESTS_FETCH_SUCCESS,
  8. FOLLOW_REQUESTS_EXPAND_SUCCESS,
  9. ACCOUNT_FOLLOW_SUCCESS,
  10. ACCOUNT_UNFOLLOW_SUCCESS,
  11. } from 'flavours/glitch/actions/accounts';
  12. import {
  13. BLOCKS_FETCH_SUCCESS,
  14. BLOCKS_EXPAND_SUCCESS,
  15. } from 'flavours/glitch/actions/blocks';
  16. import {
  17. MUTES_FETCH_SUCCESS,
  18. MUTES_EXPAND_SUCCESS,
  19. } from 'flavours/glitch/actions/mutes';
  20. import { COMPOSE_SUGGESTIONS_READY } from 'flavours/glitch/actions/compose';
  21. import {
  22. REBLOG_SUCCESS,
  23. UNREBLOG_SUCCESS,
  24. FAVOURITE_SUCCESS,
  25. UNFAVOURITE_SUCCESS,
  26. BOOKMARK_SUCCESS,
  27. UNBOOKMARK_SUCCESS,
  28. REBLOGS_FETCH_SUCCESS,
  29. FAVOURITES_FETCH_SUCCESS,
  30. } from 'flavours/glitch/actions/interactions';
  31. import {
  32. TIMELINE_REFRESH_SUCCESS,
  33. TIMELINE_UPDATE,
  34. TIMELINE_EXPAND_SUCCESS,
  35. } from 'flavours/glitch/actions/timelines';
  36. import {
  37. STATUS_FETCH_SUCCESS,
  38. CONTEXT_FETCH_SUCCESS,
  39. } from 'flavours/glitch/actions/statuses';
  40. import { SEARCH_FETCH_SUCCESS } from 'flavours/glitch/actions/search';
  41. import {
  42. NOTIFICATIONS_UPDATE,
  43. NOTIFICATIONS_REFRESH_SUCCESS,
  44. NOTIFICATIONS_EXPAND_SUCCESS,
  45. } from 'flavours/glitch/actions/notifications';
  46. import {
  47. FAVOURITED_STATUSES_FETCH_SUCCESS,
  48. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  49. } from 'flavours/glitch/actions/favourites';
  50. import {
  51. BOOKMARKED_STATUSES_FETCH_SUCCESS,
  52. BOOKMARKED_STATUSES_EXPAND_SUCCESS,
  53. } from 'flavours/glitch/actions/bookmarks';
  54. import {
  55. LIST_ACCOUNTS_FETCH_SUCCESS,
  56. LIST_EDITOR_SUGGESTIONS_READY,
  57. } from 'flavours/glitch/actions/lists';
  58. import { STORE_HYDRATE } from 'flavours/glitch/actions/store';
  59. import { Map as ImmutableMap, fromJS } from 'immutable';
  60. const normalizeAccount = (state, account) => state.set(account.id, fromJS({
  61. followers_count: account.followers_count,
  62. following_count: account.following_count,
  63. statuses_count: account.statuses_count,
  64. }));
  65. const normalizeAccounts = (state, accounts) => {
  66. accounts.forEach(account => {
  67. state = normalizeAccount(state, account);
  68. });
  69. return state;
  70. };
  71. const normalizeAccountFromStatus = (state, status) => {
  72. state = normalizeAccount(state, status.account);
  73. if (status.reblog && status.reblog.account) {
  74. state = normalizeAccount(state, status.reblog.account);
  75. }
  76. return state;
  77. };
  78. const normalizeAccountsFromStatuses = (state, statuses) => {
  79. statuses.forEach(status => {
  80. state = normalizeAccountFromStatus(state, status);
  81. });
  82. return state;
  83. };
  84. const initialState = ImmutableMap();
  85. export default function accountsCounters(state = initialState, action) {
  86. switch(action.type) {
  87. case STORE_HYDRATE:
  88. return state.merge(action.state.get('accounts').map(item => fromJS({
  89. followers_count: item.get('followers_count'),
  90. following_count: item.get('following_count'),
  91. statuses_count: item.get('statuses_count'),
  92. })));
  93. case ACCOUNT_FETCH_SUCCESS:
  94. case NOTIFICATIONS_UPDATE:
  95. return normalizeAccount(state, action.account);
  96. case FOLLOWERS_FETCH_SUCCESS:
  97. case FOLLOWERS_EXPAND_SUCCESS:
  98. case FOLLOWING_FETCH_SUCCESS:
  99. case FOLLOWING_EXPAND_SUCCESS:
  100. case REBLOGS_FETCH_SUCCESS:
  101. case FAVOURITES_FETCH_SUCCESS:
  102. case COMPOSE_SUGGESTIONS_READY:
  103. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  104. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  105. case BLOCKS_FETCH_SUCCESS:
  106. case BLOCKS_EXPAND_SUCCESS:
  107. case MUTES_FETCH_SUCCESS:
  108. case MUTES_EXPAND_SUCCESS:
  109. case LIST_ACCOUNTS_FETCH_SUCCESS:
  110. case LIST_EDITOR_SUGGESTIONS_READY:
  111. return action.accounts ? normalizeAccounts(state, action.accounts) : state;
  112. case NOTIFICATIONS_REFRESH_SUCCESS:
  113. case NOTIFICATIONS_EXPAND_SUCCESS:
  114. case SEARCH_FETCH_SUCCESS:
  115. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  116. case TIMELINE_REFRESH_SUCCESS:
  117. case TIMELINE_EXPAND_SUCCESS:
  118. case CONTEXT_FETCH_SUCCESS:
  119. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  120. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  121. case BOOKMARKED_STATUSES_FETCH_SUCCESS:
  122. case BOOKMARKED_STATUSES_EXPAND_SUCCESS:
  123. return normalizeAccountsFromStatuses(state, action.statuses);
  124. case REBLOG_SUCCESS:
  125. case FAVOURITE_SUCCESS:
  126. case UNREBLOG_SUCCESS:
  127. case UNFAVOURITE_SUCCESS:
  128. case BOOKMARK_SUCCESS:
  129. case UNBOOKMARK_SUCCESS:
  130. return normalizeAccountFromStatus(state, action.response);
  131. case TIMELINE_UPDATE:
  132. case STATUS_FETCH_SUCCESS:
  133. return normalizeAccountFromStatus(state, action.status);
  134. case ACCOUNT_FOLLOW_SUCCESS:
  135. if (action.alreadyFollowing) {
  136. return state;
  137. }
  138. return state.updateIn([action.relationship.id, 'followers_count'], num => num + 1);
  139. case ACCOUNT_UNFOLLOW_SUCCESS:
  140. return state.updateIn([action.relationship.id, 'followers_count'], num => Math.max(0, num - 1));
  141. default:
  142. return state;
  143. }
  144. };