Skip to content

Commit

Permalink
[lib] update processOutboundP2PMessages return type
Browse files Browse the repository at this point in the history
Summary:
[ENG-8424](https://linear.app/comm/issue/ENG-8424/update-inputstatecontainer-to-support-multimedia-messages-being-sent).

Addresses https://phab.comm.dev/D13325#inline-76680.

Depends on D13332

Test Plan: Repeat test plan from D13325

Reviewers: tomek

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13351
  • Loading branch information
xsanm committed Sep 16, 2024
1 parent 2c56d0e commit 23686a4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
12 changes: 6 additions & 6 deletions lib/hooks/input-state-container-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function useInputStateContainerSendTextMessage(): (
: threadInfo.members;
const recipientsIDs = recipients.map(recipient => recipient.id);

const messageIDs = await sendComposableDMOperation({
const result = await sendComposableDMOperation({
type: dmOperationSpecificationTypes.OUTBOUND,
op: {
type: 'send_text_message',
Expand All @@ -90,9 +90,9 @@ function useInputStateContainerSendTextMessage(): (
composableMessageID: localID,
});

if (messageIDs.length > 0) {
if (result.result === 'failure' && result.failedMessageIDs.length > 0) {
const e: any = new Error('Failed to send message to all peers');
e.failedOutboundP2PMessageIDs = messageIDs;
e.failedOutboundP2PMessageIDs = result.failedMessageIDs;
throw e;
}
return {
Expand Down Expand Up @@ -170,7 +170,7 @@ function useInputStateContainerSendMultimediaMessage(): (
const messageID = uuid.v4();
const time = Date.now();

const messageIDs = await sendComposableDMOperation({
const result = await sendComposableDMOperation({
type: dmOperationSpecificationTypes.OUTBOUND,
op: {
type: 'send_multimedia_message',
Expand All @@ -192,9 +192,9 @@ function useInputStateContainerSendMultimediaMessage(): (
composableMessageID: localID,
});

if (messageIDs.length > 0) {
if (result.result === 'failure' && result.failedMessageIDs.length > 0) {
const e: any = new Error('Failed to send message to all peers');
e.failedOutboundP2PMessageIDs = messageIDs;
e.failedOutboundP2PMessageIDs = result.failedMessageIDs;
throw e;
}
return {
Expand Down
9 changes: 6 additions & 3 deletions lib/shared/dm-ops/process-dm-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { useGetLatestMessageEdit } from '../../hooks/latest-message-edit.js';
import { useDispatchWithMetadata } from '../../hooks/ops-hooks.js';
import { mergeUpdatesWithMessageInfos } from '../../reducers/message-reducer.js';
import { getAllPeerUserIDAndDeviceIDs } from '../../selectors/user-selectors.js';
import { usePeerToPeerCommunication } from '../../tunnelbroker/peer-to-peer-context.js';
import {
usePeerToPeerCommunication,
type ProcessOutboundP2PMessagesResult,
} from '../../tunnelbroker/peer-to-peer-context.js';
import {
processDMOpsActionType,
queueDMOpsActionType,
Expand Down Expand Up @@ -322,7 +325,7 @@ function useProcessAndSendDMOperation(): (

function useSendComposableDMOperation(): (
dmOperationSpecification: OutboundComposableDMOperationSpecification,
) => Promise<$ReadOnlyArray<string>> {
) => Promise<ProcessOutboundP2PMessagesResult> {
const threadInfos = useSelector(state => state.threadStore.threadInfos);
const { getDMOpsSendingPromise } = usePeerToPeerCommunication();
const dispatchWithMetadata = useDispatchWithMetadata();
Expand All @@ -333,7 +336,7 @@ function useSendComposableDMOperation(): (
return React.useCallback(
async (
dmOperationSpecification: OutboundComposableDMOperationSpecification,
): Promise<$ReadOnlyArray<string>> => {
): Promise<ProcessOutboundP2PMessagesResult> => {
const { promise, dmOpID } = getDMOpsSendingPromise();

const { op, composableMessageID, recipients } = dmOperationSpecification;
Expand Down
51 changes: 38 additions & 13 deletions lib/tunnelbroker/peer-to-peer-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type PeerToPeerContextType = {
notificationsCreationData: ?NotificationsCreationData,
) => void,
+getDMOpsSendingPromise: () => {
+promise: Promise<$ReadOnlyArray<string>>,
+promise: Promise<ProcessOutboundP2PMessagesResult>,
+dmOpID: string,
},
+broadcastEphemeralMessage: (
Expand All @@ -53,6 +53,13 @@ type Props = {
+children: React.Node,
};

export type ProcessOutboundP2PMessagesResult =
| { +result: 'success' }
| {
+result: 'failure',
+failedMessageIDs: $ReadOnlyArray<string>,
};

async function processOutboundP2PMessages(
sendMessage: (
message: TunnelbrokerClientMessageToDevice,
Expand All @@ -61,19 +68,25 @@ async function processOutboundP2PMessages(
identityContext: IdentityClientContextType,
peerOlmSessionsCreator: (userID: string, deviceID: string) => Promise<void>,
messageIDs: ?$ReadOnlyArray<string>,
): Promise<$ReadOnlyArray<string>> {
): Promise<ProcessOutboundP2PMessagesResult> {
let authMetadata;
try {
authMetadata = await identityContext.getAuthMetadata();
} catch (e) {
return [];
return {
result: 'failure',
failedMessageIDs: messageIDs ?? [],
};
}
if (
!authMetadata.deviceID ||
!authMetadata.userID ||
!authMetadata.accessToken
) {
return [];
return {
result: 'failure',
failedMessageIDs: messageIDs ?? [],
};
}

const { olmAPI, sqliteAPI } = getConfig();
Expand Down Expand Up @@ -194,9 +207,19 @@ async function processOutboundP2PMessages(
);

await Promise.all(devicePromises);
// Returning messageIDs of failed messages.
const sentMessages = new Set(Object.keys(sentMessagesMap));
return messageIDs?.filter(id => !sentMessages.has(id)) ?? [];

const sentMessagesSet = new Set(Object.keys(sentMessagesMap));
const failedMessageIDs =
messageIDs?.filter(id => !sentMessagesSet.has(id)) ?? [];
if (failedMessageIDs.length > 0) {
return {
result: 'failure',
failedMessageIDs,
};
}
return {
result: 'success',
};
}

const AUTOMATIC_RETRY_FREQUENCY = 30 * 1000;
Expand All @@ -212,7 +235,7 @@ function PeerToPeerProvider(props: Props): React.Node {
Map<
string,
{
+resolve: (messageIDs: $ReadOnlyArray<string>) => mixed,
+resolve: (result: ProcessOutboundP2PMessagesResult) => mixed,
+reject: Error => mixed,
},
>,
Expand All @@ -223,9 +246,11 @@ function PeerToPeerProvider(props: Props): React.Node {
// connection) it will still resolve but with an empty array.
const getDMOpsSendingPromise = React.useCallback(() => {
const dmOpID = uuid.v4();
const promise = new Promise<$ReadOnlyArray<string>>((resolve, reject) => {
dmOpsSendingPromiseResolvers.current.set(dmOpID, { resolve, reject });
});
const promise = new Promise<ProcessOutboundP2PMessagesResult>(
(resolve, reject) => {
dmOpsSendingPromiseResolvers.current.set(dmOpID, { resolve, reject });
},
);

return { promise, dmOpID };
}, []);
Expand Down Expand Up @@ -260,7 +285,7 @@ function PeerToPeerProvider(props: Props): React.Node {
do {
const queueFront = processingQueue.current.shift();
try {
const [sentMessagesIDs] = await Promise.all([
const [result] = await Promise.all([
processOutboundP2PMessages(
sendMessageToDevice,
identityContext,
Expand All @@ -272,7 +297,7 @@ function PeerToPeerProvider(props: Props): React.Node {
if (queueFront.dmOpID) {
dmOpsSendingPromiseResolvers.current
.get(queueFront.dmOpID)
?.resolve?.(sentMessagesIDs);
?.resolve?.(result);
}
} catch (e) {
console.log(
Expand Down

0 comments on commit 23686a4

Please sign in to comment.