feat(crypto-nodejs): Implement OlmMachine.receive_sync_changes.

This commit is contained in:
Ivan Enderlin
2022-05-30 17:46:00 +02:00
parent e2ecba7d45
commit 1ecf90bb42
6 changed files with 98 additions and 17 deletions

View File

@@ -29,6 +29,7 @@ ruma = { version = "0.6.2", features = ["client-api-c", "rand", "unstable-msc267
vodozemac = { git = "https://github.com/matrix-org/vodozemac/", rev = "d0e744287a14319c2a9148fef3747548c740fc36" }
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"
[build-dependencies]
napi-build = "2.0.0"

View File

@@ -16,3 +16,10 @@ impl From<Error> for napi::Error {
value.0
}
}
pub fn into_err<E>(error: E) -> napi::Error
where
E: std::error::Error,
{
Error::from(error).into()
}

View File

@@ -3,7 +3,7 @@
use napi_derive::*;
use crate::errors::*;
use crate::into_err;
/// A Matrix [user ID].
///
@@ -25,11 +25,7 @@ impl UserId {
/// Parse/validate and create a new `UserId`.
#[napi(constructor)]
pub fn new(id: String) -> Result<UserId, napi::Error> {
Ok(Self::new_with(
ruma::UserId::parse(id.as_str())
.map_err(Error::from)
.map_err(Into::<napi::Error>::into)?,
))
Ok(Self::new_with(ruma::UserId::parse(id.as_str()).map_err(into_err)?))
}
/// Returns the user's localpart.
@@ -114,9 +110,7 @@ impl RoomId {
/// Parse/validate and create a new `RoomId`.
#[napi(constructor)]
pub fn new(id: String) -> Result<RoomId, napi::Error> {
Ok(Self::new_with(
ruma::RoomId::parse(id).map_err(Error::from).map_err(Into::<napi::Error>::into)?,
))
Ok(Self::new_with(ruma::RoomId::parse(id).map_err(into_err)?))
}
/// Returns the user's localpart.
@@ -156,11 +150,7 @@ impl ServerName {
/// Parse/validate and create a new `ServerName`.
#[napi(constructor)]
pub fn new(name: String) -> Result<ServerName, napi::Error> {
Ok(Self {
inner: ruma::ServerName::parse(name)
.map_err(Error::from)
.map_err(Into::<napi::Error>::into)?,
})
Ok(Self { inner: ruma::ServerName::parse(name).map_err(into_err)? })
}
/// Returns the host of the server name.

View File

@@ -20,5 +20,6 @@ mod errors;
pub mod events;
pub mod identifiers;
pub mod machine;
pub mod sync_events;
pub use crate::errors::Error;
use crate::errors::into_err;

View File

@@ -1,8 +1,11 @@
//! The crypto specific Olm objects.
use napi_derive::*;
use std::collections::{BTreeMap, HashMap};
use crate::identifiers;
use napi_derive::*;
use ruma::{DeviceKeyAlgorithm, UInt};
use crate::{identifiers, into_err, sync_events};
/// State machine implementation of the Olm/Megolm encryption protocol
/// used for Matrix end to end encryption.
@@ -51,6 +54,44 @@ impl OlmMachine {
self.inner.identity_keys().into()
}
#[napi]
pub async fn receive_sync_changes(
&self,
to_device_events: String,
changed_devices: &sync_events::DeviceLists,
one_time_key_counts: HashMap<String, u32>,
unused_fallback_keys: Vec<String>,
) -> Result<String, napi::Error> {
let to_device_events = serde_json::from_str(to_device_events.as_ref()).map_err(into_err)?;
let changed_devices = changed_devices.inner.clone();
let one_time_key_counts = one_time_key_counts
.iter()
.filter_map(|(key, value)| {
Some((DeviceKeyAlgorithm::from(key.as_str()), UInt::new(*value as u64)?))
})
.collect::<BTreeMap<DeviceKeyAlgorithm, UInt>>();
let unused_fallback_keys = Some(
unused_fallback_keys
.into_iter()
.map(|key| DeviceKeyAlgorithm::from(key.as_str()))
.collect::<Vec<DeviceKeyAlgorithm>>(),
);
Ok(serde_json::to_string(
&self
.inner
.receive_sync_changes(
to_device_events,
&changed_devices,
&one_time_key_counts,
unused_fallback_keys.as_deref(),
)
.await
.map_err(into_err)?,
)
.map_err(into_err)?)
}
#[napi]
pub async fn update_tracked_users(&self, users: Vec<&identifiers::UserId>) {
let users: Vec<ruma::OwnedUserId> =

View File

@@ -0,0 +1,41 @@
//! `GET /_matrix/client/*/sync`.
use napi_derive::*;
use crate::identifiers;
/// Information on E2E device updates.
#[napi]
pub struct DeviceLists {
pub(crate) inner: ruma::api::client::sync::sync_events::v3::DeviceLists,
}
#[napi]
impl DeviceLists {
/// Create an empty `DeviceLists`.
#[napi(constructor)]
pub fn new(changed: Vec<&identifiers::UserId>, left: Vec<&identifiers::UserId>) -> Self {
let mut inner = ruma::api::client::sync::sync_events::v3::DeviceLists::default();
inner.changed = changed.into_iter().map(|user| user.inner.clone()).collect();
inner.left = left.into_iter().map(|user| user.inner.clone()).collect();
Self { inner }
}
/// Returns true if there are no device list updates.
#[napi]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[napi]
pub fn changed(&self) -> Vec<identifiers::UserId> {
self.inner.changed.iter().map(|user| identifiers::UserId::new_with(user.clone())).collect()
}
#[napi]
pub fn left(&self) -> Vec<identifiers::UserId> {
self.inner.left.iter().map(|user| identifiers::UserId::new_with(user.clone())).collect()
}
}