base_client: Expose the methods necessary for message encryption.

master
Damir Jelić 2020-04-09 16:27:09 +02:00
parent 01664bc613
commit 8583232873
1 changed files with 42 additions and 0 deletions

View File

@ -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<Vec<send_event_to_device::Request>> {
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<MessageEventContent> {
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.