diff --git a/lib/actions/holder-actions.js b/lib/actions/holder-actions.js --- a/lib/actions/holder-actions.js +++ b/lib/actions/holder-actions.js @@ -132,6 +132,34 @@ return { added, notAdded, removed, notRemoved }; } +function useClearAllHolders(): () => Promise { + const dispatchActionPromise = useDispatchActionPromise(); + const identityContext = React.useContext(IdentityClientContext); + const getAuthMetadata = identityContext?.getAuthMetadata; + + const holdersToRemove = useSelector(state => + Object.entries(state.holderStore.storedHolders) + .filter(([, holderInfo]) => holderInfo.status !== 'PENDING_REMOVAL') + .map(([blobHash, { holder }]) => ({ blobHash, holder })), + ); + + return React.useCallback(async () => { + invariant(getAuthMetadata, 'Identity context not set'); + const authMetadata = await getAuthMetadata(); + const input = { + holdersToRemove, + holdersToAdd: [], + }; + const promise = processHoldersAction(input, authMetadata); + void dispatchActionPromise( + processHoldersActionTypes, + promise, + undefined, + input, + ); + }, [dispatchActionPromise, getAuthMetadata, holdersToRemove]); +} + function useProcessBlobHolders(): ( blobOperations: $ReadOnlyArray, ) => Promise { @@ -197,4 +225,4 @@ ); } -export { useProcessBlobHolders }; +export { useClearAllHolders, useProcessBlobHolders }; diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -3,6 +3,7 @@ import invariant from 'invariant'; import * as React from 'react'; +import { useClearAllHolders } from './holder-actions.js'; import { useUserIdentityCache } from '../components/user-identity-cache.react.js'; import { useBroadcastDeviceListUpdates, @@ -154,6 +155,8 @@ const preRequestUserState = usePreRequestUserState(); const callKeyserverLogOut = useKeyserverCall(keyserverLogOut); + const removeAllHolders = useClearAllHolders(); + const commServicesAccessToken = useSelector( state => state.commServicesAccessToken, ); @@ -161,6 +164,12 @@ const { logOutType, skipIdentityLogOut } = options; return React.useCallback( async (keyserverIDs?: $ReadOnlyArray) => { + const holdersPromise = (async () => { + try { + await removeAllHolders(); + } catch {} + })(); + const identityPromise = (async () => { if ( skipIdentityLogOut || @@ -203,6 +212,7 @@ preRequestUserState, keyserverIDs, }), + holdersPromise, identityPromise, ]); return { @@ -219,6 +229,7 @@ identityClient, logOutType, preRequestUserState, + removeAllHolders, skipIdentityLogOut, ], );