Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6509fe6
WidgetDriver: Introduce `to-device` filter.
toger5 Apr 22, 2025
0338b68
WidgetDriver: add toDevice capability parsing
toger5 Apr 7, 2025
023822e
WidgetDriver: ffi for toDevice filter
toger5 Apr 22, 2025
9f84d9a
WidgetDriver: add ToDevice widget events and machine actions.
toger5 Apr 7, 2025
79ad365
WidgetDriver: add matrix driver toDevice support (reading and sending…
toger5 Apr 23, 2025
35199a7
WidgetDriver: integration tests for the toDevice feature.
toger5 Apr 7, 2025
aa35aad
WidgetDriver: Add `io.element.call.encryption_keys` to-device capabil…
toger5 Apr 30, 2025
20480a2
fixup: review remove Display impl for serialization
BillCarsonFr May 20, 2025
231898c
fixup: improve comments
BillCarsonFr May 20, 2025
4ba6d61
review: Use blanket impl for MatrixDriverResponseBased on TryInto
BillCarsonFr May 20, 2025
8c6752b
review: Removed unneeded From impl
BillCarsonFr May 20, 2025
a890515
review: Rename NotifyNewToDeviceEvent into NotifyNewToDeviceMessage
BillCarsonFr May 20, 2025
5436639
review: Corrects minor typos and improve doc
BillCarsonFr May 20, 2025
720082c
review: revert add_props_to_raw for encrypted to-device
BillCarsonFr May 21, 2025
4e5181f
fixup: insta dev-dep in cargo.lock
BillCarsonFr May 21, 2025
27145e6
Merge branch 'main' into toger5/widget-to-device-without-encryption
BillCarsonFr May 21, 2025
c30f07f
fixup: missing capitalize
BillCarsonFr May 21, 2025
0108860
fixup: post-rebase fix `then` renamed to `add_response_handler`
BillCarsonFr May 21, 2025
7534b65
fixup: widget feature needs optional anyhow dep
BillCarsonFr May 21, 2025
cca45ca
review: Use serde Error instead of anyhow for TryInto
BillCarsonFr May 21, 2025
f248d27
review: fix `to_device` typo with `to-device`
BillCarsonFr May 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/matrix-sdk/src/widget/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct Capabilities {
impl Capabilities {
/// Checks if a given event is allowed to be forwarded to the widget.
///
/// - `event_filter_input` is a minimized event respresntation that contains
/// - `event_filter_input` is a minimized event representation that contains
/// only the information needed to check if the widget is allowed to
/// receive the event. (See [`FilterInput`])
pub(super) fn allow_reading<'a>(
Expand All @@ -78,7 +78,7 @@ impl Capabilities {

/// Checks if a given event is allowed to be sent by the widget.
///
/// - `event_filter_input` is a minimized event respresntation that contains
/// - `event_filter_input` is a minimized event representation that contains
/// only the information needed to check if the widget is allowed to send
/// the event to a matrix room. (See [`FilterInput`])
pub(super) fn allow_sending<'a>(
Expand Down Expand Up @@ -121,6 +121,7 @@ impl Serialize for Capabilities {
match self.0 {
Filter::MessageLike(filter) => PrintMessageLikeEventFilter(filter).fmt(f),
Filter::State(filter) => PrintStateEventFilter(filter).fmt(f),
Filter::ToDevice(filter) => filter.fmt(f),
}
}
}
Expand Down
79 changes: 74 additions & 5 deletions crates/matrix-sdk/src/widget/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;

use ruma::{
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType},
events::{
AnyTimelineEvent, AnyToDeviceEvent, MessageLikeEventType, StateEventType, ToDeviceEventType,
},
serde::Raw,
};
use serde::Deserialize;
use tracing::debug;

use super::machine::SendEventRequest;

/// A Filter for Matrix events. That is used to decide if a given event can be
/// sent to the widget and if a widgets is allowed to send an event to to a
/// Matrix room or not.
/// A Filter for Matrix events. It is used to decide if a given event can be
/// sent to the widget and if a widget is allowed to send an event to a
/// Matrix room.
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Filter {
/// Filter for message-like events.
MessageLike(MessageLikeEventFilter),
/// Filter for state events.
State(StateEventFilter),
/// Filter for to device events.
ToDevice(ToDeviceEventFilter),
}

impl Filter {
Expand All @@ -41,6 +47,7 @@ impl Filter {
match self {
Self::MessageLike(filter) => filter.matches(filter_input),
Self::State(filter) => filter.matches(filter_input),
Self::ToDevice(filter) => filter.matches(filter_input),
}
}
/// Returns the event type that this filter is configured to match.
Expand All @@ -51,6 +58,7 @@ impl Filter {
match self {
Self::MessageLike(filter) => filter.filter_event_type(),
Self::State(filter) => filter.filter_event_type(),
Self::ToDevice(filter) => filter.event_type.to_string(),
}
}
}
Expand Down Expand Up @@ -123,6 +131,33 @@ impl<'a> StateEventFilter {
}
}

/// Filter for to-device events.
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ToDeviceEventFilter {
/// The event type this to-device-filter filters for.
pub event_type: ToDeviceEventType,
}

impl ToDeviceEventFilter {
/// Create a new `ToDeviceEventFilter` with the given event type.
pub fn new(event_type: ToDeviceEventType) -> Self {
Self { event_type }
}
}

impl ToDeviceEventFilter {
fn matches(&self, filter_input: &FilterInput) -> bool {
matches!(filter_input,FilterInput::ToDevice(f_in) if f_in.event_type == self.event_type)
}
}

impl fmt::Display for ToDeviceEventFilter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.event_type)
}
}

// Filter input:

/// The input data for the filter. This can either be constructed from a
Expand All @@ -131,8 +166,13 @@ impl<'a> StateEventFilter {
#[serde(untagged)]
pub enum FilterInput<'a> {
#[serde(borrow)]
// The order is important.
// We first need to check if we can deserialize as a state (state_key exists)
State(FilterInputState<'a>),
// only then we can check if we can deserialize as a message like.
MessageLike(FilterInputMessageLike<'a>),
// ToDevice will need to be done explicitly since it looks the same as a message like.
ToDevice(FilterInputToDevice<'a>),
}

impl<'a> FilterInput<'a> {
Expand Down Expand Up @@ -188,10 +228,29 @@ 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.
raw_event.deserialize_as()
}
}

#[derive(Debug, Deserialize)]
pub struct FilterInputToDevice<'a> {
#[serde(rename = "type")]
pub(super) event_type: &'a str,
}

/// Create a filter input of type [`FilterInput::ToDevice`]`.
impl<'a> TryFrom<&'a Raw<AnyToDeviceEvent>> for FilterInput<'a> {
type Error = serde_json::Error;
fn try_from(raw_event: 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`.
raw_event.deserialize_as::<FilterInputToDevice>().map(FilterInput::ToDevice)
}
}

impl<'a> From<&'a SendEventRequest> for FilterInput<'a> {
fn from(request: &'a SendEventRequest) -> Self {
match &request.state_key {
Expand Down Expand Up @@ -234,7 +293,9 @@ mod tests {
use super::{
Filter, FilterInput, FilterInputMessageLike, MessageLikeEventFilter, StateEventFilter,
};
use crate::widget::filter::MessageLikeFilterEventContent;
use crate::widget::filter::{
FilterInputToDevice, MessageLikeFilterEventContent, ToDeviceEventFilter,
};

fn message_event(event_type: &str) -> FilterInput<'_> {
FilterInput::MessageLike(FilterInputMessageLike { event_type, content: Default::default() })
Expand Down Expand Up @@ -443,4 +504,12 @@ mod tests {
assert_eq!(state.state_key, "@alice:example.com");
}
}

#[test]
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(),
})));
}
}
6 changes: 3 additions & 3 deletions crates/matrix-sdk/src/widget/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ impl WidgetMachine {
IncomingMessage::MatrixDriverResponse { request_id, response } => {
self.process_matrix_driver_response(request_id, response)
}
IncomingMessage::MatrixEventReceived(event) => {
IncomingMessage::MatrixEventReceived(event_raw) => {
let CapabilitiesState::Negotiated(capabilities) = &self.capabilities else {
error!("Received matrix event before capabilities negotiation");
return Vec::new();
};

capabilities
.allow_reading(&event)
.allow_reading(&event_raw)
.then(|| {
self.send_to_widget_request(NotifyNewMatrixEvent(event))
self.send_to_widget_request(NotifyNewMatrixEvent(event_raw))
.map(|(_request, action)| vec![action])
.unwrap_or_default()
})
Expand Down