diff --git a/lib/components/keyserver-connection-handler.js b/lib/components/keyserver-connection-handler.js --- a/lib/components/keyserver-connection-handler.js +++ b/lib/components/keyserver-connection-handler.js @@ -3,14 +3,22 @@ import invariant from 'invariant'; import * as React from 'react'; -import { logOutActionTypes, useLogOut } from '../actions/user-actions.js'; +import { + keyserverAuthActionTypes, + logOutActionTypes, + useKeyserverAuth, + useLogOut, +} from '../actions/user-actions.js'; +import { nonThreadCalendarFilters } from '../selectors/calendar-filter-selectors.js'; import { connectionSelector, cookieSelector, + deviceTokenSelector, } from '../selectors/keyserver-selectors.js'; import { IdentityClientContext } from '../shared/identity-client-context.js'; import { OlmSessionCreatorContext } from '../shared/olm-session-creator-context.js'; import type { BaseSocketProps } from '../socket/socket.react.js'; +import { logInActionSources } from '../types/account-types.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; @@ -26,12 +34,34 @@ const dispatchActionPromise = useDispatchActionPromise(); const callLogOut = useLogOut(); + const keyserverAuth = useKeyserverAuth(); const hasConnectionIssue = useSelector( state => !!connectionSelector(keyserverID)(state)?.connectionIssue, ); const cookie = useSelector(cookieSelector(keyserverID)); + const keyserverDeviceToken = useSelector(deviceTokenSelector(keyserverID)); + // We have an assumption that we should be always connected to Ashoat's + // keyserver. It is possible that a token which it has is correct, so we can + // try to use it. In worst case it is invalid and our push-handler will try + // to fix it. + const ashoatKeyserverDeviceToken = useSelector( + deviceTokenSelector(ashoatKeyserverID), + ); + const deviceToken = keyserverDeviceToken ?? ashoatKeyserverDeviceToken; + + const navInfo = useSelector(state => state.navInfo); + const calendarFilters = useSelector(state => state.calendarFilters); + const calendarQuery = React.useMemo( + () => ({ + startDate: navInfo.startDate, + endDate: navInfo.endDate, + filters: nonThreadCalendarFilters(calendarFilters), + }), + [calendarFilters, navInfo.endDate, navInfo.startDate], + ); + React.useEffect(() => { if (hasConnectionIssue) { void dispatchActionPromise(logOutActionTypes, callLogOut()); @@ -40,7 +70,7 @@ const identityContext = React.useContext(IdentityClientContext); invariant(identityContext, 'Identity context should be set'); - const { identityClient } = identityContext; + const { identityClient, getAuthMetadata } = identityContext; const olmSessionCreator = React.useContext(OlmSessionCreatorContext); invariant(olmSessionCreator, 'Olm session creator should be set'); @@ -55,7 +85,6 @@ const keyserverKeys = await identityClient.getKeyserverKeys(keyserverID); - // eslint-disable-next-line no-unused-vars const [notifsSession, contentSession] = await Promise.all([ olmSessionCreator.notificationsSessionCreator( cookie, @@ -68,14 +97,52 @@ keyserverKeys.contentInitializationInfo, ), ]); + + const { userID, deviceID } = await getAuthMetadata(); + invariant(userID, 'userID should be set'); + invariant(deviceID, 'deviceID should be set'); + + const deviceTokenUpdateInput = deviceToken + ? { [keyserverID]: { deviceToken } } + : {}; + + void dispatchActionPromise( + keyserverAuthActionTypes, + keyserverAuth({ + userID, + deviceID, + doNotRegister: false, + calendarQuery, + deviceTokenUpdateInput, + logInActionSource: process.env.BROWSER + ? logInActionSources.keyserverAuthFromWeb + : logInActionSources.keyserverAuthFromNative, + keyserverData: { + [keyserverID]: { + initialContentEncryptedMessage: contentSession, + initialNotificationsEncryptedMessage: notifsSession, + }, + }, + }), + ); } catch (e) { console.log( - `Error getting keys for keyserver with id ${keyserverID}`, + `Error while authenticating to keyserver with id ${keyserverID}`, e, ); } })(); - }, [keyserverID, identityClient, olmSessionCreator, cookie]); + }, [ + keyserverID, + identityClient, + olmSessionCreator, + cookie, + getAuthMetadata, + dispatchActionPromise, + keyserverAuth, + deviceToken, + calendarQuery, + ]); if (keyserverID !== ashoatKeyserverID) { return null;