Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
uhoreg committed Jun 11, 2024
1 parent 304bcf3 commit 2e0d21b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions crates/matrix-sdk-crypto/src/identities/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ impl ReadOnlyDevice {
}
}

/// Return the device keys
pub fn as_device_keys(&self) -> &DeviceKeys {
&self.inner
}
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk-crypto/src/olm/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,14 +1299,14 @@ impl Account {
}

// We didn't find a matching session; try to create a new session.
// try to get our own stored device keys
// Try to get our own stored device keys.
let device_keys = store
.get_device(&self.user_id, &self.device_id)
.await
.unwrap_or(None)
.map(|read_only_device| read_only_device.as_device_keys().clone());
// if we don't have it stored, fall back to generating a fresh
// device keys from our own Account
// If we don't have our device keys stored, fall back to
// generating a fresh device keys from our own Account.
let device_keys = match device_keys {
Some(device_keys) => device_keys,
None => self.device_keys(),
Expand Down
3 changes: 1 addition & 2 deletions crates/matrix-sdk-crypto/src/olm/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ mod tests {
);

let alice_device = ReadOnlyDevice::from_account(&alice);
// let bob_device = ReadOnlyDevice::from_account(&bob);

let message = alice_session
.encrypt(&alice_device, "m.dummy", ToDeviceDummyEventContent::new(), None)
Expand Down Expand Up @@ -338,7 +337,7 @@ mod tests {
)
.unwrap();

// check that the payload has the device keys
// Check that the encrypted payload has the device keys.
let plaintext: Value = serde_json::from_str(&bob_session_result.plaintext).unwrap();
assert_eq!(plaintext["device_keys"]["user_id"].as_str(), Some("@alice:localhost"));
}
Expand Down
7 changes: 5 additions & 2 deletions crates/matrix-sdk-crypto/src/session_manager/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ impl SessionManager {
let mut new_sessions: BTreeMap<&UserId, BTreeMap<&DeviceId, SessionInfo>> = BTreeMap::new();
let mut store_transaction = self.store.transaction().await;

// Cache of our own device keys, so we only need to fetch it once, if
// we need it at all.
let mut our_device_keys: Option<DeviceKeys> = None;

for (user_id, user_devices) in &response.one_time_keys {
Expand Down Expand Up @@ -542,14 +544,15 @@ impl SessionManager {
let device_keys = match our_device_keys {
Some(ref device_keys) => device_keys.clone(),
None => {
// Try to get our own stored device keys.
let device_keys = self
.store
.get_device(&account.user_id, &account.device_id)
.await
.unwrap_or(None)
.map(|read_only_device| read_only_device.as_device_keys().clone());
// if we don't have it stored, fall back to generating a fresh
// device keys from our own Account
// If we don't have it stored, fall back to generating a fresh
// device keys from our own Account.
let device_keys = match device_keys {
Some(device_keys) => device_keys,
None => account.device_keys(),
Expand Down
12 changes: 6 additions & 6 deletions crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ impl IndexeddbCryptoStore {
let account_info = self.get_static_account();
let mut clear_caches = false;
for device in device_changes.new.iter().chain(&device_changes.changed) {
// if our own device key changes, we need to clear the
// session cache because the sessions contain a copy of our
// device key
// If our own device key changes, we need to clear the session
// cache because the sessions contain a copy of our device key, and
// we want the sessions to use the new version.
if account_info.clone().is_some_and(|info| {
info.user_id == device.user_id() && info.device_id == device.device_id()
}) {
Expand Down Expand Up @@ -879,14 +879,14 @@ impl_crypto_store! {
async fn get_sessions(&self, sender_key: &str) -> Result<Option<Arc<Mutex<Vec<Session>>>>> {
let account_info = self.get_static_account().ok_or(CryptoStoreError::AccountUnset)?;
if self.session_cache.get(sender_key).is_none() {
// try to get our own stored device keys
// Try to get our own stored device keys.
let device_keys = self.get_device(&account_info.user_id, &account_info.device_id)
.await
.unwrap_or(None)
.map(|read_only_device| read_only_device.as_device_keys().clone());

// if we don't have it stored, fall back to generating a fresh
// device keys from our own Account
// If we don't have it stored, fall back to generating a fresh
// device keys from our own Account.
let device_keys = match device_keys {
Some(device_keys) => device_keys,
None => {
Expand Down
10 changes: 5 additions & 5 deletions crates/matrix-sdk-sqlite/src/crypto_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,9 @@ impl CryptoStore for SqliteCryptoStore {

let account_info = this.get_static_account();
for device in changes.devices.new.iter().chain(&changes.devices.changed) {
// if our own device key changes, we need to clear the
// If our own device key changes, we need to clear the
// session cache because the sessions contain a copy of our
// device key
// device key.
if account_info.clone().is_some_and(|info| {
info.user_id == device.user_id() && info.device_id == device.device_id()
}) {
Expand Down Expand Up @@ -923,15 +923,15 @@ impl CryptoStore for SqliteCryptoStore {
let account_info = self.get_static_account().ok_or(Error::AccountUnset)?;

if self.session_cache.get(sender_key).is_none() {
// try to get our own stored device keys
// Try to get our own stored device keys.
let device_keys = self
.get_device(&account_info.user_id, &account_info.device_id)
.await
.unwrap_or(None)
.map(|read_only_device| read_only_device.as_device_keys().clone());

// if we don't have it stored, fall back to generating a fresh
// device keys from our own Account
// If we don't have it stored, fall back to generating a fresh
// device keys from our own Account.
let device_keys = match device_keys {
Some(device_keys) => device_keys,
None => {
Expand Down

0 comments on commit 2e0d21b

Please sign in to comment.