闭社主体 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.

135 lines
4.0 KiB

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