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.

105 lines
3.0 KiB

  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. } from '../actions/accounts';
  11. import { COMPOSE_SUGGESTIONS_READY } from '../actions/compose';
  12. import {
  13. REBLOG_SUCCESS,
  14. UNREBLOG_SUCCESS,
  15. FAVOURITE_SUCCESS,
  16. UNFAVOURITE_SUCCESS,
  17. REBLOGS_FETCH_SUCCESS,
  18. FAVOURITES_FETCH_SUCCESS
  19. } from '../actions/interactions';
  20. import {
  21. TIMELINE_REFRESH_SUCCESS,
  22. TIMELINE_UPDATE,
  23. TIMELINE_EXPAND_SUCCESS
  24. } from '../actions/timelines';
  25. import {
  26. STATUS_FETCH_SUCCESS,
  27. CONTEXT_FETCH_SUCCESS
  28. } from '../actions/statuses';
  29. import { SEARCH_SUGGESTIONS_READY } from '../actions/search';
  30. import {
  31. NOTIFICATIONS_UPDATE,
  32. NOTIFICATIONS_REFRESH_SUCCESS,
  33. NOTIFICATIONS_EXPAND_SUCCESS
  34. } from '../actions/notifications';
  35. import { STORE_HYDRATE } from '../actions/store';
  36. import Immutable from 'immutable';
  37. const normalizeAccount = (state, account) => state.set(account.id, Immutable.fromJS(account));
  38. const normalizeAccounts = (state, accounts) => {
  39. accounts.forEach(account => {
  40. state = normalizeAccount(state, account);
  41. });
  42. return state;
  43. };
  44. const normalizeAccountFromStatus = (state, status) => {
  45. state = normalizeAccount(state, status.account);
  46. if (status.reblog && status.reblog.account) {
  47. state = normalizeAccount(state, status.reblog.account);
  48. }
  49. return state;
  50. };
  51. const normalizeAccountsFromStatuses = (state, statuses) => {
  52. statuses.forEach(status => {
  53. state = normalizeAccountFromStatus(state, status);
  54. });
  55. return state;
  56. };
  57. const initialState = Immutable.Map();
  58. export default function accounts(state = initialState, action) {
  59. switch(action.type) {
  60. case STORE_HYDRATE:
  61. return state.merge(action.state.get('accounts'));
  62. case ACCOUNT_FETCH_SUCCESS:
  63. case NOTIFICATIONS_UPDATE:
  64. return normalizeAccount(state, action.account);
  65. case FOLLOWERS_FETCH_SUCCESS:
  66. case FOLLOWERS_EXPAND_SUCCESS:
  67. case FOLLOWING_FETCH_SUCCESS:
  68. case FOLLOWING_EXPAND_SUCCESS:
  69. case REBLOGS_FETCH_SUCCESS:
  70. case FAVOURITES_FETCH_SUCCESS:
  71. case COMPOSE_SUGGESTIONS_READY:
  72. case SEARCH_SUGGESTIONS_READY:
  73. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  74. return normalizeAccounts(state, action.accounts);
  75. case NOTIFICATIONS_REFRESH_SUCCESS:
  76. case NOTIFICATIONS_EXPAND_SUCCESS:
  77. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  78. case TIMELINE_REFRESH_SUCCESS:
  79. case TIMELINE_EXPAND_SUCCESS:
  80. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  81. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  82. case CONTEXT_FETCH_SUCCESS:
  83. return normalizeAccountsFromStatuses(state, action.statuses);
  84. case REBLOG_SUCCESS:
  85. case FAVOURITE_SUCCESS:
  86. case UNREBLOG_SUCCESS:
  87. case UNFAVOURITE_SUCCESS:
  88. return normalizeAccountFromStatus(state, action.response);
  89. case TIMELINE_UPDATE:
  90. case STATUS_FETCH_SUCCESS:
  91. return normalizeAccountFromStatus(state, action.status);
  92. default:
  93. return state;
  94. }
  95. };