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.

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