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

131 lines
3.8 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(account));
  53. const normalizeAccounts = (state, accounts) => {
  54. accounts.forEach(account => {
  55. state = normalizeAccount(state, account);
  56. });
  57. return state;
  58. };
  59. const normalizeAccountFromStatus = (state, status) => {
  60. state = normalizeAccount(state, status.account);
  61. if (status.reblog && status.reblog.account) {
  62. state = normalizeAccount(state, status.reblog.account);
  63. }
  64. return state;
  65. };
  66. const normalizeAccountsFromStatuses = (state, statuses) => {
  67. statuses.forEach(status => {
  68. state = normalizeAccountFromStatus(state, status);
  69. });
  70. return state;
  71. };
  72. const initialState = Immutable.Map();
  73. export default function accounts(state = initialState, action) {
  74. switch(action.type) {
  75. case STORE_HYDRATE:
  76. return state.merge(action.state.get('accounts'));
  77. case ACCOUNT_FETCH_SUCCESS:
  78. case NOTIFICATIONS_UPDATE:
  79. return normalizeAccount(state, action.account);
  80. case FOLLOWERS_FETCH_SUCCESS:
  81. case FOLLOWERS_EXPAND_SUCCESS:
  82. case FOLLOWING_FETCH_SUCCESS:
  83. case FOLLOWING_EXPAND_SUCCESS:
  84. case REBLOGS_FETCH_SUCCESS:
  85. case FAVOURITES_FETCH_SUCCESS:
  86. case COMPOSE_SUGGESTIONS_READY:
  87. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  88. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  89. case BLOCKS_FETCH_SUCCESS:
  90. case BLOCKS_EXPAND_SUCCESS:
  91. case MUTES_FETCH_SUCCESS:
  92. case MUTES_EXPAND_SUCCESS:
  93. return normalizeAccounts(state, action.accounts);
  94. case NOTIFICATIONS_REFRESH_SUCCESS:
  95. case NOTIFICATIONS_EXPAND_SUCCESS:
  96. case SEARCH_FETCH_SUCCESS:
  97. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  98. case TIMELINE_REFRESH_SUCCESS:
  99. case TIMELINE_EXPAND_SUCCESS:
  100. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  101. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  102. case CONTEXT_FETCH_SUCCESS:
  103. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  104. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  105. return normalizeAccountsFromStatuses(state, action.statuses);
  106. case REBLOG_SUCCESS:
  107. case FAVOURITE_SUCCESS:
  108. case UNREBLOG_SUCCESS:
  109. case UNFAVOURITE_SUCCESS:
  110. return normalizeAccountFromStatus(state, action.response);
  111. case TIMELINE_UPDATE:
  112. case STATUS_FETCH_SUCCESS:
  113. return normalizeAccountFromStatus(state, action.status);
  114. case ACCOUNT_FOLLOW_SUCCESS:
  115. return state.updateIn([action.relationship.id, 'followers_count'], num => num + 1);
  116. case ACCOUNT_UNFOLLOW_SUCCESS:
  117. return state.updateIn([action.relationship.id, 'followers_count'], num => Math.max(0, num - 1));
  118. default:
  119. return state;
  120. }
  121. };