diff --git a/lib/components/platform-details-synchronizer.react.js b/lib/components/platform-details-synchronizer.react.js --- a/lib/components/platform-details-synchronizer.react.js +++ b/lib/components/platform-details-synchronizer.react.js @@ -1,5 +1,6 @@ // @flow +import invariant from 'invariant'; import * as React from 'react'; import { isLoggedIn } from '../selectors/user-selectors.js'; @@ -9,7 +10,9 @@ function PlatformDetailsSynchronizer(): React.Node { const client = React.useContext(IdentityClientContext); - const identityClient = client?.identityClient; + invariant(client, 'Identity context should be set'); + const identityClient = client.identityClient; + const getAuthMetadata = client.getAuthMetadata; const loggedIn = useSelector(isLoggedIn); @@ -20,12 +23,16 @@ if (!loggedIn) { return; } + const authMetadata = await getAuthMetadata(); + if (!authMetadata) { + return; + } try { await identityClient.syncPlatformDetails(); } catch (error) { console.log('Error syncing platform details:', error); } - }, [identityClient, loggedIn]); + }, [identityClient, loggedIn, getAuthMetadata]); const hasRun = React.useRef(false); React.useEffect(() => { diff --git a/lib/components/prekeys-handler.react.js b/lib/components/prekeys-handler.react.js --- a/lib/components/prekeys-handler.react.js +++ b/lib/components/prekeys-handler.react.js @@ -7,6 +7,7 @@ import { IdentityClientContext } from '../shared/identity-client-context.js'; import { getConfig } from '../utils/config.js'; import { useSelector } from '../utils/redux-utils.js'; +import { usingCommServicesAccessToken } from '../utils/services-utils.js'; // Time after which rotation is started const PREKEY_ROTATION_TIMEOUT = 3 * 1000; // in milliseconds @@ -21,11 +22,16 @@ if (!loggedIn) { return undefined; } + if (!usingCommServicesAccessToken) { + return undefined; + } const timeoutID = setTimeout(async () => { try { const authMetadata = await identityContext.getAuthMetadata(); - + if (!authMetadata) { + return; + } const { olmAPI } = getConfig(); await olmAPI.validateAndUploadPrekeys(authMetadata); } catch (e) { diff --git a/lib/handlers/user-infos-handler.react.js b/lib/handlers/user-infos-handler.react.js --- a/lib/handlers/user-infos-handler.react.js +++ b/lib/handlers/user-infos-handler.react.js @@ -1,5 +1,6 @@ // @flow +import invariant from 'invariant'; import * as React from 'react'; import { @@ -16,6 +17,7 @@ usersWithMissingDeviceListSelector, isLoggedInToKeyserver, } from '../selectors/user-selectors.js'; +import { IdentityClientContext } from '../shared/identity-client-context.js'; import { useTunnelbroker } from '../tunnelbroker/tunnelbroker-context.js'; import { relationshipActions } from '../types/relationship-types.js'; import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js'; @@ -28,6 +30,10 @@ } from '../utils/services-utils.js'; function UserInfosHandler(): React.Node { + const client = React.useContext(IdentityClientContext); + invariant(client, 'Identity context should be set'); + const { getAuthMetadata } = client; + const userInfos = useSelector(state => state.userStore.userInfos); const userInfosWithMissingUsernames = React.useMemo(() => { @@ -61,39 +67,46 @@ if (!usingCommServicesAccessToken || newUserIDs.length === 0) { return; } - // 1. Fetch usernames from identity - const promise = (async () => { - newUserIDs.forEach(id => requestedIDsRef.current.add(id)); - const identities = await findUserIdentities(newUserIDs); - newUserIDs.forEach(id => requestedIDsRef.current.delete(id)); + void (async () => { + const authMetadata = await getAuthMetadata(); + if (!authMetadata) { + return; + } + // 1. Fetch usernames from identity + const promise = (async () => { + newUserIDs.forEach(id => requestedIDsRef.current.add(id)); + const identities = await findUserIdentities(newUserIDs); + newUserIDs.forEach(id => requestedIDsRef.current.delete(id)); - const newUserInfos = []; - for (const id in identities) { - newUserInfos.push({ - id, - username: identities[id].username, - }); + const newUserInfos = []; + for (const id in identities) { + newUserInfos.push({ + id, + username: identities[id].username, + }); + } + return { userInfos: newUserInfos }; + })(); + void dispatchActionPromise(findUserIdentitiesActionTypes, promise); + // 2. Fetch avatars from auth keyserver + if (relyingOnAuthoritativeKeyserver) { + const userIDsWithoutOwnID = newUserIDs.filter( + id => id !== currentUserInfo?.id, + ); + if (userIDsWithoutOwnID.length === 0) { + return; + } + void dispatchActionPromise( + updateRelationshipsActionTypes, + callUpdateRelationships({ + action: relationshipActions.ACKNOWLEDGE, + userIDs: userIDsWithoutOwnID, + }), + ); } - return { userInfos: newUserInfos }; })(); - void dispatchActionPromise(findUserIdentitiesActionTypes, promise); - // 2. Fetch avatars from auth keyserver - if (relyingOnAuthoritativeKeyserver) { - const userIDsWithoutOwnID = newUserIDs.filter( - id => id !== currentUserInfo?.id, - ); - if (userIDsWithoutOwnID.length === 0) { - return; - } - void dispatchActionPromise( - updateRelationshipsActionTypes, - callUpdateRelationships({ - action: relationshipActions.ACKNOWLEDGE, - userIDs: userIDsWithoutOwnID, - }), - ); - } }, [ + getAuthMetadata, callUpdateRelationships, currentUserInfo?.id, dispatchActionPromise, @@ -117,6 +130,10 @@ return; } void (async () => { + const authMetadata = await getAuthMetadata(); + if (!authMetadata) { + return; + } try { await getAndUpdateDeviceListsForUsers(usersWithMissingDeviceList, true); } catch (e) { @@ -128,6 +145,7 @@ } })(); }, [ + getAuthMetadata, socketState.isAuthorized, getAndUpdateDeviceListsForUsers, usersWithMissingDeviceList,