From 116fe0783b243cb490307d691814f0308bab7ce1 Mon Sep 17 00:00:00 2001 From: Timo Date: Fri, 15 Nov 2024 11:41:10 +0100 Subject: [PATCH 1/2] feat(Room): Check if the user is allowed to do a room mention before trying to send a call notify event. --- crates/matrix-sdk/src/room/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 00484d31457..c8b95ca283e 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -2975,6 +2975,10 @@ impl Room { return Ok(()); } + if !self.can_user_trigger_room_notification(self.own_user_id()).await? { + return Ok(()); + } + self.send_call_notification( self.room_id().to_string().to_owned(), ApplicationType::Call, From 4f529298f1e6e3a2373f04522c2a60df1e7140fc Mon Sep 17 00:00:00 2001 From: Timo Date: Fri, 15 Nov 2024 12:24:59 +0100 Subject: [PATCH 2/2] add tests --- .../tests/integration/room/joined.rs | 47 +++++++++++++++++-- .../src/test_json/sync_events.rs | 3 ++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/crates/matrix-sdk/tests/integration/room/joined.rs b/crates/matrix-sdk/tests/integration/room/joined.rs index 2c81f1caaca..440d1a9030d 100644 --- a/crates/matrix-sdk/tests/integration/room/joined.rs +++ b/crates/matrix-sdk/tests/integration/room/joined.rs @@ -14,8 +14,8 @@ use matrix_sdk_test::{ async_test, mocks::{mock_encryption_state, mock_redaction}, test_json::{self, sync::CUSTOM_ROOM_POWER_LEVELS}, - EphemeralTestEvent, GlobalAccountDataTestEvent, JoinedRoomBuilder, SyncResponseBuilder, - DEFAULT_TEST_ROOM_ID, + EphemeralTestEvent, GlobalAccountDataTestEvent, JoinedRoomBuilder, StateTestEvent, + SyncResponseBuilder, DEFAULT_TEST_ROOM_ID, }; use ruma::{ api::client::{membership::Invite3pidInit, receipt::create_receipt::v3::ReceiptType}, @@ -635,7 +635,8 @@ async fn test_call_notifications_ring_for_dms() { let (client, server) = logged_in_client_with_server().await; let mut sync_builder = SyncResponseBuilder::new(); - sync_builder.add_joined_room(JoinedRoomBuilder::default()); + let room_builder = JoinedRoomBuilder::default().add_state_event(StateTestEvent::PowerLevels); + sync_builder.add_joined_room(room_builder); sync_builder.add_global_account_data_event(GlobalAccountDataTestEvent::Direct); mock_sync(&server, sync_builder.build_json_sync_response(), None).await; @@ -678,9 +679,10 @@ async fn test_call_notifications_notify_for_rooms() { let (client, server) = logged_in_client_with_server().await; let mut sync_builder = SyncResponseBuilder::new(); - sync_builder.add_joined_room(JoinedRoomBuilder::default()); - + let room_builder = JoinedRoomBuilder::default().add_state_event(StateTestEvent::PowerLevels); + sync_builder.add_joined_room(room_builder); mock_sync(&server, sync_builder.build_json_sync_response(), None).await; + mock_encryption_state(&server, false).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -715,6 +717,41 @@ async fn test_call_notifications_notify_for_rooms() { room.send_call_notification_if_needed().await.unwrap(); } +#[async_test] +async fn test_call_notifications_dont_notify_room_without_mention_powerlevel() { + let (client, server) = logged_in_client_with_server().await; + + let mut sync_builder = SyncResponseBuilder::new(); + let mut power_level_event = StateTestEvent::PowerLevels.into_json_value(); + // Allow noone to send room notify events. + *power_level_event.get_mut("content").unwrap().get_mut("notifications").unwrap() = + json!({"room": 101}); + + sync_builder.add_joined_room( + JoinedRoomBuilder::default().add_state_event(StateTestEvent::Custom(power_level_event)), + ); + + mock_sync(&server, sync_builder.build_json_sync_response(), None).await; + mock_encryption_state(&server, false).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(); + assert!(!room.is_direct().await.unwrap()); + assert!(!room.has_active_room_call()); + + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({"event_id": "$event_id"}))) + // Expect no calls of the send because we dont have permission to notify. + .expect(0) + .mount(&server) + .await; + + room.send_call_notification_if_needed().await.unwrap(); +} + #[async_test] async fn test_make_reply_event_doesnt_require_event_cache() { // Even if we don't have enabled the event cache, we'll resort to using the diff --git a/testing/matrix-sdk-test/src/test_json/sync_events.rs b/testing/matrix-sdk-test/src/test_json/sync_events.rs index e5f5d824b29..df68df07bc9 100644 --- a/testing/matrix-sdk-test/src/test_json/sync_events.rs +++ b/testing/matrix-sdk-test/src/test_json/sync_events.rs @@ -350,6 +350,9 @@ pub static POWER_LEVELS: Lazy = Lazy::new(|| { "kick": 50, "redact": 50, "state_default": 50, + "notifications": { + "room": 0 + }, "users": { "@example:localhost": 100, "@bob:localhost": 0