Skip to content

Commit

Permalink
widget_driver: doc and test changes (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
toger5 committed Jul 10, 2024
1 parent ac87824 commit b2caec3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/widget/machine/driver_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub(crate) struct SendEventRequest {
pub(crate) state_key: Option<String>,
/// Raw content of an event.
pub(crate) content: Box<RawJsonValue>,
/// Addition send event parameters to send a future
/// Additional send event parameters to send a future
#[serde(flatten)]
pub(crate) future_parameters: Option<FutureParameters>,
}
Expand Down
19 changes: 10 additions & 9 deletions crates/matrix-sdk/src/widget/machine/from_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
use super::SendEventRequest;
use crate::widget::StateKeySelector;

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
#[serde(tag = "action", rename_all = "snake_case", content = "data")]
pub(super) enum FromWidgetRequest {
SupportedApiVersions {},
Expand Down Expand Up @@ -112,7 +112,7 @@ pub(super) enum ApiVersion {
MSC3846,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub(super) enum ReadEventRequest {
ReadStateEvent {
Expand All @@ -137,19 +137,20 @@ pub(super) struct ReadEventResponse {
pub(crate) struct SendEventResponse {
/// The room id for the send event.
pub(crate) room_id: Option<OwnedRoomId>,
/// The event id of the send event. Its optional because if its a future one
/// does not get the event_id at this point.
/// The event id of the send event. It's optional because if it's a future
/// event, it does not get the event_id at this point.
pub(crate) event_id: Option<OwnedEventId>,
/// A token to send/insert the future into the DAG.
pub(crate) send_token: Option<String>,
/// A token to cancel this future. It will never be send if this is called.
/// A token to cancel this future event. It will never be seny if this is
/// called.
pub(crate) cancel_token: Option<String>,
/// The `future_group_id` generated for this future. Used to connect
/// multiple futures only one of the connected futures will be sent and
/// inserted into the DAG.
/// The `future_group_id` generated for this future event. Used to connect
/// multiple future events. Only one of the connected future event will be
/// sent and inserted into the DAG.
pub(crate) future_group_id: Option<String>,
/// A token used to refresh the timer of the future. This allows
/// to implement heartbeat like capabilities. An event is only sent once
/// to implement heartbeat-like capabilities. An event is only sent once
/// a refresh in the timeout interval is missed.
///
/// If the future does not have a timeout this will be `None`.
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk/src/widget/machine/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(super) struct IncomingWidgetMessage {
pub(super) kind: IncomingWidgetMessageKind,
}

#[derive(Debug)]
pub(super) enum IncomingWidgetMessageKind {
Request(Raw<FromWidgetRequest>),
Response(ToWidgetResponse),
Expand Down
31 changes: 17 additions & 14 deletions crates/matrix-sdk/src/widget/machine/tests/send_event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use assert_matches2::assert_let;
use ruma::{api::client::future::FutureParameters, events::TimelineEventType};

use super::WIDGET_ID;
Expand All @@ -23,18 +24,20 @@ fn parse_future_action() {
"type": "org.matrix.msc3401.call.member",
},
});
if let IncomingWidgetMessageKind::Request(a) =
serde_json::from_str::<IncomingWidgetMessage>(&raw).unwrap().kind
{
if let FromWidgetRequest::SendEvent(b) = a.deserialize().unwrap() {
let FutureParameters::Timeout { timeout, group_id } = b.future_parameters.unwrap()
else {
panic!()
};
assert_eq!(timeout, Duration::from_millis(10000));
assert_eq!(group_id, None);
assert_eq!(b.event_type, TimelineEventType::CallMember);
assert_eq!(b.state_key.unwrap(), "_@abc:example.org_VFKPEKYWMP".to_owned());
}
}
assert_let!(
IncomingWidgetMessageKind::Request(incoming_request) =
serde_json::from_str::<IncomingWidgetMessage>(&raw).unwrap().kind
);
assert_let!(
FromWidgetRequest::SendEvent(send_event_request) = incoming_request.deserialize().unwrap()
);
assert_let!(
FutureParameters::Timeout { timeout, group_id } =
send_event_request.future_parameters.unwrap()
);

assert_eq!(timeout, Duration::from_millis(10000));
assert_eq!(group_id, None);
assert_eq!(send_event_request.event_type, TimelineEventType::CallMember);
assert_eq!(send_event_request.state_key.unwrap(), "_@abc:example.org_VFKPEKYWMP".to_owned());
}
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 @@ -58,7 +58,7 @@ where
}
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub(super) struct ToWidgetResponse {
/// The action from the original request.
Expand Down

0 comments on commit b2caec3

Please sign in to comment.