From 8583232873ec47a040f194ba5d5743045e193fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 9 Apr 2020 16:27:09 +0200 Subject: [PATCH] base_client: Expose the methods necessary for message encryption. --- src/base_client.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/base_client.rs b/src/base_client.rs index 1844d431..42e61575 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -41,11 +41,15 @@ use tokio::sync::Mutex; #[cfg(feature = "encryption")] use crate::crypto::{OlmMachine, OneTimeKeys}; #[cfg(feature = "encryption")] +use ruma_client_api::r0::client_exchange::send_event_to_device; +#[cfg(feature = "encryption")] use ruma_client_api::r0::keys::{ claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, upload_keys::Response as KeysUploadResponse, DeviceKeys, KeyAlgorithm, }; #[cfg(feature = "encryption")] +use ruma_events::room::message::MessageEventContent; +#[cfg(feature = "encryption")] use ruma_identifiers::DeviceId; pub type Token = String; @@ -424,6 +428,44 @@ impl Client { } } + /// Get a to-device request that will share a group session for a room. + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] + pub async fn share_group_session( + &self, + room_id: &RoomId, + ) -> Result> { + let room = self.get_room(room_id).expect("No room found"); + let mut olm = self.olm.lock().await; + + match &mut *olm { + Some(o) => { + let room = room.lock().await; + let members = room.members.keys(); + Ok(o.share_megolm_session(room_id, members).await?) + } + None => panic!("Olm machine wasn't started"), + } + } + + /// Encrypt a message event content. + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] + pub async fn encrypt( + &self, + room_id: &RoomId, + content: MessageEventContent, + ) -> Result { + let mut olm = self.olm.lock().await; + + match &mut *olm { + Some(o) => Ok(MessageEventContent::Encrypted( + o.encrypt(room_id, content).await?, + )), + None => panic!("Olm machine wasn't started"), + } + } + /// Get a tuple of device and one-time keys that need to be uploaded. /// /// Returns an empty error if no keys need to be uploaded.