pleroma/packages/pl-fe/src/reducers/relationships.ts
marcin mikołajczak 0ddf6f2768 pl-fe: lint
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-08-28 14:48:35 +02:00

85 lines
2.7 KiB
TypeScript

import { Map as ImmutableMap } from 'immutable';
import get from 'lodash/get';
import { type Relationship, relationshipSchema } from 'pl-api';
import { ACCOUNT_NOTE_SUBMIT_SUCCESS } from '../actions/account-notes';
import {
ACCOUNT_BLOCK_SUCCESS,
ACCOUNT_UNBLOCK_SUCCESS,
ACCOUNT_MUTE_SUCCESS,
ACCOUNT_UNMUTE_SUCCESS,
ACCOUNT_PIN_SUCCESS,
ACCOUNT_UNPIN_SUCCESS,
ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS,
RELATIONSHIPS_FETCH_SUCCESS,
} from '../actions/accounts';
import { DOMAIN_BLOCK_SUCCESS, DOMAIN_UNBLOCK_SUCCESS } from '../actions/domain-blocks';
import { ACCOUNT_IMPORT, ACCOUNTS_IMPORT } from '../actions/importer';
import type { APIEntity } from 'pl-fe/types/entities';
import type { AnyAction } from 'redux';
type State = ImmutableMap<string, Relationship>;
type APIEntities = Array<APIEntity>;
const normalizeRelationships = (state: State, relationships: APIEntities) => {
relationships.forEach(relationship => {
try {
state = state.set(relationship.id, relationshipSchema.parse(relationship));
} catch (_e) {
// do nothing
}
});
return state;
};
const setDomainBlocking = (state: State, accounts: string[], blocking: boolean) =>
state.withMutations(map => {
accounts.forEach(id => {
map.setIn([id, 'domain_blocking'], blocking);
});
});
const importPleromaAccount = (state: State, account: APIEntity) => {
const relationship = get(account, ['pleroma', 'relationship'], {});
if (relationship.id)
return normalizeRelationships(state, [relationship]);
return state;
};
const importPleromaAccounts = (state: State, accounts: APIEntities) => {
accounts.forEach(account => {
state = importPleromaAccount(state, account);
});
return state;
};
const relationships = (state: State = ImmutableMap<string, Relationship>(), action: AnyAction) => {
switch (action.type) {
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_PIN_SUCCESS:
case ACCOUNT_UNPIN_SUCCESS:
case ACCOUNT_NOTE_SUBMIT_SUCCESS:
case ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS:
return normalizeRelationships(state, [action.relationship]);
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:
return state;
}
};
export { relationships as default };