ruma_client_api/membership/
joined_members.rs

1//! `GET /_matrix/client/*/rooms/{roomId}/joined_members`
2//!
3//! Get a map of user IDs to member info objects for members of the room. Primarily for use in
4//! Application Services.
5
6pub mod v3 {
7    //! `/v3/` ([spec])
8    //!
9    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidjoined_members
10
11    use std::collections::BTreeMap;
12
13    use ruma_common::{
14        OwnedMxcUri, OwnedRoomId, OwnedUserId,
15        api::{auth_scheme::AccessToken, request, response},
16        metadata,
17    };
18    use serde::{Deserialize, Serialize};
19
20    metadata! {
21        method: GET,
22        rate_limited: false,
23        authentication: AccessToken,
24        history: {
25            1.0 => "/_matrix/client/r0/rooms/{room_id}/joined_members",
26            1.1 => "/_matrix/client/v3/rooms/{room_id}/joined_members",
27        }
28    }
29
30    /// Request type for the `joined_members` endpoint.
31    #[request(error = crate::Error)]
32    pub struct Request {
33        /// The room to get the members of.
34        #[ruma_api(path)]
35        pub room_id: OwnedRoomId,
36    }
37
38    /// Response type for the `joined_members` endpoint.
39    #[response(error = crate::Error)]
40    pub struct Response {
41        /// A list of the rooms the user is in, i.e.
42        /// the ID of each room in which the user has joined membership.
43        pub joined: BTreeMap<OwnedUserId, RoomMember>,
44    }
45
46    impl Request {
47        /// Creates a new `Request` with the given room ID.
48        pub fn new(room_id: OwnedRoomId) -> Self {
49            Self { room_id }
50        }
51    }
52
53    impl Response {
54        /// Creates a new `Response` with the given joined rooms.
55        pub fn new(joined: BTreeMap<OwnedUserId, RoomMember>) -> Self {
56            Self { joined }
57        }
58    }
59
60    /// Information about a room member.
61    #[derive(Clone, Debug, Default, Deserialize, Serialize)]
62    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
63    pub struct RoomMember {
64        /// The display name of the user.
65        #[serde(skip_serializing_if = "Option::is_none")]
66        pub display_name: Option<String>,
67
68        /// The mxc avatar url of the user.
69        ///
70        /// If you activate the `compat-empty-string-null` feature, this field being an empty
71        /// string in JSON will result in `None` here during deserialization.
72        #[serde(skip_serializing_if = "Option::is_none")]
73        #[cfg_attr(
74            feature = "compat-empty-string-null",
75            serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
76        )]
77        pub avatar_url: Option<OwnedMxcUri>,
78    }
79
80    impl RoomMember {
81        /// Creates an empty `RoomMember`.
82        pub fn new() -> Self {
83            Default::default()
84        }
85    }
86
87    #[cfg(test)]
88    mod tests {
89        use ruma_common::mxc_uri;
90        use serde_json::{from_value as from_json_value, json};
91
92        use super::RoomMember;
93
94        #[test]
95        fn deserialize_room_member() {
96            let member = from_json_value::<RoomMember>(json!({
97                "display_name": "alice",
98                "avatar_url": "mxc://localhost/wefuiwegh8742w",
99            }))
100            .unwrap();
101            assert_eq!(member.display_name.as_deref(), Some("alice"));
102            assert_eq!(
103                member.avatar_url.as_deref(),
104                Some(mxc_uri!("mxc://localhost/wefuiwegh8742w"))
105            );
106
107            #[cfg(feature = "compat-empty-string-null")]
108            {
109                let member = from_json_value::<RoomMember>(json!({
110                    "display_name": "alice",
111                    "avatar_url": "",
112                }))
113                .unwrap();
114                assert_eq!(member.display_name.as_deref(), Some("alice"));
115                assert_eq!(member.avatar_url, None);
116            }
117        }
118    }
119}