Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3571141
D12995.id43172.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D12995.id43172.diff
View Options
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
@@ -14,6 +14,7 @@
type DMOperationSpecification,
} from '../shared/dm-ops/dm-op-utils.js';
import {
+ type AuthMetadata,
IdentityClientContext,
type IdentityClientContextType,
} from '../shared/identity-client-context.js';
@@ -36,6 +37,11 @@
dmOpID: ?string,
) => void,
+sendDMOperation: (op: DMOperationSpecification) => Promise<void>,
+ +broadcastEphemeralMessage: (
+ contentPayload: string,
+ recipients: $ReadOnlyArray<{ +userID: string, +deviceID: string }>,
+ authMetadata: AuthMetadata,
+ ) => Promise<void>,
};
const PeerToPeerContext: React.Context<?PeerToPeerContextType> =
@@ -246,6 +252,68 @@
[peerOlmSessionsCreator, identityContext, sendMessageToDevice],
);
+ const broadcastEphemeralMessage = React.useCallback(
+ async (
+ contentPayload: string,
+ recipients: $ReadOnlyArray<{ +userID: string, +deviceID: string }>,
+ authMetadata: AuthMetadata,
+ ) => {
+ const { userID: thisUserID, deviceID: thisDeviceID } = authMetadata;
+ if (!thisDeviceID || !thisUserID) {
+ throw new Error('No auth metadata');
+ }
+ const { olmAPI } = getConfig();
+ await olmAPI.initializeCryptoAccount();
+
+ // We want it distinct by device ID to avoid potentially creating
+ // multiple Olm sessions with the same device simultaneously.
+ const recipientsDistinctByDeviceID = [
+ ...new Map(recipients.map(item => [item.deviceID, item])).values(),
+ ];
+ const promises = recipientsDistinctByDeviceID.map(async recipient => {
+ try {
+ const encryptedData = await olmAPI.encrypt(
+ contentPayload,
+ recipient.deviceID,
+ );
+ const encryptedMessage: EncryptedMessage = {
+ type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE,
+ senderInfo: { deviceID: thisDeviceID, userID: thisUserID },
+ encryptedData,
+ };
+ await sendMessageToDevice({
+ deviceID: recipient.deviceID,
+ payload: JSON.stringify(encryptedMessage),
+ });
+ } catch {
+ try {
+ await peerOlmSessionsCreator(recipient.userID, recipient.deviceID);
+ const encryptedData = await olmAPI.encrypt(
+ contentPayload,
+ recipient.deviceID,
+ );
+ const encryptedMessage: EncryptedMessage = {
+ type: peerToPeerMessageTypes.ENCRYPTED_MESSAGE,
+ senderInfo: { deviceID: thisDeviceID, userID: thisUserID },
+ encryptedData,
+ };
+ await sendMessageToDevice({
+ deviceID: recipient.deviceID,
+ payload: JSON.stringify(encryptedMessage),
+ });
+ } catch (err) {
+ console.warn(
+ `Error sending Olm-encrypted message to device ${recipient.deviceID}:`,
+ err,
+ );
+ }
+ }
+ });
+ await Promise.all(promises);
+ },
+ [peerOlmSessionsCreator, sendMessageToDevice],
+ );
+
React.useEffect(() => {
const intervalID = setInterval(
processOutboundMessages,
@@ -258,8 +326,9 @@
() => ({
processOutboundMessages,
sendDMOperation,
+ broadcastEphemeralMessage,
}),
- [processOutboundMessages, sendDMOperation],
+ [broadcastEphemeralMessage, processOutboundMessages, sendDMOperation],
);
return (
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 29, 7:47 AM (5 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2731241
Default Alt Text
D12995.id43172.diff (3 KB)
Attached To
Mode
D12995: [lib] Add hook to broadcast Olm-encrypted messages
Attached
Detach File
Event Timeline
Log In to Comment