matrix_sdk_base/response_processors/ephemeral_events.rs
1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use ruma::{events::AnySyncEphemeralRoomEvent, serde::Raw, RoomId};
16use tracing::info;
17
18use super::Context;
19
20/// Dispatch [`AnySyncEphemeralRoomEvent`]s on the [`Context`].
21pub fn dispatch(
22 context: &mut Context,
23 raw_events: &[Raw<AnySyncEphemeralRoomEvent>],
24 room_id: &RoomId,
25) {
26 for raw_event in raw_events {
27 dispatch_receipt(context, raw_event, room_id);
28 }
29}
30
31/// Dispatch the [`AnySyncEphemeralRoomEvent::Receipt`] on the [`Context`].
32pub(super) fn dispatch_receipt(
33 context: &mut Context,
34 raw_event: &Raw<AnySyncEphemeralRoomEvent>,
35 room_id: &RoomId,
36) {
37 match raw_event.deserialize() {
38 Ok(AnySyncEphemeralRoomEvent::Receipt(event)) => {
39 context.state_changes.add_receipts(room_id, event.content);
40 }
41
42 Ok(_) => {}
43
44 Err(e) => {
45 let event_id = raw_event.get_field::<String>("event_id").ok().flatten();
46
47 info!(?room_id, event_id, "Failed to deserialize ephemeral room event: {e}");
48 }
49 }
50}