From 635a6e04a028fef220314c47dd59fa35bd03b07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sat, 11 Jun 2022 21:03:09 +0200 Subject: [PATCH] Add support for timestamp massaging According to MSC3316 --- crates/ruma-client-api/CHANGELOG.md | 1 + crates/ruma-client-api/Cargo.toml | 1 + .../src/message/send_message_event.rs | 25 +++++++- .../src/state/send_state_event.rs | 58 ++++++++++++++++++- crates/ruma/Cargo.toml | 2 + 5 files changed, 84 insertions(+), 3 deletions(-) diff --git a/crates/ruma-client-api/CHANGELOG.md b/crates/ruma-client-api/CHANGELOG.md index 4a2f3f6a..689ac59d 100644 --- a/crates/ruma-client-api/CHANGELOG.md +++ b/crates/ruma-client-api/CHANGELOG.md @@ -10,6 +10,7 @@ Improvements: * Add unstable support for refresh tokens (MSC2918) * Add `ErrorKind::{UnableToAuthorizeJoin, UnableToGrantJoin}` encountered for restricted rooms +* Add unstable support for timestamp massaging (MSC3316) # 0.14.1 diff --git a/crates/ruma-client-api/Cargo.toml b/crates/ruma-client-api/Cargo.toml index 5abe36ff..3a890156 100644 --- a/crates/ruma-client-api/Cargo.toml +++ b/crates/ruma-client-api/Cargo.toml @@ -24,6 +24,7 @@ unstable-msc2654 = [] unstable-msc2676 = [] unstable-msc2677 = [] unstable-msc2918 = [] +unstable-msc3316 = [] unstable-msc3440 = [] unstable-msc3488 = [] client = [] diff --git a/crates/ruma-client-api/src/message/send_message_event.rs b/crates/ruma-client-api/src/message/send_message_event.rs index 714aa34e..fcd5c140 100644 --- a/crates/ruma-client-api/src/message/send_message_event.rs +++ b/crates/ruma-client-api/src/message/send_message_event.rs @@ -5,6 +5,8 @@ pub mod v3 { //! //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid + #[cfg(feature = "unstable-msc3316")] + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_common::{ api::ruma_api, events::{AnyMessageLikeEventContent, MessageLikeEventContent, MessageLikeEventType}, @@ -45,6 +47,18 @@ pub mod v3 { /// The event content to send. #[ruma_api(body)] pub body: Raw, + + /// Timestamp to use for the `origin_server_ts` of the event. + /// + /// This is called [timestamp massaging] and can only be used by Appservices. + /// + /// Note that this does not change the position of the event in the timeline. + /// + /// [timestamp massaging]: https://github.com/matrix-org/matrix-spec-proposals/pull/3316 + #[cfg(feature = "unstable-msc3316")] + #[ruma_api(query)] + #[serde(skip_serializing_if = "Option::is_none", rename = "ts")] + pub timestamp: Option, } response: { @@ -75,6 +89,8 @@ pub mod v3 { txn_id, event_type: content.event_type(), body: Raw::from_json(to_raw_json_value(content)?), + #[cfg(feature = "unstable-msc3316")] + timestamp: None, }) } @@ -86,7 +102,14 @@ pub mod v3 { event_type: MessageLikeEventType, body: Raw, ) -> Self { - Self { room_id, event_type, txn_id, body } + Self { + room_id, + event_type, + txn_id, + body, + #[cfg(feature = "unstable-msc3316")] + timestamp: None, + } } } diff --git a/crates/ruma-client-api/src/state/send_state_event.rs b/crates/ruma-client-api/src/state/send_state_event.rs index c5e79229..45458266 100644 --- a/crates/ruma-client-api/src/state/send_state_event.rs +++ b/crates/ruma-client-api/src/state/send_state_event.rs @@ -5,6 +5,8 @@ pub mod v3 { //! //! [spec]: https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidstateeventtypestatekey + #[cfg(feature = "unstable-msc3316")] + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_common::{ api::ruma_api, events::{AnyStateEventContent, StateEventContent, StateEventType}, @@ -51,6 +53,16 @@ pub mod v3 { /// The event content to send. pub body: Raw, + + /// Timestamp to use for the `origin_server_ts` of the event. + /// + /// This is called [timestamp massaging] and can only be used by Appservices. + /// + /// Note that this does not change the position of the event in the timeline. + /// + /// [timestamp massaging]: https://github.com/matrix-org/matrix-spec-proposals/pull/3316 + #[cfg(feature = "unstable-msc3316")] + pub timestamp: Option, } impl<'a> Request<'a> { @@ -73,6 +85,8 @@ pub mod v3 { state_key, event_type: content.event_type(), body: Raw::from_json(to_raw_json_value(content)?), + #[cfg(feature = "unstable-msc3316")] + timestamp: None, }) } @@ -84,7 +98,14 @@ pub mod v3 { state_key: &'a str, body: Raw, ) -> Self { - Self { room_id, event_type, state_key, body } + Self { + room_id, + event_type, + state_key, + body, + #[cfg(feature = "unstable-msc3316")] + timestamp: None, + } } } @@ -139,6 +160,13 @@ pub mod v3 { url.push_str(&Cow::from(utf8_percent_encode(self.state_key, NON_ALPHANUMERIC))); } + #[cfg(feature = "unstable-msc3316")] + { + let request_query = RequestQuery { timestamp: self.timestamp }; + url.push('?'); + url.push_str(&ruma_common::serde::urlencoded::to_string(request_query)?); + } + let http_request = http::Request::builder() .method(http::Method::PUT) .uri(url) @@ -197,9 +225,35 @@ pub mod v3 { (a, b, "".into()) }; + #[cfg(feature = "unstable-msc3316")] + let request_query: RequestQuery = + ruma_common::serde::urlencoded::from_str(request.uri().query().unwrap_or(""))?; + let body = serde_json::from_slice(request.body().as_ref())?; - Ok(Self { room_id, event_type, state_key, body }) + Ok(Self { + room_id, + event_type, + state_key, + body, + #[cfg(feature = "unstable-msc3316")] + timestamp: request_query.timestamp, + }) } } + + /// Data in the request's query string. + #[cfg(feature = "unstable-msc3316")] + #[derive(Debug)] + #[cfg_attr(feature = "client", derive(serde::Serialize))] + #[cfg_attr(feature = "server", derive(serde::Deserialize))] + struct RequestQuery { + /// Timestamp to use for the `origin_server_ts` of the event. + #[serde( + rename = "org.matrix.msc3316.ts", + alias = "ts", + skip_serializing_if = "Option::is_none" + )] + pub timestamp: Option, + } } diff --git a/crates/ruma/Cargo.toml b/crates/ruma/Cargo.toml index 89629207..1b0dcc30 100644 --- a/crates/ruma/Cargo.toml +++ b/crates/ruma/Cargo.toml @@ -138,6 +138,7 @@ unstable-msc2870 = ["ruma-signatures?/unstable-msc2870"] unstable-msc2918 = ["ruma-client-api?/unstable-msc2918"] unstable-msc3245 = ["ruma-common/unstable-msc3245"] unstable-msc3246 = ["ruma-common/unstable-msc3246"] +unstable-msc3316 = ["ruma-client-api?/unstable-msc3316"] unstable-msc3381 = ["ruma-common/unstable-msc3381"] unstable-msc3440 = [ "ruma-client-api?/unstable-msc3440", @@ -169,6 +170,7 @@ __ci = [ "unstable-msc2918", "unstable-msc3245", "unstable-msc3246", + "unstable-msc3316", "unstable-msc3381", "unstable-msc3440", "unstable-msc3488",