feat: query param to return the full room state event or just the content
`?format=event|content` Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
abebde0cf1
commit
3b4946d66e
@ -9,11 +9,9 @@ pub mod v3 {
|
|||||||
|
|
||||||
use ruma_common::{
|
use ruma_common::{
|
||||||
api::{response, Metadata},
|
api::{response, Metadata},
|
||||||
metadata,
|
metadata, OwnedRoomId,
|
||||||
serde::Raw,
|
|
||||||
OwnedRoomId,
|
|
||||||
};
|
};
|
||||||
use ruma_events::{AnyStateEventContent, StateEventType};
|
use ruma_events::StateEventType;
|
||||||
|
|
||||||
const METADATA: Metadata = metadata! {
|
const METADATA: Metadata = metadata! {
|
||||||
method: GET,
|
method: GET,
|
||||||
@ -37,12 +35,16 @@ pub mod v3 {
|
|||||||
|
|
||||||
/// The key of the state to look up.
|
/// The key of the state to look up.
|
||||||
pub state_key: String,
|
pub state_key: String,
|
||||||
|
|
||||||
|
/// Optional parameter to return the event content
|
||||||
|
/// or the full state event.
|
||||||
|
pub format: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
/// Creates a new `Request` with the given room ID, event type and state key.
|
/// Creates a new `Request` with the given room ID, event type and state key.
|
||||||
pub fn new(room_id: OwnedRoomId, event_type: StateEventType, state_key: String) -> Self {
|
pub fn new(room_id: OwnedRoomId, event_type: StateEventType, state_key: String) -> Self {
|
||||||
Self { room_id, event_type, state_key }
|
Self { room_id, event_type, state_key, format: None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,16 +53,23 @@ pub mod v3 {
|
|||||||
pub struct Response {
|
pub struct Response {
|
||||||
/// The content of the state event.
|
/// The content of the state event.
|
||||||
///
|
///
|
||||||
/// Since the inner type of the `Raw` does not implement `Deserialize`, you need to use
|
/// This is `serde_json::Value` due to complexity issues with returning only the
|
||||||
/// [`Raw::deserialize_as`] to deserialize it.
|
/// actual JSON content without a top level key.
|
||||||
#[ruma_api(body)]
|
#[serde(flatten, skip_serializing_if = "Option::is_none")]
|
||||||
pub content: Raw<AnyStateEventContent>,
|
pub content: Option<serde_json::Value>,
|
||||||
|
|
||||||
|
/// The full state event
|
||||||
|
///
|
||||||
|
/// This is `serde_json::Value` due to complexity issues with returning only the
|
||||||
|
/// actual JSON content without a top level key.
|
||||||
|
#[serde(flatten, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub event: Option<serde_json::Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
/// Creates a new `Response` with the given content.
|
/// Creates a new `Response` with the given content.
|
||||||
pub fn new(content: Raw<AnyStateEventContent>) -> Self {
|
pub fn new(content: serde_json::Value, event: serde_json::Value) -> Self {
|
||||||
Self { content }
|
Self { content: Some(content), event: Some(event) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,13 +88,15 @@ pub mod v3 {
|
|||||||
) -> Result<http::Request<T>, ruma_common::api::error::IntoHttpError> {
|
) -> Result<http::Request<T>, ruma_common::api::error::IntoHttpError> {
|
||||||
use http::header;
|
use http::header;
|
||||||
|
|
||||||
|
let query_string = serde_html_form::to_string(RequestQuery { format: self.format })?;
|
||||||
|
|
||||||
http::Request::builder()
|
http::Request::builder()
|
||||||
.method(http::Method::GET)
|
.method(http::Method::GET)
|
||||||
.uri(METADATA.make_endpoint_url(
|
.uri(METADATA.make_endpoint_url(
|
||||||
considering_versions,
|
considering_versions,
|
||||||
base_url,
|
base_url,
|
||||||
&[&self.room_id, &self.event_type, &self.state_key],
|
&[&self.room_id, &self.event_type, &self.state_key],
|
||||||
"",
|
&query_string,
|
||||||
)?)
|
)?)
|
||||||
.header(header::CONTENT_TYPE, "application/json")
|
.header(header::CONTENT_TYPE, "application/json")
|
||||||
.header(
|
.header(
|
||||||
@ -110,7 +121,7 @@ pub mod v3 {
|
|||||||
const METADATA: Metadata = METADATA;
|
const METADATA: Metadata = METADATA;
|
||||||
|
|
||||||
fn try_from_http_request<B, S>(
|
fn try_from_http_request<B, S>(
|
||||||
_request: http::Request<B>,
|
request: http::Request<B>,
|
||||||
path_args: &[S],
|
path_args: &[S],
|
||||||
) -> Result<Self, ruma_common::api::error::FromHttpRequestError>
|
) -> Result<Self, ruma_common::api::error::FromHttpRequestError>
|
||||||
where
|
where
|
||||||
@ -139,7 +150,21 @@ pub mod v3 {
|
|||||||
(a, b, "".into())
|
(a, b, "".into())
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Self { room_id, event_type, state_key })
|
let request_query: RequestQuery =
|
||||||
|
serde_html_form::from_str(request.uri().query().unwrap_or(""))?;
|
||||||
|
|
||||||
|
Ok(Self { room_id, event_type, state_key, format: request_query.format })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Data in the request's query string.
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "client", derive(serde::Serialize))]
|
||||||
|
#[cfg_attr(feature = "server", derive(serde::Deserialize))]
|
||||||
|
struct RequestQuery {
|
||||||
|
/// Optional parameter to return the event content
|
||||||
|
/// or the full state event.
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
format: Option<String>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user