matrix-sdk: Fix the last Ruma incompatibilities.

master
Damir Jelić 2020-05-05 15:29:25 +02:00
parent a54fec7ac5
commit 7b6e030823
9 changed files with 142 additions and 138 deletions

View File

@ -28,7 +28,7 @@ use futures::future::Future;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tokio::time::delay_for as sleep; use tokio::time::delay_for as sleep;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use tracing::debug; use tracing::{debug, warn};
use tracing::{info, instrument, trace}; use tracing::{info, instrument, trace};
use http::Method as HttpMethod; use http::Method as HttpMethod;
@ -37,7 +37,7 @@ use reqwest::header::{HeaderValue, InvalidHeaderValue};
use url::Url; use url::Url;
use crate::events::room::message::MessageEventContent; use crate::events::room::message::MessageEventContent;
use crate::events::{EventJson, EventType}; use crate::events::EventType;
use crate::identifiers::{RoomId, RoomIdOrAliasId, UserId}; use crate::identifiers::{RoomId, RoomIdOrAliasId, UserId};
use crate::Endpoint; use crate::Endpoint;
@ -212,8 +212,6 @@ impl SyncSettings {
} }
} }
#[cfg(feature = "encryption")]
use api::r0::client_exchange::send_event_to_device;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use api::r0::keys::{claim_keys, get_keys, upload_keys, KeyAlgorithm}; use api::r0::keys::{claim_keys, get_keys, upload_keys, KeyAlgorithm};
use api::r0::membership::join_room_by_id; use api::r0::membership::join_room_by_id;
@ -229,6 +227,8 @@ use api::r0::message::get_message_events;
use api::r0::room::create_room; use api::r0::room::create_room;
use api::r0::session::login; use api::r0::session::login;
use api::r0::sync::sync_events; use api::r0::sync::sync_events;
#[cfg(feature = "encryption")]
use api::r0::to_device::send_event_to_device;
impl AsyncClient { impl AsyncClient {
/// Creates a new client for making HTTP requests to the given homeserver. /// Creates a new client for making HTTP requests to the given homeserver.
@ -437,11 +437,11 @@ impl AsyncClient {
pub async fn join_room_by_id_or_alias( pub async fn join_room_by_id_or_alias(
&self, &self,
alias: &RoomIdOrAliasId, alias: &RoomIdOrAliasId,
server_name: &str, server_names: &[String],
) -> Result<join_room_by_id_or_alias::Response> { ) -> Result<join_room_by_id_or_alias::Response> {
let request = join_room_by_id_or_alias::Request { let request = join_room_by_id_or_alias::Request {
room_id_or_alias: alias.clone(), room_id_or_alias: alias.clone(),
server_name: server_name.to_owned(), server_name: server_names.to_owned(),
third_party_signed: None, third_party_signed: None,
}; };
self.send(request).await self.send(request).await
@ -694,7 +694,8 @@ impl AsyncClient {
} }
// look at AccountData to further cut down users by collecting ignored users // look at AccountData to further cut down users by collecting ignored users
for account_data in &mut room.account_data.events { if let Some(account_data) = &room.account_data {
for account_data in &account_data.events {
{ {
if let Ok(e) = account_data.deserialize() { if let Ok(e) = account_data.deserialize() {
let mut client = self.base_client.write().await; let mut client = self.base_client.write().await;
@ -705,6 +706,7 @@ impl AsyncClient {
} }
} }
} }
}
// After the room has been created and state/timeline events accounted for we use the room_id of the newly created // After the room has been created and state/timeline events accounted for we use the room_id of the newly created
// room to add any presence events that relate to a user in the current room. This is not super // room to add any presence events that relate to a user in the current room. This is not super
@ -833,11 +835,19 @@ impl AsyncClient {
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
{ {
if self.base_client.read().await.should_upload_keys().await { if self.base_client.read().await.should_upload_keys().await {
let _ = self.keys_upload().await; let response = self.keys_upload().await;
if let Err(e) = response {
warn!("Error while uploading E2EE keys {:?}", e);
}
} }
if self.base_client.read().await.should_query_keys().await { if self.base_client.read().await.should_query_keys().await {
let _ = self.keys_query().await; let response = self.keys_query().await;
if let Err(e) = response {
warn!("Error while querying device keys {:?}", e);
}
} }
} }
@ -972,11 +982,13 @@ impl AsyncClient {
pub async fn room_send( pub async fn room_send(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
#[allow(unused_mut)] mut content: MessageEventContent, content: MessageEventContent,
txn_id: Option<Uuid>, txn_id: Option<Uuid>,
) -> Result<create_message_event::Response> { ) -> Result<create_message_event::Response> {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut event_type = EventType::RoomMessage; let mut event_type = EventType::RoomMessage;
#[allow(unused_mut)]
let mut raw_content = serde_json::value::to_raw_value(&content)?;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
{ {
@ -1019,12 +1031,14 @@ impl AsyncClient {
self.share_group_session(room_id).await?; self.share_group_session(room_id).await?;
} }
content = self raw_content = serde_json::value::to_raw_value(
&self
.base_client .base_client
.read() .read()
.await .await
.encrypt(room_id, content) .encrypt(room_id, content)
.await?; .await?,
)?;
event_type = EventType::RoomEncrypted; event_type = EventType::RoomEncrypted;
} }
} }
@ -1033,7 +1047,7 @@ impl AsyncClient {
room_id: room_id.clone(), room_id: room_id.clone(),
event_type, event_type,
txn_id: txn_id.unwrap_or_else(Uuid::new_v4).to_string(), txn_id: txn_id.unwrap_or_else(Uuid::new_v4).to_string(),
data: EventJson::from(content), data: raw_content,
}; };
let response = self.send(request).await?; let response = self.send(request).await?;

View File

@ -41,15 +41,15 @@ use crate::EventEmitter;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tokio::sync::RwLock; use tokio::sync::RwLock;
#[cfg(feature = "encryption")]
use crate::api::r0::client_exchange::send_event_to_device;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use crate::api::r0::keys::{ use crate::api::r0::keys::{
claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse,
upload_keys::Response as KeysUploadResponse, DeviceKeys, KeyAlgorithm, upload_keys::Response as KeysUploadResponse, DeviceKeys, KeyAlgorithm,
}; };
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use crate::events::room::message::MessageEventContent; use crate::api::r0::to_device::send_event_to_device;
#[cfg(feature = "encryption")]
use crate::events::room::{encrypted::EncryptedEventContent, message::MessageEventContent};
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use crate::identifiers::DeviceId; use crate::identifiers::DeviceId;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
@ -532,18 +532,13 @@ impl Client {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
content: MessageEventContent, content: MessageEventContent,
) -> Result<MessageEventContent> { ) -> Result<EncryptedEventContent> {
let mut olm = self.olm.lock().await; let mut olm = self.olm.lock().await;
// TODO enable this again once we can send encrypted event match &mut *olm {
// contents with ruma. Some(o) => Ok(o.encrypt(room_id, content).await?),
// match &mut *olm { None => panic!("Olm machine wasn't started"),
// Some(o) => Ok(MessageEventContent::Encrypted( }
// o.encrypt(room_id, content).await?,
// )),
// None => panic!("Olm machine wasn't started"),
// }
Ok(content)
} }
/// Get a tuple of device and one-time keys that need to be uploaded. /// Get a tuple of device and one-time keys that need to be uploaded.

View File

@ -117,7 +117,6 @@ impl RoomMember {
None => false, None => false,
// we ignore the error here as only a buggy or malicious server would send this // we ignore the error here as only a buggy or malicious server would send this
Error => false, Error => false,
_ => false,
} }
} }

View File

@ -20,39 +20,39 @@ async-trait = "0.1.30"
matrix-sdk-types = { path = "../matrix_sdk_types" } matrix-sdk-types = { path = "../matrix_sdk_types" }
olm-rs = { version = "0.5.0", features = ["serde"]} olm-rs = { version = "0.5.0", features = ["serde"] }
serde = { version = "1.0.106", features = ["derive"] } serde = { version = "1.0.106", features = ["derive"] }
serde_json = { version = "1.0.51" } serde_json = "1.0.52"
cjson = { version = "0.1.0" } cjson = "0.1.0"
zeroize = { version = "1.1.0", features = ["zeroize_derive"] } zeroize = { version = "1.1.0", features = ["zeroize_derive"] }
uuid = { version = "0.8.1", features = ["v4"] } uuid = { version = "0.8.1", features = ["v4"] }
url = "2.1.1" url = "2.1.1"
# Misc dependencies # Misc dependencies
thiserror = "1.0.14" thiserror = "1.0.16"
tracing = "0.1.13" tracing = "0.1.13"
atomic = "0.4.5" atomic = "0.4.5"
dashmap = "3.10.0" dashmap = "3.11.1"
[dependencies.tracing-futures] [dependencies.tracing-futures]
version = "0.2.3" version = "0.2.4"
default-features = false default-features = false
features = ["std", "std-future"] features = ["std", "std-future"]
[dependencies.tokio] [dependencies.tokio]
version = "0.2.16" version = "0.2.20"
default-features = false default-features = false
features = ["sync", "time"] features = ["sync", "time"]
[dependencies.sqlx] [dependencies.sqlx]
version = "0.3.3" version = "0.3.4"
optional = true optional = true
default-features = false default-features = false
features = ["runtime-tokio", "sqlite"] features = ["runtime-tokio", "sqlite"]
[dev-dependencies] [dev-dependencies]
tokio = { version = "0.2.16", features = ["rt-threaded", "macros"] } tokio = { version = "0.2.20", features = ["rt-threaded", "macros"] }
ruma-identifiers = { version = "0.16.0", features = ["rand"] } ruma-identifiers = { version = "0.16.1", features = ["rand"] }
serde_json = "1.0.51" serde_json = "1.0.52"
tempfile = "3.1.0" tempfile = "3.1.0"
http = "*" http = "0.2.1"

View File

@ -138,7 +138,8 @@ impl Device {
device_keys device_keys
.unsigned .unsigned
.as_ref() .as_ref()
.map(|d| d.device_display_name.clone()), .map(|d| d.device_display_name.clone())
.flatten(),
); );
let _ = mem::replace( let _ = mem::replace(
@ -202,7 +203,8 @@ impl From<&DeviceKeys> for Device {
device_keys device_keys
.unsigned .unsigned
.as_ref() .as_ref()
.map(|d| d.device_display_name.clone()), .map(|d| d.device_display_name.clone())
.flatten(),
), ),
deleted: Arc::new(AtomicBool::new(false)), deleted: Arc::new(AtomicBool::new(false)),
trust_state: Arc::new(Atomic::new(TrustState::Unset)), trust_state: Arc::new(Atomic::new(TrustState::Unset)),
@ -294,7 +296,7 @@ pub(crate) mod test {
let mut device_keys = device_keys(); let mut device_keys = device_keys();
device_keys.unsigned.as_mut().unwrap().device_display_name = device_keys.unsigned.as_mut().unwrap().device_display_name =
"Alice's work computer".to_owned(); Some("Alice's work computer".to_owned());
device.update_device(&device_keys); device.update_device(&device_keys);
assert_eq!( assert_eq!(

View File

@ -17,6 +17,7 @@
#![deny( #![deny(
missing_debug_implementations, missing_debug_implementations,
dead_code,
missing_docs, missing_docs,
trivial_casts, trivial_casts,
trivial_numeric_casts, trivial_numeric_casts,

View File

@ -49,12 +49,11 @@ use matrix_sdk_types::identifiers::{DeviceId, RoomId, UserId};
use api::r0::keys; use api::r0::keys;
use api::r0::{ use api::r0::{
client_exchange::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices},
keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey, SignedKey}, keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey, SignedKey},
sync::sync_events::Response as SyncResponse, sync::sync_events::Response as SyncResponse,
to_device::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices},
}; };
use cjson;
use serde_json::{json, Value}; use serde_json::{json, Value};
use tracing::{debug, error, info, instrument, trace, warn}; use tracing::{debug, error, info, instrument, trace, warn};
@ -1292,10 +1291,10 @@ impl OlmMachine {
// TODO enable this again once we can send encrypted event // TODO enable this again once we can send encrypted event
// contents with ruma. // contents with ruma.
// user_messages.insert( user_messages.insert(
// DeviceIdOrAllDevices::DeviceId(device.device_id().clone()), DeviceIdOrAllDevices::DeviceId(device.device_id().clone()),
// EventJson::from(MessageEventContent::Encrypted(encrypted_content)), serde_json::value::to_raw_value(&encrypted_content)?,
// ); );
} }
message_vec.push(ToDeviceRequest { message_vec.push(ToDeviceRequest {
@ -1546,7 +1545,7 @@ mod test {
use crate::Device; use crate::Device;
use matrix_sdk_types::api::r0::{ use matrix_sdk_types::api::r0::{
client_exchange::send_event_to_device::Request as ToDeviceRequest, keys, keys, to_device::send_event_to_device::Request as ToDeviceRequest,
}; };
use matrix_sdk_types::events::{ use matrix_sdk_types::events::{
collections::all::RoomEvent, collections::all::RoomEvent,
@ -1603,7 +1602,6 @@ mod test {
.values() .values()
.next() .next()
.unwrap() .unwrap()
.json()
.get(), .get(),
) )
.unwrap(); .unwrap();
@ -1991,98 +1989,94 @@ mod test {
} }
} }
// TODO this is disabled so CI passes, we can't enable this until ruma gets #[tokio::test]
// the ability back to send encrypted content. async fn test_room_key_sharing() {
// #[tokio::test] let (mut alice, mut bob) = get_machine_pair_with_session().await;
// async fn test_room_key_sharing() {
// let (mut alice, mut bob) = get_machine_pair_with_session().await;
// let room_id = RoomId::try_from("!test:example.org").unwrap(); let room_id = RoomId::try_from("!test:example.org").unwrap();
// let to_device_requests = alice let to_device_requests = alice
// .share_group_session(&room_id, [bob.user_id.clone()].iter()) .share_group_session(&room_id, [bob.user_id.clone()].iter())
// .await .await
// .unwrap(); .unwrap();
// let event = ToDeviceEncrypted { let event = ToDeviceEncrypted {
// sender: alice.user_id.clone(), sender: alice.user_id.clone(),
// content: to_device_requests_to_content(to_device_requests), content: to_device_requests_to_content(to_device_requests),
// }; };
// let alice_session = alice.outbound_group_sessions.get(&room_id).unwrap(); let alice_session = alice.outbound_group_sessions.get(&room_id).unwrap();
// let event = bob.decrypt_to_device_event(&event).await.unwrap(); let event = bob.decrypt_to_device_event(&event).await.unwrap();
// if let AnyToDeviceEvent::RoomKey(e) = event.deserialize().unwrap() { if let AnyToDeviceEvent::RoomKey(e) = event.deserialize().unwrap() {
// assert_eq!(e.sender, alice.user_id); assert_eq!(e.sender, alice.user_id);
// } else { } else {
// panic!("Event had the wrong type"); panic!("Event had the wrong type");
// } }
// let session = bob let session = bob
// .store .store
// .get_inbound_group_session( .get_inbound_group_session(
// &room_id, &room_id,
// alice.account.identity_keys().curve25519(), alice.account.identity_keys().curve25519(),
// alice_session.session_id(), alice_session.session_id(),
// ) )
// .await; .await;
// assert!(session.unwrap().is_some()); assert!(session.unwrap().is_some());
// } }
// TODO this is disabled so CI passes, we can't enable this until ruma gets #[tokio::test]
// the ability back to send encrypted content. async fn test_megolm_encryption() {
// #[tokio::test] let (mut alice, mut bob) = get_machine_pair_with_setup_sessions().await;
// async fn test_megolm_encryption() { let room_id = RoomId::try_from("!test:example.org").unwrap();
// let (mut alice, mut bob) = get_machine_pair_with_setup_sessions().await;
// let room_id = RoomId::try_from("!test:example.org").unwrap();
// let to_device_requests = alice let to_device_requests = alice
// .share_group_session(&room_id, [bob.user_id().clone()].iter()) .share_group_session(&room_id, [bob.user_id().clone()].iter())
// .await .await
// .unwrap(); .unwrap();
// let event = ToDeviceEncrypted { let event = ToDeviceEncrypted {
// sender: alice.user_id().clone(), sender: alice.user_id().clone(),
// content: to_device_requests_to_content(to_device_requests), content: to_device_requests_to_content(to_device_requests),
// }; };
// bob.decrypt_to_device_event(&event).await.unwrap(); bob.decrypt_to_device_event(&event).await.unwrap();
// let plaintext = "It is a secret to everybody"; let plaintext = "It is a secret to everybody";
// let content = MessageEventContent::Text(TextMessageEventContent::new_plain(plaintext)); let content = MessageEventContent::Text(TextMessageEventContent::new_plain(plaintext));
// let encrypted_content = alice.encrypt(&room_id, content.clone()).await.unwrap(); let encrypted_content = alice.encrypt(&room_id, content.clone()).await.unwrap();
// let event = EncryptedEvent { let event = EncryptedEvent {
// event_id: EventId::new("example.org").unwrap(), event_id: EventId::new("example.org").unwrap(),
// origin_server_ts: SystemTime::now(), origin_server_ts: SystemTime::now(),
// room_id: Some(room_id.clone()), room_id: Some(room_id.clone()),
// sender: alice.user_id().clone(), sender: alice.user_id().clone(),
// content: encrypted_content, content: encrypted_content,
// unsigned: UnsignedData::default(), unsigned: UnsignedData::default(),
// }; };
// let decrypted_event = bob let decrypted_event = bob
// .decrypt_room_event(&event) .decrypt_room_event(&event)
// .await .await
// .unwrap() .unwrap()
// .deserialize() .deserialize()
// .unwrap(); .unwrap();
// let decrypted_event = match decrypted_event { let decrypted_event = match decrypted_event {
// RoomEvent::RoomMessage(e) => e, RoomEvent::RoomMessage(e) => e,
// _ => panic!("Decrypted room event has the wrong type"), _ => panic!("Decrypted room event has the wrong type"),
// }; };
// assert_eq!(&decrypted_event.sender, alice.user_id()); assert_eq!(&decrypted_event.sender, alice.user_id());
// assert_eq!(&decrypted_event.room_id, &Some(room_id)); assert_eq!(&decrypted_event.room_id, &Some(room_id));
// if let MessageEventContent::Text(c) = &decrypted_event.content { if let MessageEventContent::Text(c) = &decrypted_event.content {
// assert_eq!(&c.body, plaintext); assert_eq!(&c.body, plaintext);
// } else { } else {
// panic!("Decrypted event has a missmatched content"); panic!("Decrypted event has a missmatched content");
// } }
// } }
} }

View File

@ -23,7 +23,6 @@ use url::Url;
use async_trait::async_trait; use async_trait::async_trait;
use olm_rs::PicklingMode; use olm_rs::PicklingMode;
use serde_json;
use sqlx::{query, query_as, sqlite::SqliteQueryAs, Connect, Executor, SqliteConnection}; use sqlx::{query, query_as, sqlite::SqliteQueryAs, Connect, Executor, SqliteConnection};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use zeroize::Zeroizing; use zeroize::Zeroizing;

View File

@ -12,7 +12,7 @@ version = "0.1.0"
[dependencies] [dependencies]
js_int = "0.1.5" js_int = "0.1.5"
ruma-api = "0.16.0-rc.3" ruma-api = "0.16.0"
ruma-client-api = "0.8.0-rc.5" ruma-client-api = { git = "https://github.com/matrix-org/ruma-client-api" }
ruma-events = "0.21.0" ruma-events = "0.21.0"
ruma-identifiers = "0.16.1" ruma-identifiers = "0.16.1"