diff --git a/lib/tunnelbroker/use-peer-to-peer-message-handler.js b/lib/tunnelbroker/use-peer-to-peer-message-handler.js --- a/lib/tunnelbroker/use-peer-to-peer-message-handler.js +++ b/lib/tunnelbroker/use-peer-to-peer-message-handler.js @@ -21,6 +21,7 @@ import { peerToPeerMessageTypes, type PeerToPeerMessage, + type SenderInfo, } from '../types/tunnelbroker/peer-to-peer-message-types.js'; import { userActionsP2PMessageTypes, @@ -34,6 +35,57 @@ import { getClientMessageIDFromTunnelbrokerMessageID } from '../utils/peer-to-peer-communication-utils.js'; import { useSelector } from '../utils/redux-utils.js'; +// handles `peerToPeerMessageTypes.ENCRYPTED_MESSAGE` +function useHandleOlmMessageToDevice() { + const identityContext = React.useContext(IdentityClientContext); + invariant(identityContext, 'Identity context should be set'); + + const { identityClient } = identityContext; + const broadcastDeviceListUpdates = useBroadcastDeviceListUpdates(); + const allPeerDevices = useSelector(getAllPeerDevices); + + return React.useCallback( + async (decryptedMessageContent: string, senderInfo: SenderInfo) => { + const parsedMessageToDevice = JSON.parse(decryptedMessageContent); + + // Handle user-action messages + if (!userActionP2PMessageValidator.is(parsedMessageToDevice)) { + return; + } + const userActionMessage: UserActionP2PMessage = parsedMessageToDevice; + + if ( + userActionMessage.type === + userActionsP2PMessageTypes.LOG_OUT_PRIMARY_DEVICE + ) { + console.log( + 'Received primary device logout message from', + senderInfo.deviceID, + ); + } else if ( + userActionMessage.type === + userActionsP2PMessageTypes.LOG_OUT_SECONDARY_DEVICE + ) { + const { userID, deviceID: deviceIDToLogOut } = senderInfo; + await removeDeviceFromDeviceList( + identityClient, + userID, + deviceIDToLogOut, + ); + await broadcastDeviceListUpdates( + allPeerDevices.filter(deviceID => deviceID !== deviceIDToLogOut), + ); + } else { + console.warn( + 'Unsupported P2P user action message:', + userActionMessage.type, + ); + } + }, + [allPeerDevices, broadcastDeviceListUpdates, identityClient], + ); +} + function usePeerToPeerMessageHandler(): ( message: PeerToPeerMessage, messageID: string, @@ -47,7 +99,8 @@ const foreignPeerDevices = useSelector(getForeignPeerDevices); const broadcastDeviceListUpdates = useBroadcastDeviceListUpdates(); const getAndUpdateDeviceListsForUsers = useGetAndUpdateDeviceListsForUsers(); - const allPeerDevices = useSelector(getAllPeerDevices); + + const handleOlmMessageToDevice = useHandleOlmMessageToDevice(); return React.useCallback( async (message: PeerToPeerMessage, messageID: string) => { @@ -137,44 +190,9 @@ ); try { - const parsedMessageToDevice = JSON.parse(decrypted); - if (!userActionP2PMessageValidator.is(parsedMessageToDevice)) { - return; - } - const userActionMessage: UserActionP2PMessage = - parsedMessageToDevice; - - if ( - userActionMessage.type === - userActionsP2PMessageTypes.LOG_OUT_PRIMARY_DEVICE - ) { - console.log( - 'Received primary device logout message from', - message.senderInfo.deviceID, - ); - } else if ( - userActionMessage.type === - userActionsP2PMessageTypes.LOG_OUT_SECONDARY_DEVICE - ) { - const { userID, deviceID: deviceIDToLogOut } = message.senderInfo; - await removeDeviceFromDeviceList( - identityClient, - userID, - deviceIDToLogOut, - ); - await broadcastDeviceListUpdates( - allPeerDevices.filter( - deviceID => deviceID !== deviceIDToLogOut, - ), - ); - } else { - console.warn( - 'Unsupported P2P user action message:', - userActionMessage.type, - ); - } + await handleOlmMessageToDevice(decrypted, message.senderInfo); } catch (e) { - console.log(e); + console.log('Failed processing Olm P2P message:', e); } } catch (e) { if (e.message?.includes(olmSessionErrors.messageAlreadyDecrypted)) { @@ -283,10 +301,10 @@ foreignPeerDevices, getAndUpdateDeviceListsForUsers, getAuthMetadata, + handleOlmMessageToDevice, identityClient, olmAPI, sqliteAPI, - allPeerDevices, ], ); }