~cytrogen/masto-fe

ref: 110c8fb8ccb1363f649d3eb30ca822f415145e6a masto-fe/app/javascript/mastodon/reducers/accounts_counters.js -rw-r--r-- 1.5 KiB
110c8fb8 — Sqx. Flann [bugfix] account for data structure change in instance API fallback (#63) 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Map as ImmutableMap, fromJS } from 'immutable';

import { me } from 'mastodon/initial_state';

import {
  ACCOUNT_FOLLOW_SUCCESS,
  ACCOUNT_UNFOLLOW_SUCCESS,
} from '../actions/accounts';
import { ACCOUNT_IMPORT, ACCOUNTS_IMPORT } from '../actions/importer';

const normalizeAccount = (state, account) => state.set(account.id, fromJS({
  followers_count: account.followers_count,
  following_count: account.following_count,
  statuses_count: account.statuses_count,
}));

const normalizeAccounts = (state, accounts) => {
  accounts.forEach(account => {
    state = normalizeAccount(state, account);
  });

  return state;
};

const incrementFollowers = (state, accountId) =>
  state.updateIn([accountId, 'followers_count'], num => num + 1)
    .updateIn([me, 'following_count'], num => num + 1);

const decrementFollowers = (state, accountId) =>
  state.updateIn([accountId, 'followers_count'], num => Math.max(0, num - 1))
    .updateIn([me, 'following_count'], num => Math.max(0, num - 1));

const initialState = ImmutableMap();

export default function accountsCounters(state = initialState, action) {
  switch(action.type) {
  case ACCOUNT_IMPORT:
    return normalizeAccount(state, action.account);
  case ACCOUNTS_IMPORT:
    return normalizeAccounts(state, action.accounts);
  case ACCOUNT_FOLLOW_SUCCESS:
    return action.alreadyFollowing ? state :
      incrementFollowers(state, action.relationship.id);
  case ACCOUNT_UNFOLLOW_SUCCESS:
    return decrementFollowers(state, action.relationship.id);
  default:
    return state;
  }
}