matrix_sdk_base/response_processors/
verification.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::{
16    RoomId,
17    events::{
18        AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncMessageLikeEvent,
19        room::message::MessageType,
20    },
21};
22
23use super::e2ee::E2EE;
24use crate::Result;
25
26/// Process the given event as a verification event if it is a candidate. The
27/// event must be decrypted.
28pub async fn process_if_relevant(
29    event: &AnySyncTimelineEvent,
30    e2ee: E2EE<'_>,
31    room_id: &RoomId,
32) -> Result<()> {
33    if !e2ee.verification_is_allowed {
34        return Ok(());
35    }
36
37    let Some(olm) = e2ee.olm_machine else {
38        return Ok(());
39    };
40
41    let AnySyncTimelineEvent::MessageLike(event) = event else {
42        return Ok(());
43    };
44
45    match event {
46        // This is an original (i.e. non-redacted) `m.room.message` event and its
47        // content is a verification request…
48        AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(original_event))
49            if matches!(&original_event.content.msgtype, MessageType::VerificationRequest(_)) => {}
50
51        // … or this is verification request event.
52        AnySyncMessageLikeEvent::KeyVerificationReady(_)
53        | AnySyncMessageLikeEvent::KeyVerificationStart(_)
54        | AnySyncMessageLikeEvent::KeyVerificationCancel(_)
55        | AnySyncMessageLikeEvent::KeyVerificationAccept(_)
56        | AnySyncMessageLikeEvent::KeyVerificationKey(_)
57        | AnySyncMessageLikeEvent::KeyVerificationMac(_)
58        | AnySyncMessageLikeEvent::KeyVerificationDone(_) => {}
59
60        _ => {
61            // No need to handle those other event types.
62            return Ok(());
63        }
64    }
65
66    Ok(olm.receive_verification_event(&event.clone().into_full_event(room_id.to_owned())).await?)
67}