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.

104 lines
3.0 KiB

  1. import {
  2. ACCOUNT_SET_SELF,
  3. ACCOUNT_FETCH_SUCCESS,
  4. FOLLOWERS_FETCH_SUCCESS,
  5. FOLLOWERS_EXPAND_SUCCESS,
  6. FOLLOWING_FETCH_SUCCESS,
  7. FOLLOWING_EXPAND_SUCCESS,
  8. ACCOUNT_TIMELINE_FETCH_SUCCESS,
  9. ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  10. FOLLOW_REQUESTS_FETCH_SUCCESS
  11. } from '../actions/accounts';
  12. import { COMPOSE_SUGGESTIONS_READY } from '../actions/compose';
  13. import {
  14. REBLOG_SUCCESS,
  15. UNREBLOG_SUCCESS,
  16. FAVOURITE_SUCCESS,
  17. UNFAVOURITE_SUCCESS,
  18. REBLOGS_FETCH_SUCCESS,
  19. FAVOURITES_FETCH_SUCCESS
  20. } from '../actions/interactions';
  21. import {
  22. TIMELINE_REFRESH_SUCCESS,
  23. TIMELINE_UPDATE,
  24. TIMELINE_EXPAND_SUCCESS
  25. } from '../actions/timelines';
  26. import {
  27. STATUS_FETCH_SUCCESS,
  28. CONTEXT_FETCH_SUCCESS
  29. } from '../actions/statuses';
  30. import { SEARCH_SUGGESTIONS_READY } from '../actions/search';
  31. import {
  32. NOTIFICATIONS_UPDATE,
  33. NOTIFICATIONS_REFRESH_SUCCESS,
  34. NOTIFICATIONS_EXPAND_SUCCESS
  35. } from '../actions/notifications';
  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 ACCOUNT_SET_SELF:
  61. case ACCOUNT_FETCH_SUCCESS:
  62. case NOTIFICATIONS_UPDATE:
  63. return normalizeAccount(state, action.account);
  64. case FOLLOWERS_FETCH_SUCCESS:
  65. case FOLLOWERS_EXPAND_SUCCESS:
  66. case FOLLOWING_FETCH_SUCCESS:
  67. case FOLLOWING_EXPAND_SUCCESS:
  68. case REBLOGS_FETCH_SUCCESS:
  69. case FAVOURITES_FETCH_SUCCESS:
  70. case COMPOSE_SUGGESTIONS_READY:
  71. case SEARCH_SUGGESTIONS_READY:
  72. case FOLLOW_REQUESTS_FETCH_SUCCESS:
  73. return normalizeAccounts(state, action.accounts);
  74. case NOTIFICATIONS_REFRESH_SUCCESS:
  75. case NOTIFICATIONS_EXPAND_SUCCESS:
  76. return normalizeAccountsFromStatuses(normalizeAccounts(state, action.accounts), action.statuses);
  77. case TIMELINE_REFRESH_SUCCESS:
  78. case TIMELINE_EXPAND_SUCCESS:
  79. case ACCOUNT_TIMELINE_FETCH_SUCCESS:
  80. case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
  81. case CONTEXT_FETCH_SUCCESS:
  82. return normalizeAccountsFromStatuses(state, action.statuses);
  83. case REBLOG_SUCCESS:
  84. case FAVOURITE_SUCCESS:
  85. case UNREBLOG_SUCCESS:
  86. case UNFAVOURITE_SUCCESS:
  87. return normalizeAccountFromStatus(state, action.response);
  88. case TIMELINE_UPDATE:
  89. case STATUS_FETCH_SUCCESS:
  90. return normalizeAccountFromStatus(state, action.status);
  91. default:
  92. return state;
  93. }
  94. };