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::time::delay_for as sleep;
#[cfg(feature = "encryption")]
use tracing::debug;
use tracing::{debug, warn};
use tracing::{info, instrument, trace};
use http::Method as HttpMethod;
@ -37,7 +37,7 @@ use reqwest::header::{HeaderValue, InvalidHeaderValue};
use url::Url;
use crate::events::room::message::MessageEventContent;
use crate::events::{EventJson, EventType};
use crate::events::EventType;
use crate::identifiers::{RoomId, RoomIdOrAliasId, UserId};
use crate::Endpoint;
@ -212,8 +212,6 @@ impl SyncSettings {
}
}
#[cfg(feature = "encryption")]
use api::r0::client_exchange::send_event_to_device;
#[cfg(feature = "encryption")]
use api::r0::keys::{claim_keys, get_keys, upload_keys, KeyAlgorithm};
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::session::login;
use api::r0::sync::sync_events;
#[cfg(feature = "encryption")]
use api::r0::to_device::send_event_to_device;
impl AsyncClient {
/// 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(
&self,
alias: &RoomIdOrAliasId,
server_name: &str,
server_names: &[String],
) -> Result<join_room_by_id_or_alias::Response> {
let request = join_room_by_id_or_alias::Request {
room_id_or_alias: alias.clone(),
server_name: server_name.to_owned(),
server_name: server_names.to_owned(),
third_party_signed: None,
};
self.send(request).await
@ -694,14 +694,16 @@ impl AsyncClient {
}
// look at AccountData to further cut down users by collecting ignored users
for account_data in &mut room.account_data.events {
{
if let Ok(e) = account_data.deserialize() {
let mut client = self.base_client.write().await;
if client.receive_account_data_event(&room_id, &e).await {
updated = true;
if let Some(account_data) = &room.account_data {
for account_data in &account_data.events {
{
if let Ok(e) = account_data.deserialize() {
let mut client = self.base_client.write().await;
if client.receive_account_data_event(&room_id, &e).await {
updated = true;
}
client.emit_account_data_event(room_id, &e).await;
}
client.emit_account_data_event(room_id, &e).await;
}
}
}
@ -833,11 +835,19 @@ impl AsyncClient {
#[cfg(feature = "encryption")]
{
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 {
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(
&self,
room_id: &RoomId,
#[allow(unused_mut)] mut content: MessageEventContent,
content: MessageEventContent,
txn_id: Option<Uuid>,
) -> Result<create_message_event::Response> {
#[allow(unused_mut)]
let mut event_type = EventType::RoomMessage;
#[allow(unused_mut)]
let mut raw_content = serde_json::value::to_raw_value(&content)?;
#[cfg(feature = "encryption")]
{
@ -1019,12 +1031,14 @@ impl AsyncClient {
self.share_group_session(room_id).await?;
}
content = self
.base_client
.read()
.await
.encrypt(room_id, content)
.await?;
raw_content = serde_json::value::to_raw_value(
&self
.base_client
.read()
.await
.encrypt(room_id, content)
.await?,
)?;
event_type = EventType::RoomEncrypted;
}
}
@ -1033,7 +1047,7 @@ impl AsyncClient {
room_id: room_id.clone(),
event_type,
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?;

View File

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

View File

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

View File

@ -20,39 +20,39 @@ async-trait = "0.1.30"
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_json = { version = "1.0.51" }
cjson = { version = "0.1.0" }
serde_json = "1.0.52"
cjson = "0.1.0"
zeroize = { version = "1.1.0", features = ["zeroize_derive"] }
uuid = { version = "0.8.1", features = ["v4"] }
url = "2.1.1"
# Misc dependencies
thiserror = "1.0.14"
thiserror = "1.0.16"
tracing = "0.1.13"
atomic = "0.4.5"
dashmap = "3.10.0"
dashmap = "3.11.1"
[dependencies.tracing-futures]
version = "0.2.3"
version = "0.2.4"
default-features = false
features = ["std", "std-future"]
[dependencies.tokio]
version = "0.2.16"
version = "0.2.20"
default-features = false
features = ["sync", "time"]
[dependencies.sqlx]
version = "0.3.3"
version = "0.3.4"
optional = true
default-features = false
features = ["runtime-tokio", "sqlite"]
[dev-dependencies]
tokio = { version = "0.2.16", features = ["rt-threaded", "macros"] }
ruma-identifiers = { version = "0.16.0", features = ["rand"] }
serde_json = "1.0.51"
tokio = { version = "0.2.20", features = ["rt-threaded", "macros"] }
ruma-identifiers = { version = "0.16.1", features = ["rand"] }
serde_json = "1.0.52"
tempfile = "3.1.0"
http = "*"
http = "0.2.1"

View File

@ -138,7 +138,8 @@ impl Device {
device_keys
.unsigned
.as_ref()
.map(|d| d.device_display_name.clone()),
.map(|d| d.device_display_name.clone())
.flatten(),
);
let _ = mem::replace(
@ -202,7 +203,8 @@ impl From<&DeviceKeys> for Device {
device_keys
.unsigned
.as_ref()
.map(|d| d.device_display_name.clone()),
.map(|d| d.device_display_name.clone())
.flatten(),
),
deleted: Arc::new(AtomicBool::new(false)),
trust_state: Arc::new(Atomic::new(TrustState::Unset)),
@ -294,7 +296,7 @@ pub(crate) mod test {
let mut device_keys = device_keys();
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);
assert_eq!(

View File

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

View File

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

View File

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