matrix_sdk_crypto/types/requests/
enums.rs

1// Copyright 2020 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 std::sync::Arc;
16
17use ruma::{
18    api::client::{
19        backup::add_backup_keys::v3::Response as KeysBackupResponse,
20        keys::{
21            claim_keys::v3::{Request as KeysClaimRequest, Response as KeysClaimResponse},
22            get_keys::v3::Response as KeysQueryResponse,
23            upload_keys::v3::{Request as KeysUploadRequest, Response as KeysUploadResponse},
24            upload_signatures::v3::{
25                Request as SignatureUploadRequest, Response as SignatureUploadResponse,
26            },
27            upload_signing_keys::v3::Response as SigningKeysUploadResponse,
28        },
29        message::send_message_event::v3::Response as RoomMessageResponse,
30        to_device::send_event_to_device::v3::Response as ToDeviceResponse,
31    },
32    TransactionId,
33};
34
35use super::{
36    KeysQueryRequest, OutgoingRequest, OutgoingVerificationRequest, RoomMessageRequest,
37    ToDeviceRequest,
38};
39
40/// Enum over the different outgoing requests we can have.
41#[derive(Debug)]
42pub enum AnyOutgoingRequest {
43    /// The `/keys/upload` request, uploading device and one-time keys.
44    KeysUpload(KeysUploadRequest),
45    /// The `/keys/query` request, fetching the device and cross signing keys of
46    /// other users.
47    KeysQuery(KeysQueryRequest),
48    /// The request to claim one-time keys for a user/device pair from the
49    /// server, after the response is received an 1-to-1 Olm session will be
50    /// established with the user/device pair.
51    KeysClaim(KeysClaimRequest),
52    /// The to-device requests, this request is used for a couple of different
53    /// things, the main use is key requests/forwards and interactive device
54    /// verification.
55    ToDeviceRequest(ToDeviceRequest),
56    /// Signature upload request, this request is used after a successful device
57    /// or user verification is done.
58    SignatureUpload(SignatureUploadRequest),
59    /// A room message request, usually for sending in-room interactive
60    /// verification events.
61    // `Box` the `RoomMessageRequest` to reduce the size of the enum.
62    RoomMessage(Box<RoomMessageRequest>),
63}
64
65#[cfg(test)]
66impl AnyOutgoingRequest {
67    /// Test helper to destructure the [`OutgoingRequests`] as a
68    /// [`ToDeviceRequest`].
69    pub fn to_device(&self) -> Option<&ToDeviceRequest> {
70        as_variant::as_variant!(self, Self::ToDeviceRequest)
71    }
72}
73
74impl From<KeysQueryRequest> for AnyOutgoingRequest {
75    fn from(request: KeysQueryRequest) -> Self {
76        Self::KeysQuery(request)
77    }
78}
79
80impl From<KeysClaimRequest> for AnyOutgoingRequest {
81    fn from(r: KeysClaimRequest) -> Self {
82        Self::KeysClaim(r)
83    }
84}
85
86impl From<KeysUploadRequest> for AnyOutgoingRequest {
87    fn from(request: KeysUploadRequest) -> Self {
88        Self::KeysUpload(request)
89    }
90}
91
92impl From<ToDeviceRequest> for AnyOutgoingRequest {
93    fn from(request: ToDeviceRequest) -> Self {
94        Self::ToDeviceRequest(request)
95    }
96}
97
98impl From<RoomMessageRequest> for AnyOutgoingRequest {
99    fn from(request: RoomMessageRequest) -> Self {
100        Self::RoomMessage(Box::new(request))
101    }
102}
103
104impl From<SignatureUploadRequest> for AnyOutgoingRequest {
105    fn from(request: SignatureUploadRequest) -> Self {
106        Self::SignatureUpload(request)
107    }
108}
109
110impl From<OutgoingVerificationRequest> for OutgoingRequest {
111    fn from(r: OutgoingVerificationRequest) -> Self {
112        Self { request_id: r.request_id().to_owned(), request: Arc::new(r.into()) }
113    }
114}
115
116impl From<SignatureUploadRequest> for OutgoingRequest {
117    fn from(r: SignatureUploadRequest) -> Self {
118        Self { request_id: TransactionId::new(), request: Arc::new(r.into()) }
119    }
120}
121
122impl From<KeysUploadRequest> for OutgoingRequest {
123    fn from(r: KeysUploadRequest) -> Self {
124        Self { request_id: TransactionId::new(), request: Arc::new(r.into()) }
125    }
126}
127
128impl From<OutgoingVerificationRequest> for AnyOutgoingRequest {
129    fn from(request: OutgoingVerificationRequest) -> Self {
130        match request {
131            OutgoingVerificationRequest::ToDevice(r) => AnyOutgoingRequest::ToDeviceRequest(r),
132            OutgoingVerificationRequest::InRoom(r) => AnyOutgoingRequest::RoomMessage(r),
133        }
134    }
135}
136
137/// Enum over all the incoming responses we need to receive.
138#[derive(Debug)]
139pub enum AnyIncomingResponse<'a> {
140    /// The `/keys/upload` response, notifying us about the amount of uploaded
141    /// one-time keys.
142    KeysUpload(&'a KeysUploadResponse),
143    /// The `/keys/query` response, giving us the device and cross signing keys
144    /// of other users.
145    KeysQuery(&'a KeysQueryResponse),
146    /// The to-device response, an empty response.
147    ToDevice(&'a ToDeviceResponse),
148    /// The key claiming requests, giving us new one-time keys of other users so
149    /// new Olm sessions can be created.
150    KeysClaim(&'a KeysClaimResponse),
151    /// The cross signing `/keys/upload` response, marking our private cross
152    /// signing identity as shared.
153    SigningKeysUpload(&'a SigningKeysUploadResponse),
154    /// The cross signing signature upload response.
155    SignatureUpload(&'a SignatureUploadResponse),
156    /// A room message response, usually for interactive verifications.
157    RoomMessage(&'a RoomMessageResponse),
158    /// Response for the server-side room key backup request.
159    KeysBackup(&'a KeysBackupResponse),
160}
161
162impl<'a> From<&'a KeysUploadResponse> for AnyIncomingResponse<'a> {
163    fn from(response: &'a KeysUploadResponse) -> Self {
164        AnyIncomingResponse::KeysUpload(response)
165    }
166}
167
168impl<'a> From<&'a KeysBackupResponse> for AnyIncomingResponse<'a> {
169    fn from(response: &'a KeysBackupResponse) -> Self {
170        AnyIncomingResponse::KeysBackup(response)
171    }
172}
173
174impl<'a> From<&'a KeysQueryResponse> for AnyIncomingResponse<'a> {
175    fn from(response: &'a KeysQueryResponse) -> Self {
176        AnyIncomingResponse::KeysQuery(response)
177    }
178}
179
180impl<'a> From<&'a ToDeviceResponse> for AnyIncomingResponse<'a> {
181    fn from(response: &'a ToDeviceResponse) -> Self {
182        AnyIncomingResponse::ToDevice(response)
183    }
184}
185
186impl<'a> From<&'a RoomMessageResponse> for AnyIncomingResponse<'a> {
187    fn from(response: &'a RoomMessageResponse) -> Self {
188        AnyIncomingResponse::RoomMessage(response)
189    }
190}
191
192impl<'a> From<&'a KeysClaimResponse> for AnyIncomingResponse<'a> {
193    fn from(response: &'a KeysClaimResponse) -> Self {
194        AnyIncomingResponse::KeysClaim(response)
195    }
196}
197
198impl<'a> From<&'a SignatureUploadResponse> for AnyIncomingResponse<'a> {
199    fn from(response: &'a SignatureUploadResponse) -> Self {
200        AnyIncomingResponse::SignatureUpload(response)
201    }
202}