use serde_json::{Value, json};
use super::VtaClient;
use crate::error::VtaError;
use crate::trust_tasks;
const ROOM_TT_TIMEOUT: u64 = 30;
impl VtaClient {
pub async fn room_present(&self, room_id: &str, action: &str) -> Result<Value, VtaError> {
let payload = json!({
"roomId": room_id,
"action": action,
});
self.dispatch_trust_task(
trust_tasks::TASK_ROOMS_KEYS_PRESENT_0_2,
payload,
ROOM_TT_TIMEOUT,
)
.await
}
pub async fn room_open(
&self,
room_id: &str,
key: &str,
version: u64,
ciphertext: &str,
nonce: &str,
epoch: u32,
) -> Result<Value, VtaError> {
let payload = json!({
"roomId": room_id,
"key": key,
"version": version,
"sealed": {
"ciphertext": ciphertext,
"nonce": nonce,
"epoch": epoch,
},
});
self.dispatch_trust_task(
trust_tasks::TASK_ROOMS_KEYS_OPEN_0_1,
payload,
ROOM_TT_TIMEOUT,
)
.await
}
pub async fn room_file_key(
&self,
room_id: &str,
file_id: &str,
open_epoch: Option<u64>,
) -> Result<trust_tasks_rs::specs::rooms::keys::file_key::v0_1::Response, VtaError> {
let mut payload = json!({
"roomId": room_id,
"fileId": file_id,
"purpose": if open_epoch.is_some() { "open" } else { "seal" },
});
if let Some(epoch) = open_epoch {
payload["epoch"] = json!(epoch);
}
let value = self
.dispatch_trust_task(
trust_tasks::TASK_ROOMS_KEYS_FILE_KEY_0_1,
payload,
ROOM_TT_TIMEOUT,
)
.await?;
serde_json::from_value(value).map_err(|e| {
VtaError::Protocol(format!(
"rooms/keys/file-key response does not match its schema: {e}"
))
})
}
}