diff --git a/lib/shared/dm-ops/dm-op-utils.js b/lib/shared/dm-ops/dm-op-utils.js --- a/lib/shared/dm-ops/dm-op-utils.js +++ b/lib/shared/dm-ops/dm-op-utils.js @@ -2,7 +2,6 @@ import uuid from 'uuid'; -import type { AuxUserStore } from '../../types/aux-user-types.js'; import type { DMOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import type { RawMessageInfo } from '../../types/message-types.js'; @@ -17,21 +16,22 @@ import type { ClientUpdateInfo } from '../../types/update-types.js'; import type { CurrentUserInfo } from '../../types/user-types.js'; import { getContentSigningKey } from '../../utils/crypto-utils.js'; -import { values } from '../../utils/objects.js'; import { messageSpecs } from '../messages/message-specs.js'; function generateMessagesToPeers( message: DMOperation, - peers: $ReadOnlyArray, - userID: string, + peers: $ReadOnlyArray<{ + +userID: string, + +deviceID: string, + }>, supportsAutoRetry: boolean, ): $ReadOnlyArray { const outboundP2PMessages = []; - for (const peerID of peers) { + for (const peer of peers) { const messageToPeer: OutboundP2PMessage = { messageID: uuid.v4(), - deviceID: peerID, - userID, + deviceID: peer.deviceID, + userID: peer.userID, timestamp: new Date().getTime().toString(), plaintext: JSON.stringify(message), ciphertext: '', @@ -51,25 +51,30 @@ async function createMessagesToPeersFromDMOp( operation: DMOperationSpecification, - auxUserStore: AuxUserStore, + allPeerUserIDAndDeviceIDs: $ReadOnlyArray<{ + +userID: string, + +deviceID: string, + }>, currentUserInfo: ?CurrentUserInfo, ): Promise<$ReadOnlyArray> { if (!currentUserInfo?.id) { return []; } - const selfDevices = - auxUserStore.auxUserInfos[currentUserInfo.id].deviceList?.devices ?? []; - const allPeerDevices = values(auxUserStore.auxUserInfos) - .map(info => info.deviceList?.devices ?? []) - .flat(); - const devices = - operation.recipients === 'all_peer_devices' ? allPeerDevices : selfDevices; + + let peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs; + if (operation.recipients === 'self_devices') { + peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter( + peer => peer.userID === currentUserInfo.id, + ); + } + const thisDeviceID = await getContentSigningKey(); - const targetDevices = devices.filter(id => id !== thisDeviceID); + const targetPeers = peerUserIDAndDeviceIDs.filter( + peer => peer.deviceID !== thisDeviceID, + ); return generateMessagesToPeers( operation.op, - targetDevices, - currentUserInfo.id, + targetPeers, operation.supportsAutoRetry, ); } diff --git a/lib/tunnelbroker/peer-to-peer-context.js b/lib/tunnelbroker/peer-to-peer-context.js --- a/lib/tunnelbroker/peer-to-peer-context.js +++ b/lib/tunnelbroker/peer-to-peer-context.js @@ -9,6 +9,7 @@ useTunnelbroker, } from './tunnelbroker-context.js'; import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js'; +import { getAllPeerUserIDAndDeviceIDs } from '../selectors/user-selectors.js'; import { createMessagesToPeersFromDMOp, type DMOperationSpecification, @@ -169,7 +170,7 @@ const dmOpsSendingPromiseResolvers = React.useRef< Map mixed, +reject: Error => mixed }>, >(new Map()); - const auxUserStore = useSelector(state => state.auxUserStore); + const allPeerUserIDAndDeviceIDs = useSelector(getAllPeerUserIDAndDeviceIDs); const currentUserInfo = useSelector(state => state.currentUserInfo); const sendDMOperation = React.useCallback( @@ -181,7 +182,7 @@ const messages = await createMessagesToPeersFromDMOp( op, - auxUserStore, + allPeerUserIDAndDeviceIDs, currentUserInfo, ); dispatch({ @@ -194,7 +195,7 @@ return promise; }, - [auxUserStore, currentUserInfo, dispatch], + [allPeerUserIDAndDeviceIDs, currentUserInfo, dispatch], ); const processingQueue = React.useRef<