From 0126a7223a7b52e2ccc078f347d58455a358b4ac Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 15 Sep 2021 15:30:32 +0200 Subject: [PATCH] client-api: Use generics for content in `send_{message,state}_event::Request::new` --- .../src/r0/message/send_message_event.rs | 21 ++++++++++++++++--- .../src/r0/state/send_state_event.rs | 21 ++++++++++++++++--- crates/ruma/CHANGELOG.md | 7 +++++++ crates/ruma/examples/hello_isahc.rs | 6 +++--- crates/ruma/examples/hello_world.rs | 6 +++--- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/crates/ruma-client-api/src/r0/message/send_message_event.rs b/crates/ruma-client-api/src/r0/message/send_message_event.rs index 841a9847..d6fdde9f 100644 --- a/crates/ruma-client-api/src/r0/message/send_message_event.rs +++ b/crates/ruma-client-api/src/r0/message/send_message_event.rs @@ -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( + room_id: &'a RoomId, + txn_id: &'a str, + content: &'a T, + ) -> serde_json::Result { + 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 diff --git a/crates/ruma-client-api/src/r0/state/send_state_event.rs b/crates/ruma-client-api/src/r0/state/send_state_event.rs index 0f2a96ef..78c94533 100644 --- a/crates/ruma-client-api/src/r0/state/send_state_event.rs +++ b/crates/ruma-client-api/src/r0/state/send_state_event.rs @@ -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( + room_id: &'a RoomId, + state_key: &'a str, + content: &'a T, + ) -> serde_json::Result { + 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. diff --git a/crates/ruma/CHANGELOG.md b/crates/ruma/CHANGELOG.md index c5ded04a..5aa188d8 100644 --- a/crates/ruma/CHANGELOG.md +++ b/crates/ruma/CHANGELOG.md @@ -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: diff --git a/crates/ruma/examples/hello_isahc.rs b/crates/ruma/examples/hello_isahc.rs index aa5e75a3..06303df6 100644 --- a/crates/ruma/examples/hello_isahc.rs +++ b/crates/ruma/examples/hello_isahc.rs @@ -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(()) diff --git a/crates/ruma/examples/hello_world.rs b/crates/ruma/examples/hello_world.rs index 94e10005..9388670f 100644 --- a/crates/ruma/examples/hello_world.rs +++ b/crates/ruma/examples/hello_world.rs @@ -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(())