client-api: Use generics for content in send_{message,state}_event::Request::new
This commit is contained in:
parent
f214aef8ad
commit
0126a7223a
@ -1,9 +1,10 @@
|
|||||||
//! [PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}](https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid)
|
//! [PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}](https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid)
|
||||||
|
|
||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
use ruma_events::{AnyMessageEventContent, EventContent as _};
|
use ruma_events::{AnyMessageEventContent, MessageEventContent};
|
||||||
use ruma_identifiers::{EventId, RoomId};
|
use ruma_identifiers::{EventId, RoomId};
|
||||||
use ruma_serde::Raw;
|
use ruma_serde::Raw;
|
||||||
|
use serde_json::value::to_raw_value as to_raw_json_value;
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -47,8 +48,22 @@ ruma_api! {
|
|||||||
|
|
||||||
impl<'a> Request<'a> {
|
impl<'a> Request<'a> {
|
||||||
/// Creates a new `Request` with the given room id, transaction id and event content.
|
/// Creates a new `Request` with the given room id, transaction id and event content.
|
||||||
pub fn new(room_id: &'a RoomId, txn_id: &'a str, content: &'a AnyMessageEventContent) -> Self {
|
///
|
||||||
Self { room_id, txn_id, event_type: content.event_type(), body: content.into() }
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Since `Request` stores the request body in serialized form, this function can fail if `T`s
|
||||||
|
/// [`Serialize`][serde::Serialize] implementation can fail.
|
||||||
|
pub fn new<T: MessageEventContent>(
|
||||||
|
room_id: &'a RoomId,
|
||||||
|
txn_id: &'a str,
|
||||||
|
content: &'a T,
|
||||||
|
) -> serde_json::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
room_id,
|
||||||
|
txn_id,
|
||||||
|
event_type: content.event_type(),
|
||||||
|
body: Raw::from_json(to_raw_json_value(content)?),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `Request` with the given room id, transaction id, event type and raw event
|
/// Creates a new `Request` with the given room id, transaction id, event type and raw event
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
//! [PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}](https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey)
|
//! [PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}](https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey)
|
||||||
|
|
||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
use ruma_events::{AnyStateEventContent, EventContent as _};
|
use ruma_events::{AnyStateEventContent, StateEventContent};
|
||||||
use ruma_identifiers::{EventId, RoomId};
|
use ruma_identifiers::{EventId, RoomId};
|
||||||
use ruma_serde::{Outgoing, Raw};
|
use ruma_serde::{Outgoing, Raw};
|
||||||
|
use serde_json::value::to_raw_value as to_raw_json_value;
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -45,8 +46,22 @@ pub struct Request<'a> {
|
|||||||
|
|
||||||
impl<'a> Request<'a> {
|
impl<'a> Request<'a> {
|
||||||
/// Creates a new `Request` with the given room id, state key and event content.
|
/// Creates a new `Request` with the given room id, state key and event content.
|
||||||
pub fn new(room_id: &'a RoomId, state_key: &'a str, content: &'a AnyStateEventContent) -> Self {
|
///
|
||||||
Self { room_id, event_type: content.event_type(), body: content.into(), state_key }
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Since `Request` stores the request body in serialized form, this function can fail if `T`s
|
||||||
|
/// [`Serialize`][serde::Serialize] implementation can fail.
|
||||||
|
pub fn new<T: StateEventContent>(
|
||||||
|
room_id: &'a RoomId,
|
||||||
|
state_key: &'a str,
|
||||||
|
content: &'a T,
|
||||||
|
) -> serde_json::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
room_id,
|
||||||
|
state_key,
|
||||||
|
event_type: content.event_type(),
|
||||||
|
body: Raw::from_json(to_raw_json_value(content)?),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `Request` with the given room id, event type, state key and raw event content.
|
/// Creates a new `Request` with the given room id, event type, state key and raw event content.
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
|
Breaking changes:
|
||||||
|
|
||||||
|
* Update `send_{message,state}_event::Request::new`'s `content` parameters to be
|
||||||
|
generic, such that custom events can easily be sent
|
||||||
|
* To migrate, simply stop wrapping content structs in `AnyMessageEventContent`
|
||||||
|
before passing them to those constructors
|
||||||
|
|
||||||
# 0.4.0
|
# 0.4.0
|
||||||
|
|
||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
@ -5,7 +5,7 @@ use std::{convert::TryFrom, env, process::exit};
|
|||||||
|
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::r0::{alias::get_alias, membership::join_room_by_id, message::send_message_event},
|
api::client::r0::{alias::get_alias, membership::join_room_by_id, message::send_message_event},
|
||||||
events::{room::message::MessageEventContent, AnyMessageEventContent},
|
events::room::message::MessageEventContent,
|
||||||
RoomAliasId,
|
RoomAliasId,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -27,8 +27,8 @@ async fn hello_world(
|
|||||||
.send_request(send_message_event::Request::new(
|
.send_request(send_message_event::Request::new(
|
||||||
&room_id,
|
&room_id,
|
||||||
"1",
|
"1",
|
||||||
&AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain("Hello World!")),
|
&MessageEventContent::text_plain("Hello World!"),
|
||||||
))
|
)?)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -2,7 +2,7 @@ use std::{convert::TryFrom, env, process::exit};
|
|||||||
|
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::r0::{alias::get_alias, membership::join_room_by_id, message::send_message_event},
|
api::client::r0::{alias::get_alias, membership::join_room_by_id, message::send_message_event},
|
||||||
events::{room::message::MessageEventContent, AnyMessageEventContent},
|
events::room::message::MessageEventContent,
|
||||||
RoomAliasId,
|
RoomAliasId,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -23,8 +23,8 @@ async fn hello_world(
|
|||||||
.send_request(send_message_event::Request::new(
|
.send_request(send_message_event::Request::new(
|
||||||
&room_id,
|
&room_id,
|
||||||
"1",
|
"1",
|
||||||
&AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain("Hello World!")),
|
&MessageEventContent::text_plain("Hello World!"),
|
||||||
))
|
)?)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user