mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-12 18:21:21 -04:00
feat(room): Add fn RoomPrivacySettings::update_room_visibility.
This commit is contained in:
committed by
Jorge Martin Espinosa
parent
587545ae82
commit
d807d71e22
@@ -12,7 +12,7 @@ use ruma::{
|
||||
},
|
||||
OwnedRoomAliasId,
|
||||
};
|
||||
|
||||
use ruma::api::client::directory::set_room_visibility;
|
||||
use crate::{Client, Result};
|
||||
|
||||
/// A helper to group the methods in [Room](crate::Room) related to the room's
|
||||
@@ -106,6 +106,19 @@ impl<'a> RoomPrivacySettings<'a> {
|
||||
let response = self.client.send(request).await?;
|
||||
Ok(response.visibility)
|
||||
}
|
||||
|
||||
/// Update the visibility for this room in the room directory.
|
||||
///
|
||||
/// [Public](`Visibility::Public`) rooms are listed in the room directory
|
||||
/// and can be found using it.
|
||||
pub async fn update_room_visibility(&'a self, visibility: Visibility) -> Result<()> {
|
||||
let request =
|
||||
set_room_visibility::v3::Request::new(self.room.room_id().to_owned(), visibility);
|
||||
|
||||
self.client.send(request).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
@@ -119,7 +132,7 @@ mod tests {
|
||||
},
|
||||
owned_room_alias_id, room_id,
|
||||
};
|
||||
|
||||
use ruma::api::client::room::Visibility;
|
||||
use crate::test_utils::mocks::MatrixMockServer;
|
||||
|
||||
#[async_test]
|
||||
@@ -226,4 +239,18 @@ mod tests {
|
||||
let ret = room.privacy_settings().update_join_rule(JoinRule::Public).await;
|
||||
assert!(ret.is_ok());
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
async fn test_update_room_visibility() {
|
||||
let server = MatrixMockServer::new().await;
|
||||
let client = server.client_builder().build().await;
|
||||
|
||||
let room_id = room_id!("!a:b.c");
|
||||
let room = server.sync_joined_room(&client, room_id).await;
|
||||
|
||||
server.mock_room_directory_set_room_visibility().ok().mock_once().mount().await;
|
||||
|
||||
let ret = room.privacy_settings().update_room_visibility(Visibility::Private).await;
|
||||
assert!(ret.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -566,6 +566,44 @@ impl MatrixMockServer {
|
||||
MockEndpoint { mock, server: &self.server, endpoint: PublicRoomsEndpoint }
|
||||
}
|
||||
|
||||
/// Create a prebuilt mock for setting a room's visibility in the room
|
||||
/// directory.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # tokio_test::block_on(async {
|
||||
/// use matrix_sdk::{ruma::room_id, test_utils::mocks::MatrixMockServer};
|
||||
/// use ruma::api::client::room::Visibility;
|
||||
///
|
||||
/// let mock_server = MatrixMockServer::new().await;
|
||||
/// let client = mock_server.client_builder().build().await;
|
||||
///
|
||||
/// mock_server
|
||||
/// .mock_room_directory_set_room_visibility()
|
||||
/// .ok()
|
||||
/// .mock_once()
|
||||
/// .mount()
|
||||
/// .await;
|
||||
///
|
||||
/// let room = mock_server
|
||||
/// .sync_joined_room(&client, room_id!("!room_id:localhost"))
|
||||
/// .await;
|
||||
///
|
||||
/// room.privacy_settings()
|
||||
/// .update_room_visibility(Visibility::Private)
|
||||
/// .await
|
||||
/// .expect("We should be able to update the room's visibility");
|
||||
/// # anyhow::Ok(()) });
|
||||
/// ```
|
||||
pub fn mock_room_directory_set_room_visibility(
|
||||
&self,
|
||||
) -> MockEndpoint<'_, SetRoomVisibilityEndpoint> {
|
||||
let mock = Mock::given(method("PUT"))
|
||||
.and(path_regex(r"^/_matrix/client/v3/directory/list/room/.*$"));
|
||||
MockEndpoint { mock, server: &self.server, endpoint: SetRoomVisibilityEndpoint }
|
||||
}
|
||||
|
||||
/// Create a prebuilt mock for getting a room's visibility in the room
|
||||
/// directory.
|
||||
///
|
||||
@@ -1874,6 +1912,17 @@ impl<'a> MockEndpoint<'a, GetRoomVisibilityEndpoint> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A prebuilt mock for setting the room's visibility in the room directory.
|
||||
pub struct SetRoomVisibilityEndpoint;
|
||||
|
||||
impl<'a> MockEndpoint<'a, SetRoomVisibilityEndpoint> {
|
||||
/// Returns an endpoint that updates the room's visibility.
|
||||
pub fn ok(self) -> MatrixMock<'a> {
|
||||
let mock = self.mock.respond_with(ResponseTemplate::new(200).set_body_json(json!({})));
|
||||
MatrixMock { server: self.server, mock }
|
||||
}
|
||||
}
|
||||
|
||||
/// A prebuilt mock for `GET room_keys/version`: storage ("backup") of room
|
||||
/// keys.
|
||||
pub struct RoomKeysVersionEndpoint;
|
||||
|
||||
Reference in New Issue
Block a user