Skip to content

Toger5/widget-to-device-with-encryption #4992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: toger5/widget-to-device-without-encryption
Choose a base branch
from
2 changes: 2 additions & 0 deletions bindings/matrix-sdk-ffi/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ pub fn get_element_call_required_permissions(
event_type: "org.matrix.rageshake_request".to_owned(),
},
// To read and send encryption keys
WidgetEventFilter::ToDevice { event_type: "io.element.call.encryption_keys".to_owned() },
// TODO change this to the appropriate to-device version once ready
// remove this once all calling supports to-device encryption
WidgetEventFilter::MessageLikeWithType {
event_type: "io.element.call.encryption_keys".to_owned(),
},
Expand Down
61 changes: 61 additions & 0 deletions crates/matrix-sdk/src/test_utils/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,56 @@ impl MatrixMockServer {
self.mock_endpoint(mock, DeleteRoomKeysVersionEndpoint).expect_default_access_token()
}

/// Creates a prebuilt mock for the `/sendToDevice` endpoint.
///
/// This mock can be used to simulate sending to-device messages in tests.
/// # Examples
///
/// ```
/// # #[cfg(feature = "e2e-encryption")]
/// # {
/// # tokio_test::block_on(async {
/// use std::collections::BTreeMap;
/// use matrix_sdk::{
/// ruma::{
/// serde::Raw,
/// api::client::to_device::send_event_to_device::v3::Request as ToDeviceRequest,
/// to_device::DeviceIdOrAllDevices,
/// user_id,owned_device_id
/// },
/// test_utils::mocks::MatrixMockServer,
/// };
/// use serde_json::json;
///
/// let mock_server = MatrixMockServer::new().await;
/// let client = mock_server.client_builder().build().await;
///
/// mock_server.mock_send_to_device().ok().mock_once().mount().await;
///
/// let request = ToDeviceRequest::new_raw(
/// "m.custom.event".into(),
/// "txn_id".into(),
/// BTreeMap::from([
/// (user_id!("@alice:localhost").to_owned(), BTreeMap::from([(
/// DeviceIdOrAllDevices::AllDevices,
/// Raw::new(&ruma::events::AnyToDeviceEventContent::Dummy(ruma::events::dummy::ToDeviceDummyEventContent {})).unwrap(),
/// )])),
/// ])
/// );
///
/// client
/// .send(request)
/// .await
/// .expect("We should be able to send a to-device message");
/// # anyhow::Ok(()) });
/// # }
/// ```
pub fn mock_send_to_device(&self) -> MockEndpoint<'_, SendToDeviceEndpoint> {
let mock =
Mock::given(method("PUT")).and(path_regex(r"^/_matrix/client/v3/sendToDevice/.*/.*"));
self.mock_endpoint(mock, SendToDeviceEndpoint).expect_default_access_token()
}

/// Create a prebuilt mock for getting the room members in a room.
///
/// # Examples
Expand Down Expand Up @@ -2315,6 +2365,17 @@ impl<'a> MockEndpoint<'a, DeleteRoomKeysVersionEndpoint> {
}
}

/// A prebuilt mock for the `/sendToDevice` endpoint.
///
/// This mock can be used to simulate sending to-device messages in tests.
pub struct SendToDeviceEndpoint;
impl<'a> MockEndpoint<'a, SendToDeviceEndpoint> {
/// Returns a successful response with default data.
pub fn ok(self) -> MatrixMock<'a> {
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
}
}

/// A prebuilt mock for `GET /members` request.
pub struct GetRoomMembersEndpoint;

Expand Down
3 changes: 1 addition & 2 deletions crates/matrix-sdk/src/widget/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,8 @@ impl<'de> Deserialize<'de> for Capabilities {
mod tests {
use ruma::events::StateEventType;

use crate::widget::filter::ToDeviceEventFilter;

use super::*;
use crate::widget::filter::ToDeviceEventFilter;

#[test]
fn deserialization_of_no_capabilities() {
Expand Down
12 changes: 6 additions & 6 deletions crates/matrix-sdk/src/widget/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ impl<'a> TryFrom<&'a Raw<AnyTimelineEvent>> for FilterInput<'a> {
type Error = serde_json::Error;

fn try_from(raw_event: &'a Raw<AnyTimelineEvent>) -> Result<Self, Self::Error> {
// FilterInput first checks if it can deserialize as a state event (state_key exists)
// and then as a message like event.
// FilterInput first checks if it can deserialize as a state event (state_key
// exists) and then as a message like event.
raw_event.deserialize_as()
}
}
Expand All @@ -244,9 +244,9 @@ pub struct FilterInputToDevice<'a> {
impl<'a> TryFrom<&'a Raw<AnyToDeviceEvent>> for FilterInput<'a> {
type Error = serde_json::Error;
fn try_from(raw_event: &'a Raw<AnyToDeviceEvent>) -> Result<Self, Self::Error> {
// deserialize_as::<FilterInput> will first try state, message like and then to-device.
// The `AnyToDeviceEvent` would match message like first, so we need to explicitly
// deserialize as `FilterInputToDevice`.
// deserialize_as::<FilterInput> will first try state, message like and then
// to-device. The `AnyToDeviceEvent` would match message like first, so
// we need to explicitly deserialize as `FilterInputToDevice`.
raw_event.deserialize_as::<FilterInputToDevice<'a>>().map(FilterInput::ToDevice)
}
}
Expand Down Expand Up @@ -515,7 +515,7 @@ mod tests {
fn test_to_device_filter_does_match() {
let f = Filter::ToDevice(ToDeviceEventFilter::new("my.custom.to.device".into()));
assert!(f.matches(&FilterInput::ToDevice(FilterInputToDevice {
event_type: "my.custom.to.device".into(),
event_type: "my.custom.to.device",
})));
}
}
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/widget/machine/to_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ToWidgetRequest for NotifyNewMatrixEvent {
#[derive(Deserialize)]
pub(crate) struct Empty {}

/// Notify the widget that we received a new matrix event.
/// Notify the widget that we received a new matrix to device event.
/// This is a "response" to the widget subscribing to the events in the room.
#[derive(Serialize)]
#[serde(transparent)]
Expand Down
Loading
Loading