diff --git a/Cargo.toml b/Cargo.toml index 897be39a..44af39f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,68 +1,6 @@ -[package] -authors = ["Damir Jelić = std::result::Result; diff --git a/src/event_emitter/mod.rs b/matrix_sdk/src/event_emitter/mod.rs similarity index 99% rename from src/event_emitter/mod.rs rename to matrix_sdk/src/event_emitter/mod.rs index b233b2da..c49b1b65 100644 --- a/src/event_emitter/mod.rs +++ b/matrix_sdk/src/event_emitter/mod.rs @@ -238,7 +238,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("tests/data/sync.json") + .with_body_from_file("../test_data/sync.json") .create(); let vec = Arc::new(Mutex::new(Vec::new())); diff --git a/src/lib.rs b/matrix_sdk/src/lib.rs similarity index 95% rename from src/lib.rs rename to matrix_sdk/src/lib.rs index 93bfa375..d3ec1178 100644 --- a/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -44,14 +44,11 @@ mod session; #[cfg(test)] pub mod test_builder; -#[cfg(feature = "encryption")] -mod crypto; - pub use async_client::{AsyncClient, AsyncClientConfig, SyncSettings}; pub use base_client::Client; -#[cfg(feature = "encryption")] -pub use crypto::{Device, TrustState}; pub use event_emitter::EventEmitter; +#[cfg(feature = "encryption")] +pub use matrix_sdk_crypto::{Device, TrustState}; pub use models::Room; pub use request_builder::{MessagesRequestBuilder, RoomBuilder}; diff --git a/src/models/mod.rs b/matrix_sdk/src/models/mod.rs similarity index 100% rename from src/models/mod.rs rename to matrix_sdk/src/models/mod.rs diff --git a/src/models/room.rs b/matrix_sdk/src/models/room.rs similarity index 98% rename from src/models/room.rs rename to matrix_sdk/src/models/room.rs index e96f8f1d..64c9f442 100644 --- a/src/models/room.rs +++ b/matrix_sdk/src/models/room.rs @@ -484,7 +484,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("tests/data/sync.json") + .with_body_from_file("../test_data/sync.json") .create(); let client = AsyncClient::new(homeserver, Some(session)).unwrap(); @@ -514,9 +514,9 @@ mod test { let uid = UserId::try_from("@example:localhost").unwrap(); let mut bld = EventBuilder::default() - .add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember) + .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) .add_room_event_from_file( - "./tests/data/events/power_levels.json", + "../test_data/events/power_levels.json", RoomEvent::RoomPowerLevels, ) .build_room_runner(&rid, &uid); @@ -542,7 +542,7 @@ mod test { let uid = UserId::try_from("@example:localhost").unwrap(); let mut bld = EventBuilder::default() - .add_state_event_from_file("./tests/data/events/aliases.json", StateEvent::RoomAliases) + .add_state_event_from_file("../test_data/events/aliases.json", StateEvent::RoomAliases) .build_room_runner(&rid, &uid); let room = bld.to_room(); @@ -557,7 +557,7 @@ mod test { let mut bld = EventBuilder::default() .add_state_event_from_file( - "./tests/data/events/alias.json", + "../test_data/events/alias.json", StateEvent::RoomCanonicalAlias, ) .build_room_runner(&rid, &uid); @@ -573,7 +573,7 @@ mod test { let uid = UserId::try_from("@example:localhost").unwrap(); let mut bld = EventBuilder::default() - .add_state_event_from_file("./tests/data/events/name.json", StateEvent::RoomName) + .add_state_event_from_file("../test_data/events/name.json", StateEvent::RoomName) .build_room_runner(&rid, &uid); let room = bld.to_room(); @@ -587,7 +587,7 @@ mod test { let mut bld = EventBuilder::default().build_with_response( // this sync has no room.name or room.alias events so only relies on summary - "tests/data/sync_with_summary.json", + "../test_data/sync_with_summary.json", "GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ); diff --git a/src/models/room_member.rs b/matrix_sdk/src/models/room_member.rs similarity index 96% rename from src/models/room_member.rs rename to matrix_sdk/src/models/room_member.rs index e1211112..f9275728 100644 --- a/src/models/room_member.rs +++ b/matrix_sdk/src/models/room_member.rs @@ -199,9 +199,9 @@ mod test { let rid = RoomId::try_from("!roomid:room.com").unwrap(); let uid = UserId::try_from("@example:localhost").unwrap(); let mut bld = EventBuilder::default() - .add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember) + .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) .add_room_event_from_file( - "./tests/data/events/power_levels.json", + "../test_data/events/power_levels.json", RoomEvent::RoomPowerLevels, ) .build_room_runner(&rid, &uid); @@ -220,12 +220,12 @@ mod test { let rid = RoomId::try_from("!roomid:room.com").unwrap(); let uid = UserId::try_from("@example:localhost").unwrap(); let mut bld = EventBuilder::default() - .add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember) + .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) .add_room_event_from_file( - "./tests/data/events/power_levels.json", + "../test_data/events/power_levels.json", RoomEvent::RoomPowerLevels, ) - .add_presence_event_from_file("./tests/data/events/presence.json") + .add_presence_event_from_file("../test_data/events/presence.json") .build_room_runner(&rid, &uid); let room = bld.to_room(); diff --git a/src/request_builder.rs b/matrix_sdk/src/request_builder.rs similarity index 99% rename from src/request_builder.rs rename to matrix_sdk/src/request_builder.rs index bdfa9168..9f1db9df 100644 --- a/src/request_builder.rs +++ b/matrix_sdk/src/request_builder.rs @@ -307,7 +307,7 @@ mod test { let _m = mock("POST", "/_matrix/client/r0/createRoom") .with_status(200) - .with_body_from_file("./tests/data/room_id.json") + .with_body_from_file("../test_data/room_id.json") .create(); let session = Session { @@ -354,7 +354,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/messages".to_string()), ) .with_status(200) - .with_body_from_file("./tests/data/room_messages.json") + .with_body_from_file("../test_data/room_messages.json") .create(); let session = Session { diff --git a/src/session.rs b/matrix_sdk/src/session.rs similarity index 100% rename from src/session.rs rename to matrix_sdk/src/session.rs diff --git a/src/test_builder.rs b/matrix_sdk/src/test_builder.rs similarity index 100% rename from src/test_builder.rs rename to matrix_sdk/src/test_builder.rs diff --git a/tests/async_client_tests.rs b/matrix_sdk/tests/async_client_tests.rs similarity index 93% rename from tests/async_client_tests.rs rename to matrix_sdk/tests/async_client_tests.rs index 3c9dacdc..ae5baf6f 100644 --- a/tests/async_client_tests.rs +++ b/matrix_sdk/tests/async_client_tests.rs @@ -14,7 +14,7 @@ async fn login() { let _m = mock("POST", "/_matrix/client/r0/login") .with_status(200) - .with_body_from_file("tests/data/login_response.json") + .with_body_from_file("../test_data/login_response.json") .create(); let client = AsyncClient::new(homeserver, None).unwrap(); @@ -43,7 +43,7 @@ async fn sync() { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("tests/data/sync.json") + .with_body_from_file("../test_data/sync.json") .create(); let client = AsyncClient::new(homeserver, Some(session)).unwrap(); @@ -72,7 +72,7 @@ async fn room_names() { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("tests/data/sync.json") + .with_body_from_file("../test_data/sync.json") .create(); let client = AsyncClient::new(homeserver, Some(session)).unwrap(); diff --git a/matrix_sdk_crypto/Cargo.toml b/matrix_sdk_crypto/Cargo.toml new file mode 100644 index 00000000..373d8f76 --- /dev/null +++ b/matrix_sdk_crypto/Cargo.toml @@ -0,0 +1,58 @@ +[package] +authors = ["Damir Jelić for Device { for (key_id, key) in device_keys.keys.iter() { let key_id = key_id.0; - keys.insert(key_id, key.clone()); + let _ = keys.insert(key_id, key.clone()); } Device { @@ -221,9 +221,9 @@ pub(crate) mod test { use serde_json::json; use std::convert::{From, TryFrom}; - use crate::api::r0::keys::{DeviceKeys, KeyAlgorithm}; - use crate::crypto::device::{Device, TrustState}; - use crate::identifiers::UserId; + use crate::device::{Device, TrustState}; + use matrix_sdk_types::api::r0::keys::{DeviceKeys, KeyAlgorithm}; + use matrix_sdk_types::identifiers::UserId; fn device_keys() -> DeviceKeys { let user_id = UserId::try_from("@alice:example.org").unwrap(); diff --git a/src/crypto/error.rs b/matrix_sdk_crypto/src/error.rs similarity index 100% rename from src/crypto/error.rs rename to matrix_sdk_crypto/src/error.rs diff --git a/src/crypto/mod.rs b/matrix_sdk_crypto/src/lib.rs similarity index 85% rename from src/crypto/mod.rs rename to matrix_sdk_crypto/src/lib.rs index 992ac022..fa7f9b27 100644 --- a/src/crypto/mod.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! This is the encryption part of the matrix-sdk. It contains a state machine +//! that will aid in adding encryption support to a client library. + mod device; mod error; mod machine; diff --git a/src/crypto/machine.rs b/matrix_sdk_crypto/src/machine.rs similarity index 98% rename from src/crypto/machine.rs rename to matrix_sdk_crypto/src/machine.rs index 34568505..a5567cad 100644 --- a/src/crypto/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -31,8 +31,8 @@ use super::store::memorystore::MemoryStore; use super::store::sqlite::SqliteStore; use super::{device::Device, CryptoStore}; -use crate::api; -use crate::events::{ +use matrix_sdk_types::api; +use matrix_sdk_types::events::{ collections::all::RoomEvent, room::encrypted::{ CiphertextInfo, EncryptedEvent, EncryptedEventContent, MegolmV1AesSha2Content, @@ -45,7 +45,7 @@ use crate::events::{ }, Algorithm, EventJson, EventType, }; -use crate::identifiers::{DeviceId, RoomId, UserId}; +use matrix_sdk_types::identifiers::{DeviceId, RoomId, UserId}; use api::r0::keys; use api::r0::{ @@ -94,7 +94,7 @@ impl std::fmt::Debug for OlmMachine { } impl OlmMachine { - const ALGORITHMS: &'static [&'static ruma_events::Algorithm] = &[ + const ALGORITHMS: &'static [&'static Algorithm] = &[ &Algorithm::OlmV1Curve25519AesSha2, &Algorithm::MegolmV1AesSha2, ]; @@ -136,7 +136,6 @@ impl OlmMachine { } }; - // TODO load the tracked users here. Ok(OlmMachine { user_id: user_id.clone(), device_id: device_id.to_owned(), @@ -1100,7 +1099,15 @@ impl OlmMachine { } // TODO accept an algorithm here - pub(crate) async fn share_group_session<'a, I>( + /// Get to-device requests to share a group session with users in a room. + /// + /// # Arguments + /// + /// `room_id` - The room id of the room where the group session will be + /// used. + /// + /// `users` - The list of users that should receive the group session. + pub async fn share_group_session<'a, I>( &mut self, room_id: &RoomId, users: I, @@ -1395,7 +1402,7 @@ mod test { static USER_ID: &str = "@bob:example.org"; static DEVICE_ID: &str = "DEVICEID"; - use js_int::UInt; + use matrix_sdk_types::js_int::UInt; use std::collections::BTreeMap; use std::convert::TryFrom; use std::fs::File; @@ -1403,12 +1410,16 @@ mod test { use std::sync::atomic::AtomicU64; use std::time::SystemTime; + use http::Response; use serde_json::json; - use crate::api::r0::{client_exchange::send_event_to_device::Request as ToDeviceRequest, keys}; - use crate::crypto::machine::{OlmMachine, OneTimeKeys}; - use crate::crypto::Device; - use crate::events::{ + use crate::machine::{OlmMachine, OneTimeKeys}; + use crate::Device; + + use matrix_sdk_types::api::r0::{ + client_exchange::send_event_to_device::Request as ToDeviceRequest, keys, + }; + use matrix_sdk_types::events::{ collections::all::RoomEvent, room::{ encrypted::{EncryptedEvent, EncryptedEventContent}, @@ -1417,9 +1428,7 @@ mod test { to_device::{AnyToDeviceEvent, ToDeviceEncrypted}, EventJson, EventType, }; - use crate::identifiers::{DeviceId, EventId, RoomId, UserId}; - - use http::Response; + use matrix_sdk_types::identifiers::{DeviceId, EventId, RoomId, UserId}; fn alice_id() -> UserId { UserId::try_from("@alice:example.org").unwrap() @@ -1444,12 +1453,12 @@ mod test { } fn keys_upload_response() -> keys::upload_keys::Response { - let data = response_from_file("tests/data/keys_upload.json"); + let data = response_from_file("../test_data/keys_upload.json"); keys::upload_keys::Response::try_from(data).expect("Can't parse the keys upload response") } fn keys_query_response() -> keys::get_keys::Response { - let data = response_from_file("tests/data/keys_query.json"); + let data = response_from_file("../test_data/keys_query.json"); keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response") } diff --git a/src/crypto/memory_stores.rs b/matrix_sdk_crypto/src/memory_stores.rs similarity index 96% rename from src/crypto/memory_stores.rs rename to matrix_sdk_crypto/src/memory_stores.rs index ba1cd6f4..30419037 100644 --- a/src/crypto/memory_stores.rs +++ b/matrix_sdk_crypto/src/memory_stores.rs @@ -20,7 +20,7 @@ use tokio::sync::Mutex; use super::device::Device; use super::olm::{InboundGroupSession, Session}; -use crate::identifiers::{DeviceId, RoomId, UserId}; +use matrix_sdk_types::identifiers::{DeviceId, RoomId, UserId}; /// In-memory store for Olm Sessions. #[derive(Debug)] @@ -210,11 +210,11 @@ impl DeviceStore { mod test { use std::convert::TryFrom; - use crate::crypto::device::test::get_device; - use crate::crypto::memory_stores::{DeviceStore, GroupSessionStore, SessionStore}; - use crate::crypto::olm::test::get_account_and_session; - use crate::crypto::olm::{InboundGroupSession, OutboundGroupSession}; - use crate::identifiers::RoomId; + use crate::device::test::get_device; + use crate::memory_stores::{DeviceStore, GroupSessionStore, SessionStore}; + use crate::olm::test::get_account_and_session; + use crate::olm::{InboundGroupSession, OutboundGroupSession}; + use matrix_sdk_types::identifiers::RoomId; #[tokio::test] async fn test_session_store() { diff --git a/src/crypto/olm.rs b/matrix_sdk_crypto/src/olm.rs similarity index 98% rename from src/crypto/olm.rs rename to matrix_sdk_crypto/src/olm.rs index 76fba626..15c2e45b 100644 --- a/src/crypto/olm.rs +++ b/matrix_sdk_crypto/src/olm.rs @@ -35,8 +35,8 @@ pub use olm_rs::{ utility::OlmUtility, }; -use crate::api::r0::keys::SignedKey; -use crate::identifiers::RoomId; +use matrix_sdk_types::api::r0::keys::SignedKey; +use matrix_sdk_types::identifiers::RoomId; /// The Olm account. /// An account is the central identity for encrypted communication between two @@ -620,10 +620,10 @@ impl std::fmt::Debug for OutboundGroupSession { #[cfg(test)] pub(crate) mod test { - use crate::crypto::olm::{Account, InboundGroupSession, OutboundGroupSession, Session}; - use crate::identifiers::RoomId; + use crate::olm::{Account, InboundGroupSession, OutboundGroupSession, Session}; + use matrix_sdk_types::api::r0::keys::SignedKey; + use matrix_sdk_types::identifiers::RoomId; use olm_rs::session::OlmMessage; - use ruma_client_api::r0::keys::SignedKey; use std::collections::BTreeMap; use std::convert::TryFrom; diff --git a/src/crypto/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs similarity index 92% rename from src/crypto/store/memorystore.rs rename to matrix_sdk_crypto/src/store/memorystore.rs index 3100a2e3..2099b75f 100644 --- a/src/crypto/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -19,9 +19,9 @@ use async_trait::async_trait; use tokio::sync::Mutex; use super::{Account, CryptoStore, InboundGroupSession, Result, Session}; -use crate::crypto::device::Device; -use crate::crypto::memory_stores::{DeviceStore, GroupSessionStore, SessionStore, UserDevices}; -use crate::identifiers::{DeviceId, RoomId, UserId}; +use crate::device::Device; +use crate::memory_stores::{DeviceStore, GroupSessionStore, SessionStore, UserDevices}; +use matrix_sdk_types::identifiers::{DeviceId, RoomId, UserId}; #[derive(Debug)] pub struct MemoryStore { @@ -107,12 +107,12 @@ impl CryptoStore for MemoryStore { mod test { use std::convert::TryFrom; - use crate::crypto::device::test::get_device; - use crate::crypto::olm::test::get_account_and_session; - use crate::crypto::olm::{InboundGroupSession, OutboundGroupSession}; - use crate::crypto::store::memorystore::MemoryStore; - use crate::crypto::store::CryptoStore; - use crate::identifiers::RoomId; + use crate::device::test::get_device; + use crate::olm::test::get_account_and_session; + use crate::olm::{InboundGroupSession, OutboundGroupSession}; + use crate::store::memorystore::MemoryStore; + use crate::store::CryptoStore; + use matrix_sdk_types::identifiers::RoomId; #[tokio::test] async fn test_session_store() { diff --git a/src/crypto/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs similarity index 98% rename from src/crypto/store/mod.rs rename to matrix_sdk_crypto/src/store/mod.rs index cf92a1f2..0cb6283f 100644 --- a/src/crypto/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -26,7 +26,7 @@ use tokio::sync::Mutex; use super::device::Device; use super::memory_stores::UserDevices; use super::olm::{Account, InboundGroupSession, Session}; -use crate::identifiers::{DeviceId, RoomId, UserId}; +use matrix_sdk_types::identifiers::{DeviceId, RoomId, UserId}; use olm_rs::errors::{OlmAccountError, OlmGroupSessionError, OlmSessionError}; pub mod memorystore; diff --git a/src/crypto/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs similarity index 98% rename from src/crypto/store/sqlite.rs rename to matrix_sdk_crypto/src/store/sqlite.rs index 2c92d4fd..0a5686be 100644 --- a/src/crypto/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -29,11 +29,11 @@ use tokio::sync::Mutex; use zeroize::Zeroizing; use super::{Account, CryptoStore, CryptoStoreError, InboundGroupSession, Result, Session}; -use crate::api::r0::keys::KeyAlgorithm; -use crate::crypto::device::{Device, TrustState}; -use crate::crypto::memory_stores::{DeviceStore, GroupSessionStore, SessionStore, UserDevices}; -use crate::events::Algorithm; -use crate::identifiers::{DeviceId, RoomId, UserId}; +use crate::device::{Device, TrustState}; +use crate::memory_stores::{DeviceStore, GroupSessionStore, SessionStore, UserDevices}; +use matrix_sdk_types::api::r0::keys::KeyAlgorithm; +use matrix_sdk_types::events::Algorithm; +use matrix_sdk_types::identifiers::{DeviceId, RoomId, UserId}; pub struct SqliteStore { user_id: Arc, @@ -639,9 +639,9 @@ impl std::fmt::Debug for SqliteStore { #[cfg(test)] mod test { - use crate::api::r0::keys::SignedKey; - use crate::crypto::device::test::get_device; - use crate::crypto::olm::GroupSessionKey; + use crate::device::test::get_device; + use crate::olm::GroupSessionKey; + use matrix_sdk_types::api::r0::keys::SignedKey; use olm_rs::outbound_group_session::OlmOutboundGroupSession; use std::collections::BTreeMap; use tempfile::tempdir; diff --git a/matrix_sdk_types/Cargo.toml b/matrix_sdk_types/Cargo.toml new file mode 100644 index 00000000..e15c5048 --- /dev/null +++ b/matrix_sdk_types/Cargo.toml @@ -0,0 +1,18 @@ +[package] +authors = ["Damir Jelić