Stream following relationships

This commit is contained in:
Alex Gleason 2021-01-28 16:22:31 -06:00
parent e1d77a7012
commit 36f6f12856
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 54 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import { getSettings } from 'soapbox/actions/settings';
import messages from 'soapbox/locales/messages';
export const STREAMING_CHAT_UPDATE = 'STREAMING_CHAT_UPDATE';
export const STREAMING_FOLLOW_RELATIONSHIPS_UPDATE = 'STREAMING_FOLLOW_RELATIONSHIPS_UPDATE';
const validLocale = locale => Object.keys(messages).includes(locale);
@ -21,6 +22,17 @@ const getLocale = state => {
return validLocale(locale) ? locale : 'en';
};
function updateFollowRelationships(relationships) {
return (dispatch, getState) => {
const me = getState().get('me');
return dispatch({
type: STREAMING_FOLLOW_RELATIONSHIPS_UPDATE,
me,
...relationships,
});
};
}
export function connectTimelineStream(timelineId, path, pollingRefresh = null, accept = null) {
return connectStream (path, pollingRefresh, (dispatch, getState) => {
@ -69,6 +81,9 @@ export function connectTimelineStream(timelineId, path, pollingRefresh = null, a
});
});
break;
case 'pleroma:follow_relationships_update':
dispatch(updateFollowRelationships(JSON.parse(data.payload)));
break;
}
},
};

View File

@ -3,6 +3,7 @@ import {
ACCOUNT_UNFOLLOW_SUCCESS,
} from '../actions/accounts';
import { ACCOUNT_IMPORT, ACCOUNTS_IMPORT } from '../actions/importer';
import { STREAMING_FOLLOW_RELATIONSHIPS_UPDATE } from 'soapbox/actions/streaming';
import { Map as ImmutableMap, fromJS } from 'immutable';
const normalizeAccount = (state, account) => state.set(account.id, fromJS({
@ -19,6 +20,17 @@ const normalizeAccounts = (state, accounts) => {
return state;
};
const updateFollowCounters = (state, counterUpdates) => {
return state.withMutations(state => {
counterUpdates.forEach(counterUpdate => {
state.update(counterUpdate.id, ImmutableMap(), counters => counters.merge({
followers_count: counterUpdate.follower_count,
following_count: counterUpdate.following_count,
}));
});
});
};
const initialState = ImmutableMap();
export default function accountsCounters(state = initialState, action) {
@ -32,6 +44,8 @@ export default function accountsCounters(state = initialState, action) {
state.updateIn([action.relationship.id, 'followers_count'], num => num + 1);
case ACCOUNT_UNFOLLOW_SUCCESS:
return state.updateIn([action.relationship.id, 'followers_count'], num => Math.max(0, num - 1));
case STREAMING_FOLLOW_RELATIONSHIPS_UPDATE:
return updateFollowCounters(state, [action.follower, action.following]);
default:
return state;
}

View File

@ -21,6 +21,7 @@ import {
DOMAIN_BLOCK_SUCCESS,
DOMAIN_UNBLOCK_SUCCESS,
} from '../actions/domain_blocks';
import { STREAMING_FOLLOW_RELATIONSHIPS_UPDATE } from 'soapbox/actions/streaming';
import { Map as ImmutableMap, fromJS } from 'immutable';
import { get } from 'lodash';
@ -57,6 +58,24 @@ const importPleromaAccounts = (state, accounts) => {
return state;
};
const followStateToRelationship = followState => {
switch(followState) {
case 'follow_pending':
return { following: false, requested: true };
case 'follow_accept':
return { following: true, requested: false };
case 'follow_reject':
return { following: false, requested: false };
default:
return {};
}
};
const updateFollowRelationship = (state, id, followState) => {
const map = followStateToRelationship(followState);
return state.update(id, ImmutableMap(), relationship => relationship.merge(map));
};
const initialState = ImmutableMap();
export default function relationships(state = initialState, action) {
@ -88,6 +107,12 @@ export default function relationships(state = initialState, action) {
return setDomainBlocking(state, action.accounts, true);
case DOMAIN_UNBLOCK_SUCCESS:
return setDomainBlocking(state, action.accounts, false);
case STREAMING_FOLLOW_RELATIONSHIPS_UPDATE:
if (action.follower.id === action.me) {
return updateFollowRelationship(state, action.following.id, action.state);
} else {
return state;
}
default:
return state;
}