diff --git a/lib/hooks/peer-list-hooks.js b/lib/hooks/peer-list-hooks.js --- a/lib/hooks/peer-list-hooks.js +++ b/lib/hooks/peer-list-hooks.js @@ -16,6 +16,7 @@ UsersDevicesPlatformDetails, SignedDeviceList, RawDeviceList, + UserDevicesPlatformDetails, } from '../types/identity-service-types.js'; import { type DeviceListUpdated, @@ -194,9 +195,41 @@ }, [broadcastEphemeralMessage, getAuthMetadata, includeOwnDevices, peers]); } +export type CurrentIdentityUserState = { + +currentDeviceList: SignedDeviceList, + +currentUserPlatformDetails: UserDevicesPlatformDetails, +}; +function useCurrentIdentityUserState(): () => Promise { + const identityContext = React.useContext(IdentityClientContext); + invariant(identityContext, 'Identity context should be set'); + const { identityClient, getAuthMetadata } = identityContext; + + return React.useCallback(async () => { + const { userID } = await getAuthMetadata(); + if (!userID) { + throw new Error('Missing userID'); + } + + const { getDeviceListsForUsers } = identityClient; + const deviceListsResponse = await getDeviceListsForUsers([userID]); + const currentDeviceList = + deviceListsResponse.usersSignedDeviceLists[userID]; + const currentUserPlatformDetails = + deviceListsResponse.usersDevicesPlatformDetails[userID]; + if (!currentDeviceList || !currentUserPlatformDetails) { + throw new Error('Device list not found for current user'); + } + return { + currentDeviceList, + currentUserPlatformDetails, + }; + }, [getAuthMetadata, identityClient]); +} + export { useGetDeviceListsForUsers, useBroadcastDeviceListUpdates, useGetAndUpdateDeviceListsForUsers, useBroadcastAccountDeletion, + useCurrentIdentityUserState, }; diff --git a/native/backup/backup-handler.js b/native/backup/backup-handler.js --- a/native/backup/backup-handler.js +++ b/native/backup/backup-handler.js @@ -7,6 +7,7 @@ import { createUserKeysBackupActionTypes } from 'lib/actions/backup-actions.js'; import { useBroadcastDeviceListUpdates, + useCurrentIdentityUserState, useGetAndUpdateDeviceListsForUsers, } from 'lib/hooks/peer-list-hooks.js'; import { useCheckIfPrimaryDevice } from 'lib/hooks/primary-device-hooks.js'; @@ -82,6 +83,7 @@ const getAndUpdateDeviceListsForUsers = useGetAndUpdateDeviceListsForUsers(); const broadcastDeviceListUpdates = useBroadcastDeviceListUpdates(); + const getCurrentIdentityUserState = useCurrentIdentityUserState(); const allPeerDevices = useSelector(getAllPeerDevices); React.useEffect(() => { @@ -144,22 +146,12 @@ const isPrimaryDevice = await checkIfPrimaryDevice(); const { getAuthMetadata, identityClient } = identityContext; const { userID, deviceID } = await getAuthMetadata(); - let currentDeviceList, currentUserPlatformDetails, deviceListIsSigned; + let currentIdentityUserState, deviceListIsSigned; try { - if (!userID || !userIdentifier) { - throw new Error('Missing userID or userIdentifier'); - } - const deviceListsResponse = await identityClient.getDeviceListsForUsers( - [userID], - ); - currentDeviceList = deviceListsResponse.usersSignedDeviceLists[userID]; - currentUserPlatformDetails = - deviceListsResponse.usersDevicesPlatformDetails[userID]; - if (!currentDeviceList || !currentUserPlatformDetails) { - throw new Error('Device list not found for current user'); - } + currentIdentityUserState = await getCurrentIdentityUserState(); - deviceListIsSigned = !!currentDeviceList.curPrimarySignature; + deviceListIsSigned = + !!currentIdentityUserState.currentDeviceList.curPrimarySignature; if (!isPrimaryDevice && deviceListIsSigned) { backupUploadInProgress.current = false; return; @@ -198,9 +190,14 @@ // 2. create in-memory device list (reorder and sign) const newDeviceList = await reorderAndSignDeviceList( deviceID, - rawDeviceListFromSignedList(currentDeviceList), + rawDeviceListFromSignedList( + currentIdentityUserState.currentDeviceList, + ), ); + if (!userID || !userIdentifier) { + throw new Error('Missing userID or userIdentifier'); + } // 3. UpdateDeviceList RPC transaction await updateDeviceList(newDeviceList.signedList); dispatch({ @@ -208,7 +205,7 @@ payload: { deviceLists: { [userID]: newDeviceList.rawList }, usersPlatformDetails: { - [userID]: currentUserPlatformDetails, + [userID]: currentIdentityUserState.currentUserPlatformDetails, }, }, }); @@ -269,6 +266,7 @@ dispatch, dispatchActionPromise, getAndUpdateDeviceListsForUsers, + getCurrentIdentityUserState, handlerStarted, identityContext, latestBackupInfo,