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    RoomMessage(RoomMessageRequest),
62}
63
64#[cfg(test)]
65impl AnyOutgoingRequest {
66    /// Test helper to destructure the [`OutgoingRequests`] as a
67    /// [`ToDeviceRequest`].
68    pub fn to_device(&self) -> Option<&ToDeviceRequest> {
69        as_variant::as_variant!(self, Self::ToDeviceRequest)
70    }
71}
72
73impl From<KeysQueryRequest> for AnyOutgoingRequest {
74    fn from(request: KeysQueryRequest) -> Self {
75        Self::KeysQuery(request)
76    }
77}
78
79impl From<KeysClaimRequest> for AnyOutgoingRequest {
80    fn from(r: KeysClaimRequest) -> Self {
81        Self::KeysClaim(r)
82    }
83}
84
85impl From<KeysUploadRequest> for AnyOutgoingRequest {
86    fn from(request: KeysUploadRequest) -> Self {
87        Self::KeysUpload(request)
88    }
89}
90
91impl From<ToDeviceRequest> for AnyOutgoingRequest {
92    fn from(request: ToDeviceRequest) -> Self {
93        Self::ToDeviceRequest(request)
94    }
95}
96
97impl From<RoomMessageRequest> for AnyOutgoingRequest {
98    fn from(request: RoomMessageRequest) -> Self {
99        Self::RoomMessage(request)
100    }
101}
102
103impl From<SignatureUploadRequest> for AnyOutgoingRequest {
104    fn from(request: SignatureUploadRequest) -> Self {
105        Self::SignatureUpload(request)
106    }
107}
108
109impl From<OutgoingVerificationRequest> for OutgoingRequest {
110    fn from(r: OutgoingVerificationRequest) -> Self {
111        Self { request_id: r.request_id().to_owned(), request: Arc::new(r.into()) }
112    }
113}
114
115impl From<SignatureUploadRequest> for OutgoingRequest {
116    fn from(r: SignatureUploadRequest) -> Self {
117        Self { request_id: TransactionId::new(), request: Arc::new(r.into()) }
118    }
119}
120
121impl From<KeysUploadRequest> for OutgoingRequest {
122    fn from(r: KeysUploadRequest) -> Self {
123        Self { request_id: TransactionId::new(), request: Arc::new(r.into()) }
124    }
125}
126
127impl From<OutgoingVerificationRequest> for AnyOutgoingRequest {
128    fn from(request: OutgoingVerificationRequest) -> Self {
129        match request {
130            OutgoingVerificationRequest::ToDevice(r) => AnyOutgoingRequest::ToDeviceRequest(r),
131            OutgoingVerificationRequest::InRoom(r) => AnyOutgoingRequest::RoomMessage(r),
132        }
133    }
134}
135
136/// Enum over all the incoming responses we need to receive.
137#[derive(Debug)]
138pub enum AnyIncomingResponse<'a> {
139    /// The `/keys/upload` response, notifying us about the amount of uploaded
140    /// one-time keys.
141    KeysUpload(&'a KeysUploadResponse),
142    /// The `/keys/query` response, giving us the device and cross signing keys
143    /// of other users.
144    KeysQuery(&'a KeysQueryResponse),
145    /// The to-device response, an empty response.
146    ToDevice(&'a ToDeviceResponse),
147    /// The key claiming requests, giving us new one-time keys of other users so
148    /// new Olm sessions can be created.
149    KeysClaim(&'a KeysClaimResponse),
150    /// The cross signing `/keys/upload` response, marking our private cross
151    /// signing identity as shared.
152    SigningKeysUpload(&'a SigningKeysUploadResponse),
153    /// The cross signing signature upload response.
154    SignatureUpload(&'a SignatureUploadResponse),
155    /// A room message response, usually for interactive verifications.
156    RoomMessage(&'a RoomMessageResponse),
157    /// Response for the server-side room key backup request.
158    KeysBackup(&'a KeysBackupResponse),
159}
160
161impl<'a> From<&'a KeysUploadResponse> for AnyIncomingResponse<'a> {
162    fn from(response: &'a KeysUploadResponse) -> Self {
163        AnyIncomingResponse::KeysUpload(response)
164    }
165}
166
167impl<'a> From<&'a KeysBackupResponse> for AnyIncomingResponse<'a> {
168    fn from(response: &'a KeysBackupResponse) -> Self {
169        AnyIncomingResponse::KeysBackup(response)
170    }
171}
172
173impl<'a> From<&'a KeysQueryResponse> for AnyIncomingResponse<'a> {
174    fn from(response: &'a KeysQueryResponse) -> Self {
175        AnyIncomingResponse::KeysQuery(response)
176    }
177}
178
179impl<'a> From<&'a ToDeviceResponse> for AnyIncomingResponse<'a> {
180    fn from(response: &'a ToDeviceResponse) -> Self {
181        AnyIncomingResponse::ToDevice(response)
182    }
183}
184
185impl<'a> From<&'a RoomMessageResponse> for AnyIncomingResponse<'a> {
186    fn from(response: &'a RoomMessageResponse) -> Self {
187        AnyIncomingResponse::RoomMessage(response)
188    }
189}
190
191impl<'a> From<&'a KeysClaimResponse> for AnyIncomingResponse<'a> {
192    fn from(response: &'a KeysClaimResponse) -> Self {
193        AnyIncomingResponse::KeysClaim(response)
194    }
195}
196
197impl<'a> From<&'a SignatureUploadResponse> for AnyIncomingResponse<'a> {
198    fn from(response: &'a SignatureUploadResponse) -> Self {
199        AnyIncomingResponse::SignatureUpload(response)
200    }
201}