Skip to content
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

[WIP] WidgetDriver: add to_device support #4155

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion bindings/matrix-sdk-ffi/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
use language_tags::LanguageTag;
use matrix_sdk::{
async_trait,
widget::{MessageLikeEventFilter, StateEventFilter},
widget::{MessageLikeEventFilter, StateEventFilter, ToDeviceEventFilter},
};
use tracing::error;

Expand Down Expand Up @@ -427,6 +427,8 @@ pub enum WidgetEventFilter {
StateWithType { event_type: String },
/// Matches state events with the given `type` and `state_key`.
StateWithTypeAndStateKey { event_type: String, state_key: String },
/// Matches ToDevice events with the given `type`.
ToDeviceWithType { event_type: String },
}

impl From<WidgetEventFilter> for matrix_sdk::widget::EventFilter {
Expand All @@ -444,6 +446,9 @@ impl From<WidgetEventFilter> for matrix_sdk::widget::EventFilter {
WidgetEventFilter::StateWithTypeAndStateKey { event_type, state_key } => {
Self::State(StateEventFilter::WithTypeAndStateKey(event_type.into(), state_key))
}
WidgetEventFilter::ToDeviceWithType { event_type } => {
Self::ToDevice(ToDeviceEventFilter(event_type.into()))
}
}
}
}
Expand All @@ -465,6 +470,9 @@ impl From<matrix_sdk::widget::EventFilter> for WidgetEventFilter {
F::State(StateEventFilter::WithTypeAndStateKey(event_type, state_key)) => {
Self::StateWithTypeAndStateKey { event_type: event_type.to_string(), state_key }
}
F::ToDevice(to_device_event_filter) => {
Self::ToDeviceWithType { event_type: to_device_event_filter.0.to_string() }
}
}
}
}
Expand Down
77 changes: 60 additions & 17 deletions crates/matrix-sdk/src/widget/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
use std::fmt;

use async_trait::async_trait;
use ruma::{events::AnyTimelineEvent, serde::Raw};
use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
use tracing::{debug, error};

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

use super::{
filter::MatrixEventFilterInput, EventFilter, MessageLikeEventFilter, StateEventFilter,
filter::{EventAndRequestFilter, MatrixEventFilterInput, MatrixEventFilterInputData},
machine::MatrixEvent,
EventFilter, MessageLikeEventFilter, StateEventFilter,
};

/// Must be implemented by a component that provides functionality of deciding
Expand Down Expand Up @@ -59,23 +62,38 @@ pub struct Capabilities {

impl Capabilities {
/// Tells if a given raw event matches the read filter.
pub fn raw_event_matches_read_filter(&self, raw: &Raw<AnyTimelineEvent>) -> bool {
let filter_in = match raw.deserialize_as::<MatrixEventFilterInput>() {
Ok(filter) => filter,
Err(err) => {
error!("Failed to deserialize raw event as MatrixEventFilterInput: {err}");
return false;
pub fn raw_event_matches_read_filter(&self, raw: &MatrixEvent) -> bool {
let filter_in = match raw {
MatrixEvent::Timeline(raw) => {
match raw.deserialize_as::<MatrixEventFilterInputData>() {
Ok(filter) => MatrixEventFilterInput::Timeline(filter),
Err(err) => {
error!("Failed to deserialize raw event as MatrixEventFilterInput: {err}");
return false;
}
}
}
MatrixEvent::ToDevice(raw) => {
match raw.deserialize_as::<MatrixEventFilterInputData>() {
Ok(filter) => MatrixEventFilterInput::ToDevice(filter),
Err(err) => {
error!("Failed to deserialize raw event as MatrixEventFilterInput: {err}");
return false;
}
}
}
};

self.read.iter().any(|f| f.matches(&filter_in))
self.read.iter().any(|f| f.matches_event(&filter_in))
}
}

const SEND_EVENT: &str = "org.matrix.msc2762.send.event";
const READ_EVENT: &str = "org.matrix.msc2762.receive.event";
const RECEIVE_EVENT: &str = "org.matrix.msc2762.receive.event";
const SEND_STATE: &str = "org.matrix.msc2762.send.state_event";
const READ_STATE: &str = "org.matrix.msc2762.receive.state_event";
const RECEIVE_STATE: &str = "org.matrix.msc2762.receive.state_event";
const SEND_TODEVICE: &str = "org.matrix.msc3819.send.to_device";
const RECEIVE_TODEVICE: &str = "org.matrix.msc3819.receive.to_device";
const REQUIRES_CLIENT: &str = "io.element.requires_client";
pub(super) const SEND_DELAYED_EVENT: &str = "org.matrix.msc4157.send.delayed_event";
pub(super) const UPDATE_DELAYED_EVENT: &str = "org.matrix.msc4157.update_delayed_event";
Expand All @@ -91,6 +109,7 @@ impl Serialize for Capabilities {
match self.0 {
EventFilter::MessageLike(filter) => PrintMessageLikeEventFilter(filter).fmt(f),
EventFilter::State(filter) => PrintStateEventFilter(filter).fmt(f),
EventFilter::ToDevice(filter) => filter.fmt(f),
}
}
}
Expand Down Expand Up @@ -136,15 +155,17 @@ impl Serialize for Capabilities {
}
for filter in &self.read {
let name = match filter {
EventFilter::MessageLike(_) => READ_EVENT,
EventFilter::State(_) => READ_STATE,
EventFilter::MessageLike(_) => RECEIVE_EVENT,
EventFilter::State(_) => RECEIVE_STATE,
EventFilter::ToDevice(_) => RECEIVE_TODEVICE,
};
seq.serialize_element(&format!("{name}:{}", PrintEventFilter(filter)))?;
}
for filter in &self.send {
let name = match filter {
EventFilter::MessageLike(_) => SEND_EVENT,
EventFilter::State(_) => SEND_STATE,
EventFilter::ToDevice(_) => SEND_TODEVICE,
};
seq.serialize_element(&format!("{name}:{}", PrintEventFilter(filter)))?;
}
Expand Down Expand Up @@ -184,18 +205,24 @@ impl<'de> Deserialize<'de> for Capabilities {
}

match s.split_once(':') {
Some((READ_EVENT, filter_s)) => Ok(Permission::Read(EventFilter::MessageLike(
parse_message_event_filter(filter_s),
))),
Some((RECEIVE_EVENT, filter_s)) => Ok(Permission::Read(
EventFilter::MessageLike(parse_message_event_filter(filter_s)),
)),
Some((SEND_EVENT, filter_s)) => Ok(Permission::Send(EventFilter::MessageLike(
parse_message_event_filter(filter_s),
))),
Some((READ_STATE, filter_s)) => {
Some((RECEIVE_STATE, filter_s)) => {
Ok(Permission::Read(EventFilter::State(parse_state_event_filter(filter_s))))
}
Some((SEND_STATE, filter_s)) => {
Ok(Permission::Send(EventFilter::State(parse_state_event_filter(filter_s))))
}
Some((RECEIVE_TODEVICE, filter_s)) => Ok(Permission::Read(
EventFilter::ToDevice(parse_to_device_event_filter(filter_s)),
)),
Some((SEND_TODEVICE, filter_s)) => Ok(Permission::Send(EventFilter::ToDevice(
parse_to_device_event_filter(filter_s),
))),
_ => {
debug!("Unknown capability `{s}`");
Ok(Self::Unknown)
Expand All @@ -222,6 +249,10 @@ impl<'de> Deserialize<'de> for Capabilities {
}
}

fn parse_to_device_event_filter(s: &str) -> ToDeviceEventFilter {
ToDeviceEventFilter(s.into())
}

let mut capabilities = Capabilities::default();
for capability in Vec::<Permission>::deserialize(deserializer)? {
match capability {
Expand Down Expand Up @@ -263,8 +294,10 @@ mod tests {
"org.matrix.msc2762.receive.event:org.matrix.rageshake_request",
"org.matrix.msc2762.receive.state_event:m.room.member",
"org.matrix.msc2762.receive.state_event:org.matrix.msc3401.call.member",
"org.matrix.msc3819.receive.to_device:io.element.call.encryption_keys",
"org.matrix.msc2762.send.event:org.matrix.rageshake_request",
"org.matrix.msc2762.send.state_event:org.matrix.msc3401.call.member#@user:matrix.server",
"org.matrix.msc3819.send.to_device:io.element.call.encryption_keys",
"org.matrix.msc4157.send.delayed_event",
"org.matrix.msc4157.update_delayed_event"
]"#;
Expand All @@ -279,6 +312,9 @@ mod tests {
EventFilter::State(StateEventFilter::WithType(
"org.matrix.msc3401.call.member".into(),
)),
EventFilter::ToDevice(ToDeviceEventFilter(
"io.element.call.encryption_keys".into(),
)),
],
send: vec![
EventFilter::MessageLike(MessageLikeEventFilter::WithType(
Expand All @@ -288,6 +324,9 @@ mod tests {
"org.matrix.msc3401.call.member".into(),
"@user:matrix.server".into(),
)),
EventFilter::ToDevice(ToDeviceEventFilter(
"io.element.call.encryption_keys".into(),
)),
],
requires_client: true,
update_delayed_event: true,
Expand All @@ -309,6 +348,9 @@ mod tests {
"org.matrix.msc3401.call.member".into(),
"@user:matrix.server".into(),
)),
EventFilter::ToDevice(ToDeviceEventFilter(
"io.element.call.encryption_keys".into(),
)),
],
send: vec![
EventFilter::MessageLike(MessageLikeEventFilter::WithType(
Expand All @@ -318,6 +360,7 @@ mod tests {
"org.matrix.msc3401.call.member".into(),
"@user:matrix.server".into(),
)),
EventFilter::ToDevice(ToDeviceEventFilter("my.org.other.to_device_event".into())),
],
requires_client: true,
update_delayed_event: false,
Expand Down
Loading
Loading