闭社主体 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
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. 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 emojify from '../emoji';
  48. import { Map as ImmutableMap, fromJS } from 'immutable';
  49. import escapeTextContentForBrowser from 'escape-html';
  50. const normalizeAccount = (state, account) => {
  51. account = { ...account };
  52. delete account.followers_count;
  53. delete account.following_count;
  54. delete account.statuses_count;
  55. const displayName = account.display_name.length === 0 ? account.username : account.display_name;
  56. account.display_name_html = emojify(escapeTextContentForBrowser(displayName));
  57. account.note_emojified = emojify(account.note);
  58. return state.set(account.id, fromJS(account));
  59. };
  60. const normalizeAccounts = (state, accounts) => {
  61. accounts.forEach(account => {
  62. state = normalizeAccount(state, account);
  63. });
  64. return state;
  65. };
  66. const normalizeAccountFromStatus = (state, status) => {
  67. state = normalizeAccount(state, status.account);
  68. if (status.reblog && status.reblog.account) {
  69. state = normalizeAccount(state, status.reblog.account);
  70. }
  71. return state;
  72. };
  73. const normalizeAccountsFromStatuses = (state, statuses) => {
  74. statuses.forEach(status => {
  75. state = normalizeAccountFromStatus(state, status);
  76. });
  77. return state;
  78. };
  79. const initialState = ImmutableMap();
  80. export default function accounts(state = initialState, action) {
  81. switch(action.type) {
  82. case STORE_HYDRATE:
  83. return state.merge(action.state.get('accounts'));
  84. case ACCOUNT_FETCH_SUCCESS:
  85. case NOTIFICATIONS_UPDATE:
  86. return normalizeAccount(state, action.account);
  87. case FOLLOWERS_FETCH_SUCCESS:
  88. case FOLLOWERS_EXPAND_SUCCESS:
  89. case FOLLOWING_FETCH_SUCCESS:
  90. case FOLLOWING_EXPAND_SUCCESS:
  91. case REBLOGS_FETCH_SUCCESS:
  92. case FAVOURITES_FETCH_SUCCESS:
  93. case COMPOSE_SUGGESTIONS_READY:
  94. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  95. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  96. case BLOCKS_FETCH_SUCCESS:
  97. case BLOCKS_EXPAND_SUCCESS:
  98. case MUTES_FETCH_SUCCESS:
  99. case MUTES_EXPAND_SUCCESS:
  100. return normalizeAccounts(state, action.accounts);
  101. case NOTIFICATIONS_REFRESH_SUCCESS:
  102. case NOTIFICATIONS_EXPAND_SUCCESS:
  103. case SEARCH_FETCH_SUCCESS:
  104. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  105. case TIMELINE_REFRESH_SUCCESS:
  106. case TIMELINE_EXPAND_SUCCESS:
  107. case CONTEXT_FETCH_SUCCESS:
  108. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  109. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  110. return normalizeAccountsFromStatuses(state, action.statuses);
  111. case REBLOG_SUCCESS:
  112. case FAVOURITE_SUCCESS:
  113. case UNREBLOG_SUCCESS:
  114. case UNFAVOURITE_SUCCESS:
  115. return normalizeAccountFromStatus(state, action.response);
  116. case TIMELINE_UPDATE:
  117. case STATUS_FETCH_SUCCESS:
  118. return normalizeAccountFromStatus(state, action.status);
  119. default:
  120. return state;
  121. }
  122. };