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.

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