Skip to content

Commit 55342a8

Browse files
committed
refactor(sdk): explicit the case where we can't compute the push actions
1 parent 9950268 commit 55342a8

File tree

6 files changed

+36
-43
lines changed

6 files changed

+36
-43
lines changed

crates/matrix-sdk-ui/src/notification_client.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ impl NotificationClient {
218218

219219
sleep(Duration::from_millis(wait)).await;
220220

221-
let new_event =
222-
room.decrypt_event(raw_event.cast_ref(), &push_action_ctx).await?;
221+
let new_event = room
222+
.decrypt_event(raw_event.cast_ref(), push_action_ctx.as_ref())
223+
.await?;
223224

224225
match new_event.kind {
225226
matrix_sdk::deserialized_responses::TimelineEventKind::UnableToDecrypt {
@@ -260,7 +261,7 @@ impl NotificationClient {
260261

261262
match encryption_sync {
262263
Ok(sync) => match sync.run_fixed_iterations(2, sync_permit_guard).await {
263-
Ok(()) => match room.decrypt_event(raw_event.cast_ref(), &push_action_ctx).await {
264+
Ok(()) => match room.decrypt_event(raw_event.cast_ref(), push_action_ctx.as_ref()).await {
264265
Ok(new_event) => match new_event.kind {
265266
matrix_sdk::deserialized_responses::TimelineEventKind::UnableToDecrypt {
266267
utd_info, ..
@@ -516,12 +517,12 @@ impl NotificationClient {
516517
raw_event = RawNotificationEvent::Timeline(timeline_event.into_raw());
517518
push_actions
518519
} else {
519-
room.push_context().await?.for_event(timeline_event)
520+
room.event_push_actions(timeline_event).await?
520521
}
521522
}
522523
RawNotificationEvent::Invite(invite_event) => {
523524
// Invite events can't be encrypted, so they should be in clear text.
524-
room.push_context().await?.for_event(invite_event)
525+
room.event_push_actions(invite_event).await?
525526
}
526527
};
527528

crates/matrix-sdk-ui/src/timeline/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ pub(super) trait Decryptor: AsyncTraitDeps + Clone + 'static {
306306
impl Decryptor for Room {
307307
async fn decrypt_event_impl(&self, raw: &Raw<AnySyncTimelineEvent>) -> Result<TimelineEvent> {
308308
let push_action_ctx = self.push_context().await?;
309-
self.decrypt_event(raw.cast_ref(), &push_action_ctx).await
309+
self.decrypt_event(raw.cast_ref(), push_action_ctx.as_ref()).await
310310
}
311311
}
312312

crates/matrix-sdk/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ All notable changes to this project will be documented in this file.
1212

1313
### Refactor
1414

15-
- The `Room::push_context()` has been renamed into `Room::push_condition_room_ctx()`. The newer
15+
- `Room::push_context()` has been renamed into `Room::push_condition_room_ctx()`. The newer
1616
`Room::push_context` now returns a `matrix_sdk::Room::PushContext`, which can be used to compute
1717
the push actions for any event.
18+
([#4962](https://github.com/matrix-org/matrix-rust-sdk/pull/4962))
19+
- `Room::decrypt_event()` now requires an extra `matrix_sdk::Room::PushContext` parameter to
20+
compute the push notifications for the decrypted event.
21+
([#4962](https://github.com/matrix-org/matrix-rust-sdk/pull/4962))
1822

1923
## [0.11.0] - 2025-04-11
2024

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,7 @@ const TYPING_NOTICE_RESEND_TIMEOUT: Duration = Duration::from_secs(3);
196196
#[derive(Debug)]
197197
pub struct PushContext {
198198
/// The Ruma context used to compute the push actions.
199-
///
200-
/// May be missing if some state events were missing for the current room
201-
/// (e.g. the member information for the logged-in user was missing).
202-
push_condition_room_ctx: Option<PushConditionRoomCtx>,
199+
push_condition_room_ctx: PushConditionRoomCtx,
203200

204201
/// Push rules for this room, based on the push rules state event, or the
205202
/// global server default as defined by [`Ruleset::server_default`].
@@ -208,11 +205,8 @@ pub struct PushContext {
208205

209206
impl PushContext {
210207
/// Compute the push rules for a given event.
211-
///
212-
/// Will return `None` if and only if the underlying
213-
/// [`PushConditionRoomCtx`] is missing.
214-
pub fn for_event<T>(&self, event: &Raw<T>) -> Option<Vec<Action>> {
215-
Some(self.push_rules.get_actions(event, self.push_condition_room_ctx.as_ref()?).to_owned())
208+
pub fn for_event<T>(&self, event: &Raw<T>) -> Vec<Action> {
209+
self.push_rules.get_actions(event, &self.push_condition_room_ctx).to_owned()
216210
}
217211
}
218212

@@ -360,7 +354,10 @@ impl Room {
360354

361355
let push_action_ctx = self.push_context().await?;
362356
let chunk = join_all(
363-
http_response.chunk.into_iter().map(|ev| self.try_decrypt_event(ev, &push_action_ctx)),
357+
http_response
358+
.chunk
359+
.into_iter()
360+
.map(|ev| self.try_decrypt_event(ev, push_action_ctx.as_ref())),
364361
)
365362
.await;
366363

@@ -471,7 +468,7 @@ impl Room {
471468
async fn try_decrypt_event(
472469
&self,
473470
event: Raw<AnyTimelineEvent>,
474-
push_ctx: &PushContext,
471+
push_ctx: Option<&PushContext>,
475472
) -> TimelineEvent {
476473
#[cfg(feature = "e2e-encryption")]
477474
if let Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomEncrypted(
@@ -484,7 +481,7 @@ impl Room {
484481
}
485482

486483
let mut event = TimelineEvent::new(event.cast());
487-
event.push_actions = push_ctx.for_event(event.raw());
484+
event.push_actions = push_ctx.map(|ctx| ctx.for_event(event.raw()));
488485

489486
event
490487
}
@@ -503,7 +500,7 @@ impl Room {
503500

504501
let raw_event = self.client.send(request).with_request_config(request_config).await?.event;
505502
let push_action_ctx = self.push_context().await?;
506-
let event = self.try_decrypt_event(raw_event, &push_action_ctx).await;
503+
let event = self.try_decrypt_event(raw_event, push_action_ctx.as_ref()).await;
507504

508505
// Save the event into the event cache, if it's set up.
509506
if let Ok((cache, _handles)) = self.event_cache().await {
@@ -560,8 +557,9 @@ impl Room {
560557
let response = self.client.send(request).with_request_config(request_config).await?;
561558

562559
let push_action_ctx = self.push_context().await?;
560+
let push_action_ctx = push_action_ctx.as_ref();
563561
let target_event = if let Some(event) = response.event {
564-
Some(self.try_decrypt_event(event, &push_action_ctx).await)
562+
Some(self.try_decrypt_event(event, push_action_ctx).await)
565563
} else {
566564
None
567565
};
@@ -574,13 +572,13 @@ impl Room {
574572
response
575573
.events_before
576574
.into_iter()
577-
.map(|ev| self.try_decrypt_event(ev, &push_action_ctx)),
575+
.map(|ev| self.try_decrypt_event(ev, push_action_ctx)),
578576
),
579577
join_all(
580578
response
581579
.events_after
582580
.into_iter()
583-
.map(|ev| self.try_decrypt_event(ev, &push_action_ctx)),
581+
.map(|ev| self.try_decrypt_event(ev, push_action_ctx)),
584582
),
585583
);
586584

@@ -1369,7 +1367,7 @@ impl Room {
13691367
pub async fn decrypt_event(
13701368
&self,
13711369
event: &Raw<OriginalSyncRoomEncryptedEvent>,
1372-
push_ctx: &PushContext,
1370+
push_ctx: Option<&PushContext>,
13731371
) -> Result<TimelineEvent> {
13741372
let machine = self.client.olm_machine().await;
13751373
let machine = machine.as_ref().ok_or(Error::NoOlmMachine)?;
@@ -1391,7 +1389,7 @@ impl Room {
13911389
}
13921390
};
13931391

1394-
event.push_actions = push_ctx.for_event(event.raw());
1392+
event.push_actions = push_ctx.map(|ctx| ctx.for_event(event.raw()));
13951393
Ok(event)
13961394
}
13971395

@@ -2952,31 +2950,21 @@ impl Room {
29522950

29532951
/// Retrieves a [`PushContext`] that can be used to compute the push
29542952
/// actions for events.
2955-
pub async fn push_context(&self) -> Result<PushContext> {
2956-
let push_condition_room_ctx = self.push_condition_room_ctx().await?;
2957-
if push_condition_room_ctx.is_none() {
2953+
pub async fn push_context(&self) -> Result<Option<PushContext>> {
2954+
let Some(push_condition_room_ctx) = self.push_condition_room_ctx().await? else {
29582955
debug!("Could not aggregate push context");
2959-
}
2956+
return Ok(None);
2957+
};
29602958
let push_rules = self.client().account().push_rules().await?;
2961-
Ok(PushContext { push_condition_room_ctx, push_rules })
2959+
Ok(Some(PushContext { push_condition_room_ctx, push_rules }))
29622960
}
29632961

29642962
/// Get the push actions for the given event with the current room state.
29652963
///
29662964
/// Note that it is possible that no push action is returned because the
29672965
/// current room state does not have all the required state events.
2968-
#[deprecated(
2969-
note = "Use `Room::push_context` to retrieve a `PushContext` instead, and call `PushContext::for_event` on your event."
2970-
)]
29712966
pub async fn event_push_actions<T>(&self, event: &Raw<T>) -> Result<Option<Vec<Action>>> {
2972-
let Some(push_context) = self.push_condition_room_ctx().await? else {
2973-
debug!("Could not aggregate push context");
2974-
return Ok(None);
2975-
};
2976-
2977-
let push_rules = self.client().account().push_rules().await?;
2978-
2979-
Ok(Some(push_rules.get_actions(event, &push_context).to_owned()))
2967+
Ok(self.push_context().await?.map(|ctx| ctx.for_event(event)))
29802968
}
29812969

29822970
/// The membership details of the (latest) invite for the logged-in user in

crates/matrix-sdk/tests/integration/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ async fn test_encrypt_room_event() {
773773
let push_action_ctx =
774774
room.push_context().await.expect("We should be able to get the push action context");
775775
let timeline_event = room
776-
.decrypt_event(&event, &push_action_ctx)
776+
.decrypt_event(&event, push_action_ctx.as_ref())
777777
.await
778778
.expect("We should be able to decrypt an event that we ourselves have encrypted");
779779

testing/matrix-sdk-integration-testing/src/tests/nse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ async fn decrypt_event(
426426
) -> Option<(OwnedEventId, String)> {
427427
let room = client.get_room(room_id).unwrap();
428428
let push_action_ctx = room.push_context().await.unwrap();
429-
let Ok(decrypted) = room.decrypt_event(event, &push_action_ctx).await else {
429+
let Ok(decrypted) = room.decrypt_event(event, push_action_ctx.as_ref()).await else {
430430
return None;
431431
};
432432

0 commit comments

Comments
 (0)