Skip to content

Commit b6d71a3

Browse files
committed
refactor(timeline): make use of PushContext in the RoomDataProvider trait
1 parent 4c8e2fd commit b6d71a3

File tree

6 files changed

+22
-35
lines changed

6 files changed

+22
-35
lines changed

crates/matrix-sdk-ui/src/timeline/controller/decryption_retry_task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ async fn decrypt_by_index<D: Decryptor>(
255255
should_retry: impl Fn(&str) -> bool,
256256
retry_indices: Vec<usize>,
257257
) {
258-
let push_rules_context = room_data_provider.push_rules_and_context().await;
258+
let push_context = room_data_provider.push_context().await;
259259
let unable_to_decrypt_hook = state.meta.unable_to_decrypt_hook.clone();
260260

261261
let retry_one = |item: Arc<TimelineItem>| {
@@ -324,7 +324,7 @@ async fn decrypt_by_index<D: Decryptor>(
324324
.retry_event_decryption(
325325
retry_one,
326326
retry_indices,
327-
push_rules_context,
327+
push_context,
328328
room_data_provider,
329329
settings,
330330
)

crates/matrix-sdk-ui/src/timeline/controller/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ impl TimelineController {
15001500

15011501
#[instrument(skip(self), fields(room_id = ?self.room().room_id()))]
15021502
pub(super) async fn retry_event_decryption(&self, session_ids: Option<BTreeSet<String>>) {
1503-
self.retry_event_decryption_inner(self.room().to_owned(), session_ids).await
1503+
self.retry_event_decryption_inner(self.room().clone(), session_ids).await
15041504
}
15051505
}
15061506

crates/matrix-sdk-ui/src/timeline/controller/state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use std::{future::Future, sync::Arc};
1616

1717
use eyeball_im::VectorDiff;
18-
use matrix_sdk::{deserialized_responses::TimelineEvent, send_queue::SendHandle};
18+
use matrix_sdk::{
19+
deserialized_responses::TimelineEvent, room::PushContext, send_queue::SendHandle,
20+
};
1921
#[cfg(test)]
2022
use ruma::events::receipt::ReceiptEventContent;
2123
use ruma::{
@@ -178,7 +180,7 @@ impl TimelineState {
178180
&mut self,
179181
retry_one: impl Fn(Arc<TimelineItem>) -> Fut,
180182
retry_indices: Vec<usize>,
181-
push_rules_context: Option<(ruma::push::Ruleset, ruma::push::PushConditionRoomCtx)>,
183+
push_context: Option<PushContext>,
182184
room_data_provider: &P,
183185
settings: &TimelineSettings,
184186
) where
@@ -199,9 +201,7 @@ impl TimelineState {
199201
continue;
200202
};
201203

202-
event.push_actions = push_rules_context.as_ref().map(|(push_rules, push_context)| {
203-
push_rules.get_actions(event.raw(), push_context).to_owned()
204-
});
204+
event.push_actions = push_context.as_ref().map(|ctx| ctx.for_event(event.raw()));
205205

206206
let handle_one_res = txn
207207
.handle_remote_event(

crates/matrix-sdk-ui/src/timeline/tests/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use matrix_sdk::{
3131
crypto::OlmMachine,
3232
deserialized_responses::{EncryptionInfo, TimelineEvent},
3333
event_cache::paginator::{PaginableRoom, PaginatorError},
34-
room::{EventWithContextResponse, Messages, MessagesOptions},
34+
room::{EventWithContextResponse, Messages, MessagesOptions, PushContext},
3535
send_queue::RoomSendQueueUpdate,
3636
BoxFuture,
3737
};
@@ -387,7 +387,7 @@ impl RoomDataProvider for TestRoomDataProvider {
387387
map
388388
}
389389

390-
async fn push_rules_and_context(&self) -> Option<(Ruleset, PushConditionRoomCtx)> {
390+
async fn push_context(&self) -> Option<PushContext> {
391391
let push_rules = Ruleset::server_default(&ALICE);
392392
let power_levels = PushConditionPowerLevelsCtx {
393393
users: BTreeMap::new(),
@@ -402,7 +402,7 @@ impl RoomDataProvider for TestRoomDataProvider {
402402
power_levels: Some(power_levels),
403403
};
404404

405-
Some((push_rules, push_context))
405+
Some(PushContext::new(push_context, push_rules))
406406
}
407407

408408
async fn load_fully_read_marker(&self) -> Option<OwnedEventId> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use matrix_sdk::{
2222
crypto::types::events::CryptoContextInfo,
2323
deserialized_responses::{EncryptionInfo, TimelineEvent},
2424
event_cache::paginator::PaginableRoom,
25+
room::PushContext,
2526
AsyncTraitDeps, Result, Room, SendOutsideWasm,
2627
};
2728
use matrix_sdk_base::{latest_event::LatestEvent, RoomInfo};
@@ -31,11 +32,10 @@ use ruma::{
3132
receipt::{Receipt, ReceiptThread, ReceiptType},
3233
AnyMessageLikeEventContent, AnySyncTimelineEvent,
3334
},
34-
push::{PushConditionRoomCtx, Ruleset},
3535
serde::Raw,
3636
EventId, OwnedEventId, OwnedTransactionId, OwnedUserId, RoomVersionId, UserId,
3737
};
38-
use tracing::{debug, error};
38+
use tracing::error;
3939

4040
use super::{Profile, RedactError, TimelineBuilder};
4141
use crate::timeline::{self, pinned_events_loader::PinnedEventsRoom, Timeline};
@@ -104,9 +104,7 @@ pub(super) trait RoomDataProvider:
104104
/// Load the current fully-read event id, from storage.
105105
fn load_fully_read_marker(&self) -> impl Future<Output = Option<OwnedEventId>> + '_;
106106

107-
fn push_rules_and_context(
108-
&self,
109-
) -> impl Future<Output = Option<(Ruleset, PushConditionRoomCtx)>> + SendOutsideWasm + '_;
107+
fn push_context(&self) -> impl Future<Output = Option<PushContext>> + SendOutsideWasm + '_;
110108

111109
/// Send an event to that room.
112110
fn send(
@@ -224,24 +222,8 @@ impl RoomDataProvider for Room {
224222
unthreaded_receipts
225223
}
226224

227-
async fn push_rules_and_context(&self) -> Option<(Ruleset, PushConditionRoomCtx)> {
228-
match self.push_condition_room_ctx().await {
229-
Ok(Some(push_context)) => match self.client().account().push_rules().await {
230-
Ok(push_rules) => Some((push_rules, push_context)),
231-
Err(e) => {
232-
error!("Could not get push rules: {e}");
233-
None
234-
}
235-
},
236-
Ok(None) => {
237-
debug!("Could not aggregate push context");
238-
None
239-
}
240-
Err(e) => {
241-
error!("Could not get push context: {e}");
242-
None
243-
}
244-
}
225+
async fn push_context(&self) -> Option<PushContext> {
226+
self.push_context().await.ok().flatten()
245227
}
246228

247229
async fn load_fully_read_marker(&self) -> Option<OwnedEventId> {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ pub struct PushContext {
204204
}
205205

206206
impl PushContext {
207+
/// Create a new [`PushContext`] from its inner components.
208+
pub fn new(push_condition_room_ctx: PushConditionRoomCtx, push_rules: Ruleset) -> Self {
209+
Self { push_condition_room_ctx, push_rules }
210+
}
211+
207212
/// Compute the push rules for a given event.
208213
pub fn for_event<T>(&self, event: &Raw<T>) -> Vec<Action> {
209214
self.push_rules.get_actions(event, &self.push_condition_room_ctx).to_owned()
@@ -2945,7 +2950,7 @@ impl Room {
29452950
return Ok(None);
29462951
};
29472952
let push_rules = self.client().account().push_rules().await?;
2948-
Ok(Some(PushContext { push_condition_room_ctx, push_rules }))
2953+
Ok(Some(PushContext::new(push_condition_room_ctx, push_rules)))
29492954
}
29502955

29512956
/// Get the push actions for the given event with the current room state.

0 commit comments

Comments
 (0)