Skip to content

Commit

Permalink
[lib] convert createMessagesToPeersFromDMOp to a hook
Browse files Browse the repository at this point in the history
Summary:
[ENG-9394](https://linear.app/comm/issue/ENG-9394/create-a-thick-thread-when-possible-while-searching-for-a-personal)

Cleanup and needed to implement handling missing peers as part of generating messages.

Depends on D13484

Test Plan: Flow, just refactor

Reviewers: ashoat, tomek

Reviewed By: ashoat

Differential Revision: https://phab.comm.dev/D13485
  • Loading branch information
xsanm committed Sep 30, 2024
1 parent 2c5ac82 commit 04ea8c3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 61 deletions.
103 changes: 55 additions & 48 deletions lib/shared/dm-ops/dm-op-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useFindUserIdentities } from '../../actions/user-actions.js';
import { useLoggedInUserInfo } from '../../hooks/account-hooks.js';
import { useGetLatestMessageEdit } from '../../hooks/latest-message-edit.js';
import { mergeUpdatesWithMessageInfos } from '../../reducers/message-reducer.js';
import { getAllPeerUserIDAndDeviceIDs } from '../../selectors/user-selectors.js';
import type {
CreateThickRawThreadInfoInput,
DMAddMembersOperation,
Expand Down Expand Up @@ -120,58 +121,64 @@ export type DMOperationSpecification =
| OutboundDMOperationSpecification
| InboundDMOperationSpecification;

async function createMessagesToPeersFromDMOp(
function useCreateMessagesToPeersFromDMOp(): (
operation: DMOperation,
recipients: OutboundDMOperationSpecificationRecipients,
allPeerUserIDAndDeviceIDs: $ReadOnlyArray<{
+userID: string,
+deviceID: string,
}>,
utilities: ProcessDMOperationUtilities,
): Promise<$ReadOnlyArray<OutboundP2PMessage>> {
const { viewerID, threadInfos } = utilities;
if (!viewerID) {
return [];
}
) => Promise<$ReadOnlyArray<OutboundP2PMessage>> {
const allPeerUserIDAndDeviceIDs = useSelector(getAllPeerUserIDAndDeviceIDs);
const utilities = useSendDMOperationUtils();

let peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs;
if (recipients.type === 'self_devices') {
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(
peer => peer.userID === viewerID,
);
} else if (recipients.type === 'some_users') {
const userIDs = new Set(recipients.userIDs);
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer =>
userIDs.has(peer.userID),
);
} else if (recipients.type === 'all_thread_members') {
const { threadID } = recipients;
if (!threadInfos[threadID]) {
console.log(
`all_thread_members called for threadID ${threadID}, which is ` +
'missing from the ThreadStore. if sending a message soon after ' +
'thread creation, consider some_users instead',
return React.useCallback(
async (
operation: DMOperation,
recipients: OutboundDMOperationSpecificationRecipients,
): Promise<$ReadOnlyArray<OutboundP2PMessage>> => {
const { viewerID, threadInfos } = utilities;
if (!viewerID) {
return [];
}

let peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs;
if (recipients.type === 'self_devices') {
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(
peer => peer.userID === viewerID,
);
} else if (recipients.type === 'some_users') {
const userIDs = new Set(recipients.userIDs);
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer =>
userIDs.has(peer.userID),
);
} else if (recipients.type === 'all_thread_members') {
const { threadID } = recipients;
if (!threadInfos[threadID]) {
console.log(
`all_thread_members called for threadID ${threadID}, which is ` +
'missing from the ThreadStore. if sending a message soon after ' +
'thread creation, consider some_users instead',
);
}
const members = threadInfos[recipients.threadID]?.members ?? [];
const memberIDs = members.map(member => member.id);

const userIDs = new Set(memberIDs);
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer =>
userIDs.has(peer.userID),
);
} else if (recipients.type === 'some_devices') {
const deviceIDs = new Set(recipients.deviceIDs);
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer =>
deviceIDs.has(peer.deviceID),
);
}

const thisDeviceID = await getContentSigningKey();
const targetPeers = peerUserIDAndDeviceIDs.filter(
peer => peer.deviceID !== thisDeviceID,
);
}
const members = threadInfos[recipients.threadID]?.members ?? [];
const memberIDs = members.map(member => member.id);

const userIDs = new Set(memberIDs);
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer =>
userIDs.has(peer.userID),
);
} else if (recipients.type === 'some_devices') {
const deviceIDs = new Set(recipients.deviceIDs);
peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer =>
deviceIDs.has(peer.deviceID),
);
}

const thisDeviceID = await getContentSigningKey();
const targetPeers = peerUserIDAndDeviceIDs.filter(
peer => peer.deviceID !== thisDeviceID,
return generateMessagesToPeers(operation, targetPeers);
},
[allPeerUserIDAndDeviceIDs, utilities],
);
return generateMessagesToPeers(operation, targetPeers);
}

function getCreateThickRawThreadInfoInputFromThreadInfo(
Expand Down Expand Up @@ -417,7 +424,7 @@ function useSendDMOperationUtils(): $ReadOnly<{
}

export {
createMessagesToPeersFromDMOp,
useCreateMessagesToPeersFromDMOp,
useAddDMThreadMembers,
getCreateThickRawThreadInfoInputFromThreadInfo,
getThreadUpdatesForNewMessages,
Expand Down
21 changes: 8 additions & 13 deletions lib/shared/dm-ops/process-dm-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { dmOpSpecs } from './dm-op-specs.js';
import {
type OutboundDMOperationSpecification,
type DMOperationSpecification,
createMessagesToPeersFromDMOp,
useCreateMessagesToPeersFromDMOp,
dmOperationSpecificationTypes,
type OutboundComposableDMOperationSpecification,
getThreadUpdatesForNewMessages,
Expand All @@ -18,7 +18,6 @@ import {
import { useProcessBlobHolders } from '../../actions/holder-actions.js';
import { processNewUserIDsActionType } from '../../actions/user-actions.js';
import { useDispatchWithMetadata } from '../../hooks/ops-hooks.js';
import { getAllPeerUserIDAndDeviceIDs } from '../../selectors/user-selectors.js';
import {
usePeerToPeerCommunication,
type ProcessOutboundP2PMessagesResult,
Expand All @@ -40,8 +39,8 @@ function useProcessDMOperation(): (
) => Promise<void> {
const baseUtilities = useSendDMOperationUtils();
const dispatchWithMetadata = useDispatchWithMetadata();
const allPeerUserIDAndDeviceIDs = useSelector(getAllPeerUserIDAndDeviceIDs);
const processBlobHolders = useProcessBlobHolders();
const createMessagesToPeersFromDMOp = useCreateMessagesToPeersFromDMOp();

const dispatch = useDispatch();

Expand Down Expand Up @@ -69,8 +68,6 @@ function useProcessDMOperation(): (
outboundP2PMessages = await createMessagesToPeersFromDMOp(
dmOp,
dmOperationSpecification.recipients,
allPeerUserIDAndDeviceIDs,
utilities,
);
}

Expand Down Expand Up @@ -250,10 +247,10 @@ function useProcessDMOperation(): (
},
[
baseUtilities,
processBlobHolders,
dispatchWithMetadata,
allPeerUserIDAndDeviceIDs,
createMessagesToPeersFromDMOp,
dispatch,
processBlobHolders,
],
);
}
Expand All @@ -279,10 +276,10 @@ function useSendComposableDMOperation(): (
) => Promise<ProcessOutboundP2PMessagesResult> {
const { getDMOpsSendingPromise } = usePeerToPeerCommunication();
const dispatchWithMetadata = useDispatchWithMetadata();
const allPeerUserIDAndDeviceIDs = useSelector(getAllPeerUserIDAndDeviceIDs);
const baseUtilities = useSendDMOperationUtils();
const { processOutboundMessages } = usePeerToPeerCommunication();
const localMessageInfos = useSelector(state => state.messageStore.local);
const createMessagesToPeersFromDMOp = useCreateMessagesToPeersFromDMOp();

return React.useCallback(
async (
Expand Down Expand Up @@ -329,8 +326,6 @@ function useSendComposableDMOperation(): (
const outboundP2PMessages = await createMessagesToPeersFromDMOp(
op,
recipients,
allPeerUserIDAndDeviceIDs,
utilities,
);

const spec = dmOpSpecs[op.type];
Expand Down Expand Up @@ -384,12 +379,12 @@ function useSendComposableDMOperation(): (
}
},
[
allPeerUserIDAndDeviceIDs,
dispatchWithMetadata,
baseUtilities,
getDMOpsSendingPromise,
localMessageInfos,
createMessagesToPeersFromDMOp,
dispatchWithMetadata,
processOutboundMessages,
baseUtilities,
],
);
}
Expand Down

0 comments on commit 04ea8c3

Please sign in to comment.