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,7 +3,6 @@ import invariant from 'invariant'; import * as React from 'react'; -import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js'; import { useBroadcastDeviceListUpdates, useBroadcastAccountDeletion, @@ -32,7 +31,7 @@ permissionsAndAuthRelatedRequestTimeout, callIdentityServiceTimeout, } from '../shared/timeouts.js'; -import { useTunnelbroker } from '../tunnelbroker/tunnelbroker-context.js'; +import { usePeerToPeerCommunication } from '../tunnelbroker/peer-to-peer-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 { broadcastEphemeralMessage } = usePeerToPeerCommunication(); 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,15 @@ 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 broadcastEphemeralMessage( + JSON.stringify(messageContents), + recipients, + authMetadata, + ); // - logOut() performs device list update by calling Identity RPC // - broadcastDeviceListUpdates asks peers to download it from identity @@ -344,12 +302,11 @@ await broadcastDeviceListUpdates(foreignPeerDevices); return logOutResult; }, [ - createOlmSessionsWithPeer, broadcastDeviceListUpdates, + broadcastEphemeralMessage, foreignPeerDevices, identityContext, logOut, - sendMessageToDevice, ]); } @@ -358,14 +315,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 { broadcastEphemeralMessage } = usePeerToPeerCommunication(); return React.useCallback(async () => { const { identityClient, getAuthMetadata } = identityContext; @@ -388,44 +344,16 @@ 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 broadcastEphemeralMessage( + JSON.stringify(messageContents), + [recipient], + authMetadata, + ); // log out of identity service, keyserver and visually return logOut(); - }, [createOlmSessionsWithPeer, identityContext, sendMessageToDevice, logOut]); + }, [identityContext, broadcastEphemeralMessage, logOut]); } const claimUsernameActionTypes = Object.freeze({