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

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