Skip to content

Commit

Permalink
sdk: Add a method to change the room name
Browse files Browse the repository at this point in the history
Signed-off-by: Kévin Commaille <[email protected]>
  • Loading branch information
zecakeh authored Mar 15, 2023
1 parent 248b8db commit fe69948
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/matrix-sdk/src/room/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use ruma::{
room::{
avatar::{ImageInfo, RoomAvatarEventContent},
message::RoomMessageEventContent,
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
topic::RoomTopicEventContent,
},
Expand Down Expand Up @@ -863,6 +864,11 @@ impl Joined {
self.send_state_event(RoomPowerLevelsEventContent::from(power_levels)).await
}

/// Sets the name of this room.
pub async fn set_name(&self, name: Option<String>) -> Result<send_state_event::v3::Response> {
self.send_state_event(RoomNameEventContent::new(name)).await
}

/// Sets a new topic for this room.
pub async fn set_room_topic(&self, topic: &str) -> Result<send_state_event::v3::Response> {
let topic_event = RoomTopicEventContent::new(topic.into());
Expand Down
27 changes: 26 additions & 1 deletion crates/matrix-sdk/tests/integration/room/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ruma::{
};
use serde_json::json;
use wiremock::{
matchers::{body_partial_json, header, method, path, path_regex},
matchers::{body_json, body_partial_json, header, method, path, path_regex},
Mock, ResponseTemplate,
};

Expand Down Expand Up @@ -560,3 +560,28 @@ async fn fetch_members_deduplication() {
results.iter().filter(|r| r.as_ref().unwrap().as_ref().unwrap().is_some()).count();
assert_eq!(response_count, 1);
}

#[async_test]
async fn set_name() {
let (client, server) = synced_client().await;

mock_sync(&server, &*test_json::SYNC, None).await;
let sync_settings = SyncSettings::new();
client.sync_once(sync_settings).await.unwrap();

let room = client.get_joined_room(&test_json::DEFAULT_SYNC_ROOM_ID).unwrap();
let name = "The room name";

Mock::given(method("PUT"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.name/$"))
.and(header("authorization", "Bearer 1234"))
.and(body_json(json!({
"name": name,
})))
.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID))
.expect(1)
.mount(&server)
.await;

room.set_name(Some(name.to_owned())).await.unwrap();
}

0 comments on commit fe69948

Please sign in to comment.