2020-08-21 11:34:25 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-12-15 15:35:54 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2020-09-12 01:19:22 +00:00
|
|
|
use std::{collections::BTreeMap, sync::Arc, time::Duration};
|
2020-08-21 14:26:34 +00:00
|
|
|
|
2020-08-21 11:34:25 +00:00
|
|
|
use matrix_sdk_common::{
|
|
|
|
api::r0::{
|
|
|
|
keys::{
|
2020-08-21 12:40:49 +00:00
|
|
|
claim_keys::Response as KeysClaimResponse,
|
2020-09-12 01:19:22 +00:00
|
|
|
get_keys::Response as KeysQueryResponse,
|
2020-08-21 11:34:25 +00:00
|
|
|
upload_keys::{Request as KeysUploadRequest, Response as KeysUploadResponse},
|
2020-10-27 15:39:23 +00:00
|
|
|
upload_signatures::{
|
|
|
|
Request as SignatureUploadRequest, Response as SignatureUploadResponse,
|
|
|
|
},
|
2020-10-19 14:03:01 +00:00
|
|
|
upload_signing_keys::Response as SigningKeysUploadResponse,
|
|
|
|
CrossSigningKey,
|
2020-08-21 11:34:25 +00:00
|
|
|
},
|
2020-12-15 15:35:54 +00:00
|
|
|
message::send_message_event::Response as RoomMessageResponse,
|
2020-09-03 18:02:55 +00:00
|
|
|
to_device::{send_event_to_device::Response as ToDeviceResponse, DeviceIdOrAllDevices},
|
2020-08-21 11:34:25 +00:00
|
|
|
},
|
2020-12-15 15:35:54 +00:00
|
|
|
events::{AnyMessageEventContent, EventType},
|
|
|
|
identifiers::{DeviceIdBox, RoomId, UserId},
|
2020-08-21 11:34:25 +00:00
|
|
|
uuid::Uuid,
|
|
|
|
};
|
2021-01-25 16:14:13 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-09-03 18:02:55 +00:00
|
|
|
use serde_json::value::RawValue as RawJsonValue;
|
|
|
|
|
2021-05-12 17:20:52 +00:00
|
|
|
/// Customized version of
|
|
|
|
/// `ruma_client_api::r0::to_device::send_event_to_device::Request`, using a
|
2020-09-03 18:02:55 +00:00
|
|
|
/// UUID for the transaction ID.
|
2021-01-25 16:14:13 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2020-09-03 18:02:55 +00:00
|
|
|
pub struct ToDeviceRequest {
|
|
|
|
/// Type of event being sent to each device.
|
|
|
|
pub event_type: EventType,
|
|
|
|
|
2021-05-12 17:20:52 +00:00
|
|
|
/// A request identifier unique to the access token used to send the
|
|
|
|
/// request.
|
2020-09-03 18:02:55 +00:00
|
|
|
pub txn_id: Uuid,
|
|
|
|
|
|
|
|
/// A map of users to devices to a content for a message event to be
|
|
|
|
/// sent to the user's device. Individual message events can be sent
|
|
|
|
/// to devices, but all events must be of the same type.
|
|
|
|
/// The content's type for this field will be updated in a future
|
|
|
|
/// release, until then you can create a value using
|
|
|
|
/// `serde_json::value::to_raw_value`.
|
|
|
|
pub messages: BTreeMap<UserId, BTreeMap<DeviceIdOrAllDevices, Box<RawJsonValue>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToDeviceRequest {
|
|
|
|
/// Gets the transaction ID as a string.
|
|
|
|
pub fn txn_id_string(&self) -> String {
|
|
|
|
self.txn_id.to_string()
|
|
|
|
}
|
2021-03-13 10:50:05 +00:00
|
|
|
|
|
|
|
/// Get the number of unique messages this request contains.
|
|
|
|
///
|
|
|
|
/// *Note*: A single message may be sent to multiple devices, so this may or
|
|
|
|
/// may not be the number of devices that will receive the messages as well.
|
|
|
|
pub fn message_count(&self) -> usize {
|
|
|
|
self.messages.values().map(|d| d.len()).sum()
|
|
|
|
}
|
2020-09-03 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:03:01 +00:00
|
|
|
/// Request that will publish a cross signing identity.
|
|
|
|
///
|
|
|
|
/// This uploads the public cross signing key triplet.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct UploadSigningKeysRequest {
|
|
|
|
/// The user's master key.
|
|
|
|
pub master_key: Option<CrossSigningKey>,
|
2021-05-12 17:20:52 +00:00
|
|
|
/// The user's self-signing key. Must be signed with the accompanied master,
|
|
|
|
/// or by the user's most recently uploaded master key if no master key
|
|
|
|
/// is included in the request.
|
2020-10-19 14:03:01 +00:00
|
|
|
pub self_signing_key: Option<CrossSigningKey>,
|
2021-05-12 17:20:52 +00:00
|
|
|
/// The user's user-signing key. Must be signed with the accompanied master,
|
|
|
|
/// or by the user's most recently uploaded master key if no master key
|
|
|
|
/// is included in the request.
|
2020-10-19 14:03:01 +00:00
|
|
|
pub user_signing_key: Option<CrossSigningKey>,
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:20:52 +00:00
|
|
|
/// Customized version of `ruma_client_api::r0::keys::get_keys::Request`,
|
|
|
|
/// without any references.
|
2020-09-12 01:19:22 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct KeysQueryRequest {
|
|
|
|
/// The time (in milliseconds) to wait when downloading keys from remote
|
|
|
|
/// servers. 10 seconds is the recommended default.
|
|
|
|
pub timeout: Option<Duration>,
|
|
|
|
|
|
|
|
/// The keys to be downloaded. An empty list indicates all devices for
|
|
|
|
/// the corresponding user.
|
|
|
|
pub device_keys: BTreeMap<UserId, Vec<DeviceIdBox>>,
|
|
|
|
|
|
|
|
/// If the client is fetching keys as a result of a device update
|
|
|
|
/// received in a sync request, this should be the 'since' token of that
|
|
|
|
/// sync request, or any later sync token. This allows the server to
|
|
|
|
/// ensure its response contains the keys advertised by the notification
|
|
|
|
/// in that sync.
|
|
|
|
pub token: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeysQueryRequest {
|
|
|
|
pub(crate) fn new(device_keys: BTreeMap<UserId, Vec<DeviceIdBox>>) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { timeout: None, device_keys, token: None }
|
2020-09-12 01:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:57:34 +00:00
|
|
|
/// Enum over the different outgoing requests we can have.
|
2020-08-21 11:34:25 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum OutgoingRequests {
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The keys upload request, uploading device and one-time keys.
|
2020-08-21 11:34:25 +00:00
|
|
|
KeysUpload(KeysUploadRequest),
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The keys query request, fetching the device and cross singing keys of
|
|
|
|
/// other users.
|
2020-08-21 11:34:25 +00:00
|
|
|
KeysQuery(KeysQueryRequest),
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The to-device requests, this request is used for a couple of different
|
|
|
|
/// things, the main use is key requests/forwards and interactive device
|
|
|
|
/// verification.
|
2020-08-21 11:34:25 +00:00
|
|
|
ToDeviceRequest(ToDeviceRequest),
|
2020-10-27 15:39:23 +00:00
|
|
|
/// Signature upload request, this request is used after a successful device
|
|
|
|
/// or user verification is done.
|
|
|
|
SignatureUpload(SignatureUploadRequest),
|
2020-12-15 15:35:54 +00:00
|
|
|
RoomMessage(RoomMessageRequest),
|
2020-08-21 11:34:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:24:54 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
impl OutgoingRequests {
|
|
|
|
pub fn to_device(&self) -> Option<&ToDeviceRequest> {
|
|
|
|
match self {
|
|
|
|
OutgoingRequests::ToDeviceRequest(r) => Some(r),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:34:25 +00:00
|
|
|
impl From<KeysQueryRequest> for OutgoingRequests {
|
|
|
|
fn from(request: KeysQueryRequest) -> Self {
|
|
|
|
OutgoingRequests::KeysQuery(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<KeysUploadRequest> for OutgoingRequests {
|
|
|
|
fn from(request: KeysUploadRequest) -> Self {
|
|
|
|
OutgoingRequests::KeysUpload(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:26:34 +00:00
|
|
|
impl From<ToDeviceRequest> for OutgoingRequests {
|
|
|
|
fn from(request: ToDeviceRequest) -> Self {
|
|
|
|
OutgoingRequests::ToDeviceRequest(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:35:54 +00:00
|
|
|
impl From<RoomMessageRequest> for OutgoingRequests {
|
|
|
|
fn from(request: RoomMessageRequest) -> Self {
|
|
|
|
OutgoingRequests::RoomMessage(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:39:23 +00:00
|
|
|
impl From<SignatureUploadRequest> for OutgoingRequests {
|
|
|
|
fn from(request: SignatureUploadRequest) -> Self {
|
|
|
|
OutgoingRequests::SignatureUpload(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 11:15:11 +00:00
|
|
|
impl From<OutgoingVerificationRequest> for OutgoingRequest {
|
|
|
|
fn from(r: OutgoingVerificationRequest) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { request_id: r.request_id(), request: Arc::new(r.into()) }
|
2020-12-17 11:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SignatureUploadRequest> for OutgoingRequest {
|
|
|
|
fn from(r: SignatureUploadRequest) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { request_id: Uuid::new_v4(), request: Arc::new(r.into()) }
|
2020-12-17 11:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:57:34 +00:00
|
|
|
/// Enum over all the incoming responses we need to receive.
|
2020-08-21 11:34:25 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum IncomingResponse<'a> {
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The keys upload response, notifying us about the amount of uploaded
|
|
|
|
/// one-time keys.
|
2020-08-21 11:34:25 +00:00
|
|
|
KeysUpload(&'a KeysUploadResponse),
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The keys query response, giving us the device and cross singing keys of
|
|
|
|
/// other users.
|
2020-08-21 11:34:25 +00:00
|
|
|
KeysQuery(&'a KeysQueryResponse),
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The to-device response, an empty response.
|
2020-08-21 11:34:25 +00:00
|
|
|
ToDevice(&'a ToDeviceResponse),
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The key claiming requests, giving us new one-time keys of other users so
|
|
|
|
/// new Olm sessions can be created.
|
2020-08-21 12:40:49 +00:00
|
|
|
KeysClaim(&'a KeysClaimResponse),
|
2020-10-19 14:03:01 +00:00
|
|
|
/// The cross signing keys upload response, marking our private cross
|
|
|
|
/// signing identity as shared.
|
|
|
|
SigningKeysUpload(&'a SigningKeysUploadResponse),
|
2020-10-27 15:39:23 +00:00
|
|
|
/// The cross signing keys upload response, marking our private cross
|
|
|
|
/// signing identity as shared.
|
|
|
|
SignatureUpload(&'a SignatureUploadResponse),
|
2020-12-15 15:35:54 +00:00
|
|
|
RoomMessage(&'a RoomMessageResponse),
|
2020-08-21 11:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a KeysUploadResponse> for IncomingResponse<'a> {
|
|
|
|
fn from(response: &'a KeysUploadResponse) -> Self {
|
|
|
|
IncomingResponse::KeysUpload(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a KeysQueryResponse> for IncomingResponse<'a> {
|
|
|
|
fn from(response: &'a KeysQueryResponse) -> Self {
|
|
|
|
IncomingResponse::KeysQuery(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a ToDeviceResponse> for IncomingResponse<'a> {
|
|
|
|
fn from(response: &'a ToDeviceResponse) -> Self {
|
|
|
|
IncomingResponse::ToDevice(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:35:54 +00:00
|
|
|
impl<'a> From<&'a RoomMessageResponse> for IncomingResponse<'a> {
|
|
|
|
fn from(response: &'a RoomMessageResponse) -> Self {
|
|
|
|
IncomingResponse::RoomMessage(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:40:49 +00:00
|
|
|
impl<'a> From<&'a KeysClaimResponse> for IncomingResponse<'a> {
|
|
|
|
fn from(response: &'a KeysClaimResponse) -> Self {
|
|
|
|
IncomingResponse::KeysClaim(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:39:23 +00:00
|
|
|
impl<'a> From<&'a SignatureUploadResponse> for IncomingResponse<'a> {
|
|
|
|
fn from(response: &'a SignatureUploadResponse) -> Self {
|
|
|
|
IncomingResponse::SignatureUpload(response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:57:34 +00:00
|
|
|
/// Outgoing request type, holds the unique ID of the request and the actual
|
|
|
|
/// request.
|
2020-08-21 14:26:34 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-08-21 11:34:25 +00:00
|
|
|
pub struct OutgoingRequest {
|
|
|
|
/// The unique id of a request, needs to be passed when receiving a
|
|
|
|
/// response.
|
2020-08-21 14:26:34 +00:00
|
|
|
pub(crate) request_id: Uuid,
|
2020-08-23 14:57:34 +00:00
|
|
|
/// The underlying outgoing request.
|
2020-08-21 14:26:34 +00:00
|
|
|
pub(crate) request: Arc<OutgoingRequests>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutgoingRequest {
|
|
|
|
/// Get the unique id of this request.
|
|
|
|
pub fn request_id(&self) -> &Uuid {
|
|
|
|
&self.request_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the underlying outgoing request.
|
|
|
|
pub fn request(&self) -> &OutgoingRequests {
|
|
|
|
&self.request
|
|
|
|
}
|
2020-08-21 11:34:25 +00:00
|
|
|
}
|
2020-12-15 15:35:54 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct RoomMessageRequest {
|
|
|
|
/// The room to send the event to.
|
|
|
|
pub room_id: RoomId,
|
|
|
|
|
|
|
|
/// The transaction ID for this event.
|
|
|
|
///
|
|
|
|
/// Clients should generate an ID unique across requests with the
|
|
|
|
/// same access token; it will be used by the server to ensure
|
|
|
|
/// idempotency of requests.
|
|
|
|
pub txn_id: Uuid,
|
|
|
|
|
|
|
|
/// The event content to send.
|
|
|
|
pub content: AnyMessageEventContent,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum OutgoingVerificationRequest {
|
|
|
|
ToDevice(ToDeviceRequest),
|
|
|
|
InRoom(RoomMessageRequest),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutgoingVerificationRequest {
|
|
|
|
pub fn request_id(&self) -> Uuid {
|
|
|
|
match self {
|
|
|
|
OutgoingVerificationRequest::ToDevice(t) => t.txn_id,
|
|
|
|
OutgoingVerificationRequest::InRoom(r) => r.txn_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ToDeviceRequest> for OutgoingVerificationRequest {
|
|
|
|
fn from(r: ToDeviceRequest) -> Self {
|
|
|
|
OutgoingVerificationRequest::ToDevice(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RoomMessageRequest> for OutgoingVerificationRequest {
|
|
|
|
fn from(r: RoomMessageRequest) -> Self {
|
|
|
|
OutgoingVerificationRequest::InRoom(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OutgoingVerificationRequest> for OutgoingRequests {
|
|
|
|
fn from(request: OutgoingVerificationRequest) -> Self {
|
|
|
|
match request {
|
|
|
|
OutgoingVerificationRequest::ToDevice(r) => OutgoingRequests::ToDeviceRequest(r),
|
|
|
|
OutgoingVerificationRequest::InRoom(r) => OutgoingRequests::RoomMessage(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|