Skip to content

Commit

Permalink
feat(notification client): always retry decryption if it failed before
Browse files Browse the repository at this point in the history
Now that we can decrypt on both of {single|multi} process setups (i.e. available for both android
and ios), we can enable retrying decrypting notifications by default.
  • Loading branch information
bnjbvr committed Sep 5, 2023
1 parent f13adb2 commit 11b3be1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
8 changes: 0 additions & 8 deletions bindings/matrix-sdk-ffi/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ impl NotificationClientBuilder {
Arc::new(Self { builder })
}

/// Automatically retry decryption once, if the notification was received
/// encrypted.
pub fn retry_decryption(self: Arc<Self>) -> Arc<Self> {
let this = unwrap_or_clone_arc(self);
let builder = this.builder.retry_decryption();
Arc::new(Self { builder })
}

pub fn finish(self: Arc<Self>) -> Arc<NotificationClient> {
let this = unwrap_or_clone_arc(self);
Arc::new(NotificationClient { inner: this.builder.build() })
Expand Down
38 changes: 7 additions & 31 deletions crates/matrix-sdk-ui/src/notification_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ pub struct NotificationClient {
/// SDK client that uses the same state store as the caller's context.
parent_client: Client,

/// Should we retry decrypting an event, after it was impossible to decrypt
/// on the first attempt?
retry_decryption: bool,

/// Is the notification client running on its own process or not?
process_setup: NotificationProcessSetup,

Expand Down Expand Up @@ -140,18 +136,15 @@ impl NotificationClient {
/// This is *not* reentrant.
///
/// Will return true if and only:
/// - retry_decryption was enabled,
/// - the event was encrypted,
/// - we successfully ran an encryption sync.
async fn maybe_retry_decryption(
/// - we successfully ran an encryption sync or waited long enough for an
/// existing encryption sync to
/// decrypt the event.
async fn retry_decryption(
&self,
room: &Room,
raw_event: &Raw<AnySyncTimelineEvent>,
) -> Result<Option<TimelineEvent>, Error> {
if !self.retry_decryption {
return Ok(None);
}

let event: AnySyncTimelineEvent =
raw_event.deserialize().map_err(|_| Error::InvalidRumaEvent)?;

Expand Down Expand Up @@ -412,9 +405,7 @@ impl NotificationClient {
let push_actions = match &raw_event {
RawNotificationEvent::Timeline(timeline_event) => {
// Timeline events may be encrypted, so make sure they get decrypted first.
if let Some(timeline_event) =
self.maybe_retry_decryption(&room, timeline_event).await?
{
if let Some(timeline_event) = self.retry_decryption(&room, timeline_event).await? {
raw_event = RawNotificationEvent::Timeline(timeline_event.event.cast());
timeline_event.push_actions
} else {
Expand Down Expand Up @@ -466,7 +457,7 @@ impl NotificationClient {
room.event_with_context(event_id, true).await?.ok_or(Error::ContextMissingEvent)?;

if let Some(decrypted_event) =
self.maybe_retry_decryption(&room, timeline_event.event.cast_ref()).await?
self.retry_decryption(&room, timeline_event.event.cast_ref()).await?
{
timeline_event = decrypted_event;
}
Expand Down Expand Up @@ -519,7 +510,6 @@ pub struct NotificationClientBuilder {
client: Client,
/// SDK client that uses the same state store as the caller's context.
parent_client: Client,
retry_decryption: bool,
filter_by_push_rules: bool,

/// Is the notification client running on its own process or not?
Expand All @@ -533,13 +523,7 @@ impl NotificationClientBuilder {
) -> Result<Self, Error> {
let client = parent_client.notification_client().await?;

Ok(Self {
client,
parent_client,
retry_decryption: false,
filter_by_push_rules: false,
process_setup,
})
Ok(Self { client, parent_client, filter_by_push_rules: false, process_setup })
}

/// Filter out the notification event according to the push rules present in
Expand All @@ -549,20 +533,12 @@ impl NotificationClientBuilder {
self
}

/// Automatically retry decryption once, if the notification was received
/// encrypted.
pub fn retry_decryption(mut self) -> Self {
self.retry_decryption = true;
self
}

/// Finishes configuring the `NotificationClient`.
pub fn build(self) -> NotificationClient {
NotificationClient {
client: self.client,
parent_client: self.parent_client,
filter_by_push_rules: self.filter_by_push_rules,
retry_decryption: self.retry_decryption,
sliding_sync_mutex: AsyncMutex::new(()),
process_setup: self.process_setup,
}
Expand Down

0 comments on commit 11b3be1

Please sign in to comment.