Skip to content
Merged
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
13 changes: 12 additions & 1 deletion crates/matrix-sdk/src/widget/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

use ruma::{
events::{
AnyTimelineEvent, AnyToDeviceEvent, MessageLikeEventType, StateEventType, ToDeviceEventType,
AnyStateEvent, AnyTimelineEvent, AnyToDeviceEvent, MessageLikeEventType, StateEventType,
ToDeviceEventType,
},
serde::Raw,
};
Expand Down Expand Up @@ -224,6 +225,16 @@ impl<'a> TryFrom<&'a Raw<AnyTimelineEvent>> for FilterInput<'a> {
}
}

/// Create a filter input based on [`AnyStateEvent`].
/// This will create a [`FilterInput::State`].
impl<'a> TryFrom<&'a Raw<AnyStateEvent>> for FilterInput<'a> {
type Error = serde_json::Error;

fn try_from(raw_event: &'a Raw<AnyStateEvent>) -> Result<Self, Self::Error> {
raw_event.deserialize_as()
}
}

#[derive(Debug, Deserialize)]
pub struct FilterInputToDevice<'a> {
#[serde(rename = "type")]
Expand Down
72 changes: 45 additions & 27 deletions crates/matrix-sdk/src/widget/machine/driver_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ruma::{
account::request_openid_token, delayed_events::update_delayed_event,
to_device::send_event_to_device,
},
events::{AnyTimelineEvent, AnyToDeviceEventContent},
events::{AnyStateEvent, AnyTimelineEvent, AnyToDeviceEventContent},
serde::Raw,
to_device::DeviceIdOrAllDevices,
OwnedUserId,
Expand All @@ -48,14 +48,14 @@ pub(crate) enum MatrixDriverRequestData {
/// Get OpenId token for a given request ID.
GetOpenId,

/// Read message event(s).
ReadMessageLikeEvent(ReadMessageLikeEventRequest),
/// Read events from the timeline.
ReadEvents(ReadEventsRequest),

/// Read state event(s).
ReadStateEvent(ReadStateEventRequest),
/// Read room state entries.
ReadState(ReadStateRequest),

/// Send Matrix event that corresponds to the given description.
SendMatrixEvent(SendEventRequest),
SendEvent(SendEventRequest),

/// Send a to-device message over the Matrix homeserver.
SendToDeviceEvent(SendToDeviceRequest),
Expand Down Expand Up @@ -170,33 +170,39 @@ impl FromMatrixDriverResponse for request_openid_token::v3::Response {
}
}

/// Ask the client to read Matrix event(s) that corresponds to the given
/// Ask the client to read Matrix events that correspond to the given
/// description and return a list of events as a response.
#[derive(Clone, Debug)]
pub(crate) struct ReadMessageLikeEventRequest {
pub(crate) struct ReadEventsRequest {
/// The event type to read.
// TODO: This wants to be `MessageLikeEventType`` but we need a type which supports `as_str()`
// as soon as ruma supports `as_str()` on `MessageLikeEventType` we can use it here.
pub(crate) event_type: String,

/// The `state_key` to read. If None, this will read events regardless of
/// whether they are state events. If `Some(Any)`, this will only read state
/// events of the given type. If set to a specific state key, this will only
/// read state events of the given type matching that state key.
pub(crate) state_key: Option<StateKeySelector>,

/// The maximum number of events to return.
pub(crate) limit: u32,
}

impl From<ReadMessageLikeEventRequest> for MatrixDriverRequestData {
fn from(value: ReadMessageLikeEventRequest) -> Self {
MatrixDriverRequestData::ReadMessageLikeEvent(value)
impl From<ReadEventsRequest> for MatrixDriverRequestData {
fn from(value: ReadEventsRequest) -> Self {
MatrixDriverRequestData::ReadEvents(value)
}
}

impl MatrixDriverRequest for ReadMessageLikeEventRequest {
impl MatrixDriverRequest for ReadEventsRequest {
type Response = Vec<Raw<AnyTimelineEvent>>;
}

impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixEventRead(response) => Some(response),
MatrixDriverResponse::EventsRead(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
Expand All @@ -205,28 +211,40 @@ impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
}
}

/// Ask the client to read Matrix event(s) that corresponds to the given
/// description and return a list of events as a response.
/// Ask the client to read Matrix room state entries corresponding to the given
/// description and return a list of state events as a response.
#[derive(Clone, Debug)]
pub(crate) struct ReadStateEventRequest {
pub(crate) struct ReadStateRequest {
/// The event type to read.
// TODO: This wants to be `TimelineEventType` but we need a type which supports `as_str()`
// as soon as ruma supports `as_str()` on `TimelineEventType` we can use it here.
pub(crate) event_type: String,

/// The `state_key` to read, or `Any` to receive any/all events of the given
/// type, regardless of their `state_key`.
/// The `state_key` to read, or `Any` to receive any/all room state entries
/// of the given type, regardless of their `state_key`.
pub(crate) state_key: StateKeySelector,
}

impl From<ReadStateEventRequest> for MatrixDriverRequestData {
fn from(value: ReadStateEventRequest) -> Self {
MatrixDriverRequestData::ReadStateEvent(value)
impl From<ReadStateRequest> for MatrixDriverRequestData {
fn from(value: ReadStateRequest) -> Self {
MatrixDriverRequestData::ReadState(value)
}
}

impl MatrixDriverRequest for ReadStateEventRequest {
type Response = Vec<Raw<AnyTimelineEvent>>;
impl MatrixDriverRequest for ReadStateRequest {
type Response = Vec<Raw<AnyStateEvent>>;
}

impl FromMatrixDriverResponse for Vec<Raw<AnyStateEvent>> {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::StateRead(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}

/// Ask the client to send Matrix event that corresponds to the given
Expand All @@ -251,7 +269,7 @@ pub(crate) struct SendEventRequest {

impl From<SendEventRequest> for MatrixDriverRequestData {
fn from(value: SendEventRequest) -> Self {
MatrixDriverRequestData::SendMatrixEvent(value)
MatrixDriverRequestData::SendEvent(value)
}
}

Expand All @@ -262,7 +280,7 @@ impl MatrixDriverRequest for SendEventRequest {
impl FromMatrixDriverResponse for SendEventResponse {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixEventSent(response) => Some(response),
MatrixDriverResponse::EventSent(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
Expand Down Expand Up @@ -303,7 +321,7 @@ impl TryInto<send_event_to_device::v3::Response> for MatrixDriverResponse {

fn try_into(self) -> Result<send_event_to_device::v3::Response, Self::Error> {
match self {
MatrixDriverResponse::MatrixToDeviceSent(response) => Ok(response),
MatrixDriverResponse::ToDeviceSent(response) => Ok(response),
_ => Err(de::Error::custom("bug in MatrixDriver, received wrong event response")),
}
}
Expand All @@ -330,7 +348,7 @@ impl MatrixDriverRequest for UpdateDelayedEventRequest {
impl FromMatrixDriverResponse for update_delayed_event::unstable::Response {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixDelayedEventUpdate(response) => Some(response),
MatrixDriverResponse::DelayedEventUpdated(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
Expand Down
32 changes: 15 additions & 17 deletions crates/matrix-sdk/src/widget/machine/from_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(super) enum FromWidgetRequest {
#[serde(rename = "get_openid")]
GetOpenId {},
#[serde(rename = "org.matrix.msc2876.read_events")]
ReadEvent(ReadEventRequest),
ReadEvent(ReadEventsRequest),
SendEvent(SendEventRequest),
SendToDevice(SendToDeviceRequest),
#[serde(rename = "org.matrix.msc4157.update_delayed_event")]
Expand Down Expand Up @@ -133,6 +133,7 @@ impl SupportedApiVersionsResponse {
ApiVersion::V0_0_1,
ApiVersion::V0_0_2,
ApiVersion::MSC2762,
ApiVersion::MSC2762UpdateState,
ApiVersion::MSC2871,
ApiVersion::MSC3819,
],
Expand All @@ -151,11 +152,15 @@ pub(super) enum ApiVersion {
#[serde(rename = "0.0.2")]
V0_0_2,

/// Supports sending and receiving of events.
/// Supports sending and receiving events.
#[serde(rename = "org.matrix.msc2762")]
MSC2762,

/// Supports sending of approved capabilities back to the widget.
/// Supports receiving room state with the `update_state` action.
#[serde(rename = "org.matrix.msc2762_update_state")]
MSC2762UpdateState,

/// Supports sending approved capabilities back to the widget.
#[serde(rename = "org.matrix.msc2871")]
MSC2871,

Expand All @@ -171,7 +176,7 @@ pub(super) enum ApiVersion {
#[serde(rename = "org.matrix.msc2876")]
MSC2876,

/// Supports sending and receiving of to-device events.
/// Supports sending and receiving to-device events.
#[serde(rename = "org.matrix.msc3819")]
MSC3819,

Expand All @@ -181,22 +186,15 @@ pub(super) enum ApiVersion {
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub(super) enum ReadEventRequest {
ReadStateEvent {
#[serde(rename = "type")]
event_type: String,
state_key: StateKeySelector,
},
ReadMessageLikeEvent {
#[serde(rename = "type")]
event_type: String,
limit: Option<u32>,
},
pub(super) struct ReadEventsRequest {
#[serde(rename = "type")]
pub(super) event_type: String,
pub(super) state_key: Option<StateKeySelector>,
pub(super) limit: Option<u32>,
}

#[derive(Debug, Serialize)]
pub(super) struct ReadEventResponse {
pub(super) struct ReadEventsResponse {
pub(super) events: Vec<Raw<AnyTimelineEvent>>,
}

Expand Down
35 changes: 25 additions & 10 deletions crates/matrix-sdk/src/widget/machine/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

use ruma::{
api::client::{account::request_openid_token, delayed_events, to_device::send_event_to_device},
events::{AnyTimelineEvent, AnyToDeviceEvent},
events::{AnyStateEvent, AnyTimelineEvent, AnyToDeviceEvent},
serde::Raw,
};
use serde::{de, Deserialize, Deserializer};
use serde_json::value::RawValue as RawJsonValue;
use uuid::Uuid;

#[cfg(doc)]
use super::MatrixDriverRequestData;
use super::{
from_widget::{FromWidgetRequest, SendEventResponse},
to_widget::ToWidgetResponse,
Expand Down Expand Up @@ -50,27 +52,40 @@ pub(crate) enum IncomingMessage {
/// ([`crate::widget::Action::SubscribeTimeline`] request).
MatrixEventReceived(Raw<AnyTimelineEvent>),

/// The `MatrixDriver` notified the `WidgetMachine` of a change in room
/// state.
///
/// This means that the machine previously subscribed to some events
/// ([`crate::widget::Action::Subscribe`] request).
StateUpdateReceived(Vec<Raw<AnyStateEvent>>),

/// The `MatrixDriver` notified the `WidgetMachine` of a new to-device
/// event.
ToDeviceReceived(Raw<AnyToDeviceEvent>),
}

pub(crate) enum MatrixDriverResponse {
/// Client acquired capabilities from the user.
///
/// A response to an `Action::AcquireCapabilities` command.
/// A response to a [`MatrixDriverRequestData::AcquireCapabilities`]
/// command.
CapabilitiesAcquired(Capabilities),
/// Client got OpenId token for a given request ID.
/// A response to an `Action::GetOpenId` command.
/// A response to a [`MatrixDriverRequestData::GetOpenId`] command.
OpenIdReceived(request_openid_token::v3::Response),
/// Client read some Matrix event(s).
/// A response to an `Action::ReadMatrixEvent` commands.
MatrixEventRead(Vec<Raw<AnyTimelineEvent>>),
/// A response to a [`MatrixDriverRequestData::ReadEvents`] command.
EventsRead(Vec<Raw<AnyTimelineEvent>>),
/// Client read some Matrix room state entries.
/// A response to a [`MatrixDriverRequestData::ReadState`] command.
StateRead(Vec<Raw<AnyStateEvent>>),
/// Client sent some Matrix event. The response contains the event ID.
/// A response to an `Action::SendMatrixEvent` command.
MatrixEventSent(SendEventResponse),
MatrixToDeviceSent(send_event_to_device::v3::Response),
MatrixDelayedEventUpdate(delayed_events::update_delayed_event::unstable::Response),
/// A response to a [`MatrixDriverRequestData::SendEvent`] command.
EventSent(SendEventResponse),
/// A response to a `Action::SendToDevice` command.
ToDeviceSent(send_event_to_device::v3::Response),
/// Client updated a delayed event.
/// A response to a [`MatrixDriverRequestData::UpdateDelayedEvent`] command.
DelayedEventUpdated(delayed_events::update_delayed_event::unstable::Response),
}

pub(super) struct IncomingWidgetMessage {
Expand Down
Loading
Loading