Skip to main content

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