From 475fffb64d2cb7774686bbbcc717e0585fb5dcd7 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 31 May 2022 15:53:12 +0200 Subject: [PATCH] feat(crypto-nodejs): Implement `OlmMachine.encrypt_room_event`. --- .../matrix-sdk-crypto-nodejs/src/machine.rs | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/crates/matrix-sdk-crypto-nodejs/src/machine.rs b/crates/matrix-sdk-crypto-nodejs/src/machine.rs index 36b700556..989253671 100644 --- a/crates/matrix-sdk-crypto-nodejs/src/machine.rs +++ b/crates/matrix-sdk-crypto-nodejs/src/machine.rs @@ -8,6 +8,7 @@ use std::{ use napi::{bindgen_prelude::ToNapiValue, Either}; use napi_derive::*; use ruma::{DeviceKeyAlgorithm, OwnedTransactionId, UInt}; +use serde_json::Value as JsonValue; use crate::{ events, identifiers, into_err, requests, responses, responses::response_from_string, @@ -84,7 +85,7 @@ impl OlmMachine { .collect::>(), ); - Ok(serde_json::to_string( + serde_json::to_string( &self .inner .receive_sync_changes( @@ -96,7 +97,7 @@ impl OlmMachine { .await .map_err(into_err)?, ) - .map_err(into_err)?) + .map_err(into_err) } // We could be tempted to use `requests::OutgoingRequests` as its @@ -203,14 +204,34 @@ impl OlmMachine { let encryption_settings = matrix_sdk_crypto::olm::EncryptionSettings::from(encryption_settings); - Ok(serde_json::to_string( + serde_json::to_string( &self .inner .share_room_key(&room_id, users.iter().map(AsRef::as_ref), encryption_settings) .await .map_err(into_err)?, ) - .map_err(into_err)?) + .map_err(into_err) + } + + #[napi] + pub async fn encrypt_room_event( + &self, + room_id: &identifiers::RoomId, + event_type: String, + content: String, + ) -> Result { + let room_id = room_id.inner.clone(); + let content: JsonValue = serde_json::from_str(content.as_str()).map_err(into_err)?; + + serde_json::to_string( + &self + .inner + .encrypt_room_event_raw(&room_id, content, event_type.as_ref()) + .await + .map_err(into_err)?, + ) + .map_err(into_err) } }