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.
1415use std::sync::Arc;
1617use 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};
3435use super::{
36 KeysQueryRequest, OutgoingRequest, OutgoingVerificationRequest, RoomMessageRequest,
37 ToDeviceRequest,
38};
3940/// 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.
44KeysUpload(KeysUploadRequest),
45/// The `/keys/query` request, fetching the device and cross signing keys of
46 /// other users.
47KeysQuery(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.
51KeysClaim(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.
55ToDeviceRequest(ToDeviceRequest),
56/// Signature upload request, this request is used after a successful device
57 /// or user verification is done.
58SignatureUpload(SignatureUploadRequest),
59/// A room message request, usually for sending in-room interactive
60 /// verification events.
61RoomMessage(RoomMessageRequest),
62}
6364#[cfg(test)]
65impl AnyOutgoingRequest {
66/// Test helper to destructure the [`OutgoingRequests`] as a
67 /// [`ToDeviceRequest`].
68pub fn to_device(&self) -> Option<&ToDeviceRequest> {
69as_variant::as_variant!(self, Self::ToDeviceRequest)
70 }
71}
7273impl From<KeysQueryRequest> for AnyOutgoingRequest {
74fn from(request: KeysQueryRequest) -> Self {
75Self::KeysQuery(request)
76 }
77}
7879impl From<KeysClaimRequest> for AnyOutgoingRequest {
80fn from(r: KeysClaimRequest) -> Self {
81Self::KeysClaim(r)
82 }
83}
8485impl From<KeysUploadRequest> for AnyOutgoingRequest {
86fn from(request: KeysUploadRequest) -> Self {
87Self::KeysUpload(request)
88 }
89}
9091impl From<ToDeviceRequest> for AnyOutgoingRequest {
92fn from(request: ToDeviceRequest) -> Self {
93Self::ToDeviceRequest(request)
94 }
95}
9697impl From<RoomMessageRequest> for AnyOutgoingRequest {
98fn from(request: RoomMessageRequest) -> Self {
99Self::RoomMessage(request)
100 }
101}
102103impl From<SignatureUploadRequest> for AnyOutgoingRequest {
104fn from(request: SignatureUploadRequest) -> Self {
105Self::SignatureUpload(request)
106 }
107}
108109impl From<OutgoingVerificationRequest> for OutgoingRequest {
110fn from(r: OutgoingVerificationRequest) -> Self {
111Self { request_id: r.request_id().to_owned(), request: Arc::new(r.into()) }
112 }
113}
114115impl From<SignatureUploadRequest> for OutgoingRequest {
116fn from(r: SignatureUploadRequest) -> Self {
117Self { request_id: TransactionId::new(), request: Arc::new(r.into()) }
118 }
119}
120121impl From<KeysUploadRequest> for OutgoingRequest {
122fn from(r: KeysUploadRequest) -> Self {
123Self { request_id: TransactionId::new(), request: Arc::new(r.into()) }
124 }
125}
126127impl From<OutgoingVerificationRequest> for AnyOutgoingRequest {
128fn from(request: OutgoingVerificationRequest) -> Self {
129match request {
130 OutgoingVerificationRequest::ToDevice(r) => AnyOutgoingRequest::ToDeviceRequest(r),
131 OutgoingVerificationRequest::InRoom(r) => AnyOutgoingRequest::RoomMessage(r),
132 }
133 }
134}
135136/// 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.
141KeysUpload(&'a KeysUploadResponse),
142/// The `/keys/query` response, giving us the device and cross signing keys
143 /// of other users.
144KeysQuery(&'a KeysQueryResponse),
145/// The to-device response, an empty response.
146ToDevice(&'a ToDeviceResponse),
147/// The key claiming requests, giving us new one-time keys of other users so
148 /// new Olm sessions can be created.
149KeysClaim(&'a KeysClaimResponse),
150/// The cross signing `/keys/upload` response, marking our private cross
151 /// signing identity as shared.
152SigningKeysUpload(&'a SigningKeysUploadResponse),
153/// The cross signing signature upload response.
154SignatureUpload(&'a SignatureUploadResponse),
155/// A room message response, usually for interactive verifications.
156RoomMessage(&'a RoomMessageResponse),
157/// Response for the server-side room key backup request.
158KeysBackup(&'a KeysBackupResponse),
159}
160161impl<'a> From<&'a KeysUploadResponse> for AnyIncomingResponse<'a> {
162fn from(response: &'a KeysUploadResponse) -> Self {
163 AnyIncomingResponse::KeysUpload(response)
164 }
165}
166167impl<'a> From<&'a KeysBackupResponse> for AnyIncomingResponse<'a> {
168fn from(response: &'a KeysBackupResponse) -> Self {
169 AnyIncomingResponse::KeysBackup(response)
170 }
171}
172173impl<'a> From<&'a KeysQueryResponse> for AnyIncomingResponse<'a> {
174fn from(response: &'a KeysQueryResponse) -> Self {
175 AnyIncomingResponse::KeysQuery(response)
176 }
177}
178179impl<'a> From<&'a ToDeviceResponse> for AnyIncomingResponse<'a> {
180fn from(response: &'a ToDeviceResponse) -> Self {
181 AnyIncomingResponse::ToDevice(response)
182 }
183}
184185impl<'a> From<&'a RoomMessageResponse> for AnyIncomingResponse<'a> {
186fn from(response: &'a RoomMessageResponse) -> Self {
187 AnyIncomingResponse::RoomMessage(response)
188 }
189}
190191impl<'a> From<&'a KeysClaimResponse> for AnyIncomingResponse<'a> {
192fn from(response: &'a KeysClaimResponse) -> Self {
193 AnyIncomingResponse::KeysClaim(response)
194 }
195}
196197impl<'a> From<&'a SignatureUploadResponse> for AnyIncomingResponse<'a> {
198fn from(response: &'a SignatureUploadResponse) -> Self {
199 AnyIncomingResponse::SignatureUpload(response)
200 }
201}