refactor(base): Create the ephemeral_events response processor.

This patch creates the `ephemeral_events` response processors:
`dispatch` and `dispatch_one`.

This remove duplicated code.
This commit is contained in:
Ivan Enderlin
2025-04-11 15:49:27 +02:00
parent fe0b954019
commit 085de8bdae
4 changed files with 61 additions and 31 deletions

View File

@@ -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<String> = 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();

View File

@@ -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<AnySyncEphemeralRoomEvent>],
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<AnySyncEphemeralRoomEvent>,
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::<String>("event_id").ok().flatten();
info!(?room_id, event_id, "Failed to deserialize ephemeral room event: {e}");
}
}
}

View File

@@ -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;

View File

@@ -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<String> = 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