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.