|
1 | 1 | use assert_matches2::assert_matches;
|
2 |
| -use matrix_sdk::{room::ThreadSubscription, test_utils::mocks::MatrixMockServer}; |
| 2 | +use matrix_sdk::{ |
| 3 | + notification_settings::RoomNotificationMode, |
| 4 | + room::ThreadSubscription, |
| 5 | + test_utils::mocks::{MatrixMockServer, PushRuleIdSpec}, |
| 6 | +}; |
3 | 7 | use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder, ALICE};
|
4 |
| -use ruma::{event_id, owned_event_id, room_id}; |
| 8 | +use ruma::{event_id, owned_event_id, push::RuleKind, room_id}; |
5 | 9 |
|
6 | 10 | #[async_test]
|
7 | 11 | async fn test_subscribe_thread() {
|
@@ -238,3 +242,187 @@ async fn test_thread_push_rule_is_triggered_for_subscribed_threads() {
|
238 | 242 | let actions = push_context.for_event(&event).await;
|
239 | 243 | assert!(actions.is_empty());
|
240 | 244 | }
|
| 245 | + |
| 246 | +#[async_test] |
| 247 | +async fn test_thread_push_rules_and_notification_modes() { |
| 248 | + // This test checks that, given a combination of a global notification mode, and |
| 249 | + // a room notification mode, we do get notifications for thread events according |
| 250 | + // to the subscriptions. |
| 251 | + |
| 252 | + let server = MatrixMockServer::new().await; |
| 253 | + let client = server |
| 254 | + .client_builder() |
| 255 | + .on_builder(|builder| { |
| 256 | + builder.with_threading_support(matrix_sdk::ThreadingSupport::Enabled { |
| 257 | + with_subscriptions: true, |
| 258 | + }) |
| 259 | + }) |
| 260 | + .build() |
| 261 | + .await; |
| 262 | + |
| 263 | + let room_id = room_id!("!test:example.org"); |
| 264 | + let f = EventFactory::new().room(room_id).sender(*ALICE); |
| 265 | + // Make it so that the client has a member event for the current user. |
| 266 | + let room = server |
| 267 | + .sync_room( |
| 268 | + &client, |
| 269 | + JoinedRoomBuilder::new(room_id).add_state_event(f.member(client.user_id().unwrap())), |
| 270 | + ) |
| 271 | + .await; |
| 272 | + |
| 273 | + // Sanity check: we can get a push context. |
| 274 | + room.push_context() |
| 275 | + .await |
| 276 | + .expect("getting a push context works") |
| 277 | + .expect("the push context should exist"); |
| 278 | + |
| 279 | + // Mock push rules endpoints, allowing any modification to any rule. |
| 280 | + server.mock_set_push_rules_actions(RuleKind::Underride, PushRuleIdSpec::Any).ok().mount().await; |
| 281 | + server.mock_set_push_rules_actions(RuleKind::Room, PushRuleIdSpec::Any).ok().mount().await; |
| 282 | + server.mock_set_push_rules(RuleKind::Underride, PushRuleIdSpec::Any).ok().mount().await; |
| 283 | + server.mock_set_push_rules(RuleKind::Room, PushRuleIdSpec::Any).ok().mount().await; |
| 284 | + server.mock_set_push_rules(RuleKind::Override, PushRuleIdSpec::Any).ok().mount().await; |
| 285 | + server.mock_delete_push_rules(RuleKind::Room, PushRuleIdSpec::Any).ok().mount().await; |
| 286 | + server.mock_delete_push_rules(RuleKind::Override, PushRuleIdSpec::Any).ok().mount().await; |
| 287 | + |
| 288 | + // Given a thread I'm subscribed to, |
| 289 | + let thread_root_id = owned_event_id!("$root"); |
| 290 | + |
| 291 | + server |
| 292 | + .mock_get_thread_subscription() |
| 293 | + .match_room_id(room_id.to_owned()) |
| 294 | + .match_thread_id(thread_root_id.clone()) |
| 295 | + .ok(true) |
| 296 | + .mount() |
| 297 | + .await; |
| 298 | + |
| 299 | + // Given an event in the thread I'm subscribed to, |
| 300 | + let event = |
| 301 | + f.text_msg("hello to you too!").in_thread(&thread_root_id, &thread_root_id).into_raw_sync(); |
| 302 | + |
| 303 | + // If global mode = AllMessages, |
| 304 | + let settings = client.notification_settings().await; |
| 305 | + |
| 306 | + let is_encrypted = false; |
| 307 | + let is_one_to_one = false; |
| 308 | + settings |
| 309 | + .set_default_room_notification_mode( |
| 310 | + is_encrypted.into(), |
| 311 | + is_one_to_one.into(), |
| 312 | + RoomNotificationMode::AllMessages, |
| 313 | + ) |
| 314 | + .await |
| 315 | + .unwrap(); |
| 316 | + |
| 317 | + // If room mode = AllMessages, |
| 318 | + settings.set_room_notification_mode(room_id, RoomNotificationMode::AllMessages).await.unwrap(); |
| 319 | + // Ack the push rules change via sync, for it to be applied in the push context. |
| 320 | + let ruleset = settings.ruleset().await; |
| 321 | + server |
| 322 | + .mock_sync() |
| 323 | + .ok_and_run(&client, |builder| { |
| 324 | + builder.add_global_account_data(f.push_rules(ruleset)); |
| 325 | + }) |
| 326 | + .await; |
| 327 | + |
| 328 | + // The thread event will trigger a notification. |
| 329 | + let actions = room.push_context().await.unwrap().unwrap().traced_for_event(&event).await; |
| 330 | + assert!(actions.iter().any(|action| action.should_notify())); |
| 331 | + |
| 332 | + // If room mode = mentions and keywords only, |
| 333 | + settings |
| 334 | + .set_room_notification_mode(room_id, RoomNotificationMode::MentionsAndKeywordsOnly) |
| 335 | + .await |
| 336 | + .unwrap(); |
| 337 | + let ruleset = settings.ruleset().await; |
| 338 | + server |
| 339 | + .mock_sync() |
| 340 | + .ok_and_run(&client, |builder| { |
| 341 | + builder.add_global_account_data(f.push_rules(ruleset)); |
| 342 | + }) |
| 343 | + .await; |
| 344 | + |
| 345 | + // The thread event will trigger a notification. |
| 346 | + let actions = room.push_context().await.unwrap().unwrap().traced_for_event(&event).await; |
| 347 | + // TODO: unexpected! this should trigger a thread mention |
| 348 | + //assert!(actions.iter().any(|action| action.should_notify())); |
| 349 | + assert!(!actions.iter().any(|action| action.should_notify())); |
| 350 | + |
| 351 | + // If room mode = mute, |
| 352 | + settings.set_room_notification_mode(room_id, RoomNotificationMode::Mute).await.unwrap(); |
| 353 | + let ruleset = settings.ruleset().await; |
| 354 | + server |
| 355 | + .mock_sync() |
| 356 | + .ok_and_run(&client, |builder| { |
| 357 | + builder.add_global_account_data(f.push_rules(ruleset)); |
| 358 | + }) |
| 359 | + .await; |
| 360 | + |
| 361 | + // The thread event will trigger a notification. |
| 362 | + let actions = room.push_context().await.unwrap().unwrap().traced_for_event(&event).await; |
| 363 | + // TODO: unexpected! this should trigger a thread mention |
| 364 | + //assert!(actions.iter().any(|action| action.should_notify())); |
| 365 | + assert!(!actions.iter().any(|action| action.should_notify())); |
| 366 | + |
| 367 | + // Now, if global mode = mentions only, |
| 368 | + settings |
| 369 | + .set_default_room_notification_mode( |
| 370 | + is_encrypted.into(), |
| 371 | + is_one_to_one.into(), |
| 372 | + RoomNotificationMode::MentionsAndKeywordsOnly, |
| 373 | + ) |
| 374 | + .await |
| 375 | + .unwrap(); |
| 376 | + |
| 377 | + // If room mode = AllMessages, |
| 378 | + settings.set_room_notification_mode(room_id, RoomNotificationMode::AllMessages).await.unwrap(); |
| 379 | + // Ack the push rules change via sync, for it to be applied in the push context. |
| 380 | + let ruleset = settings.ruleset().await; |
| 381 | + server |
| 382 | + .mock_sync() |
| 383 | + .ok_and_run(&client, |builder| { |
| 384 | + builder.add_global_account_data(f.push_rules(ruleset)); |
| 385 | + }) |
| 386 | + .await; |
| 387 | + |
| 388 | + // The thread event will trigger a notification. |
| 389 | + let actions = room.push_context().await.unwrap().unwrap().traced_for_event(&event).await; |
| 390 | + assert!(actions.iter().any(|action| action.should_notify())); |
| 391 | + |
| 392 | + // If room mode = Mentions only, |
| 393 | + settings |
| 394 | + .set_room_notification_mode(room_id, RoomNotificationMode::MentionsAndKeywordsOnly) |
| 395 | + .await |
| 396 | + .unwrap(); |
| 397 | + // Ack the push rules change via sync, for it to be applied in the push context. |
| 398 | + let ruleset = settings.ruleset().await; |
| 399 | + server |
| 400 | + .mock_sync() |
| 401 | + .ok_and_run(&client, |builder| { |
| 402 | + builder.add_global_account_data(f.push_rules(ruleset)); |
| 403 | + }) |
| 404 | + .await; |
| 405 | + |
| 406 | + // The thread event will trigger a notification. |
| 407 | + let actions = room.push_context().await.unwrap().unwrap().traced_for_event(&event).await; |
| 408 | + // TODO: unexpected! this should trigger a thread mention |
| 409 | + //assert!(actions.iter().any(|action| action.should_notify())); |
| 410 | + assert!(!actions.iter().any(|action| action.should_notify())); |
| 411 | + |
| 412 | + // If room mode = mute, |
| 413 | + settings.set_room_notification_mode(room_id, RoomNotificationMode::Mute).await.unwrap(); |
| 414 | + // Ack the push rules change via sync, for it to be applied in the push context. |
| 415 | + let ruleset = settings.ruleset().await; |
| 416 | + server |
| 417 | + .mock_sync() |
| 418 | + .ok_and_run(&client, |builder| { |
| 419 | + builder.add_global_account_data(f.push_rules(ruleset)); |
| 420 | + }) |
| 421 | + .await; |
| 422 | + |
| 423 | + // The thread event will trigger a notification. |
| 424 | + let actions = room.push_context().await.unwrap().unwrap().traced_for_event(&event).await; |
| 425 | + // TODO: unexpected! this should trigger a thread mention |
| 426 | + //assert!(actions.iter().any(|action| action.should_notify())); |
| 427 | + assert!(!actions.iter().any(|action| action.should_notify())); |
| 428 | +} |
0 commit comments