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.
1415use matrix_sdk_common::deserialized_responses::TimelineEvent;
16use matrix_sdk_crypto::{DecryptionSettings, RoomEventDecryptionResult};
17use ruma::{events::AnySyncTimelineEvent, serde::Raw, RoomId};
1819use super::{
20super::{verification, Context},
21 E2EE,
22};
23use crate::Result;
2425/// Attempt to decrypt the given raw event into a [`TimelineEvent`].
26///
27/// In the case of a decryption error, returns a [`TimelineEvent`]
28/// representing the decryption error; in the case of problems with our
29/// application, returns `Err`.
30///
31/// Returns `Ok(None)` if encryption is not configured.
32pub async fn sync_timeline_event(
33 context: &mut Context,
34 e2ee: E2EE<'_>,
35 event: &Raw<AnySyncTimelineEvent>,
36 room_id: &RoomId,
37) -> Result<Option<TimelineEvent>> {
38let Some(olm) = e2ee.olm_machine else { return Ok(None) };
3940let decryption_settings =
41 DecryptionSettings { sender_device_trust_requirement: e2ee.decryption_trust_requirement };
4243Ok(Some(
44match olm.try_decrypt_room_event(event.cast_ref(), room_id, &decryption_settings).await? {
45 RoomEventDecryptionResult::Decrypted(decrypted) => {
46let timeline_event = TimelineEvent::from(decrypted);
4748if let Ok(sync_timeline_event) = timeline_event.raw().deserialize() {
49 verification::process_if_relevant(context, &sync_timeline_event, e2ee, room_id)
50 .await?;
51 }
5253 timeline_event
54 }
55 RoomEventDecryptionResult::UnableToDecrypt(utd_info) => {
56 TimelineEvent::new_utd_event(event.clone(), utd_info)
57 }
58 },
59 ))
60}