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.

91 lines
2.5 KiB

  1. import {
  2. ACCOUNT_SET_SELF,
  3. ACCOUNT_FETCH_SUCCESS,
  4. FOLLOWERS_FETCH_SUCCESS,
  5. FOLLOWING_FETCH_SUCCESS,
  6. ACCOUNT_TIMELINE_FETCH_SUCCESS,
  7. ACCOUNT_TIMELINE_EXPAND_SUCCESS
  8. } from '../actions/accounts';
  9. import { SUGGESTIONS_FETCH_SUCCESS } from '../actions/suggestions';
  10. import { COMPOSE_SUGGESTIONS_READY } from '../actions/compose';
  11. import {
  12. REBLOG_SUCCESS,
  13. UNREBLOG_SUCCESS,
  14. FAVOURITE_SUCCESS,
  15. UNFAVOURITE_SUCCESS,
  16. REBLOGS_FETCH_SUCCESS,
  17. FAVOURITES_FETCH_SUCCESS
  18. } from '../actions/interactions';
  19. import {
  20. TIMELINE_REFRESH_SUCCESS,
  21. TIMELINE_UPDATE,
  22. TIMELINE_EXPAND_SUCCESS
  23. } from '../actions/timelines';
  24. import {
  25. STATUS_FETCH_SUCCESS,
  26. CONTEXT_FETCH_SUCCESS
  27. } from '../actions/statuses';
  28. import { SEARCH_SUGGESTIONS_READY } from '../actions/search';
  29. import Immutable from 'immutable';
  30. const normalizeAccount = (state, account) => state.set(account.id, Immutable.fromJS(account));
  31. const normalizeAccounts = (state, accounts) => {
  32. accounts.forEach(account => {
  33. state = normalizeAccount(state, account);
  34. });
  35. return state;
  36. };
  37. const normalizeAccountFromStatus = (state, status) => {
  38. state = normalizeAccount(state, status.account);
  39. if (status.reblog && status.reblog.account) {
  40. state = normalizeAccount(state, status.reblog.account);
  41. }
  42. return state;
  43. };
  44. const normalizeAccountsFromStatuses = (state, statuses) => {
  45. statuses.forEach(status => {
  46. state = normalizeAccountFromStatus(state, status);
  47. });
  48. return state;
  49. };
  50. const initialState = Immutable.Map();
  51. export default function accounts(state = initialState, action) {
  52. switch(action.type) {
  53. case ACCOUNT_SET_SELF:
  54. case ACCOUNT_FETCH_SUCCESS:
  55. return normalizeAccount(state, action.account);
  56. case SUGGESTIONS_FETCH_SUCCESS:
  57. case FOLLOWERS_FETCH_SUCCESS:
  58. case FOLLOWING_FETCH_SUCCESS:
  59. case REBLOGS_FETCH_SUCCESS:
  60. case FAVOURITES_FETCH_SUCCESS:
  61. case COMPOSE_SUGGESTIONS_READY:
  62. case SEARCH_SUGGESTIONS_READY:
  63. return normalizeAccounts(state, action.accounts);
  64. case TIMELINE_REFRESH_SUCCESS:
  65. case TIMELINE_EXPAND_SUCCESS:
  66. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  67. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  68. case CONTEXT_FETCH_SUCCESS:
  69. return normalizeAccountsFromStatuses(state, action.statuses);
  70. case REBLOG_SUCCESS:
  71. case FAVOURITE_SUCCESS:
  72. case UNREBLOG_SUCCESS:
  73. case UNFAVOURITE_SUCCESS:
  74. return normalizeAccountFromStatus(state, action.response);
  75. case TIMELINE_UPDATE:
  76. case STATUS_FETCH_SUCCESS:
  77. return normalizeAccountFromStatus(state, action.status);
  78. default:
  79. return state;
  80. }
  81. };