pl-fe: relationship entities handling cleanup

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-09-28 22:34:50 +02:00
parent 93be391e23
commit 1d26e6f6aa
10 changed files with 29 additions and 405 deletions

View File

@ -1,42 +1,14 @@
import { importEntities } from 'pl-fe/entity-store/actions';
import { Entities } from 'pl-fe/entity-store/entities';
import { getClient } from '../api';
import type { RootState } from 'pl-fe/store';
import type { AnyAction } from 'redux';
const ACCOUNT_NOTE_SUBMIT_REQUEST = 'ACCOUNT_NOTE_SUBMIT_REQUEST' as const;
const ACCOUNT_NOTE_SUBMIT_SUCCESS = 'ACCOUNT_NOTE_SUBMIT_SUCCESS' as const;
const ACCOUNT_NOTE_SUBMIT_FAIL = 'ACCOUNT_NOTE_SUBMIT_FAIL' as const;
const submitAccountNote = (accountId: string, value: string) =>
(dispatch: React.Dispatch<AnyAction>, getState: () => RootState) => {
dispatch(submitAccountNoteRequest(accountId));
(dispatch: React.Dispatch<AnyAction>, getState: () => RootState) =>
getClient(getState).accounts.updateAccountNote(accountId, value)
.then(response => dispatch(importEntities([response], Entities.RELATIONSHIPS)));
return getClient(getState).accounts.updateAccountNote(accountId, value)
.then(response => {
dispatch(submitAccountNoteSuccess(response));
}).catch(error => dispatch(submitAccountNoteFail(accountId, error)));
};
const submitAccountNoteRequest = (accountId: string) => ({
type: ACCOUNT_NOTE_SUBMIT_REQUEST,
accountId,
});
const submitAccountNoteSuccess = (relationship: any) => ({
type: ACCOUNT_NOTE_SUBMIT_SUCCESS,
accountId: relationship.id,
relationship,
});
const submitAccountNoteFail = (accountId: string, error: unknown) => ({
type: ACCOUNT_NOTE_SUBMIT_FAIL,
accountId,
error,
});
export {
submitAccountNote,
ACCOUNT_NOTE_SUBMIT_REQUEST,
ACCOUNT_NOTE_SUBMIT_SUCCESS,
ACCOUNT_NOTE_SUBMIT_FAIL,
};
export { submitAccountNote };

View File

@ -26,30 +26,10 @@ const ACCOUNT_BLOCK_REQUEST = 'ACCOUNT_BLOCK_REQUEST' as const;
const ACCOUNT_BLOCK_SUCCESS = 'ACCOUNT_BLOCK_SUCCESS' as const;
const ACCOUNT_BLOCK_FAIL = 'ACCOUNT_BLOCK_FAIL' as const;
const ACCOUNT_UNBLOCK_REQUEST = 'ACCOUNT_UNBLOCK_REQUEST' as const;
const ACCOUNT_UNBLOCK_SUCCESS = 'ACCOUNT_UNBLOCK_SUCCESS' as const;
const ACCOUNT_UNBLOCK_FAIL = 'ACCOUNT_UNBLOCK_FAIL' as const;
const ACCOUNT_MUTE_REQUEST = 'ACCOUNT_MUTE_REQUEST' as const;
const ACCOUNT_MUTE_SUCCESS = 'ACCOUNT_MUTE_SUCCESS' as const;
const ACCOUNT_MUTE_FAIL = 'ACCOUNT_MUTE_FAIL' as const;
const ACCOUNT_UNMUTE_REQUEST = 'ACCOUNT_UNMUTE_REQUEST' as const;
const ACCOUNT_UNMUTE_SUCCESS = 'ACCOUNT_UNMUTE_SUCCESS' as const;
const ACCOUNT_UNMUTE_FAIL = 'ACCOUNT_UNMUTE_FAIL' as const;
const ACCOUNT_PIN_REQUEST = 'ACCOUNT_PIN_REQUEST' as const;
const ACCOUNT_PIN_SUCCESS = 'ACCOUNT_PIN_SUCCESS' as const;
const ACCOUNT_PIN_FAIL = 'ACCOUNT_PIN_FAIL' as const;
const ACCOUNT_UNPIN_REQUEST = 'ACCOUNT_UNPIN_REQUEST' as const;
const ACCOUNT_UNPIN_SUCCESS = 'ACCOUNT_UNPIN_SUCCESS' as const;
const ACCOUNT_UNPIN_FAIL = 'ACCOUNT_UNPIN_FAIL' as const;
const ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST = 'ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST' as const;
const ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS = 'ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS' as const;
const ACCOUNT_REMOVE_FROM_FOLLOWERS_FAIL = 'ACCOUNT_REMOVE_FROM_FOLLOWERS_FAIL' as const;
const PINNED_ACCOUNTS_FETCH_REQUEST = 'PINNED_ACCOUNTS_FETCH_REQUEST' as const;
const PINNED_ACCOUNTS_FETCH_SUCCESS = 'PINNED_ACCOUNTS_FETCH_SUCCESS' as const;
const PINNED_ACCOUNTS_FETCH_FAIL = 'PINNED_ACCOUNTS_FETCH_FAIL' as const;
@ -62,10 +42,6 @@ const ACCOUNT_LOOKUP_REQUEST = 'ACCOUNT_LOOKUP_REQUEST' as const;
const ACCOUNT_LOOKUP_SUCCESS = 'ACCOUNT_LOOKUP_SUCCESS' as const;
const ACCOUNT_LOOKUP_FAIL = 'ACCOUNT_LOOKUP_FAIL' as const;
const RELATIONSHIPS_FETCH_REQUEST = 'RELATIONSHIPS_FETCH_REQUEST' as const;
const RELATIONSHIPS_FETCH_SUCCESS = 'RELATIONSHIPS_FETCH_SUCCESS' as const;
const RELATIONSHIPS_FETCH_FAIL = 'RELATIONSHIPS_FETCH_FAIL' as const;
const FOLLOW_REQUESTS_FETCH_REQUEST = 'FOLLOW_REQUESTS_FETCH_REQUEST' as const;
const FOLLOW_REQUESTS_FETCH_SUCCESS = 'FOLLOW_REQUESTS_FETCH_SUCCESS' as const;
const FOLLOW_REQUESTS_FETCH_FAIL = 'FOLLOW_REQUESTS_FETCH_FAIL' as const;
@ -90,10 +66,6 @@ const BIRTHDAY_REMINDERS_FETCH_REQUEST = 'BIRTHDAY_REMINDERS_FETCH_REQUEST' as c
const BIRTHDAY_REMINDERS_FETCH_SUCCESS = 'BIRTHDAY_REMINDERS_FETCH_SUCCESS' as const;
const BIRTHDAY_REMINDERS_FETCH_FAIL = 'BIRTHDAY_REMINDERS_FETCH_FAIL' as const;
const ACCOUNT_BITE_REQUEST = 'ACCOUNT_BITE_REQUEST' as const;
const ACCOUNT_BITE_SUCCESS = 'ACCOUNT_BITE_SUCCESS' as const;
const ACCOUNT_BITE_FAIL = 'ACCOUNT_BITE_FAIL' as const;
const maybeRedirectLogin = (error: { response: PlfeResponse }, history?: History) => {
// The client is unauthorized - redirect to login.
if (history && error?.response?.status === 401) {
@ -208,14 +180,10 @@ const unblockAccount = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return null;
dispatch(unblockAccountRequest(accountId));
return getClient(getState).filtering.unblockAccount(accountId)
.then(response => {
dispatch(importEntities([response], Entities.RELATIONSHIPS));
return dispatch(unblockAccountSuccess(response));
})
.catch(error => dispatch(unblockAccountFail(error)));
});
};
const blockAccountRequest = (accountId: string) => ({
@ -234,21 +202,6 @@ const blockAccountFail = (error: unknown) => ({
error,
});
const unblockAccountRequest = (accountId: string) => ({
type: ACCOUNT_UNBLOCK_REQUEST,
accountId,
});
const unblockAccountSuccess = (relationship: Relationship) => ({
type: ACCOUNT_UNBLOCK_SUCCESS,
relationship,
});
const unblockAccountFail = (error: unknown) => ({
type: ACCOUNT_UNBLOCK_FAIL,
error,
});
const muteAccount = (accountId: string, notifications?: boolean, duration = 0) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return null;
@ -284,14 +237,8 @@ const unmuteAccount = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return null;
dispatch(unmuteAccountRequest(accountId));
return getClient(getState()).filtering.unmuteAccount(accountId)
.then(response => {
dispatch(importEntities([response], Entities.RELATIONSHIPS));
return dispatch(unmuteAccountSuccess(response));
})
.catch(error => dispatch(unmuteAccountFail(accountId, error)));
.then(response => dispatch(importEntities([response], Entities.RELATIONSHIPS)));
};
const muteAccountRequest = (accountId: string) => ({
@ -311,85 +258,29 @@ const muteAccountFail = (accountId: string, error: unknown) => ({
error,
});
const unmuteAccountRequest = (accountId: string) => ({
type: ACCOUNT_UNMUTE_REQUEST,
accountId,
});
const unmuteAccountSuccess = (relationship: Relationship) => ({
type: ACCOUNT_UNMUTE_SUCCESS,
relationship,
});
const unmuteAccountFail = (accountId: string, error: unknown) => ({
type: ACCOUNT_UNMUTE_FAIL,
accountId,
error,
});
const removeFromFollowers = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return null;
dispatch(removeFromFollowersRequest(accountId));
return getClient(getState()).accounts.removeAccountFromFollowers(accountId)
.then(response => dispatch(removeFromFollowersSuccess(response)))
.catch(error => dispatch(removeFromFollowersFail(accountId, error)));
.then(response => dispatch(importEntities([response], Entities.RELATIONSHIPS)));
};
const removeFromFollowersRequest = (accountId: string) => ({
type: ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST,
accountId,
});
const removeFromFollowersSuccess = (relationship: Relationship) => ({
type: ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS,
relationship,
});
const removeFromFollowersFail = (accountId: string, error: unknown) => ({
type: ACCOUNT_REMOVE_FROM_FOLLOWERS_FAIL,
accountId,
error,
});
const fetchRelationships = (accountIds: string[]) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return null;
const loadedRelationships = getState().relationships;
const newAccountIds = accountIds.filter(id => loadedRelationships.get(id, null) === null);
const loadedRelationships = getState().entities[Entities.RELATIONSHIPS]?.store;
const newAccountIds = accountIds.filter(id => !loadedRelationships?.[id]);
if (newAccountIds.length === 0) {
return null;
}
dispatch(fetchRelationshipsRequest(newAccountIds));
return getClient(getState()).accounts.getRelationships(newAccountIds)
.then(response => {
dispatch(importEntities(response, Entities.RELATIONSHIPS));
dispatch(fetchRelationshipsSuccess(response));
})
.catch(error => dispatch(fetchRelationshipsFail(error)));
.then(response => dispatch(importEntities(response, Entities.RELATIONSHIPS)));
};
const fetchRelationshipsRequest = (accountIds: string[]) => ({
type: RELATIONSHIPS_FETCH_REQUEST,
accountIds,
});
const fetchRelationshipsSuccess = (relationships: Array<Relationship>) => ({
type: RELATIONSHIPS_FETCH_SUCCESS,
relationships,
});
const fetchRelationshipsFail = (error: unknown) => ({
type: RELATIONSHIPS_FETCH_FAIL,
error,
});
const fetchFollowRequests = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return null;
@ -508,26 +399,18 @@ const pinAccount = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return dispatch(noOp);
dispatch(pinAccountRequest(accountId));
return getClient(getState).accounts.pinAccount(accountId).then(response => {
dispatch(pinAccountSuccess(response));
}).catch(error => {
dispatch(pinAccountFail(error));
});
return getClient(getState).accounts.pinAccount(accountId).then(response =>
dispatch(importEntities([response], Entities.RELATIONSHIPS)),
);
};
const unpinAccount = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return dispatch(noOp);
dispatch(unpinAccountRequest(accountId));
return getClient(getState).accounts.unpinAccount(accountId).then(response => {
dispatch(unpinAccountSuccess(response));
}).catch(error => {
dispatch(unpinAccountFail(error));
});
return getClient(getState).accounts.unpinAccount(accountId).then(response =>
dispatch(importEntities([response], Entities.RELATIONSHIPS)),
);
};
const updateNotificationSettings = (params: UpdateNotificationSettingsParams) =>
@ -541,36 +424,6 @@ const updateNotificationSettings = (params: UpdateNotificationSettingsParams) =>
});
};
const pinAccountRequest = (accountId: string) => ({
type: ACCOUNT_PIN_REQUEST,
accountId,
});
const pinAccountSuccess = (relationship: Relationship) => ({
type: ACCOUNT_PIN_SUCCESS,
relationship,
});
const pinAccountFail = (error: unknown) => ({
type: ACCOUNT_PIN_FAIL,
error,
});
const unpinAccountRequest = (accountId: string) => ({
type: ACCOUNT_UNPIN_REQUEST,
accountId,
});
const unpinAccountSuccess = (relationship: Relationship) => ({
type: ACCOUNT_UNPIN_SUCCESS,
relationship,
});
const unpinAccountFail = (error: unknown) => ({
type: ACCOUNT_UNPIN_FAIL,
error,
});
const fetchPinnedAccounts = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch(fetchPinnedAccountsRequest(accountId));
@ -653,33 +506,9 @@ const biteAccount = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const client = getClient(getState);
dispatch(biteAccountRequest(accountId));
return client.accounts.biteAccount(accountId)
.then(() => {
return dispatch(biteAccountSuccess(accountId));
})
.catch(error => {
dispatch(biteAccountFail(accountId, error));
throw error;
});
return client.accounts.biteAccount(accountId);
};
const biteAccountRequest = (accountId: string) => ({
type: ACCOUNT_BITE_REQUEST,
accountId,
});
const biteAccountSuccess = (accountId: string) => ({
type: ACCOUNT_BITE_SUCCESS,
});
const biteAccountFail = (accountId: string, error: unknown) => ({
type: ACCOUNT_BITE_FAIL,
accountId,
error,
});
export {
ACCOUNT_CREATE_REQUEST,
ACCOUNT_CREATE_SUCCESS,
@ -690,24 +519,9 @@ export {
ACCOUNT_BLOCK_REQUEST,
ACCOUNT_BLOCK_SUCCESS,
ACCOUNT_BLOCK_FAIL,
ACCOUNT_UNBLOCK_REQUEST,
ACCOUNT_UNBLOCK_SUCCESS,
ACCOUNT_UNBLOCK_FAIL,
ACCOUNT_MUTE_REQUEST,
ACCOUNT_MUTE_SUCCESS,
ACCOUNT_MUTE_FAIL,
ACCOUNT_UNMUTE_REQUEST,
ACCOUNT_UNMUTE_SUCCESS,
ACCOUNT_UNMUTE_FAIL,
ACCOUNT_PIN_REQUEST,
ACCOUNT_PIN_SUCCESS,
ACCOUNT_PIN_FAIL,
ACCOUNT_UNPIN_REQUEST,
ACCOUNT_UNPIN_SUCCESS,
ACCOUNT_UNPIN_FAIL,
ACCOUNT_REMOVE_FROM_FOLLOWERS_REQUEST,
ACCOUNT_REMOVE_FROM_FOLLOWERS_SUCCESS,
ACCOUNT_REMOVE_FROM_FOLLOWERS_FAIL,
PINNED_ACCOUNTS_FETCH_REQUEST,
PINNED_ACCOUNTS_FETCH_SUCCESS,
PINNED_ACCOUNTS_FETCH_FAIL,
@ -717,9 +531,6 @@ export {
ACCOUNT_LOOKUP_REQUEST,
ACCOUNT_LOOKUP_SUCCESS,
ACCOUNT_LOOKUP_FAIL,
RELATIONSHIPS_FETCH_REQUEST,
RELATIONSHIPS_FETCH_SUCCESS,
RELATIONSHIPS_FETCH_FAIL,
FOLLOW_REQUESTS_FETCH_REQUEST,
FOLLOW_REQUESTS_FETCH_SUCCESS,
FOLLOW_REQUESTS_FETCH_FAIL,
@ -738,9 +549,6 @@ export {
BIRTHDAY_REMINDERS_FETCH_REQUEST,
BIRTHDAY_REMINDERS_FETCH_SUCCESS,
BIRTHDAY_REMINDERS_FETCH_FAIL,
ACCOUNT_BITE_REQUEST,
ACCOUNT_BITE_SUCCESS,
ACCOUNT_BITE_FAIL,
createAccount,
fetchAccount,
fetchAccountByUsername,
@ -752,25 +560,13 @@ export {
blockAccountRequest,
blockAccountSuccess,
blockAccountFail,
unblockAccountRequest,
unblockAccountSuccess,
unblockAccountFail,
muteAccount,
unmuteAccount,
muteAccountRequest,
muteAccountSuccess,
muteAccountFail,
unmuteAccountRequest,
unmuteAccountSuccess,
unmuteAccountFail,
removeFromFollowers,
removeFromFollowersRequest,
removeFromFollowersSuccess,
removeFromFollowersFail,
fetchRelationships,
fetchRelationshipsRequest,
fetchRelationshipsSuccess,
fetchRelationshipsFail,
fetchFollowRequests,
fetchFollowRequestsRequest,
fetchFollowRequestsSuccess,
@ -790,12 +586,6 @@ export {
pinAccount,
unpinAccount,
updateNotificationSettings,
pinAccountRequest,
pinAccountSuccess,
pinAccountFail,
unpinAccountRequest,
unpinAccountSuccess,
unpinAccountFail,
fetchPinnedAccounts,
fetchPinnedAccountsRequest,
fetchPinnedAccountsSuccess,
@ -804,7 +594,4 @@ export {
accountLookup,
fetchBirthdayReminders,
biteAccount,
biteAccountRequest,
biteAccountSuccess,
biteAccountFail,
};

View File

@ -31,6 +31,7 @@ const blockDomain = (domain: string) =>
dispatch(blockDomainRequest(domain));
return getClient(getState).filtering.blockDomain(domain).then(() => {
// TODO: Update relationships on block
const accounts = selectAccountsByDomain(getState(), domain);
if (!accounts) return;
dispatch(blockDomainSuccess(domain, accounts));
@ -63,6 +64,7 @@ const unblockDomain = (domain: string) =>
dispatch(unblockDomainRequest(domain));
return getClient(getState).filtering.unblockDomain(domain).then(() => {
// TODO: Update relationships on unblock
const accounts = selectAccountsByDomain(getState(), domain);
if (!accounts) return;
dispatch(unblockDomainSuccess(domain, accounts));

View File

@ -5,8 +5,6 @@ import { normalizeAccount, normalizeGroup } from 'pl-fe/normalizers';
import type { Account as BaseAccount, Group, Poll, Status as BaseStatus } from 'pl-api';
import type { AppDispatch } from 'pl-fe/store';
const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT';
const ACCOUNTS_IMPORT = 'ACCOUNTS_IMPORT';
const STATUS_IMPORT = 'STATUS_IMPORT';
const STATUSES_IMPORT = 'STATUSES_IMPORT';
const POLLS_IMPORT = 'POLLS_IMPORT';
@ -14,10 +12,12 @@ const POLLS_IMPORT = 'POLLS_IMPORT';
const importAccount = (data: BaseAccount) => importAccounts([data]);
const importAccounts = (data: Array<BaseAccount>) => (dispatch: AppDispatch) => {
dispatch({ type: ACCOUNTS_IMPORT, accounts: data });
try {
const accounts = data.map(normalizeAccount);
const relationships = accounts.map(account => account.relationship).filter(relationship => !!relationship);
dispatch(importEntities(accounts, Entities.ACCOUNTS));
dispatch(importEntities(relationships, Entities.RELATIONSHIPS));
} catch (e) {
//
}
@ -153,8 +153,6 @@ const importFetchedPoll = (poll: Poll) =>
};
export {
ACCOUNT_IMPORT,
ACCOUNTS_IMPORT,
STATUS_IMPORT,
STATUSES_IMPORT,
POLLS_IMPORT,

View File

@ -34,7 +34,7 @@ const AccountTimeline: React.FC<IAccountTimeline> = ({ params, withReplies = fal
const statusIds = useAppSelector(state => getStatusIds(state, { type: `account:${path}`, prefix: 'account_timeline' }));
const featuredStatusIds = useAppSelector(state => getStatusIds(state, { type: `account:${account?.id}:with_replies:pinned`, prefix: 'account_timeline' }));
const isBlocked = useAppSelector(state => state.relationships.getIn([account?.id, 'blocked_by']) === true);
const isBlocked = account?.relationship?.blocked_by;
const unavailable = isBlocked && !features.blockersVisible;
const isLoading = useAppSelector(state => state.timelines.get(`account:${path}`)?.isLoading === true);
const hasMore = useAppSelector(state => state.timelines.get(`account:${path}`)?.hasMore === true);

View File

@ -1,22 +1,14 @@
import { useMutation } from '@tanstack/react-query';
import { fetchRelationshipsFail, fetchRelationshipsSuccess } from 'pl-fe/actions/accounts';
import { useAppDispatch, useClient } from 'pl-fe/hooks';
import { useClient } from 'pl-fe/hooks';
const useFetchRelationships = () => {
const client = useClient();
const dispatch = useAppDispatch();
return useMutation({
mutationFn: ({ accountIds }: { accountIds: string[]}) => {
return client.accounts.getRelationships(accountIds);
},
onSuccess(response) {
dispatch(fetchRelationshipsSuccess(response));
},
onError(error) {
dispatch(fetchRelationshipsFail(error));
},
});
};

View File

@ -35,7 +35,6 @@ import plfe from './pl-fe';
import polls from './polls';
import profile_hover_card from './profile-hover-card';
import push_notifications from './push-notifications';
import relationships from './relationships';
import scheduled_statuses from './scheduled-statuses';
import search from './search';
import security from './security';
@ -83,7 +82,6 @@ const reducers = {
polls,
profile_hover_card,
push_notifications,
relationships,
scheduled_statuses,
search,
security,

View File

@ -1,40 +0,0 @@
import { Map as ImmutableMap } from 'immutable';
import lain from 'pl-fe/__fixtures__/lain.json';
import { ACCOUNT_IMPORT } from 'pl-fe/actions/importer';
import reducer from './relationships';
describe('relationships reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {} as any)).toEqual(ImmutableMap());
});
describe('ACCOUNT_IMPORT', () => {
it('should import the relationship', () => {
const action = {
type: ACCOUNT_IMPORT,
account: lain,
};
const state = ImmutableMap<string, any>();
expect(reducer(state, action).toJS()).toEqual({
'9v5bqYwY2jfmvPNhTM': {
blocked_by: false,
blocking: false,
domain_blocking: false,
endorsed: false,
followed_by: true,
following: true,
id: '9v5bqYwY2jfmvPNhTM',
muting: false,
muting_notifications: false,
note: '',
notifying: false,
requested: false,
showing_reblogs: true,
subscribing: false,
},
});
});
});
});

View File

@ -1,85 +0,0 @@
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 };

View File

@ -13,7 +13,7 @@ import { validId } from 'pl-fe/utils/auth';
import ConfigDB from 'pl-fe/utils/config-db';
import { shouldFilter } from 'pl-fe/utils/timelines';
import type { Account as BaseAccount, Filter, MediaAttachment } from 'pl-api';
import type { Account as BaseAccount, Filter, MediaAttachment, Relationship } from 'pl-api';
import type { EntityStore } from 'pl-fe/entity-store/types';
import type { Account, Group, Notification } from 'pl-fe/normalizers';
import type { MinifiedNotification } from 'pl-fe/reducers/notifications';
@ -34,7 +34,7 @@ const selectOwnAccount = (state: RootState) => {
};
const getAccountBase = (state: RootState, accountId: string) => state.entities[Entities.ACCOUNTS]?.store[accountId] as Account | undefined;
const getAccountRelationship = (state: RootState, accountId: string) => state.relationships.get(accountId);
const getAccountRelationship = (state: RootState, accountId: string) => state.entities[Entities.RELATIONSHIPS]?.store[accountId] as Relationship | undefined;
const getAccountMeta = (state: RootState, accountId: string) => state.accounts_meta[accountId];
const makeGetAccount = () => createSelector([