ruma_client_api/membership/
get_member_events.rs

1//! `GET /_matrix/client/*/rooms/{roomId}/members`
2//!
3//! Get membership events for a room.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidmembers
9
10    use ruma_common::{
11        api::{request, response, Metadata},
12        metadata,
13        serde::{Raw, StringEnum},
14        OwnedRoomId,
15    };
16    use ruma_events::room::member::RoomMemberEvent;
17
18    use crate::PrivOwnedStr;
19
20    const METADATA: Metadata = metadata! {
21        method: GET,
22        rate_limited: false,
23        authentication: AccessToken,
24        history: {
25            1.0 => "/_matrix/client/r0/rooms/:room_id/members",
26            1.1 => "/_matrix/client/v3/rooms/:room_id/members",
27        }
28    };
29
30    /// Request type for the `get_member_events` endpoint.
31    #[request(error = crate::Error)]
32    pub struct Request {
33        /// The room to get the member events for.
34        #[ruma_api(path)]
35        pub room_id: OwnedRoomId,
36
37        /// The point in time (pagination token) to return members for in the room.
38        ///
39        /// This token can be obtained from a prev_batch token returned for each room by the sync
40        /// API.
41        #[serde(skip_serializing_if = "Option::is_none")]
42        #[ruma_api(query)]
43        pub at: Option<String>,
44
45        /// The kind of memberships to filter for.
46        ///
47        /// Defaults to no filtering if unspecified. When specified alongside not_membership, the
48        /// two parameters create an 'or' condition: either the membership is the same as
49        /// membership or is not the same as not_membership.
50        #[serde(skip_serializing_if = "Option::is_none")]
51        #[ruma_api(query)]
52        pub membership: Option<MembershipEventFilter>,
53
54        /// The kind of memberships to *exclude* from the results.
55        ///
56        /// Defaults to no filtering if unspecified.
57        #[serde(skip_serializing_if = "Option::is_none")]
58        #[ruma_api(query)]
59        pub not_membership: Option<MembershipEventFilter>,
60    }
61
62    /// Response type for the `get_member_events` endpoint.
63    #[response(error = crate::Error)]
64    pub struct Response {
65        /// A list of member events.
66        pub chunk: Vec<Raw<RoomMemberEvent>>,
67    }
68
69    impl Request {
70        /// Creates a new `Request` with the given room ID.
71        pub fn new(room_id: OwnedRoomId) -> Self {
72            Self { room_id, at: None, membership: None, not_membership: None }
73        }
74    }
75
76    impl Response {
77        /// Creates a new `Response` with the given member event chunk.
78        pub fn new(chunk: Vec<Raw<RoomMemberEvent>>) -> Self {
79            Self { chunk }
80        }
81    }
82
83    /// The kind of membership events to filter for.
84    #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
85    #[derive(Clone, PartialEq, Eq, StringEnum)]
86    #[ruma_enum(rename_all = "lowercase")]
87    #[non_exhaustive]
88    pub enum MembershipEventFilter {
89        /// The user has joined.
90        Join,
91
92        /// The user has been invited.
93        Invite,
94
95        /// The user has left.
96        Leave,
97
98        /// The user has been banned.
99        Ban,
100
101        #[doc(hidden)]
102        _Custom(PrivOwnedStr),
103    }
104
105    #[cfg(all(test, feature = "server"))]
106    mod tests {
107        use ruma_common::api::IncomingRequest as _;
108
109        use super::{MembershipEventFilter, Request};
110
111        #[test]
112        fn deserialization() {
113            let uri = http::Uri::builder()
114                .scheme("https")
115                .authority("example.org")
116                .path_and_query(
117                    "/_matrix/client/r0/rooms/!dummy%3Aexample.org/members\
118                 ?not_membership=leave\
119                 &at=1026",
120                )
121                .build()
122                .unwrap();
123
124            let req = Request::try_from_http_request(
125                http::Request::builder().uri(uri).body(&[] as &[u8]).unwrap(),
126                &["!dummy:example.org"],
127            )
128            .unwrap();
129
130            assert_eq!(req.room_id, "!dummy:example.org");
131            assert_eq!(req.at.as_deref(), Some("1026"));
132            assert_eq!(req.membership, None);
133            assert_eq!(req.not_membership, Some(MembershipEventFilter::Leave));
134        }
135    }
136}