Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3337636
D12995.id43165.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D12995.id43165.diff
View Options
diff --git a/lib/hooks/peer-list-hooks.js b/lib/hooks/peer-list-hooks.js
--- a/lib/hooks/peer-list-hooks.js
+++ b/lib/hooks/peer-list-hooks.js
@@ -143,6 +143,76 @@
);
}
+// Sends Olm-encrypted message to multiple peers
+function useBroadcastOlmMessage(): (
+ contentPayload: string,
+ recipients: $ReadOnlyArray<{ +userID: string, +deviceID: string }>,
+) => Promise<void> {
+ const identityContext = React.useContext(IdentityClientContext);
+ if (!identityContext) {
+ throw new Error('Identity service client is not initialized');
+ }
+ const { getAuthMetadata } = identityContext;
+
+ const { sendMessageToDevice } = useTunnelbroker();
+ const { createOlmSessionsWithPeer } = usePeerOlmSessionsCreatorContext();
+
+ return React.useCallback(
+ async (contentPayload, recipients) => {
+ const authMetadata = await getAuthMetadata();
+ const { userID: thisUserID, deviceID: thisDeviceID } = authMetadata;
+ if (!thisDeviceID || !thisUserID) {
+ throw new Error('No auth metadata');
+ }
+ const { olmAPI } = getConfig();
+ await olmAPI.initializeCryptoAccount();
+ for (const recipient of recipients) {
+ 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 createOlmSessionsWithPeer(
+ 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,
+ );
+ }
+ }
+ }
+ },
+ [createOlmSessionsWithPeer, getAuthMetadata, sendMessageToDevice],
+ );
+}
+
function useBroadcastAccountDeletion(
options: { broadcastToOwnDevices?: boolean } = {},
): () => Promise<void> {
@@ -225,4 +295,5 @@
useBroadcastDeviceListUpdates,
useGetAndUpdateDeviceListsForUsers,
useBroadcastAccountDeletion,
+ useBroadcastOlmMessage,
};
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 22, 4:56 PM (16 h, 25 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2562929
Default Alt Text
D12995.id43165.diff (2 KB)
Attached To
Mode
D12995: [lib] Add hook to broadcast Olm-encrypted messages
Attached
Detach File
Event Timeline
Log In to Comment