ruma_client_api/tag/
get_tags.rs

1//! `GET /_matrix/client/*/user/{userId}/rooms/{roomId}/tags`
2//!
3//! Get the tags associated with a room.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3useruseridroomsroomidtags
9
10    use ruma_common::{
11        api::{request, response, Metadata},
12        metadata, OwnedRoomId, OwnedUserId,
13    };
14    use ruma_events::tag::Tags;
15
16    const METADATA: Metadata = metadata! {
17        method: GET,
18        rate_limited: false,
19        authentication: AccessToken,
20        history: {
21            1.0 => "/_matrix/client/r0/user/:user_id/rooms/:room_id/tags",
22            1.1 => "/_matrix/client/v3/user/:user_id/rooms/:room_id/tags",
23        }
24    };
25
26    /// Request type for the `get_tags` endpoint.
27    #[request(error = crate::Error)]
28    pub struct Request {
29        /// The user whose tags will be retrieved.
30        #[ruma_api(path)]
31        pub user_id: OwnedUserId,
32
33        /// The room from which tags will be retrieved.
34        #[ruma_api(path)]
35        pub room_id: OwnedRoomId,
36    }
37
38    /// Response type for the `get_tags` endpoint.
39    #[response(error = crate::Error)]
40    pub struct Response {
41        /// The user's tags for the room.
42        pub tags: Tags,
43    }
44
45    impl Request {
46        /// Creates a new `Request` with the given user ID and room ID.
47        pub fn new(user_id: OwnedUserId, room_id: OwnedRoomId) -> Self {
48            Self { user_id, room_id }
49        }
50    }
51
52    impl Response {
53        /// Creates a new `Response` with the given tags.
54        pub fn new(tags: Tags) -> Self {
55            Self { tags }
56        }
57    }
58
59    #[cfg(all(test, feature = "server"))]
60    mod server_tests {
61        use assign::assign;
62        use ruma_common::api::OutgoingResponse;
63        use ruma_events::tag::{TagInfo, Tags};
64        use serde_json::json;
65
66        use super::Response;
67
68        #[test]
69        fn serializing_get_tags_response() {
70            let mut tags = Tags::new();
71            tags.insert("m.favourite".into(), assign!(TagInfo::new(), { order: Some(0.25) }));
72            tags.insert("u.user_tag".into(), assign!(TagInfo::new(), { order: Some(0.11) }));
73            let response = Response { tags };
74
75            let http_response = response.try_into_http_response::<Vec<u8>>().unwrap();
76
77            let json_response: serde_json::Value =
78                serde_json::from_slice(http_response.body()).unwrap();
79            assert_eq!(
80                json_response,
81                json!({
82                    "tags": {
83                        "m.favourite": {
84                            "order": 0.25,
85                        },
86                        "u.user_tag": {
87                            "order": 0.11,
88                        }
89                    }
90                })
91            );
92        }
93    }
94}