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

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