From 085de8bdae4a9f22c7fbbf00426dc6a6ec173baf Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 11 Apr 2025 15:49:27 +0200 Subject: [PATCH] refactor(base): Create the `ephemeral_events` response processor. This patch creates the `ephemeral_events` response processors: `dispatch` and `dispatch_one`. This remove duplicated code. --- crates/matrix-sdk-base/src/client.rs | 23 +++------ .../response_processors/ephemeral_events.rs | 50 +++++++++++++++++++ .../src/response_processors/mod.rs | 1 + crates/matrix-sdk-base/src/sliding_sync.rs | 18 ++----- 4 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 crates/matrix-sdk-base/src/response_processors/ephemeral_events.rs diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index e23d70b2b..83b47d5af 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -38,7 +38,7 @@ use ruma::{ events::{ push_rules::{PushRulesEvent, PushRulesEventContent}, room::member::SyncRoomMemberEvent, - AnySyncEphemeralRoomEvent, StateEvent, StateEventType, + StateEvent, StateEventType, }, push::Ruleset, time::Instant, @@ -564,22 +564,11 @@ impl BaseClient { ) .await?; - for raw in &new_info.ephemeral.events { - match raw.deserialize() { - Ok(AnySyncEphemeralRoomEvent::Receipt(event)) => { - context.state_changes.add_receipts(&room_id, event.content); - } - Ok(_) => {} - Err(e) => { - let event_id: Option = raw.get_field("event_id").ok().flatten(); - #[rustfmt::skip] - info!( - ?room_id, event_id, - "Failed to deserialize ephemeral room event: {e}" - ); - } - } - } + processors::ephemeral_events::dispatch( + &mut context, + &new_info.ephemeral.events, + &room_id, + ); if new_info.timeline.limited { room_info.mark_members_missing(); diff --git a/crates/matrix-sdk-base/src/response_processors/ephemeral_events.rs b/crates/matrix-sdk-base/src/response_processors/ephemeral_events.rs new file mode 100644 index 000000000..9b8bdea5a --- /dev/null +++ b/crates/matrix-sdk-base/src/response_processors/ephemeral_events.rs @@ -0,0 +1,50 @@ +// Copyright 2025 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use ruma::{events::AnySyncEphemeralRoomEvent, serde::Raw, RoomId}; +use tracing::info; + +use super::Context; + +/// Dispatch [`AnySyncEphemeralRoomEvent`]s on the [`Context`]. +pub fn dispatch( + context: &mut Context, + raw_events: &[Raw], + room_id: &RoomId, +) { + for raw_event in raw_events { + dispatch_one(context, raw_event, room_id); + } +} + +/// Dispatch a single [`AnySyncEphemeralRoomEvent`] on the [`Context`]. +pub fn dispatch_one( + context: &mut Context, + raw_event: &Raw, + room_id: &RoomId, +) { + match raw_event.deserialize() { + Ok(AnySyncEphemeralRoomEvent::Receipt(event)) => { + context.state_changes.add_receipts(room_id, event.content); + } + + Ok(_) => {} + + Err(e) => { + let event_id = raw_event.get_field::("event_id").ok().flatten(); + + info!(?room_id, event_id, "Failed to deserialize ephemeral room event: {e}"); + } + } +} diff --git a/crates/matrix-sdk-base/src/response_processors/mod.rs b/crates/matrix-sdk-base/src/response_processors/mod.rs index 34af1cfeb..92249739d 100644 --- a/crates/matrix-sdk-base/src/response_processors/mod.rs +++ b/crates/matrix-sdk-base/src/response_processors/mod.rs @@ -16,6 +16,7 @@ pub mod account_data; pub mod changes; #[cfg(feature = "e2e-encryption")] pub mod e2ee; +pub mod ephemeral_events; #[cfg(feature = "e2e-encryption")] pub mod latest_event; pub mod profiles; diff --git a/crates/matrix-sdk-base/src/sliding_sync.rs b/crates/matrix-sdk-base/src/sliding_sync.rs index 1073b8555..dddec6358 100644 --- a/crates/matrix-sdk-base/src/sliding_sync.rs +++ b/crates/matrix-sdk-base/src/sliding_sync.rs @@ -32,7 +32,9 @@ use ruma::{ serde::Raw, JsOption, OwnedRoomId, RoomId, UserId, }; -use tracing::{instrument, trace, warn}; +#[cfg(feature = "e2e-encryption")] +use tracing::warn; +use tracing::{instrument, trace}; use super::BaseClient; #[cfg(feature = "e2e-encryption")] @@ -213,19 +215,7 @@ impl BaseClient { // so they may exist without any update for the associated room. for (room_id, raw) in &extensions.receipts.rooms { - match raw.deserialize() { - Ok(event) => { - context.state_changes.add_receipts(room_id, event.content); - } - Err(e) => { - let event_id: Option = raw.get_field("event_id").ok().flatten(); - #[rustfmt::skip] - warn!( - ?room_id, event_id, - "Failed to deserialize read receipt room event: {e}" - ); - } - } + processors::ephemeral_events::dispatch_one(&mut context, raw.cast_ref(), room_id); // We assume this can only happen in joined rooms, or something's very wrong. new_rooms