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

129 lines
3.5 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. } from '../actions/accounts';
  10. import {
  11. BLOCKS_FETCH_SUCCESS,
  12. BLOCKS_EXPAND_SUCCESS,
  13. } from '../actions/blocks';
  14. import {
  15. MUTES_FETCH_SUCCESS,
  16. MUTES_EXPAND_SUCCESS,
  17. } from '../actions/mutes';
  18. import { COMPOSE_SUGGESTIONS_READY } from '../actions/compose';
  19. import {
  20. REBLOG_SUCCESS,
  21. UNREBLOG_SUCCESS,
  22. FAVOURITE_SUCCESS,
  23. UNFAVOURITE_SUCCESS,
  24. REBLOGS_FETCH_SUCCESS,
  25. FAVOURITES_FETCH_SUCCESS,
  26. } from '../actions/interactions';
  27. import {
  28. TIMELINE_REFRESH_SUCCESS,
  29. TIMELINE_UPDATE,
  30. TIMELINE_EXPAND_SUCCESS,
  31. } from '../actions/timelines';
  32. import {
  33. STATUS_FETCH_SUCCESS,
  34. CONTEXT_FETCH_SUCCESS,
  35. } from '../actions/statuses';
  36. import { SEARCH_FETCH_SUCCESS } from '../actions/search';
  37. import {
  38. NOTIFICATIONS_UPDATE,
  39. NOTIFICATIONS_REFRESH_SUCCESS,
  40. NOTIFICATIONS_EXPAND_SUCCESS,
  41. } from '../actions/notifications';
  42. import {
  43. FAVOURITED_STATUSES_FETCH_SUCCESS,
  44. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  45. } from '../actions/favourites';
  46. import { STORE_HYDRATE } from '../actions/store';
  47. import Immutable from 'immutable';
  48. const normalizeAccount = (state, account) => {
  49. account = { ...account };
  50. delete account.followers_count;
  51. delete account.following_count;
  52. delete account.statuses_count;
  53. return state.set(account.id, Immutable.fromJS(account));
  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 = Immutable.Map();
  75. export default function accounts(state = initialState, action) {
  76. switch(action.type) {
  77. case STORE_HYDRATE:
  78. return state.merge(action.state.get('accounts'));
  79. case ACCOUNT_FETCH_SUCCESS:
  80. case NOTIFICATIONS_UPDATE:
  81. return normalizeAccount(state, action.account);
  82. case FOLLOWERS_FETCH_SUCCESS:
  83. case FOLLOWERS_EXPAND_SUCCESS:
  84. case FOLLOWING_FETCH_SUCCESS:
  85. case FOLLOWING_EXPAND_SUCCESS:
  86. case REBLOGS_FETCH_SUCCESS:
  87. case FAVOURITES_FETCH_SUCCESS:
  88. case COMPOSE_SUGGESTIONS_READY:
  89. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  90. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  91. case BLOCKS_FETCH_SUCCESS:
  92. case BLOCKS_EXPAND_SUCCESS:
  93. case MUTES_FETCH_SUCCESS:
  94. case MUTES_EXPAND_SUCCESS:
  95. return normalizeAccounts(state, action.accounts);
  96. case NOTIFICATIONS_REFRESH_SUCCESS:
  97. case NOTIFICATIONS_EXPAND_SUCCESS:
  98. case SEARCH_FETCH_SUCCESS:
  99. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  100. case TIMELINE_REFRESH_SUCCESS:
  101. case 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. default:
  115. return state;
  116. }
  117. };