Skip to content

Commit 2914d7a

Browse files
committed
feat(threads): provide has_thread_subscription_fn to push condition context
1 parent 0cdec9d commit 2914d7a

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

crates/matrix-sdk/src/room/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,15 +3022,28 @@ impl Room {
30223022
}
30233023
};
30243024

3025-
Ok(Some(assign!(
3026-
PushConditionRoomCtx::new(
3027-
room_id.to_owned(),
3028-
UInt::new(member_count).unwrap_or(UInt::MAX),
3029-
user_id.to_owned(),
3030-
user_display_name,
3031-
),
3032-
{ power_levels }
3033-
)))
3025+
let this = self.clone();
3026+
let ctx = assign!(PushConditionRoomCtx::new(
3027+
room_id.to_owned(),
3028+
UInt::new(member_count).unwrap_or(UInt::MAX),
3029+
user_id.to_owned(),
3030+
user_display_name,
3031+
),
3032+
{
3033+
power_levels,
3034+
})
3035+
.with_has_thread_subscription_fn(move |event_id: &EventId| {
3036+
let room = this.clone();
3037+
Box::pin(async move {
3038+
if let Ok(maybe_sub) = room.fetch_thread_subscription(event_id.to_owned()).await {
3039+
maybe_sub.is_some()
3040+
} else {
3041+
false
3042+
}
3043+
})
3044+
});
3045+
3046+
Ok(Some(ctx))
30343047
}
30353048

30363049
/// Retrieves a [`PushContext`] that can be used to compute the push

crates/matrix-sdk/tests/integration/room/thread.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use assert_matches2::assert_matches;
22
use matrix_sdk::{room::ThreadSubscription, test_utils::mocks::MatrixMockServer};
3-
use matrix_sdk_test::async_test;
4-
use ruma::{owned_event_id, room_id};
3+
use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder, ALICE};
4+
use ruma::{event_id, owned_event_id, room_id};
55

66
#[async_test]
77
async fn test_subscribe_thread() {
@@ -78,3 +78,64 @@ async fn test_subscribe_thread() {
7878
let subscription = room.fetch_thread_subscription(root_id).await.unwrap();
7979
assert_matches!(subscription, None);
8080
}
81+
82+
#[async_test]
83+
async fn test_thread_push_rule_is_triggered_for_subscribed_threads() {
84+
// This test checks that the evaluation of push rules for threads will correctly
85+
// call `Room::fetch_thread_subscription` for threads.
86+
87+
let server = MatrixMockServer::new().await;
88+
let client = server.client_builder().build().await;
89+
90+
let room_id = room_id!("!test:example.org");
91+
let room = server.sync_joined_room(&client, room_id).await;
92+
93+
let thread_root_id = owned_event_id!("$root");
94+
let f = EventFactory::new().room(room_id).sender(*ALICE);
95+
96+
// Make it so that the client has a member event for the current user.
97+
server
98+
.sync_room(
99+
&client,
100+
JoinedRoomBuilder::new(room_id).add_state_event(f.member(client.user_id().unwrap())),
101+
)
102+
.await;
103+
104+
// Sanity check: we can get a push context.
105+
let push_context = room
106+
.push_context()
107+
.await
108+
.expect("getting a push context works")
109+
.expect("the push context should exist");
110+
111+
// Mock the thread subscriptions endpoint so the user is subscribed to the
112+
// thread.
113+
server
114+
.mock_get_thread_subscription()
115+
.match_room_id(room_id.to_owned())
116+
.match_thread_id(thread_root_id.clone())
117+
.ok(true)
118+
.mock_once()
119+
.mount()
120+
.await;
121+
122+
// Given an event in the thread I'm subscribed to, the push rule evaluation will
123+
// trigger the thread subscription endpoint,
124+
let event =
125+
f.text_msg("hello to you too!").in_thread(&thread_root_id, &thread_root_id).into_raw_sync();
126+
127+
// And the event will trigger a notification.
128+
let actions = push_context.for_event(&event).await;
129+
assert!(actions.iter().any(|action| action.should_notify()));
130+
131+
// But for a thread that I haven't subscribed to (i.e. the endpoint returns 404,
132+
// because it's not set up), no actions are returned.
133+
let another_thread_root_id = event_id!("$another_root");
134+
let event = f
135+
.text_msg("bonjour à vous également !")
136+
.in_thread(another_thread_root_id, another_thread_root_id)
137+
.into_raw_sync();
138+
139+
let actions = push_context.for_event(&event).await;
140+
assert!(actions.is_empty());
141+
}

0 commit comments

Comments
 (0)