3 FOLLOWERS_FETCH_SUCCESS
,
4 FOLLOWERS_EXPAND_SUCCESS
,
5 FOLLOWING_FETCH_SUCCESS
,
6 FOLLOWING_EXPAND_SUCCESS
,
7 FOLLOW_REQUESTS_FETCH_SUCCESS
,
8 FOLLOW_REQUESTS_EXPAND_SUCCESS
,
9 ACCOUNT_FOLLOW_SUCCESS
,
10 ACCOUNT_UNFOLLOW_SUCCESS
,
11 } from '../actions/accounts';
14 BLOCKS_EXPAND_SUCCESS
,
15 } from '../actions/blocks';
19 } from '../actions/mutes';
20 import { COMPOSE_SUGGESTIONS_READY
} from '../actions/compose';
26 REBLOGS_FETCH_SUCCESS
,
27 FAVOURITES_FETCH_SUCCESS
,
28 } from '../actions/interactions';
30 TIMELINE_REFRESH_SUCCESS
,
32 TIMELINE_EXPAND_SUCCESS
,
33 } from '../actions/timelines';
36 CONTEXT_FETCH_SUCCESS
,
37 } from '../actions/statuses';
38 import { SEARCH_FETCH_SUCCESS
} from '../actions/search';
41 NOTIFICATIONS_REFRESH_SUCCESS
,
42 NOTIFICATIONS_EXPAND_SUCCESS
,
43 } from '../actions/notifications';
45 FAVOURITED_STATUSES_FETCH_SUCCESS
,
46 FAVOURITED_STATUSES_EXPAND_SUCCESS
,
47 } from '../actions/favourites';
48 import { STORE_HYDRATE
} from '../actions/store';
49 import { Map as ImmutableMap
, fromJS
} from 'immutable';
51 const normalizeAccount
= (state
, account
) => state
.set(account
.id
, fromJS({
52 followers_count: account
.followers_count
,
53 following_count: account
.following_count
,
54 statuses_count: account
.statuses_count
,
57 const normalizeAccounts
= (state
, accounts
) => {
58 accounts
.forEach(account
=> {
59 state
= normalizeAccount(state
, account
);
65 const normalizeAccountFromStatus
= (state
, status
) => {
66 state
= normalizeAccount(state
, status
.account
);
68 if (status
.reblog
&& status
.reblog
.account
) {
69 state
= normalizeAccount(state
, status
.reblog
.account
);
75 const normalizeAccountsFromStatuses
= (state
, statuses
) => {
76 statuses
.forEach(status
=> {
77 state
= normalizeAccountFromStatus(state
, status
);
83 const initialState
= ImmutableMap();
85 export default function accountsCounters(state
= initialState
, action
) {
88 return state
.merge(action
.state
.get('accounts').map(item
=> fromJS({
89 followers_count: item
.get('followers_count'),
90 following_count: item
.get('following_count'),
91 statuses_count: item
.get('statuses_count'),
93 case ACCOUNT_FETCH_SUCCESS:
94 case NOTIFICATIONS_UPDATE:
95 return normalizeAccount(state
, action
.account
);
96 case FOLLOWERS_FETCH_SUCCESS:
97 case FOLLOWERS_EXPAND_SUCCESS:
98 case FOLLOWING_FETCH_SUCCESS:
99 case FOLLOWING_EXPAND_SUCCESS:
100 case REBLOGS_FETCH_SUCCESS:
101 case FAVOURITES_FETCH_SUCCESS:
102 case COMPOSE_SUGGESTIONS_READY:
103 case FOLLOW_REQUESTS_FETCH_SUCCESS:
104 case FOLLOW_REQUESTS_EXPAND_SUCCESS:
105 case BLOCKS_FETCH_SUCCESS:
106 case BLOCKS_EXPAND_SUCCESS:
107 case MUTES_FETCH_SUCCESS:
108 case MUTES_EXPAND_SUCCESS:
109 return action
.accounts
? normalizeAccounts(state
, action
.accounts
) : state
;
110 case NOTIFICATIONS_REFRESH_SUCCESS:
111 case NOTIFICATIONS_EXPAND_SUCCESS:
112 case SEARCH_FETCH_SUCCESS:
113 return normalizeAccountsFromStatuses(normalizeAccounts(state
, action
.accounts
), action
.statuses
);
114 case TIMELINE_REFRESH_SUCCESS:
115 case TIMELINE_EXPAND_SUCCESS:
116 case CONTEXT_FETCH_SUCCESS:
117 case FAVOURITED_STATUSES_FETCH_SUCCESS:
118 case FAVOURITED_STATUSES_EXPAND_SUCCESS:
119 return normalizeAccountsFromStatuses(state
, action
.statuses
);
121 case FAVOURITE_SUCCESS:
122 case UNREBLOG_SUCCESS:
123 case UNFAVOURITE_SUCCESS:
124 return normalizeAccountFromStatus(state
, action
.response
);
125 case TIMELINE_UPDATE:
126 case STATUS_FETCH_SUCCESS:
127 return normalizeAccountFromStatus(state
, action
.status
);
128 case ACCOUNT_FOLLOW_SUCCESS:
129 return state
.updateIn([action
.relationship
.id
, 'followers_count'], num
=> num
+ 1);
130 case ACCOUNT_UNFOLLOW_SUCCESS:
131 return state
.updateIn([action
.relationship
.id
, 'followers_count'], num
=> Math
.max(0, num
- 1));