Skip to content

Commit

Permalink
feat(sdk): Add Room::report_room
Browse files Browse the repository at this point in the history
solves this #4681

- add room report_room api from
https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3roomsroomidreport
- expose report_room on room ffi

---------

Signed-off-by: hanadi92 <[email protected]>
  • Loading branch information
hanadi92 authored Feb 26, 2025
1 parent f02a7d1 commit 7dba05f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions bindings/matrix-sdk-ffi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ Additions:
- Add `ClientBuilder::room_key_recipient_strategy`
- Add `Room::send_raw`
- Expose `withdraw_verification` to `UserIdentity`
- Expose `report_room` to `Room`
20 changes: 19 additions & 1 deletion bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use matrix_sdk::{
use matrix_sdk_ui::timeline::{default_event_filter, RoomExt};
use mime::Mime;
use ruma::{
api::client::room::report_content,
api::client::room::{report_content, report_room},
assign,
events::{
call::notify,
Expand Down Expand Up @@ -389,6 +389,24 @@ impl Room {
Ok(())
}

/// Reports a room as inappropriate to the server.
/// The caller is not required to be joined to the room to report it.
///
/// # Arguments
///
/// * `reason` - The reason the room is being reported.
///
/// # Errors
///
/// Returns an error if the room is not found or on rate limit
pub async fn report_room(&self, reason: Option<String>) -> Result<(), ClientError> {
let mut request = report_room::v3::Request::new(self.inner.room_id().into());
request.reason = reason;

self.inner.client().send(request).await?;
Ok(())
}

/// Ignores a user.
///
/// # Arguments
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ simpler methods:
- [**breaking**] The HTTP client only allows TLS 1.2 or newer, as recommended by
[BCP 195](https://datatracker.ietf.org/doc/bcp195/).
([#4647](https://github.com/matrix-org/matrix-rust-sdk/pull/4647))
- Add `Room::report_room` api. ([#4713](https://github.com/matrix-org/matrix-rust-sdk/pull/4713))

### Bug Fixes

Expand Down
19 changes: 18 additions & 1 deletion crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use ruma::{
read_marker::set_read_marker,
receipt::create_receipt,
redact::redact_event,
room::{get_room_event, report_content},
room::{get_room_event, report_content, report_room},
state::{get_state_events_for_key, send_state_event},
tag::{create_tag, delete_tag},
typing::create_typing_event::{self, v3::Typing},
Expand Down Expand Up @@ -3021,6 +3021,23 @@ impl Room {
Ok(self.client.send(request).await?)
}

/// Reports a room as inappropriate to the server.
/// The caller is not required to be joined to the room to report it.
///
/// # Arguments
///
/// * `reason` - The reason the room is being reported.
///
/// # Errors
///
/// Returns an error if the room is not found or on rate limit
pub async fn report_room(&self, reason: Option<String>) -> Result<report_room::v3::Response> {
let mut request = report_room::v3::Request::new(self.inner.room_id().to_owned());
request.reason = reason;

Ok(self.client.send(request).await?)
}

/// Set a flag on the room to indicate that the user has explicitly marked
/// it as (un)read.
pub async fn set_unread_flag(&self, unread: bool) -> Result<()> {
Expand Down
23 changes: 23 additions & 0 deletions crates/matrix-sdk/tests/integration/room/joined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,3 +1186,26 @@ async fn test_room_member_updates_sender_on_partial_members_update() {
assert_let!(RoomMembersUpdate::Partial(user_ids) = next);
assert_eq!(user_ids, BTreeSet::from_iter(vec![user_id!("@alice:b.c").to_owned()]));
}

#[async_test]
async fn test_report_room() {
let (client, server) = logged_in_client_with_server().await;
let reason = "this makes me sad";

Mock::given(method("POST"))
.and(path_regex(r"^/_matrix/client/.*/rooms/.*/report$"))
.and(body_json(json!({
"reason": reason,
})))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EMPTY))
.mount(&server)
.await;

mock_sync(&server, &*test_json::SYNC, None).await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let _response = client.sync_once(sync_settings).await.unwrap();
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();

room.report_room(Some(reason.to_owned())).await.unwrap();
}

0 comments on commit 7dba05f

Please sign in to comment.