Skip to content

Commit

Permalink
WidgetDriver: update to ruma version that uses future->delay rena…
Browse files Browse the repository at this point in the history
…ming.
  • Loading branch information
toger5 committed Jul 29, 2024
1 parent 55652a8 commit 679eccd
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 110 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ once_cell = "1.16.0"
pin-project-lite = "0.2.9"
rand = "0.8.5"
reqwest = { version = "0.12.4", default-features = false }
ruma = { git = "https://github.com/toger5/ruma", rev = "195cc6f57464b541d6ec0e8368c0993b3f62a760", features = [
ruma = { git = "https://github.com/toger5/ruma", rev = "2ca7559b306d5aa5e6b7be2629d14c5d685fecb9", features = [
"client-api-c",
"compat-upload-signatures",
"compat-user-id",
Expand All @@ -59,7 +59,7 @@ ruma = { git = "https://github.com/toger5/ruma", rev = "195cc6f57464b541d6ec0e83
"unstable-msc4075",
"unstable-msc4140",
] }
ruma-common = { git = "https://github.com/toger5/ruma", rev = "195cc6f57464b541d6ec0e8368c0993b3f62a760" }
ruma-common = { git = "https://github.com/toger5/ruma", rev = "2ca7559b306d5aa5e6b7be2629d14c5d685fecb9" }
serde = "1.0.151"
serde_html_form = "0.2.0"
serde_json = "1.0.91"
Expand Down
15 changes: 13 additions & 2 deletions crates/matrix-sdk/src/widget/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ mod tests {

use super::*;

#[test]
fn deserialization_of_no_capabilities() {
let capabilities_str = r#"[]"#;

let parsed = serde_json::from_str::<Capabilities>(capabilities_str).unwrap();
let expected = Capabilities::default();

assert_eq!(parsed, expected);
}

#[test]
fn deserialization_of_capabilities() {
let capabilities_str = r#"[
Expand All @@ -260,7 +270,8 @@ mod tests {
"org.matrix.msc2762.receive.state_event:org.matrix.msc3401.call.member",
"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.msc4140.send.delayed_event"
"org.matrix.msc4157.send.delayed_event",
"org.matrix.msc4157.update.delayed_event"
]"#;

let parsed = serde_json::from_str::<Capabilities>(capabilities_str).unwrap();
Expand All @@ -285,7 +296,7 @@ mod tests {
],
requires_client: true,
update_future_event: true,
send_future_event: false,
send_future_event: true,
};

assert_eq!(parsed, expected);
Expand Down
27 changes: 14 additions & 13 deletions crates/matrix-sdk/src/widget/machine/driver_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
use std::marker::PhantomData;

use ruma::{
api::client::{
account::request_openid_token,
future::{update_future, FutureParameters},
},
api::client::{account::request_openid_token, delayed_events::update_delayed_event},
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType, TimelineEventType},
serde::Raw,
};
use serde::{Deserialize, Serialize};
use serde::Deserialize;

use serde_json::value::RawValue as RawJsonValue;
use tracing::error;
Expand Down Expand Up @@ -216,7 +213,8 @@ impl MatrixDriverRequest for ReadStateEventRequest {
}

/// Ask the client to send matrix event that corresponds to the given
/// description and return an event ID as a response.
/// description and returns an event ID (or a delay_id,
/// see delayed events https://github.com/matrix-org/matrix-spec-proposals/pull/4140) as a response.
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct SendEventRequest {
/// The type of the event.
Expand All @@ -226,9 +224,11 @@ pub(crate) struct SendEventRequest {
pub(crate) state_key: Option<String>,
/// Raw content of an event.
pub(crate) content: Box<RawJsonValue>,
/// Additional send event parameters to send a future event.
#[serde(flatten)]
pub(crate) future_event_parameters: Option<FutureParameters>,
/// The delay for this request in ms.
/// The widget does not use the `org.matrix.msc4140.delay` unstable prefix.
/// It uses just an optional `delay` property. If provided the response will contain a
/// delay_id instead of a event_id.
pub(crate) delay: Option<u64>,
}

impl From<SendEventRequest> for MatrixDriverRequestData {
Expand All @@ -254,9 +254,10 @@ impl FromMatrixDriverResponse for SendEventResponse {
}

/// Ask the client to send a UpdateFuture request with the given `future_id` and `action`.
#[derive(Clone, Debug, Deserialize)]
#[derive(Deserialize, Debug)]
pub(crate) struct UpdateFutureRequest {
pub(crate) action: update_future::unstable::UpdateAction,
pub(crate) action: update_delayed_event::unstable::UpdateAction,
#[serde(rename = "delay_id")]
pub(crate) future_id: String,
}

Expand All @@ -267,10 +268,10 @@ impl From<UpdateFutureRequest> for MatrixDriverRequestData {
}

impl MatrixDriverRequest for UpdateFutureRequest {
type Response = update_future::unstable::Response;
type Response = update_delayed_event::unstable::Response;
}

impl FromMatrixDriverResponse for update_future::unstable::Response {
impl FromMatrixDriverResponse for update_delayed_event::unstable::Response {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixFutureUpdate(response) => Some(response),
Expand Down
36 changes: 19 additions & 17 deletions crates/matrix-sdk/src/widget/machine/from_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
use std::fmt;

use ruma::{
api::client::future::{send_future_message_event, send_future_state_event, update_future},
api::client::delayed_events::{
delayed_message_event, delayed_state_event, update_delayed_event,
},
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType},
serde::Raw,
OwnedEventId, OwnedRoomId,
};
use serde::{Deserialize, Serialize};

use super::SendEventRequest;
use super::{SendEventRequest, UpdateFutureRequest};
use crate::widget::StateKeySelector;

#[derive(Deserialize, Debug)]
Expand All @@ -35,7 +37,7 @@ pub(super) enum FromWidgetRequest {
#[serde(rename = "org.matrix.msc2876.read_events")]
ReadEvent(ReadEventRequest),
SendEvent(SendEventRequest),
#[serde(rename = "org.matrix.msc4157.update_delayed_events")]
#[serde(rename = "org.matrix.msc4157.update_delayed_event")]
FutureUpdate(UpdateFutureRequest),
}

Expand Down Expand Up @@ -157,24 +159,24 @@ impl SendEventResponse {
}
}

impl From<send_future_message_event::unstable::Response> for SendEventResponse {
fn from(val: send_future_message_event::unstable::Response) -> Self {
SendEventResponse { room_id: None, event_id: None, future_id: Some(val.future_id) }
impl From<delayed_message_event::unstable::Response> for SendEventResponse {
fn from(val: delayed_message_event::unstable::Response) -> Self {
SendEventResponse { room_id: None, event_id: None, future_id: Some(val.delay_id) }
}
}

impl From<send_future_state_event::unstable::Response> for SendEventResponse {
fn from(val: send_future_state_event::unstable::Response) -> Self {
SendEventResponse { room_id: None, event_id: None, future_id: Some(val.future_id) }
impl From<delayed_state_event::unstable::Response> for SendEventResponse {
fn from(val: delayed_state_event::unstable::Response) -> Self {
SendEventResponse { room_id: None, event_id: None, future_id: Some(val.delay_id) }
}
}

/// Widget request for updating Future Events.
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct UpdateFutureRequest {
/// The update action.
pub(crate) action: update_future::unstable::UpdateAction,
/// The future id that should be updated with this action.
#[serde(rename = "delay_id")]
pub(crate) future_id: String,
// /// A wrapper type for the empty okay response form [`update_delayed_event`](update_delayed_event::unstable::Response)
// /// which derives Serialize.
#[derive(Serialize, Debug)]
pub(crate) struct UpdateFutureResponse {}
impl From<update_delayed_event::unstable::Response> for UpdateFutureResponse {
fn from(_: update_delayed_event::unstable::Response) -> Self {
Self {}
}
}
4 changes: 2 additions & 2 deletions crates/matrix-sdk/src/widget/machine/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use ruma::{
api::client::{account::request_openid_token, future::update_future},
api::client::{account::request_openid_token, delayed_events},
events::AnyTimelineEvent,
serde::Raw,
};
Expand Down Expand Up @@ -62,7 +62,7 @@ pub(crate) enum MatrixDriverResponse {
/// Client sent some matrix event. The response contains the event ID.
/// A response to an `Action::SendMatrixEvent` command.
MatrixEventSent(SendEventResponse),
MatrixFutureUpdate(update_future::unstable::Response),
MatrixFutureUpdate(delayed_events::update_delayed_event::unstable::Response),
}

pub(super) struct IncomingWidgetMessage {
Expand Down
9 changes: 7 additions & 2 deletions crates/matrix-sdk/src/widget/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use std::{fmt, iter, time::Duration};

use driver_req::UpdateFutureRequest;
use from_widget::UpdateFutureResponse;
use indexmap::IndexMap;
use ruma::{
serde::{JsonObject, Raw},
Expand Down Expand Up @@ -268,7 +269,11 @@ impl WidgetMachine {
future_id: req.future_id,
});
request.then(|res, machine| {
vec![machine.send_from_widget_result_response(raw_request, res)]
vec![machine.send_from_widget_result_response(
raw_request,
// This is mapped to another type because the update_delay_event::Response does not impl Serialize
res.map(Into::<UpdateFutureResponse>::into),
)]
});
request_action.map(|a| vec![a]).unwrap_or_default()
}
Expand Down Expand Up @@ -365,7 +370,7 @@ impl WidgetMachine {
Default::default()
}),
};
if !capabilities.send_future_event && request.future_event_parameters.is_some() {
if !capabilities.send_future_event && request.delay.is_some() {
return Some(self.send_from_widget_error_response(
raw_request,
format!("Not allowed: missing the {} capability.", capabilities::SEND_FUTURE_EVENT),
Expand Down
14 changes: 4 additions & 10 deletions crates/matrix-sdk/src/widget/machine/tests/send_event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::time::Duration;

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

use super::WIDGET_ID;
use crate::widget::machine::{
Expand All @@ -18,7 +16,7 @@ fn parse_future_event_widget_action() {
"action": "send_event",
"data": {
"content": {},
"future_timeout": 10000,
"delay": 10000,
"room_id": "!rXAYvblqYaGiJmeRdR:matrix.org",
"state_key": "_@abc:example.org_VFKPEKYWMP",
"type": "org.matrix.msc3401.call.member",
Expand All @@ -31,13 +29,9 @@ fn parse_future_event_widget_action() {
assert_let!(
FromWidgetRequest::SendEvent(send_event_request) = incoming_request.deserialize().unwrap()
);
assert_let!(
FutureParameters::Timeout { timeout, future_parent_id } =
send_event_request.future_event_parameters.unwrap()
);
assert_let!(delay = send_event_request.delay.unwrap());

assert_eq!(timeout, Duration::from_millis(10000));
assert_eq!(future_parent_id, None);
assert_eq!(delay, 10000);
assert_eq!(send_event_request.event_type, TimelineEventType::CallMember);
assert_eq!(send_event_request.state_key.unwrap(), "_@abc:example.org_VFKPEKYWMP".to_owned());
}
12 changes: 6 additions & 6 deletions crates/matrix-sdk/src/widget/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use matrix_sdk_base::deserialized_responses::RawAnySyncOrStrippedState;
use ruma::{
api::client::{
account::request_openid_token::v3::{Request as OpenIdRequest, Response as OpenIdResponse},
delayed_events::{self, update_delayed_event::unstable::UpdateAction},
filter::RoomEventFilter,
future::{self, update_future::unstable::UpdateAction},
},
assign,
events::{
Expand Down Expand Up @@ -113,7 +113,7 @@ impl MatrixDriver {
event_type: TimelineEventType,
state_key: Option<String>,
content: Box<RawJsonValue>,
future_event_parameters: Option<future::FutureParameters>,
future_event_parameters: Option<delayed_events::DelayParameters>,
) -> Result<SendEventResponse> {
let type_str = event_type.to_string();
Ok(match (state_key, future_event_parameters) {
Expand All @@ -124,7 +124,7 @@ impl MatrixDriver {
self.room.send_state_event_raw(&type_str, &key, content).await?.event_id,
),
(None, Some(future_event_parameters)) => {
let r = future::send_future_message_event::unstable::Request::new_raw(
let r = delayed_events::delayed_message_event::unstable::Request::new_raw(
self.room.room_id().to_owned(),
TransactionId::new(),
MessageLikeEventType::from(type_str),
Expand All @@ -134,7 +134,7 @@ impl MatrixDriver {
self.room.client.send(r, None).await.map(|r| r.into())?
}
(Some(key), Some(future_event_parameters)) => {
let r = future::send_future_state_event::unstable::Request::new_raw(
let r = delayed_events::delayed_state_event::unstable::Request::new_raw(
self.room.room_id().to_owned(),
key,
StateEventType::from(type_str),
Expand All @@ -153,8 +153,8 @@ impl MatrixDriver {
&self,
future_id: String,
action: UpdateAction,
) -> HttpResult<future::update_future::unstable::Response> {
let r = future::update_future::unstable::Request::new(future_id, action);
) -> HttpResult<delayed_events::update_delayed_event::unstable::Response> {
let r = delayed_events::update_delayed_event::unstable::Request::new(future_id, action);
self.room.client.send(r, None).await
}

Expand Down
Loading

0 comments on commit 679eccd

Please sign in to comment.