2023-06-20 12:24:39 -07:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
2022-06-16 12:32:17 -07:00
|
|
|
import get from 'lodash/get';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-10 14:01:24 -08:00
|
|
|
import { STREAMING_FOLLOW_RELATIONSHIPS_UPDATE } from 'soapbox/actions/streaming';
|
2023-05-02 16:49:13 -07:00
|
|
|
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-05-27 11:08:41 -07:00
|
|
|
import { ACCOUNT_NOTE_SUBMIT_SUCCESS } from '../actions/account-notes';
|
2020-03-27 13:59:38 -07:00
|
|
|
import {
|
|
|
|
ACCOUNT_FOLLOW_SUCCESS,
|
|
|
|
ACCOUNT_FOLLOW_REQUEST,
|
|
|
|
ACCOUNT_FOLLOW_FAIL,
|
|
|
|
ACCOUNT_UNFOLLOW_SUCCESS,
|
|
|
|
ACCOUNT_UNFOLLOW_REQUEST,
|
|
|
|
ACCOUNT_UNFOLLOW_FAIL,
|
|
|
|
ACCOUNT_BLOCK_SUCCESS,
|
|
|
|
ACCOUNT_UNBLOCK_SUCCESS,
|
|
|
|
ACCOUNT_MUTE_SUCCESS,
|
|
|
|
ACCOUNT_UNMUTE_SUCCESS,
|
2021-06-26 10:27:53 -07:00
|
|
|
ACCOUNT_SUBSCRIBE_SUCCESS,
|
|
|
|
ACCOUNT_UNSUBSCRIBE_SUCCESS,
|
2020-03-27 13:59:38 -07:00
|
|
|
ACCOUNT_PIN_SUCCESS,
|
|
|
|
ACCOUNT_UNPIN_SUCCESS,
|
2022-05-17 04:05:01 -07:00
|
|
|
ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS,
|
2020-03-27 13:59:38 -07:00
|
|
|
RELATIONSHIPS_FETCH_SUCCESS,
|
|
|
|
} from '../actions/accounts';
|
|
|
|
import {
|
|
|
|
DOMAIN_BLOCK_SUCCESS,
|
|
|
|
DOMAIN_UNBLOCK_SUCCESS,
|
2022-11-16 05:32:32 -08:00
|
|
|
} from '../actions/domain-blocks';
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
ACCOUNT_IMPORT,
|
|
|
|
ACCOUNTS_IMPORT,
|
|
|
|
} from '../actions/importer';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
2022-12-11 12:37:00 -08:00
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
type State = ImmutableMap<string, Relationship>;
|
|
|
|
type APIEntities = Array<APIEntity>;
|
|
|
|
|
|
|
|
const normalizeRelationships = (state: State, relationships: APIEntities) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
relationships.forEach(relationship => {
|
2023-05-02 16:49:13 -07:00
|
|
|
try {
|
|
|
|
state = state.set(relationship.id, relationshipSchema.parse(relationship));
|
|
|
|
} catch (_e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2023-06-20 12:24:39 -07:00
|
|
|
const setDomainBlocking = (state: State, accounts: string[], blocking: boolean) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
return state.withMutations(map => {
|
|
|
|
accounts.forEach(id => {
|
|
|
|
map.setIn([id, 'domain_blocking'], blocking);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
const importPleromaAccount = (state: State, account: APIEntity) => {
|
2020-08-02 19:45:42 -07:00
|
|
|
const relationship = get(account, ['pleroma', 'relationship'], {});
|
2023-01-29 14:41:48 -08:00
|
|
|
if (relationship.id)
|
2022-06-04 00:22:36 -07:00
|
|
|
return normalizeRelationships(state, [relationship]);
|
2020-08-02 19:45:42 -07:00
|
|
|
return state;
|
2020-07-04 18:15:20 -07:00
|
|
|
};
|
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
const importPleromaAccounts = (state: State, accounts: APIEntities) => {
|
2020-07-04 18:15:20 -07:00
|
|
|
accounts.forEach(account => {
|
|
|
|
state = importPleromaAccount(state, account);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
const followStateToRelationship = (followState: string) => {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (followState) {
|
2022-05-11 14:06:35 -07:00
|
|
|
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 {};
|
2021-01-28 14:22:31 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
const updateFollowRelationship = (state: State, id: string, followState: string) => {
|
2023-05-02 16:49:13 -07:00
|
|
|
const relationship = state.get(id) || relationshipSchema.parse({ id });
|
|
|
|
|
|
|
|
return state.set(id, {
|
|
|
|
...relationship,
|
|
|
|
...followStateToRelationship(followState),
|
|
|
|
});
|
2021-01-28 14:22:31 -08:00
|
|
|
};
|
|
|
|
|
2022-06-04 00:22:36 -07:00
|
|
|
export default function relationships(state: State = ImmutableMap<string, Relationship>(), action: AnyAction) {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case ACCOUNT_IMPORT:
|
|
|
|
return importPleromaAccount(state, action.account);
|
|
|
|
case ACCOUNTS_IMPORT:
|
|
|
|
return importPleromaAccounts(state, action.accounts);
|
|
|
|
case ACCOUNT_FOLLOW_REQUEST:
|
|
|
|
return state.setIn([action.id, 'following'], true);
|
|
|
|
case ACCOUNT_FOLLOW_FAIL:
|
|
|
|
return state.setIn([action.id, 'following'], false);
|
|
|
|
case ACCOUNT_UNFOLLOW_REQUEST:
|
|
|
|
return state.setIn([action.id, 'following'], false);
|
|
|
|
case ACCOUNT_UNFOLLOW_FAIL:
|
|
|
|
return state.setIn([action.id, 'following'], true);
|
|
|
|
case ACCOUNT_FOLLOW_SUCCESS:
|
|
|
|
case ACCOUNT_UNFOLLOW_SUCCESS:
|
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
|
|
|
case ACCOUNT_UNBLOCK_SUCCESS:
|
|
|
|
case ACCOUNT_MUTE_SUCCESS:
|
|
|
|
case ACCOUNT_UNMUTE_SUCCESS:
|
|
|
|
case ACCOUNT_SUBSCRIBE_SUCCESS:
|
|
|
|
case ACCOUNT_UNSUBSCRIBE_SUCCESS:
|
|
|
|
case ACCOUNT_PIN_SUCCESS:
|
|
|
|
case ACCOUNT_UNPIN_SUCCESS:
|
|
|
|
case ACCOUNT_NOTE_SUBMIT_SUCCESS:
|
2022-05-17 04:05:01 -07:00
|
|
|
case ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS:
|
2022-06-04 00:22:36 -07:00
|
|
|
return normalizeRelationships(state, [action.relationship]);
|
2022-05-11 14:06:35 -07:00
|
|
|
case RELATIONSHIPS_FETCH_SUCCESS:
|
|
|
|
return normalizeRelationships(state, action.relationships);
|
|
|
|
case DOMAIN_BLOCK_SUCCESS:
|
|
|
|
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:
|
2021-01-28 14:22:31 -08:00
|
|
|
return state;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|