Don't use Raw::from_json where it's not needed

This commit is contained in:
Jonas Platte
2022-03-10 11:59:31 +01:00
parent 8a60154882
commit 6af9285874
7 changed files with 19 additions and 35 deletions

View File

@@ -341,8 +341,7 @@ impl BaseClient {
}
_ => {
room_info.handle_state_event(&s.content());
let raw_event: Raw<AnySyncStateEvent> =
Raw::from_json(event.event.clone().into_json());
let raw_event: Raw<AnySyncStateEvent> = event.event.clone().cast();
changes.add_state_event(room_id, s.clone(), raw_event);
}
},

View File

@@ -116,7 +116,7 @@ impl From<RoomEvent> for SyncRoomEvent {
// RoomEvent without the room_id. By converting the raw value in this
// way, we simply cause the `room_id` field in the json to be ignored by
// a subsequent deserialization.
Self { encryption_info: o.encryption_info, event: Raw::from_json(o.event.into_json()) }
Self { encryption_info: o.encryption_info, event: o.event.cast() }
}
}

View File

@@ -370,10 +370,7 @@ impl BackupMachine {
.or_default()
.insert(session_id.clone());
let session = Raw::from_json(
serde_json::value::to_raw_value(&session)
.expect("Can't serialize a backed up room key"),
);
let session = Raw::new(&session).expect("Can't serialize a backed up room key");
backup
.entry(room_id)

View File

@@ -47,7 +47,7 @@ use ruma::{
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, RoomId, TransactionId,
UInt, UserId,
};
use serde_json::{value::to_raw_value, Value};
use serde_json::Value;
use tracing::{debug, error, info, trace, warn};
#[cfg(feature = "backups_v1")]
@@ -528,12 +528,12 @@ impl OlmMachine {
async fn keys_for_upload(&self) -> Option<upload_keys::v3::Request> {
let (device_keys, one_time_keys) = self.account.keys_for_upload().await?;
let device_keys = device_keys
.map(|d| Raw::from_json(to_raw_value(&d).expect("Coulnd't serialize device keys")));
let device_keys =
device_keys.map(|d| Raw::new(&d).expect("Coulnd't serialize device keys"));
Some(
assign!(upload_keys::v3::Request::new(), { device_keys, one_time_keys, fallback_keys: BTreeMap::new(), }),
)
Some(assign!(upload_keys::v3::Request::new(), {
device_keys, one_time_keys, fallback_keys: BTreeMap::new(),
}))
}
/// Decrypt a to-device event.

View File

@@ -46,11 +46,7 @@ use ruma::{
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, RoomId, UInt, UserId,
};
use serde::{Deserialize, Serialize};
use serde_json::{
json,
value::{to_raw_value, RawValue as RawJsonValue},
Value,
};
use serde_json::{json, value::RawValue as RawJsonValue, Value};
use sha2::{Digest, Sha256};
use tracing::{debug, info, trace, warn};
@@ -905,10 +901,8 @@ impl ReadOnlyAccount {
DeviceKeyAlgorithm::SignedCurve25519,
key_id.as_str().into(),
),
Raw::from_json(
to_raw_value(&OneTimeKey::SignedKey(signed_key))
.expect("Couldn't serialize a new signed key"),
),
Raw::new(&OneTimeKey::SignedKey(signed_key))
.expect("Couldn't serialize a new signed key"),
);
}

View File

@@ -303,17 +303,11 @@ impl Client {
/// # anyhow::Result::<()>::Ok(()) });
#[cfg(feature = "encryption")]
pub async fn bootstrap_cross_signing(&self, auth_data: Option<AuthData<'_>>) -> Result<()> {
use serde_json::value::to_raw_value;
let olm = self.olm_machine().await.ok_or(Error::AuthenticationRequired)?;
let (request, signature_request) = olm.bootstrap_cross_signing(false).await?;
let to_raw = |k| {
Raw::from_json(
to_raw_value(&k).expect("Can't serialize newly created cross signing keys"),
)
};
let to_raw = |k| Raw::new(&k).expect("Can't serialize newly created cross signing keys");
let request = assign!(UploadSigningKeysRequest::new(), {
auth: auth_data,

View File

@@ -546,7 +546,7 @@ impl Joined {
room_id = self.room_id().as_str(),
"Sending plaintext event to room because we don't have encryption support.",
);
serde_json::value::to_raw_value(&content)?
Raw::new(&content)?.cast()
};
#[cfg(feature = "encryption")]
@@ -567,8 +567,8 @@ impl Joined {
let encrypted_content =
olm.encrypt_raw(self.inner.room_id(), content, event_type).await?;
let raw_content = serde_json::value::to_raw_value(&encrypted_content)
.expect("Failed to serialize encrypted event");
let raw_content =
Raw::new(&encrypted_content).expect("Failed to serialize encrypted event").cast();
(raw_content, "m.room.encrypted")
} else {
@@ -577,14 +577,14 @@ impl Joined {
"Sending plaintext event because the room is NOT encrypted.",
);
(serde_json::value::to_raw_value(&content)?, event_type)
(Raw::new(&content)?.cast(), event_type)
};
let request = send_message_event::v3::Request::new_raw(
self.inner.room_id(),
&txn_id,
event_type,
Raw::from_json(content),
content,
);
let response = self.client.send(request, None).await?;
@@ -854,7 +854,7 @@ impl Joined {
event_type: &str,
state_key: &str,
) -> Result<send_state_event::v3::Response> {
let content = Raw::from_json(serde_json::value::to_raw_value(&content)?);
let content = Raw::new(&content)?.cast();
let request = send_state_event::v3::Request::new_raw(
self.inner.room_id(),
event_type,