bigbuffet-rw/app/soapbox/reducers/relationships.ts

141 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-06-20 12:24:39 -07:00
import { Map as ImmutableMap } from 'immutable';
import get from 'lodash/get';
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-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,
ACCOUNT_SUBSCRIBE_SUCCESS,
ACCOUNT_UNSUBSCRIBE_SUCCESS,
2020-03-27 13:59:38 -07:00
ACCOUNT_PIN_SUCCESS,
ACCOUNT_UNPIN_SUCCESS,
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';
import {
ACCOUNT_IMPORT,
ACCOUNTS_IMPORT,
} from '../actions/importer';
2020-03-27 13:59:38 -07:00
import type { AnyAction } from 'redux';
import type { APIEntity } from 'soapbox/types/entities';
2020-03-27 13:59:38 -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);
});
});
};
const importPleromaAccount = (state: State, account: APIEntity) => {
2020-08-02 19:45:42 -07:00
const relationship = get(account, ['pleroma', 'relationship'], {});
if (relationship.id)
return normalizeRelationships(state, [relationship]);
2020-08-02 19:45:42 -07:00
return state;
};
const importPleromaAccounts = (state: State, accounts: APIEntities) => {
accounts.forEach(account => {
state = importPleromaAccount(state, account);
});
return state;
};
const followStateToRelationship = (followState: string) => {
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
}
};
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
};
export default function relationships(state: State = ImmutableMap<string, Relationship>(), action: AnyAction) {
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:
case ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS:
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
}