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.

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