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,23 @@ 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 { extractKeyserverIDFromID } from '../keyserver-conn/keyserver-call-utils.js'; +import { filterThreadIDsInFilterList } from '../reducers/calendar-filters-reducer.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 +35,37 @@ 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(() => { + const filters = filterThreadIDsInFilterList( + calendarFilters, + (threadID: string) => extractKeyserverIDFromID(threadID) === keyserverID, + ); + return { + startDate: navInfo.startDate, + endDate: navInfo.endDate, + filters, + }; + }, [calendarFilters, keyserverID, navInfo.endDate, navInfo.startDate]); + React.useEffect(() => { if (hasConnectionIssue) { void dispatchActionPromise(logOutActionTypes, callLogOut()); @@ -40,7 +74,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 +89,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 +101,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 } } + : {}; + + await 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; diff --git a/lib/reducers/calendar-filters-reducer.js b/lib/reducers/calendar-filters-reducer.js --- a/lib/reducers/calendar-filters-reducer.js +++ b/lib/reducers/calendar-filters-reducer.js @@ -204,6 +204,7 @@ } export { + filterThreadIDsInFilterList, removeDeletedThreadIDsFromFilterList, removeKeyserverThreadIDsFromFilterList, };