Use AnyStateEventContent in send_state_event_for_empty_key
This commit is contained in:
parent
aead9fa852
commit
0babfb489c
@ -1,40 +1,189 @@
|
|||||||
//! [PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}](https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-state-eventtype)
|
//! [PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}](https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-state-eventtype)
|
||||||
|
|
||||||
use ruma_api::ruma_api;
|
use std::convert::TryFrom;
|
||||||
use ruma_events::EventType;
|
|
||||||
|
use ruma_api::{
|
||||||
|
error::{
|
||||||
|
FromHttpRequestError, FromHttpResponseError, IntoHttpError, RequestDeserializationError,
|
||||||
|
ResponseDeserializationError, ServerError,
|
||||||
|
},
|
||||||
|
EndpointError, Metadata, Outgoing,
|
||||||
|
};
|
||||||
|
use ruma_events::{AnyStateEventContent, EventContent as _};
|
||||||
use ruma_identifiers::{EventId, RoomId};
|
use ruma_identifiers::{EventId, RoomId};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::value::RawValue as RawJsonValue;
|
use serde_json::value::RawValue as RawJsonValue;
|
||||||
|
|
||||||
ruma_api! {
|
/// Data for a request to the `send_state_event_for_empty_key` API endpoint.
|
||||||
metadata: {
|
///
|
||||||
description: "Send a state event to a room associated with the empty state key.",
|
/// Send a state event to a room associated with the empty state key.
|
||||||
method: PUT,
|
#[derive(Clone, Debug, Outgoing)]
|
||||||
name: "create_state_event_for_empty_key",
|
#[non_exhaustive]
|
||||||
path: "/_matrix/client/r0/rooms/:room_id/state/:event_type",
|
#[incoming_no_deserialize]
|
||||||
rate_limited: false,
|
pub struct Request<'a> {
|
||||||
requires_authentication: true,
|
/// The room to set the state in.
|
||||||
}
|
pub room_id: &'a RoomId,
|
||||||
|
|
||||||
request: {
|
/// The event content to send.
|
||||||
/// The room to set the state in.
|
pub content: &'a AnyStateEventContent,
|
||||||
#[ruma_api(path)]
|
}
|
||||||
pub room_id: &'a RoomId,
|
|
||||||
|
impl<'a> Request<'a> {
|
||||||
/// The type of event to send.
|
/// Creates a new `Request` with the given room id and event content.
|
||||||
#[ruma_api(path)]
|
pub fn new(room_id: &'a RoomId, content: &'a AnyStateEventContent) -> Self {
|
||||||
pub event_type: EventType,
|
Self { room_id, content }
|
||||||
|
}
|
||||||
/// The event's content. The type for this field will be updated in a
|
}
|
||||||
/// future release, until then you can create a value using
|
|
||||||
/// `serde_json::value::to_raw_value`.
|
/// Data in the response from the `send_state_event_for_empty_key` API endpoint.
|
||||||
#[ruma_api(body)]
|
#[derive(Clone, Debug, Outgoing)]
|
||||||
pub data: Box<RawJsonValue>,
|
#[non_exhaustive]
|
||||||
}
|
#[incoming_no_deserialize]
|
||||||
|
pub struct Response {
|
||||||
response: {
|
/// A unique identifier for the event.
|
||||||
/// A unique identifier for the event.
|
pub event_id: EventId,
|
||||||
pub event_id: EventId,
|
}
|
||||||
}
|
|
||||||
|
impl Response {
|
||||||
error: crate::Error
|
/// Creates a new `Response` with the given event id.
|
||||||
|
pub fn new(event_id: EventId) -> Self {
|
||||||
|
Self { event_id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const METADATA: Metadata = Metadata {
|
||||||
|
description: "Send a state event to a room associated with the empty state key.",
|
||||||
|
method: http::Method::PUT,
|
||||||
|
name: "send_state_event_for_empty_key",
|
||||||
|
path: "/_matrix/client/r0/rooms/:room_id/state/:event_type",
|
||||||
|
rate_limited: false,
|
||||||
|
requires_authentication: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl TryFrom<http::Request<Vec<u8>>> for IncomingRequest {
|
||||||
|
type Error = FromHttpRequestError;
|
||||||
|
|
||||||
|
fn try_from(request: http::Request<Vec<u8>>) -> Result<Self, Self::Error> {
|
||||||
|
let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect();
|
||||||
|
|
||||||
|
let room_id = {
|
||||||
|
let decoded =
|
||||||
|
match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match RoomId::try_from(&*decoded) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let content = {
|
||||||
|
let request_body: Box<RawJsonValue> =
|
||||||
|
match serde_json::from_slice(request.body().as_slice()) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let event_type = {
|
||||||
|
match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match AnyStateEventContent::from_parts(&event_type, request_body) {
|
||||||
|
Ok(content) => content,
|
||||||
|
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self { room_id, content })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Data in the response body.
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
struct ResponseBody {
|
||||||
|
/// A unique identifier for the event.
|
||||||
|
event_id: EventId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Response> for http::Response<Vec<u8>> {
|
||||||
|
type Error = IntoHttpError;
|
||||||
|
|
||||||
|
fn try_from(response: Response) -> Result<Self, Self::Error> {
|
||||||
|
let response = http::Response::builder()
|
||||||
|
.header(http::header::CONTENT_TYPE, "application/json")
|
||||||
|
.body(serde_json::to_vec(&ResponseBody { event_id: response.event_id })?)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<http::Response<Vec<u8>>> for Response {
|
||||||
|
type Error = FromHttpResponseError<crate::Error>;
|
||||||
|
|
||||||
|
fn try_from(response: http::Response<Vec<u8>>) -> Result<Self, Self::Error> {
|
||||||
|
if response.status().as_u16() < 400 {
|
||||||
|
let response_body: ResponseBody =
|
||||||
|
match serde_json::from_slice(response.body().as_slice()) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(ResponseDeserializationError::new(err, response).into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self { event_id: response_body.event_id })
|
||||||
|
} else {
|
||||||
|
match <crate::Error as EndpointError>::try_from_response(response) {
|
||||||
|
Ok(err) => Err(ServerError::Known(err).into()),
|
||||||
|
Err(response_err) => Err(ServerError::Unknown(response_err).into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ruma_api::OutgoingRequest for Request<'a> {
|
||||||
|
type EndpointError = crate::Error;
|
||||||
|
type IncomingResponse = Response;
|
||||||
|
|
||||||
|
/// Metadata for the `send_message_event` endpoint.
|
||||||
|
const METADATA: Metadata = METADATA;
|
||||||
|
|
||||||
|
fn try_into_http_request(
|
||||||
|
self,
|
||||||
|
base_url: &str,
|
||||||
|
access_token: Option<&str>,
|
||||||
|
) -> Result<http::Request<Vec<u8>>, IntoHttpError> {
|
||||||
|
use http::header::{HeaderValue, AUTHORIZATION};
|
||||||
|
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
||||||
|
|
||||||
|
let http_request = http::Request::builder()
|
||||||
|
.method(http::Method::PUT)
|
||||||
|
.uri(format!(
|
||||||
|
"{}/_matrix/client/r0/rooms/{}/state/{}",
|
||||||
|
base_url.strip_suffix("/").unwrap_or(base_url),
|
||||||
|
utf8_percent_encode(self.room_id.as_str(), NON_ALPHANUMERIC),
|
||||||
|
utf8_percent_encode(self.content.event_type(), NON_ALPHANUMERIC),
|
||||||
|
))
|
||||||
|
.header(
|
||||||
|
AUTHORIZATION,
|
||||||
|
HeaderValue::from_str(&format!(
|
||||||
|
"Bearer {}",
|
||||||
|
access_token.ok_or(IntoHttpError::NeedsAuthentication)?
|
||||||
|
))?,
|
||||||
|
)
|
||||||
|
.body(serde_json::to_vec(&self.content)?)?;
|
||||||
|
|
||||||
|
Ok(http_request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ruma_api::IncomingRequest for IncomingRequest {
|
||||||
|
type EndpointError = crate::Error;
|
||||||
|
type OutgoingResponse = Response;
|
||||||
|
|
||||||
|
/// Metadata for the `send_message_event` endpoint.
|
||||||
|
const METADATA: Metadata = METADATA;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user