Skip to content

Commit d9a56e7

Browse files
committed
feat: room_membership api
1 parent 6b85e56 commit d9a56e7

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod account_validity;
1313
pub mod background_updates;
1414
pub mod experimental_features;
1515
pub mod register_users;
16+
pub mod room_membership;
1617
pub mod rooms;
1718
pub mod users;
1819
pub mod version;

src/room_membership.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Endpoints in the `/_synapse/admin/v<x>/join/` scope.
2+
3+
pub mod join_room;

src/room_membership/join_room.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Different versions of the endpoint to place users into rooms.
2+
3+
pub mod v1;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//! [POST /_synapse/admin/v1/join/:room_id_or_alias](https://github.com/element-hq/synapse/blob/master/docs/admin_api/room_membership.md)
2+
3+
use ruma::{
4+
api::{request, response, Metadata},
5+
metadata, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId,
6+
};
7+
use serde::{Deserialize, Serialize};
8+
9+
const METADATA: Metadata = metadata! {
10+
method: POST,
11+
rate_limited: false,
12+
authentication: AccessToken,
13+
history: {
14+
unstable => "/_synapse/admin/v1/join/:room_id_or_alias",
15+
}
16+
};
17+
18+
#[request]
19+
#[derive(Serialize, Deserialize, PartialEq)]
20+
pub struct Request {
21+
/// Alias or ID of the room to join.
22+
#[ruma_api(path)]
23+
pub room_id_or_alias: OwnedRoomOrAliasId,
24+
25+
/// User to join the room.
26+
pub user_id: OwnedUserId,
27+
}
28+
29+
#[response]
30+
#[derive(Serialize, Deserialize, PartialEq)]
31+
pub struct Response {
32+
/// Room ID of the joined room.
33+
pub room_id: OwnedRoomId,
34+
}
35+
36+
impl Request {
37+
/// Creates a new `Request` with the given room or alias ID and user id.
38+
pub fn new(room_id_or_alias: OwnedRoomOrAliasId, user_id: OwnedUserId) -> Self {
39+
Self { room_id_or_alias, user_id }
40+
}
41+
}
42+
43+
impl Response {
44+
/// Creates a new `Response` with the given room id
45+
pub fn new(room_id: OwnedRoomId) -> Self {
46+
Self { room_id }
47+
}
48+
}
49+
50+
#[test]
51+
fn test_join_room() {
52+
use ruma::{RoomId, UserId};
53+
use std::convert::TryFrom;
54+
55+
let room_id: &RoomId =
56+
<&RoomId>::try_from("!test:example.com").expect("Failed to create RoomId");
57+
58+
let user_id: &UserId =
59+
<&UserId>::try_from("@carl:example.com").expect("Failed to create UserId.");
60+
61+
// Check create request
62+
let request = Request::new(room_id.to_owned().into(), user_id.to_owned());
63+
64+
// Serialize
65+
let serialized_request = serde_json::to_string(&request).expect("Failed to serialize request");
66+
assert_eq!(
67+
serialized_request,
68+
"{\"room_id_or_alias\":\"!test:example.com\",\"user_id\":\"@carl:example.com\"}"
69+
);
70+
71+
// Deserialize
72+
let deserialized_request: Request =
73+
serde_json::from_str(&serialized_request).expect("Failed to deserialize request");
74+
assert_eq!(deserialized_request, request);
75+
76+
// Check create response
77+
let response = Response::new(room_id.to_owned());
78+
79+
// Serialize
80+
let serialized_response =
81+
serde_json::to_string(&response).expect("Failed to serialize response");
82+
assert_eq!(serialized_response, "{\"room_id\":\"!test:example.com\"}");
83+
84+
// Deserialize
85+
let deserialized_response: Response =
86+
serde_json::from_str(&serialized_response).expect("Failed to deserialize response");
87+
assert_eq!(deserialized_response, response);
88+
}

0 commit comments

Comments
 (0)