client-api: Use generics for content in send_{message,state}_event::Request::new

This commit is contained in:
Jonas Platte 2021-09-15 15:30:32 +02:00
parent f214aef8ad
commit 0126a7223a
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
5 changed files with 49 additions and 12 deletions

View File

@ -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)
use ruma_api::ruma_api;
use ruma_events::{AnyMessageEventContent, EventContent as _};
use ruma_events::{AnyMessageEventContent, MessageEventContent};
use ruma_identifiers::{EventId, RoomId};
use ruma_serde::Raw;
use serde_json::value::to_raw_value as to_raw_json_value;
ruma_api! {
metadata: {
@ -47,8 +48,22 @@ ruma_api! {
impl<'a> Request<'a> {
/// 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

View File

@ -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)
use ruma_api::ruma_api;
use ruma_events::{AnyStateEventContent, EventContent as _};
use ruma_events::{AnyStateEventContent, StateEventContent};
use ruma_identifiers::{EventId, RoomId};
use ruma_serde::{Outgoing, Raw};
use serde_json::value::to_raw_value as to_raw_json_value;
ruma_api! {
metadata: {
@ -45,8 +46,22 @@ pub struct Request<'a> {
impl<'a> Request<'a> {
/// 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.

View File

@ -1,5 +1,12 @@
# [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
Breaking changes:

View File

@ -5,7 +5,7 @@ use std::{convert::TryFrom, env, process::exit};
use ruma::{
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,
};
@ -27,8 +27,8 @@ async fn hello_world(
.send_request(send_message_event::Request::new(
&room_id,
"1",
&AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain("Hello World!")),
))
&MessageEventContent::text_plain("Hello World!"),
)?)
.await?;
Ok(())

View File

@ -2,7 +2,7 @@ use std::{convert::TryFrom, env, process::exit};
use ruma::{
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,
};
@ -23,8 +23,8 @@ async fn hello_world(
.send_request(send_message_event::Request::new(
&room_id,
"1",
&AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain("Hello World!")),
))
&MessageEventContent::text_plain("Hello World!"),
)?)
.await?;
Ok(())