Page MenuHomePhabricator

D13150.diff
No OneTemporary

D13150.diff

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
@@ -38,7 +38,9 @@
outboundMessageIDs: ?$ReadOnlyArray<string>,
dmOpID: ?string,
) => void,
- +sendDMOperation: (op: OutboundDMOperationSpecification) => Promise<void>,
+ +sendDMOperation: (
+ op: OutboundDMOperationSpecification,
+ ) => Promise<$ReadOnlyArray<string>>,
+broadcastEphemeralMessage: (
contentPayload: string,
recipients: $ReadOnlyArray<{ +userID: string, +deviceID: string }>,
@@ -61,14 +63,19 @@
identityContext: IdentityClientContextType,
peerOlmSessionsCreator: (userID: string, deviceID: string) => Promise<void>,
messageIDs: ?$ReadOnlyArray<string>,
-): Promise<void> {
- const authMetadata = await identityContext.getAuthMetadata();
+): Promise<$ReadOnlyArray<string>> {
+ let authMetadata;
+ try {
+ authMetadata = await identityContext.getAuthMetadata();
+ } catch (e) {
+ return [];
+ }
if (
!authMetadata.deviceID ||
!authMetadata.userID ||
!authMetadata.accessToken
) {
- return;
+ return [];
}
const { olmAPI, sqliteAPI } = getConfig();
@@ -90,31 +97,38 @@
}
}
+ const sentMessagesMap: { [messageID: string]: boolean } = {};
+
const sendMessageToPeer = async (
message: OutboundP2PMessage,
): Promise<void> => {
if (!authMetadata.deviceID || !authMetadata.userID) {
return;
}
- const encryptedMessage: EncryptedMessage = {
- type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE,
- senderInfo: {
- deviceID: authMetadata.deviceID,
- userID: authMetadata.userID,
- },
- encryptedData: JSON.parse(message.ciphertext),
- };
- await sendMessage(
- {
- deviceID: message.deviceID,
- payload: JSON.stringify(encryptedMessage),
- },
- message.messageID,
- );
- await sqliteAPI.markOutboundP2PMessageAsSent(
- message.messageID,
- message.deviceID,
- );
+ try {
+ const encryptedMessage: EncryptedMessage = {
+ type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE,
+ senderInfo: {
+ deviceID: authMetadata.deviceID,
+ userID: authMetadata.userID,
+ },
+ encryptedData: JSON.parse(message.ciphertext),
+ };
+ await sendMessage(
+ {
+ deviceID: message.deviceID,
+ payload: JSON.stringify(encryptedMessage),
+ },
+ message.messageID,
+ );
+ await sqliteAPI.markOutboundP2PMessageAsSent(
+ message.messageID,
+ message.deviceID,
+ );
+ sentMessagesMap[message.messageID] = true;
+ } catch (e) {
+ console.error(e);
+ }
};
for (const peerDeviceID in devicesMap) {
@@ -157,9 +171,14 @@
}
} else if (message.status === outboundP2PMessageStatuses.encrypted) {
await sendMessageToPeer(message);
+ } else if (message.status === outboundP2PMessageStatuses.sent) {
+ // Handle edge-case when message was sent, but it wasn't updated
+ // in the message store.
+ sentMessagesMap[message.messageID] = true;
}
}
}
+ return Object.keys(sentMessagesMap);
}
const AUTOMATIC_RETRY_FREQUENCY = 30 * 1000;
@@ -173,15 +192,24 @@
const dispatch = useDispatch();
const dmOpsSendingPromiseResolvers = React.useRef<
- Map<string, { +resolve: () => mixed, +reject: Error => mixed }>,
+ Map<
+ string,
+ {
+ +resolve: ($ReadOnlyArray<string>) => mixed,
+ +reject: Error => mixed,
+ },
+ >,
>(new Map());
const allPeerUserIDAndDeviceIDs = useSelector(getAllPeerUserIDAndDeviceIDs);
const currentUserInfo = useSelector(state => state.currentUserInfo);
+ // This returns a promise that will be resolved with arrays of successfully
+ // sent messages, so in case of failing all messages (e.g. no internet
+ // connection) it will still resolve but with an empty array.
const sendDMOperation = React.useCallback(
async (op: OutboundDMOperationSpecification) => {
const dmOpID = uuid.v4();
- const promise = new Promise<void>((resolve, reject) => {
+ const promise = new Promise<$ReadOnlyArray<string>>((resolve, reject) => {
dmOpsSendingPromiseResolvers.current.set(dmOpID, { resolve, reject });
});
@@ -223,7 +251,7 @@
do {
const queueFront = processingQueue.current.shift();
try {
- await processOutboundP2PMessages(
+ const sentMessagesIDs = await processOutboundP2PMessages(
sendMessageToDevice,
identityContext,
peerOlmSessionsCreator,
@@ -232,7 +260,7 @@
if (queueFront.dmOpID) {
dmOpsSendingPromiseResolvers.current
.get(queueFront.dmOpID)
- ?.resolve?.();
+ ?.resolve?.(sentMessagesIDs);
}
} catch (e) {
console.log(

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 26, 2:48 PM (20 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2585179
Default Alt Text
D13150.diff (4 KB)

Event Timeline