logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

accounts_counters.js (1205B)


  1. import {
  2. ACCOUNT_FOLLOW_SUCCESS,
  3. ACCOUNT_UNFOLLOW_SUCCESS,
  4. } from '../actions/accounts';
  5. import { ACCOUNT_IMPORT, ACCOUNTS_IMPORT } from '../actions/importer';
  6. import { Map as ImmutableMap, fromJS } from 'immutable';
  7. const normalizeAccount = (state, account) => state.set(account.id, fromJS({
  8. followers_count: account.followers_count,
  9. following_count: account.following_count,
  10. statuses_count: account.statuses_count,
  11. }));
  12. const normalizeAccounts = (state, accounts) => {
  13. accounts.forEach(account => {
  14. state = normalizeAccount(state, account);
  15. });
  16. return state;
  17. };
  18. const initialState = ImmutableMap();
  19. export default function accountsCounters(state = initialState, action) {
  20. switch(action.type) {
  21. case ACCOUNT_IMPORT:
  22. return normalizeAccount(state, action.account);
  23. case ACCOUNTS_IMPORT:
  24. return normalizeAccounts(state, action.accounts);
  25. case ACCOUNT_FOLLOW_SUCCESS:
  26. return action.alreadyFollowing ? state :
  27. state.updateIn([action.relationship.id, 'followers_count'], num => num + 1);
  28. case ACCOUNT_UNFOLLOW_SUCCESS:
  29. return state.updateIn([action.relationship.id, 'followers_count'], num => Math.max(0, num - 1));
  30. default:
  31. return state;
  32. }
  33. };