From 623f490d3006d1d6e79ce7e3f2754898b2dcb0e7 Mon Sep 17 00:00:00 2001 From: Daniel Kilimnik Date: Thu, 9 Jan 2025 00:35:25 +0100 Subject: [PATCH] feat: room_membership api --- src/lib.rs | 1 + src/room_membership.rs | 3 ++ src/room_membership/join_room.rs | 3 ++ src/room_membership/join_room/v1.rs | 62 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/room_membership.rs create mode 100644 src/room_membership/join_room.rs create mode 100644 src/room_membership/join_room/v1.rs diff --git a/src/lib.rs b/src/lib.rs index f0435bd..1d59d21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/room_membership.rs b/src/room_membership.rs new file mode 100644 index 0000000..451f37a --- /dev/null +++ b/src/room_membership.rs @@ -0,0 +1,3 @@ +//! Endpoints in the `/_synapse/admin/v/join/` scope. + +pub mod join_room; diff --git a/src/room_membership/join_room.rs b/src/room_membership/join_room.rs new file mode 100644 index 0000000..d5a4796 --- /dev/null +++ b/src/room_membership/join_room.rs @@ -0,0 +1,3 @@ +//! Different versions of the endpoint to place users into rooms. + +pub mod v1; diff --git a/src/room_membership/join_room/v1.rs b/src/room_membership/join_room/v1.rs new file mode 100644 index 0000000..c578405 --- /dev/null +++ b/src/room_membership/join_room/v1.rs @@ -0,0 +1,62 @@ +//! [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, +}; + +const METADATA: Metadata = metadata! { + method: POST, + rate_limited: false, + authentication: AccessToken, + history: { + unstable => "/_synapse/admin/v1/join/:room_id_or_alias", + } +}; + +#[request] +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] +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, OwnedRoomOrAliasId, 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.clone().into(), user_id.clone()); + assert_eq!(request.room_id_or_alias, OwnedRoomOrAliasId::from(room_id.clone())); + assert_eq!(request.user_id, user_id); + + // Check create response + let response = Response::new(room_id.clone()); + assert_eq!(response.room_id, room_id); +}