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.

141 lines
4.2 KiB

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 'flavours/glitch/actions/accounts';
  10. import {
  11. BLOCKS_FETCH_SUCCESS,
  12. BLOCKS_EXPAND_SUCCESS,
  13. } from 'flavours/glitch/actions/blocks';
  14. import {
  15. MUTES_FETCH_SUCCESS,
  16. MUTES_EXPAND_SUCCESS,
  17. } from 'flavours/glitch/actions/mutes';
  18. import { COMPOSE_SUGGESTIONS_READY } from 'flavours/glitch/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 'flavours/glitch/actions/interactions';
  27. import {
  28. TIMELINE_REFRESH_SUCCESS,
  29. TIMELINE_UPDATE,
  30. TIMELINE_EXPAND_SUCCESS,
  31. } from 'flavours/glitch/actions/timelines';
  32. import {
  33. STATUS_FETCH_SUCCESS,
  34. CONTEXT_FETCH_SUCCESS,
  35. } from 'flavours/glitch/actions/statuses';
  36. import { SEARCH_FETCH_SUCCESS } from 'flavours/glitch/actions/search';
  37. import {
  38. NOTIFICATIONS_UPDATE,
  39. NOTIFICATIONS_REFRESH_SUCCESS,
  40. NOTIFICATIONS_EXPAND_SUCCESS,
  41. } from 'flavours/glitch/actions/notifications';
  42. import {
  43. FAVOURITED_STATUSES_FETCH_SUCCESS,
  44. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  45. } from 'flavours/glitch/actions/favourites';
  46. import {
  47. LIST_ACCOUNTS_FETCH_SUCCESS,
  48. LIST_EDITOR_SUGGESTIONS_READY,
  49. } from 'flavours/glitch/actions/lists';
  50. import { STORE_HYDRATE } from 'flavours/glitch/actions/store';
  51. import emojify from 'flavours/glitch/util/emoji';
  52. import { Map as ImmutableMap, fromJS } from 'immutable';
  53. import escapeTextContentForBrowser from 'escape-html';
  54. const normalizeAccount = (state, account) => {
  55. account = { ...account };
  56. delete account.followers_count;
  57. delete account.following_count;
  58. delete account.statuses_count;
  59. const displayName = account.display_name.length === 0 ? account.username : account.display_name;
  60. account.display_name_html = emojify(escapeTextContentForBrowser(displayName));
  61. account.note_emojified = emojify(account.note);
  62. return state.set(account.id, fromJS(account));
  63. };
  64. const normalizeAccounts = (state, accounts) => {
  65. accounts.forEach(account => {
  66. state = normalizeAccount(state, account);
  67. });
  68. return state;
  69. };
  70. const normalizeAccountFromStatus = (state, status) => {
  71. state = normalizeAccount(state, status.account);
  72. if (status.reblog && status.reblog.account) {
  73. state = normalizeAccount(state, status.reblog.account);
  74. }
  75. return state;
  76. };
  77. const normalizeAccountsFromStatuses = (state, statuses) => {
  78. statuses.forEach(status => {
  79. state = normalizeAccountFromStatus(state, status);
  80. });
  81. return state;
  82. };
  83. const initialState = ImmutableMap();
  84. export default function accounts(state = initialState, action) {
  85. switch(action.type) {
  86. case STORE_HYDRATE:
  87. return state.merge(action.state.get('accounts'));
  88. case ACCOUNT_FETCH_SUCCESS:
  89. case NOTIFICATIONS_UPDATE:
  90. return normalizeAccount(state, action.account);
  91. case FOLLOWERS_FETCH_SUCCESS:
  92. case FOLLOWERS_EXPAND_SUCCESS:
  93. case FOLLOWING_FETCH_SUCCESS:
  94. case FOLLOWING_EXPAND_SUCCESS:
  95. case REBLOGS_FETCH_SUCCESS:
  96. case FAVOURITES_FETCH_SUCCESS:
  97. case COMPOSE_SUGGESTIONS_READY:
  98. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  99. case FOLLOW_REQUESTS_EXPAND_SUCCESS:
  100. case BLOCKS_FETCH_SUCCESS:
  101. case BLOCKS_EXPAND_SUCCESS:
  102. case MUTES_FETCH_SUCCESS:
  103. case MUTES_EXPAND_SUCCESS:
  104. case LIST_ACCOUNTS_FETCH_SUCCESS:
  105. case LIST_EDITOR_SUGGESTIONS_READY:
  106. return action.accounts ? normalizeAccounts(state, action.accounts) : state;
  107. case NOTIFICATIONS_REFRESH_SUCCESS:
  108. case NOTIFICATIONS_EXPAND_SUCCESS:
  109. case SEARCH_FETCH_SUCCESS:
  110. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  111. case TIMELINE_REFRESH_SUCCESS:
  112. case TIMELINE_EXPAND_SUCCESS:
  113. case CONTEXT_FETCH_SUCCESS:
  114. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  115. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  116. return normalizeAccountsFromStatuses(state, action.statuses);
  117. case REBLOG_SUCCESS:
  118. case FAVOURITE_SUCCESS:
  119. case UNREBLOG_SUCCESS:
  120. case UNFAVOURITE_SUCCESS:
  121. return normalizeAccountFromStatus(state, action.response);
  122. case TIMELINE_UPDATE:
  123. case STATUS_FETCH_SUCCESS:
  124. return normalizeAccountFromStatus(state, action.status);
  125. default:
  126. return state;
  127. }
  128. };