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.

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