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,10 +3,10 @@ import invariant from 'invariant'; import * as React from 'react'; -import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js'; import { useBroadcastDeviceListUpdates, useBroadcastAccountDeletion, + useBroadcastOlmMessage, } from '../hooks/peer-list-hooks.js'; import type { CallSingleKeyserverEndpoint, @@ -32,7 +32,6 @@ permissionsAndAuthRelatedRequestTimeout, callIdentityServiceTimeout, } from '../shared/timeouts.js'; -import { useTunnelbroker } from '../tunnelbroker/tunnelbroker-context.js'; import type { LegacyLogInInfo, LegacyLogInResult, @@ -74,10 +73,6 @@ SubscriptionUpdateResult, } from '../types/subscription-types.js'; import type { RawThreadInfos } from '../types/thread-types.js'; -import { - peerToPeerMessageTypes, - type EncryptedMessage, -} from '../types/tunnelbroker/peer-to-peer-message-types.js'; import { userActionsP2PMessageTypes, type PrimaryDeviceLogoutP2PMessage, @@ -268,12 +263,11 @@ throw new Error('Identity service client is not initialized'); } - const { sendMessageToDevice } = useTunnelbroker(); const broadcastDeviceListUpdates = useBroadcastDeviceListUpdates(); + const broadcastOlmMessage = useBroadcastOlmMessage(); const foreignPeerDevices = useSelector(getForeignPeerDevices); const logOut = useLogOut(primaryDeviceLogOutOptions); - const { createOlmSessionsWithPeer } = usePeerOlmSessionsCreatorContext(); return React.useCallback(async () => { const { identityClient, getAuthMetadata } = identityContext; const authMetadata = await getAuthMetadata(); @@ -288,51 +282,11 @@ throw new Error('Used primary device logout on a non-primary device'); } - // create and send Olm Tunnelbroker messages to secondaryDevices - const { olmAPI } = getConfig(); - await olmAPI.initializeCryptoAccount(); const messageContents: PrimaryDeviceLogoutP2PMessage = { type: userActionsP2PMessageTypes.LOG_OUT_PRIMARY_DEVICE, }; - for (const deviceID of secondaryDevices) { - try { - const encryptedData = await olmAPI.encrypt( - JSON.stringify(messageContents), - deviceID, - ); - const encryptedMessage: EncryptedMessage = { - type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE, - senderInfo: { deviceID: thisDeviceID, userID }, - encryptedData, - }; - await sendMessageToDevice({ - deviceID, - payload: JSON.stringify(encryptedMessage), - }); - } catch { - try { - await createOlmSessionsWithPeer(userID, deviceID); - const encryptedData = await olmAPI.encrypt( - JSON.stringify(messageContents), - deviceID, - ); - const encryptedMessage: EncryptedMessage = { - type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE, - senderInfo: { deviceID: thisDeviceID, userID }, - encryptedData, - }; - await sendMessageToDevice({ - deviceID, - payload: JSON.stringify(encryptedMessage), - }); - } catch (err) { - console.warn( - `Error sending primary device logout message to device ${deviceID}:`, - err, - ); - } - } - } + const recipients = secondaryDevices.map(deviceID => ({ userID, deviceID })); + await broadcastOlmMessage(JSON.stringify(messageContents), recipients); // - logOut() performs device list update by calling Identity RPC // - broadcastDeviceListUpdates asks peers to download it from identity @@ -344,12 +298,11 @@ await broadcastDeviceListUpdates(foreignPeerDevices); return logOutResult; }, [ - createOlmSessionsWithPeer, broadcastDeviceListUpdates, + broadcastOlmMessage, foreignPeerDevices, identityContext, logOut, - sendMessageToDevice, ]); } @@ -358,14 +311,13 @@ }); function useSecondaryDeviceLogOut(): () => Promise { - const { sendMessageToDevice } = useTunnelbroker(); const logOut = useLogOut(secondaryDeviceLogOutOptions); const identityContext = React.useContext(IdentityClientContext); if (!identityContext) { throw new Error('Identity service client is not initialized'); } - const { createOlmSessionsWithPeer } = usePeerOlmSessionsCreatorContext(); + const sendOlmMessage = useBroadcastOlmMessage(); return React.useCallback(async () => { const { identityClient, getAuthMetadata } = identityContext; @@ -388,44 +340,12 @@ const messageContents: SecondaryDeviceLogoutP2PMessage = { type: userActionsP2PMessageTypes.LOG_OUT_SECONDARY_DEVICE, }; - try { - const encryptedData = await olmAPI.encrypt( - JSON.stringify(messageContents), - primaryDeviceID, - ); - const encryptedMessage: EncryptedMessage = { - type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE, - senderInfo: { deviceID, userID }, - encryptedData, - }; - await sendMessageToDevice({ - deviceID: primaryDeviceID, - payload: JSON.stringify(encryptedMessage), - }); - } catch { - try { - await createOlmSessionsWithPeer(userID, primaryDeviceID); - const encryptedData = await olmAPI.encrypt( - JSON.stringify(messageContents), - primaryDeviceID, - ); - const encryptedMessage: EncryptedMessage = { - type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE, - senderInfo: { deviceID, userID }, - encryptedData, - }; - await sendMessageToDevice({ - deviceID: primaryDeviceID, - payload: JSON.stringify(encryptedMessage), - }); - } catch (err) { - console.warn('Error sending secondary device logout message:', err); - } - } + const recipient = { userID, deviceID: primaryDeviceID }; + await sendOlmMessage(JSON.stringify(messageContents), [recipient]); // log out of identity service, keyserver and visually return logOut(); - }, [createOlmSessionsWithPeer, identityContext, sendMessageToDevice, logOut]); + }, [identityContext, sendOlmMessage, logOut]); } const claimUsernameActionTypes = Object.freeze({