ruma_client_api/config/
set_room_account_data.rs

1//! `PUT /_matrix/client/*/user/{userId}/rooms/{roomId}/account_data/{type}`
2//!
3//! Associate account data with a room.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#put_matrixclientv3useruseridroomsroomidaccount_datatype
9
10    use ruma_common::{
11        api::{request, response, Metadata},
12        metadata,
13        serde::Raw,
14        OwnedRoomId, OwnedUserId,
15    };
16    use ruma_events::{
17        AnyRoomAccountDataEventContent, RoomAccountDataEventContent, RoomAccountDataEventType,
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/rooms/:room_id/account_data/:event_type",
27            1.1 => "/_matrix/client/v3/user/:user_id/rooms/:room_id/account_data/:event_type",
28        }
29    };
30
31    /// Request type for the `set_room_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 ID of the room to set account_data on.
41        #[ruma_api(path)]
42        pub room_id: OwnedRoomId,
43
44        /// The event type of the account_data to set.
45        ///
46        /// Custom types should be namespaced to avoid clashes.
47        #[ruma_api(path)]
48        pub event_type: RoomAccountDataEventType,
49
50        /// Arbitrary JSON to store as config data.
51        ///
52        /// To create a `RawJsonValue`, use `serde_json::value::to_raw_value`.
53        #[ruma_api(body)]
54        pub data: Raw<AnyRoomAccountDataEventContent>,
55    }
56
57    /// Response type for the `set_room_account_data` endpoint.
58    #[response(error = crate::Error)]
59    #[derive(Default)]
60    pub struct Response {}
61
62    impl Request {
63        /// Creates a new `Request` with the given data, event type, room ID and user ID.
64        ///
65        /// # Errors
66        ///
67        /// Since `Request` stores the request body in serialized form, this function can fail if
68        /// `T`s [`Serialize`][serde::Serialize] implementation can fail.
69        pub fn new<T>(
70            user_id: OwnedUserId,
71            room_id: OwnedRoomId,
72            data: &T,
73        ) -> serde_json::Result<Self>
74        where
75            T: RoomAccountDataEventContent,
76        {
77            Ok(Self {
78                user_id,
79                room_id,
80                event_type: data.event_type(),
81                data: Raw::from_json(to_raw_json_value(data)?),
82            })
83        }
84
85        /// Creates a new `Request` with the given raw data, event type, room ID and user ID.
86        pub fn new_raw(
87            user_id: OwnedUserId,
88            room_id: OwnedRoomId,
89            event_type: RoomAccountDataEventType,
90            data: Raw<AnyRoomAccountDataEventContent>,
91        ) -> Self {
92            Self { user_id, room_id, event_type, data }
93        }
94    }
95
96    impl Response {
97        /// Creates an empty `Response`.
98        pub fn new() -> Self {
99            Self {}
100        }
101    }
102}