ruma_client_api/config/
set_global_account_data.rs

1//! `PUT /_matrix/client/*/user/{userId}/account_data/{type}`
2//!
3//! Sets global account data.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#put_matrixclientv3useruseridaccount_datatype
9
10    use ruma_common::{
11        api::{request, response, Metadata},
12        metadata,
13        serde::Raw,
14        OwnedUserId,
15    };
16    use ruma_events::{
17        AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent, GlobalAccountDataEventType,
18    };
19    use serde_json::value::to_raw_value as to_raw_json_value;
20
21    const METADATA: Metadata = metadata! {
22        method: PUT,
23        rate_limited: false,
24        authentication: AccessToken,
25        history: {
26            1.0 => "/_matrix/client/r0/user/:user_id/account_data/:event_type",
27            1.1 => "/_matrix/client/v3/user/:user_id/account_data/:event_type",
28        }
29    };
30
31    /// Request type for the `set_global_account_data` endpoint.
32    #[request(error = crate::Error)]
33    pub struct Request {
34        /// The ID of the user to set account_data for.
35        ///
36        /// The access token must be authorized to make requests for this user ID.
37        #[ruma_api(path)]
38        pub user_id: OwnedUserId,
39
40        /// The event type of the account_data to set.
41        ///
42        /// Custom types should be namespaced to avoid clashes.
43        #[ruma_api(path)]
44        pub event_type: GlobalAccountDataEventType,
45
46        /// Arbitrary JSON to store as config data.
47        ///
48        /// To create a `RawJsonValue`, use `serde_json::value::to_raw_value`.
49        #[ruma_api(body)]
50        pub data: Raw<AnyGlobalAccountDataEventContent>,
51    }
52
53    /// Response type for the `set_global_account_data` endpoint.
54    #[response(error = crate::Error)]
55    #[derive(Default)]
56    pub struct Response {}
57
58    impl Request {
59        /// Creates a new `Request` with the given data, event type and user ID.
60        ///
61        /// # Errors
62        ///
63        /// Since `Request` stores the request body in serialized form, this function can fail if
64        /// `T`s [`Serialize`][serde::Serialize] implementation can fail.
65        pub fn new<T>(user_id: OwnedUserId, data: &T) -> serde_json::Result<Self>
66        where
67            T: GlobalAccountDataEventContent,
68        {
69            Ok(Self {
70                user_id,
71                event_type: data.event_type(),
72                data: Raw::from_json(to_raw_json_value(data)?),
73            })
74        }
75
76        /// Creates a new `Request` with the given raw data, event type and user ID.
77        pub fn new_raw(
78            user_id: OwnedUserId,
79            event_type: GlobalAccountDataEventType,
80            data: Raw<AnyGlobalAccountDataEventContent>,
81        ) -> Self {
82            Self { user_id, event_type, data }
83        }
84    }
85
86    impl Response {
87        /// Creates an empty `Response`.
88        pub fn new() -> Self {
89            Self {}
90        }
91    }
92}