matrix_sdk_crypto_ffi/
responses.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#![allow(missing_docs)]

use std::collections::HashMap;

use http::Response;
use matrix_sdk_crypto::{
    types::requests::{
        AnyIncomingResponse, KeysBackupRequest, OutgoingRequest,
        OutgoingVerificationRequest as SdkVerificationRequest, RoomMessageRequest, ToDeviceRequest,
        UploadSigningKeysRequest as RustUploadSigningKeysRequest,
    },
    CrossSigningBootstrapRequests,
};
use ruma::{
    api::client::{
        backup::add_backup_keys::v3::Response as KeysBackupResponse,
        keys::{
            claim_keys::v3::{Request as KeysClaimRequest, Response as KeysClaimResponse},
            get_keys::v3::Response as KeysQueryResponse,
            upload_keys::v3::Response as KeysUploadResponse,
            upload_signatures::v3::{
                Request as RustSignatureUploadRequest, Response as SignatureUploadResponse,
            },
        },
        message::send_message_event::v3::Response as RoomMessageResponse,
        sync::sync_events::DeviceLists as RumaDeviceLists,
        to_device::send_event_to_device::v3::Response as ToDeviceResponse,
    },
    assign,
    events::EventContent,
    OwnedTransactionId, UserId,
};
use serde_json::json;

#[derive(uniffi::Record)]
pub struct SignatureUploadRequest {
    pub body: String,
}

impl From<RustSignatureUploadRequest> for SignatureUploadRequest {
    fn from(r: RustSignatureUploadRequest) -> Self {
        Self {
            body: serde_json::to_string(&r.signed_keys)
                .expect("Can't serialize signature upload request"),
        }
    }
}

#[derive(uniffi::Record)]
pub struct UploadSigningKeysRequest {
    pub master_key: String,
    pub self_signing_key: String,
    pub user_signing_key: String,
}

impl From<RustUploadSigningKeysRequest> for UploadSigningKeysRequest {
    fn from(r: RustUploadSigningKeysRequest) -> Self {
        Self {
            master_key: serde_json::to_string(
                &r.master_key.expect("Request didn't contain a master key"),
            )
            .expect("Can't serialize cross signing master key"),
            self_signing_key: serde_json::to_string(
                &r.self_signing_key.expect("Request didn't contain a self-signing key"),
            )
            .expect("Can't serialize cross signing self-signing key"),
            user_signing_key: serde_json::to_string(
                &r.user_signing_key.expect("Request didn't contain a user-signing key"),
            )
            .expect("Can't serialize cross signing user-signing key"),
        }
    }
}

#[derive(uniffi::Record)]
pub struct BootstrapCrossSigningResult {
    /// Optional request to upload some device keys alongside.
    ///
    /// Must be sent first if present, and marked with `mark_request_as_sent`.
    pub upload_keys_request: Option<Request>,
    /// Request to upload the signing keys. Must be sent second.
    pub upload_signing_keys_request: UploadSigningKeysRequest,
    /// Request to upload the keys signatures, including for the signing keys
    /// and optionally for the device keys. Must be sent last.
    pub upload_signature_request: SignatureUploadRequest,
}

impl From<CrossSigningBootstrapRequests> for BootstrapCrossSigningResult {
    fn from(requests: CrossSigningBootstrapRequests) -> Self {
        Self {
            upload_signing_keys_request: requests.upload_signing_keys_req.into(),
            upload_keys_request: requests.upload_keys_req.map(Request::from),
            upload_signature_request: requests.upload_signatures_req.into(),
        }
    }
}

#[derive(uniffi::Enum)]
pub enum OutgoingVerificationRequest {
    ToDevice { request_id: String, event_type: String, body: String },
    InRoom { request_id: String, room_id: String, event_type: String, content: String },
}

impl From<SdkVerificationRequest> for OutgoingVerificationRequest {
    fn from(r: SdkVerificationRequest) -> Self {
        match r {
            SdkVerificationRequest::ToDevice(r) => r.into(),
            SdkVerificationRequest::InRoom(r) => Self::InRoom {
                request_id: r.txn_id.to_string(),
                room_id: r.room_id.to_string(),
                content: serde_json::to_string(&r.content)
                    .expect("Can't serialize message content"),
                event_type: r.content.event_type().to_string(),
            },
        }
    }
}

impl From<ToDeviceRequest> for OutgoingVerificationRequest {
    fn from(r: ToDeviceRequest) -> Self {
        Self::ToDevice {
            request_id: r.txn_id.to_string(),
            event_type: r.event_type.to_string(),
            body: serde_json::to_string(&r.messages).expect("Can't serialize to-device body"),
        }
    }
}

#[derive(Debug, uniffi::Enum)]
pub enum Request {
    ToDevice { request_id: String, event_type: String, body: String },
    KeysUpload { request_id: String, body: String },
    KeysQuery { request_id: String, users: Vec<String> },
    KeysClaim { request_id: String, one_time_keys: HashMap<String, HashMap<String, String>> },
    KeysBackup { request_id: String, version: String, rooms: String },
    RoomMessage { request_id: String, room_id: String, event_type: String, content: String },
    SignatureUpload { request_id: String, body: String },
}

impl From<OutgoingRequest> for Request {
    fn from(r: OutgoingRequest) -> Self {
        use matrix_sdk_crypto::types::requests::AnyOutgoingRequest::*;

        match r.request() {
            KeysUpload(u) => {
                let body = json!({
                    "device_keys": u.device_keys,
                    "one_time_keys": u.one_time_keys,
                    "fallback_keys": u.fallback_keys,
                });

                Request::KeysUpload {
                    request_id: r.request_id().to_string(),
                    body: serde_json::to_string(&body)
                        .expect("Can't serialize `/keys/upload` request"),
                }
            }
            KeysQuery(k) => {
                let users: Vec<String> = k.device_keys.keys().map(|u| u.to_string()).collect();
                Request::KeysQuery { request_id: r.request_id().to_string(), users }
            }
            ToDeviceRequest(t) => Request::from(t),
            SignatureUpload(t) => Request::SignatureUpload {
                request_id: r.request_id().to_string(),
                body: serde_json::to_string(&t.signed_keys)
                    .expect("Can't serialize signature upload request"),
            },
            RoomMessage(r) => Request::from(r),
            KeysClaim(c) => (r.request_id().to_owned(), c.clone()).into(),
        }
    }
}

impl From<ToDeviceRequest> for Request {
    fn from(r: ToDeviceRequest) -> Self {
        Request::ToDevice {
            request_id: r.txn_id.to_string(),
            event_type: r.event_type.to_string(),
            body: serde_json::to_string(&r.messages).expect("Can't serialize to-device body"),
        }
    }
}

impl From<(OwnedTransactionId, KeysClaimRequest)> for Request {
    fn from(request_tuple: (OwnedTransactionId, KeysClaimRequest)) -> Self {
        let (request_id, request) = request_tuple;

        Request::KeysClaim {
            request_id: request_id.to_string(),
            one_time_keys: request
                .one_time_keys
                .into_iter()
                .map(|(u, d)| {
                    (
                        u.to_string(),
                        d.into_iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(),
                    )
                })
                .collect(),
        }
    }
}

impl From<(OwnedTransactionId, KeysBackupRequest)> for Request {
    fn from(request_tuple: (OwnedTransactionId, KeysBackupRequest)) -> Self {
        let (request_id, request) = request_tuple;

        Request::KeysBackup {
            request_id: request_id.to_string(),
            version: request.version.to_owned(),
            rooms: serde_json::to_string(&request.rooms)
                .expect("Can't serialize keys backup request"),
        }
    }
}

impl From<&ToDeviceRequest> for Request {
    fn from(r: &ToDeviceRequest) -> Self {
        Request::ToDevice {
            request_id: r.txn_id.to_string(),
            event_type: r.event_type.to_string(),
            body: serde_json::to_string(&r.messages).expect("Can't serialize to-device body"),
        }
    }
}

impl From<&RoomMessageRequest> for Request {
    fn from(r: &RoomMessageRequest) -> Self {
        Self::RoomMessage {
            request_id: r.txn_id.to_string(),
            room_id: r.room_id.to_string(),
            event_type: r.content.event_type().to_string(),
            content: serde_json::to_string(&r.content).expect("Can't serialize message content"),
        }
    }
}

pub(crate) fn response_from_string(body: &str) -> Response<Vec<u8>> {
    Response::builder()
        .status(200)
        .body(body.as_bytes().to_vec())
        .expect("Can't create HTTP response")
}

#[derive(uniffi::Enum)]
pub enum RequestType {
    KeysQuery,
    KeysClaim,
    KeysUpload,
    ToDevice,
    SignatureUpload,
    KeysBackup,
    RoomMessage,
}

#[derive(uniffi::Record)]
pub struct DeviceLists {
    pub changed: Vec<String>,
    pub left: Vec<String>,
}

impl From<DeviceLists> for RumaDeviceLists {
    fn from(d: DeviceLists) -> Self {
        assign!(RumaDeviceLists::new(), {
            changed: d
                .changed
                .into_iter()
                .filter_map(|u| UserId::parse(u).ok())
                .collect(),
            left: d
                .left
                .into_iter()
                .filter_map(|u| UserId::parse(u).ok())
                .collect(),
        })
    }
}

#[derive(uniffi::Record)]
pub struct KeysImportResult {
    /// The number of room keys that were imported.
    pub imported: i64,
    /// The total number of room keys that were found in the export.
    pub total: i64,
    /// The map of keys that were imported.
    ///
    /// It's a map from room id to a map of the sender key to a list of session
    /// ids.
    pub keys: HashMap<String, HashMap<String, Vec<String>>>,
}

pub(crate) enum OwnedResponse {
    KeysClaim(KeysClaimResponse),
    KeysUpload(KeysUploadResponse),
    KeysQuery(KeysQueryResponse),
    ToDevice(ToDeviceResponse),
    SignatureUpload(SignatureUploadResponse),
    KeysBackup(KeysBackupResponse),
    RoomMessage(RoomMessageResponse),
}

impl From<KeysClaimResponse> for OwnedResponse {
    fn from(response: KeysClaimResponse) -> Self {
        OwnedResponse::KeysClaim(response)
    }
}

impl From<KeysQueryResponse> for OwnedResponse {
    fn from(response: KeysQueryResponse) -> Self {
        OwnedResponse::KeysQuery(response)
    }
}

impl From<KeysUploadResponse> for OwnedResponse {
    fn from(response: KeysUploadResponse) -> Self {
        OwnedResponse::KeysUpload(response)
    }
}

impl From<ToDeviceResponse> for OwnedResponse {
    fn from(response: ToDeviceResponse) -> Self {
        OwnedResponse::ToDevice(response)
    }
}

impl From<SignatureUploadResponse> for OwnedResponse {
    fn from(response: SignatureUploadResponse) -> Self {
        Self::SignatureUpload(response)
    }
}

impl From<KeysBackupResponse> for OwnedResponse {
    fn from(r: KeysBackupResponse) -> Self {
        Self::KeysBackup(r)
    }
}

impl From<RoomMessageResponse> for OwnedResponse {
    fn from(response: RoomMessageResponse) -> Self {
        OwnedResponse::RoomMessage(response)
    }
}

impl<'a> From<&'a OwnedResponse> for AnyIncomingResponse<'a> {
    fn from(r: &'a OwnedResponse) -> Self {
        match r {
            OwnedResponse::KeysClaim(r) => AnyIncomingResponse::KeysClaim(r),
            OwnedResponse::KeysQuery(r) => AnyIncomingResponse::KeysQuery(r),
            OwnedResponse::KeysUpload(r) => AnyIncomingResponse::KeysUpload(r),
            OwnedResponse::ToDevice(r) => AnyIncomingResponse::ToDevice(r),
            OwnedResponse::SignatureUpload(r) => AnyIncomingResponse::SignatureUpload(r),
            OwnedResponse::KeysBackup(r) => AnyIncomingResponse::KeysBackup(r),
            OwnedResponse::RoomMessage(r) => AnyIncomingResponse::RoomMessage(r),
        }
    }
}