Add support for timestamp massaging

According to MSC3316
This commit is contained in:
Kévin Commaille 2022-06-11 21:03:09 +02:00 committed by Kévin Commaille
parent 03f5b38403
commit 635a6e04a0
5 changed files with 84 additions and 3 deletions

View File

@ -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

View File

@ -24,6 +24,7 @@ unstable-msc2654 = []
unstable-msc2676 = []
unstable-msc2677 = []
unstable-msc2918 = []
unstable-msc3316 = []
unstable-msc3440 = []
unstable-msc3488 = []
client = []

View File

@ -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<AnyMessageLikeEventContent>,
/// 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<MilliSecondsSinceUnixEpoch>,
}
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<AnyMessageLikeEventContent>,
) -> Self {
Self { room_id, event_type, txn_id, body }
Self {
room_id,
event_type,
txn_id,
body,
#[cfg(feature = "unstable-msc3316")]
timestamp: None,
}
}
}

View File

@ -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<AnyStateEventContent>,
/// 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<MilliSecondsSinceUnixEpoch>,
}
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<AnyStateEventContent>,
) -> 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<MilliSecondsSinceUnixEpoch>,
}
}

View File

@ -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",