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.
56pub mod v3 {
7//! `/v3/` ([spec])
8 //!
9 //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidjoined_members
1011use std::collections::BTreeMap;
1213use ruma_common::{
14 api::{request, response, Metadata},
15 metadata, OwnedMxcUri, OwnedRoomId, OwnedUserId,
16 };
17use serde::{Deserialize, Serialize};
1819const METADATA: Metadata = metadata! {
20 method: GET,
21 rate_limited: false,
22 authentication: AccessToken,
23 history: {
241.0 => "/_matrix/client/r0/rooms/:room_id/joined_members",
251.1 => "/_matrix/client/v3/rooms/:room_id/joined_members",
26 }
27 };
2829/// Request type for the `joined_members` endpoint.
30#[request(error = crate::Error)]
31pub struct Request {
32/// The room to get the members of.
33#[ruma_api(path)]
34pub room_id: OwnedRoomId,
35 }
3637/// Response type for the `joined_members` endpoint.
38#[response(error = crate::Error)]
39pub struct Response {
40/// A list of the rooms the user is in, i.e.
41 /// the ID of each room in which the user has joined membership.
42pub joined: BTreeMap<OwnedUserId, RoomMember>,
43 }
4445impl Request {
46/// Creates a new `Request` with the given room ID.
47pub fn new(room_id: OwnedRoomId) -> Self {
48Self { room_id }
49 }
50 }
5152impl Response {
53/// Creates a new `Response` with the given joined rooms.
54pub fn new(joined: BTreeMap<OwnedUserId, RoomMember>) -> Self {
55Self { joined }
56 }
57 }
5859/// Information about a room member.
60#[derive(Clone, Debug, Default, Deserialize, Serialize)]
61 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
62pub struct RoomMember {
63/// The display name of the user.
64#[serde(skip_serializing_if = "Option::is_none")]
65pub display_name: Option<String>,
6667/// The mxc avatar url of the user.
68 ///
69 /// If you activate the `compat-empty-string-null` feature, this field being an empty
70 /// string in JSON will result in `None` here during deserialization.
71#[serde(skip_serializing_if = "Option::is_none")]
72 #[cfg_attr(
73 feature = "compat-empty-string-null",
74 serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
75 )]
76pub avatar_url: Option<OwnedMxcUri>,
77 }
7879impl RoomMember {
80/// Creates an empty `RoomMember`.
81pub fn new() -> Self {
82 Default::default()
83 }
84 }
8586#[cfg(test)]
87mod tests {
88use ruma_common::mxc_uri;
89use serde_json::{from_value as from_json_value, json};
9091use super::RoomMember;
9293#[test]
94fn deserialize_room_member() {
95let member = from_json_value::<RoomMember>(json!({
96"display_name": "alice",
97"avatar_url": "mxc://localhost/wefuiwegh8742w",
98 }))
99 .unwrap();
100assert_eq!(member.display_name.as_deref(), Some("alice"));
101assert_eq!(
102 member.avatar_url.as_deref(),
103Some(mxc_uri!("mxc://localhost/wefuiwegh8742w"))
104 );
105106#[cfg(feature = "compat-empty-string-null")]
107{
108let member = from_json_value::<RoomMember>(json!({
109"display_name": "alice",
110"avatar_url": "",
111 }))
112 .unwrap();
113assert_eq!(member.display_name.as_deref(), Some("alice"));
114assert_eq!(member.avatar_url, None);
115 }
116 }
117 }
118}