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 @@ -501,7 +501,7 @@ }); const accountDeletionBroadcastOptions = Object.freeze({ - broadcastToOwnDevices: true, + includeOwnDevices: true, }); function useDeleteAccount(): (password: ?string) => Promise { const client = React.useContext(IdentityClientContext); diff --git a/lib/hooks/peer-list-hooks.js b/lib/hooks/peer-list-hooks.js --- a/lib/hooks/peer-list-hooks.js +++ b/lib/hooks/peer-list-hooks.js @@ -7,7 +7,7 @@ import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js'; import { getAllPeerDevices, - getForeignPeerDevices, + getPeersUserAndDeviceIDs, } from '../selectors/user-selectors.js'; import { IdentityClientContext } from '../shared/identity-client-context.js'; import { useTunnelbroker } from '../tunnelbroker/tunnelbroker-context.js'; @@ -213,28 +213,24 @@ ); } -function useBroadcastAccountDeletion( - options: { broadcastToOwnDevices?: boolean } = {}, -): () => Promise { - const { broadcastToOwnDevices } = options; +function useBroadcastAccountDeletion(options: { + +includeOwnDevices: boolean, +}): () => Promise { + const { includeOwnDevices } = options; const identityContext = React.useContext(IdentityClientContext); if (!identityContext) { throw new Error('Identity service client is not initialized'); } - const { sendMessageToDevice } = useTunnelbroker(); + const { getAuthMetadata } = identityContext; - const devicesSelector = broadcastToOwnDevices - ? getAllPeerDevices - : getForeignPeerDevices; - const peerDevices = useSelector(devicesSelector); - const { createOlmSessionsWithPeer } = usePeerOlmSessionsCreatorContext(); + const peers = useSelector(getPeersUserAndDeviceIDs); + const broadcastMessage = useBroadcastOlmMessage(); return React.useCallback(async () => { - const { getAuthMetadata } = identityContext; const authMetadata = await getAuthMetadata(); - const { userID, deviceID: thisDeviceID } = authMetadata; - if (!thisDeviceID || !userID) { + const { userID: thisUserID, deviceID: thisDeviceID } = authMetadata; + if (!thisDeviceID || !thisUserID) { throw new Error('No auth metadata'); } // create and send Olm Tunnelbroker messages to peers @@ -246,48 +242,13 @@ }; const rawPayload = JSON.stringify(deletionMessage); - const recipientDeviceIDs = peerDevices.filter( - peerDeviceID => peerDeviceID !== thisDeviceID, + const recipients = peers.filter( + peer => + peer.deviceID !== thisDeviceID && + (includeOwnDevices || peer.userID !== thisUserID), ); - for (const deviceID of recipientDeviceIDs) { - try { - const encryptedData = await olmAPI.encrypt(rawPayload, 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(rawPayload, 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 account deletion message to device ${deviceID}:`, - err, - ); - } - } - } - }, [ - createOlmSessionsWithPeer, - identityContext, - peerDevices, - sendMessageToDevice, - ]); + await broadcastMessage(rawPayload, recipients); + }, [broadcastMessage, getAuthMetadata, includeOwnDevices, peers]); } export { 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 @@ -48,7 +48,7 @@ // When re-broadcasting, we want to do it only to foreign peers // to avoid a vicious circle of deletion messages sent by own devices. const accountDeletionBroadcastOptions = Object.freeze({ - broadcastToOwnDevices: false, + includeOwnDevices: false, }); // handles `peerToPeerMessageTypes.ENCRYPTED_MESSAGE`