pleroma/app/soapbox/reducers/relationships.ts

96 lines
2.8 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';
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_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;
};
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_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);
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
}