diff --git a/crates/matrix-sdk-crypto-nodejs/Cargo.toml b/crates/matrix-sdk-crypto-nodejs/Cargo.toml index b5c0525c0..c00cfafd1 100644 --- a/crates/matrix-sdk-crypto-nodejs/Cargo.toml +++ b/crates/matrix-sdk-crypto-nodejs/Cargo.toml @@ -30,6 +30,7 @@ vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "d0e74428 napi = { git = "https://github.com/Hywan/napi-rs", branch = "feat-tonapivalue-u16", default-features = false, features = ["napi4", "tokio_rt"] } napi-derive = "2.4.1" serde_json = "1.0.79" +http = "0.2.6" [build-dependencies] napi-build = "2.0.0" \ No newline at end of file diff --git a/crates/matrix-sdk-crypto-nodejs/src/lib.rs b/crates/matrix-sdk-crypto-nodejs/src/lib.rs index f35771cdb..f8bf7e463 100644 --- a/crates/matrix-sdk-crypto-nodejs/src/lib.rs +++ b/crates/matrix-sdk-crypto-nodejs/src/lib.rs @@ -18,6 +18,7 @@ mod errors; pub mod events; +pub mod responses; pub mod identifiers; pub mod machine; pub mod requests; diff --git a/crates/matrix-sdk-crypto-nodejs/src/machine.rs b/crates/matrix-sdk-crypto-nodejs/src/machine.rs index 7f31e1e5a..1ec39373c 100644 --- a/crates/matrix-sdk-crypto-nodejs/src/machine.rs +++ b/crates/matrix-sdk-crypto-nodejs/src/machine.rs @@ -4,9 +4,11 @@ use std::collections::{BTreeMap, HashMap}; use napi::Either; use napi_derive::*; -use ruma::{DeviceKeyAlgorithm, UInt}; +use ruma::{DeviceKeyAlgorithm, OwnedTransactionId, UInt}; -use crate::{identifiers, into_err, requests, sync_events}; +use crate::{ + identifiers, into_err, requests, responses, responses::response_from_string, sync_events, +}; /// State machine implementation of the Olm/Megolm encryption protocol /// used for Matrix end to end encryption. @@ -132,6 +134,24 @@ impl OlmMachine { .map_err(into_err) } + #[napi] + pub async fn mark_request_as_sent( + &self, + request_id: String, + request_type: requests::RequestType, + response: String, + ) -> Result { + let transaction_id = OwnedTransactionId::from(request_id); + let response = response_from_string(response.as_str()).map_err(into_err)?; + let incoming_response = responses::OwnedResponse::try_from((request_type, response))?; + + self.inner + .mark_request_as_sent(&transaction_id, &incoming_response) + .await + .map(|_| true) + .map_err(into_err) + } + #[napi] pub async fn update_tracked_users(&self, users: Vec<&identifiers::UserId>) { let users: Vec = diff --git a/crates/matrix-sdk-crypto-nodejs/src/responses.rs b/crates/matrix-sdk-crypto-nodejs/src/responses.rs new file mode 100644 index 000000000..1594c7142 --- /dev/null +++ b/crates/matrix-sdk-crypto-nodejs/src/responses.rs @@ -0,0 +1,125 @@ +use matrix_sdk_crypto::IncomingResponse; +pub(crate) use ruma::api::client::{ + backup::add_backup_keys::v3::Response as KeysBackupResponse, + keys::{ + claim_keys::v3::Response as KeysClaimResponse, get_keys::v3::Response as KeysQueryResponse, + upload_keys::v3::Response as KeysUploadResponse, + upload_signatures::v3::Response as SignatureUploadResponse, + }, + message::send_message_event::v3::Response as RoomMessageResponse, + to_device::send_event_to_device::v3::Response as ToDeviceResponse, +}; +use ruma::api::IncomingResponse as RumaIncomingResponse; + +use crate::{into_err, requests::RequestType}; + +pub(crate) fn response_from_string(body: &str) -> http::Result>> { + http::Response::builder().status(200).body(body.as_bytes().to_vec()) +} + +/// Intermediate private type to store an incoming owned response, +/// without the need to manage lifetime. +pub(crate) enum OwnedResponse { + KeysUpload(KeysUploadResponse), + KeysQuery(KeysQueryResponse), + KeysClaim(KeysClaimResponse), + ToDevice(ToDeviceResponse), + SignatureUpload(SignatureUploadResponse), + RoomMessage(RoomMessageResponse), + KeysBackup(KeysBackupResponse), +} + +impl From for OwnedResponse { + fn from(response: KeysUploadResponse) -> Self { + OwnedResponse::KeysUpload(response) + } +} + +impl From for OwnedResponse { + fn from(response: KeysQueryResponse) -> Self { + OwnedResponse::KeysQuery(response) + } +} + +impl From for OwnedResponse { + fn from(response: KeysClaimResponse) -> Self { + OwnedResponse::KeysClaim(response) + } +} + +impl From for OwnedResponse { + fn from(response: ToDeviceResponse) -> Self { + OwnedResponse::ToDevice(response) + } +} + +impl From for OwnedResponse { + fn from(response: SignatureUploadResponse) -> Self { + Self::SignatureUpload(response) + } +} + +impl From for OwnedResponse { + fn from(response: RoomMessageResponse) -> Self { + OwnedResponse::RoomMessage(response) + } +} + +impl From for OwnedResponse { + fn from(r: KeysBackupResponse) -> Self { + Self::KeysBackup(r) + } +} + +impl TryFrom<(RequestType, http::Response>)> for OwnedResponse { + type Error = napi::Error; + + fn try_from( + (request_type, response): (RequestType, http::Response>), + ) -> Result { + match request_type { + RequestType::KeysUpload => { + KeysUploadResponse::try_from_http_response(response).map(Into::into) + } + + RequestType::KeysQuery => { + KeysQueryResponse::try_from_http_response(response).map(Into::into) + } + + RequestType::KeysClaim => { + KeysClaimResponse::try_from_http_response(response).map(Into::into) + } + + RequestType::ToDevice => { + ToDeviceResponse::try_from_http_response(response).map(Into::into) + } + + RequestType::SignatureUpload => { + SignatureUploadResponse::try_from_http_response(response).map(Into::into) + } + + RequestType::RoomMessage => { + RoomMessageResponse::try_from_http_response(response).map(Into::into) + } + + RequestType::KeysBackup => { + KeysBackupResponse::try_from_http_response(response).map(Into::into) + } + } + .map_err(into_err) + } +} + +impl<'a> From<&'a OwnedResponse> for IncomingResponse<'a> { + fn from(response: &'a OwnedResponse) -> Self { + match response { + OwnedResponse::KeysUpload(response) => IncomingResponse::KeysUpload(response), + OwnedResponse::KeysQuery(response) => IncomingResponse::KeysQuery(response), + OwnedResponse::KeysClaim(response) => IncomingResponse::KeysClaim(response), + OwnedResponse::ToDevice(response) => IncomingResponse::ToDevice(response), + OwnedResponse::SignatureUpload(response) => IncomingResponse::SignatureUpload(response), + OwnedResponse::RoomMessage(response) => IncomingResponse::RoomMessage(response), + OwnedResponse::KeysBackup(response) => IncomingResponse::KeysBackup(response), + } + } +}