client-api: Use Raw for set_*_account_data::Request::data

This commit is contained in:
Kévin Commaille 2022-02-25 22:46:30 +01:00 committed by GitHub
parent bff34569cf
commit 60b2212a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 7 deletions

View File

@ -1,5 +1,9 @@
# [unreleased]
Breaking changes:
* Use `Raw` for `config::set_*_account_data::Request::data`.
# 0.13.0
Bug fixes:

View File

@ -6,8 +6,10 @@ pub mod v3 {
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3useruseridaccount_datatype
use ruma_api::ruma_api;
use ruma_events::{AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent};
use ruma_identifiers::UserId;
use serde_json::value::RawValue as RawJsonValue;
use ruma_serde::Raw;
use serde_json::value::to_raw_value as to_raw_json_value;
ruma_api! {
metadata: {
@ -38,7 +40,7 @@ pub mod v3 {
///
/// To create a `RawJsonValue`, use `serde_json::value::to_raw_value`.
#[ruma_api(body)]
pub data: &'a RawJsonValue,
pub data: Raw<AnyGlobalAccountDataEventContent>,
}
#[derive(Default)]
@ -49,7 +51,28 @@ pub mod v3 {
impl<'a> Request<'a> {
/// Creates a new `Request` with the given data, event type and user ID.
pub fn new(data: &'a RawJsonValue, event_type: &'a str, user_id: &'a UserId) -> Self {
///
/// # 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: GlobalAccountDataEventContent>(
data: &'a T,
user_id: &'a UserId,
) -> serde_json::Result<Self> {
Ok(Self {
user_id,
event_type: data.event_type(),
data: Raw::from_json(to_raw_json_value(data)?),
})
}
/// Creates a new `Request` with the given raw data, event type and user ID.
pub fn new_raw(
data: Raw<AnyGlobalAccountDataEventContent>,
event_type: &'a str,
user_id: &'a UserId,
) -> Self {
Self { user_id, event_type, data }
}
}

View File

@ -6,8 +6,10 @@ pub mod v3 {
//! [spec]: https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3useruseridroomsroomidaccount_datatype
use ruma_api::ruma_api;
use ruma_events::{AnyRoomAccountDataEventContent, RoomAccountDataEventContent};
use ruma_identifiers::{RoomId, UserId};
use serde_json::value::RawValue as RawJsonValue;
use ruma_serde::Raw;
use serde_json::value::to_raw_value as to_raw_json_value;
ruma_api! {
metadata: {
@ -26,7 +28,7 @@ pub mod v3 {
///
/// To create a `RawJsonValue`, use `serde_json::value::to_raw_value`.
#[ruma_api(body)]
pub data: &'a RawJsonValue,
pub data: Raw<AnyRoomAccountDataEventContent>,
/// The event type of the account_data to set.
///
@ -53,8 +55,27 @@ pub mod v3 {
impl<'a> Request<'a> {
/// Creates a new `Request` with the given data, event type, room ID and user ID.
pub fn new(
data: &'a RawJsonValue,
///
/// # 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: RoomAccountDataEventContent>(
data: &'a T,
room_id: &'a RoomId,
user_id: &'a UserId,
) -> serde_json::Result<Self> {
Ok(Self {
data: Raw::from_json(to_raw_json_value(data)?),
event_type: data.event_type(),
room_id,
user_id,
})
}
/// Creates a new `Request` with the given raw data, event type, room ID and user ID.
pub fn new_raw(
data: Raw<AnyRoomAccountDataEventContent>,
event_type: &'a str,
room_id: &'a RoomId,
user_id: &'a UserId,