Skip to content

Commit

Permalink
feat: room_membership api
Browse files Browse the repository at this point in the history
  • Loading branch information
kilimnik committed Jan 9, 2025
1 parent 0a033da commit 60b37db
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod account_validity;
pub mod background_updates;
pub mod experimental_features;
pub mod register_users;
pub mod room_membership;
pub mod rooms;
pub mod users;
pub mod version;
Expand Down
3 changes: 3 additions & 0 deletions src/room_membership.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Endpoints in the `/_synapse/admin/v<x>/join/` scope.
pub mod join_room;
3 changes: 3 additions & 0 deletions src/room_membership/join_room.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Different versions of the endpoint to place users into rooms.
pub mod v1;
84 changes: 84 additions & 0 deletions src/room_membership/join_room/v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//! [POST /_synapse/admin/v1/join/:room_id_or_alias](https://github.com/element-hq/synapse/blob/master/docs/admin_api/room_membership.md)
use ruma::{
api::{request, response, Metadata},
metadata, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId,
};
use serde::{Deserialize, Serialize};

const METADATA: Metadata = metadata! {
method: POST,
rate_limited: false,
authentication: AccessToken,
history: {
unstable => "/_synapse/admin/v1/join/:room_id_or_alias",
}
};

#[request]
#[derive(Serialize, Deserialize, PartialEq)]
pub struct Request {
/// Alias or ID of the room to join.
#[ruma_api(path)]
pub room_id_or_alias: OwnedRoomOrAliasId,

/// User to join the room.
pub user_id: OwnedUserId,
}

#[response]
#[derive(Serialize, Deserialize, PartialEq)]
pub struct Response {
/// Room ID of the joined room.
pub room_id: OwnedRoomId,
}

impl Request {
/// Creates a new `Request` with the given room or alias ID and user id.
pub fn new(room_id_or_alias: OwnedRoomOrAliasId, user_id: OwnedUserId) -> Self {
Self { room_id_or_alias, user_id }
}
}

impl Response {
/// Creates a new `Response` with the given room id
pub fn new(room_id: OwnedRoomId) -> Self {
Self { room_id }
}
}

#[test]
fn test_join_room() {
use ruma::{owned_room_id, owned_user_id, OwnedRoomId, OwnedUserId};

let room_id: OwnedRoomId = owned_room_id!("!test:example.com");
let user_id: OwnedUserId = owned_user_id!("@carl:example.com");

// Check create request
let request = Request::new(room_id.to_owned().into(), user_id.to_owned());

// Serialize
let serialized_request = serde_json::to_string(&request).expect("Failed to serialize request");
assert_eq!(
serialized_request,
"{\"room_id_or_alias\":\"!test:example.com\",\"user_id\":\"@carl:example.com\"}"
);

// Deserialize
let deserialized_request: Request =
serde_json::from_str(&serialized_request).expect("Failed to deserialize request");
assert_eq!(deserialized_request, request);

// Check create response
let response = Response::new(room_id.to_owned());

// Serialize
let serialized_response =
serde_json::to_string(&response).expect("Failed to serialize response");
assert_eq!(serialized_response, "{\"room_id\":\"!test:example.com\"}");

// Deserialize
let deserialized_response: Response =
serde_json::from_str(&serialized_response).expect("Failed to deserialize response");
assert_eq!(deserialized_response, response);
}

0 comments on commit 60b37db

Please sign in to comment.