Skip to content

Commit b0f48c9

Browse files
committed
fix(crypto): Let users fetch our own device out of the store
1 parent 68a2020 commit b0f48c9

File tree

4 files changed

+32
-47
lines changed

4 files changed

+32
-47
lines changed

crates/matrix-sdk-crypto/src/identities/device.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,26 @@ impl UserDevices {
307307
})
308308
}
309309

310+
fn own_user_id(&self) -> &UserId {
311+
self.verification_machine.own_user_id()
312+
}
313+
314+
fn own_device_id(&self) -> &DeviceId {
315+
self.verification_machine.own_device_id()
316+
}
317+
310318
/// Returns true if there is at least one devices of this user that is
311319
/// considered to be verified, false otherwise.
320+
///
321+
/// This won't consider your own device as verified, as your own device is
322+
/// always implicitly verified.
312323
pub fn is_any_verified(&self) -> bool {
313-
self.inner.values().any(|d| d.verified(&self.own_identity, &self.device_owner_identity))
324+
self.inner
325+
.values()
326+
.filter(|d| {
327+
!(d.user_id() == self.own_user_id() && d.device_id() == self.own_device_id())
328+
})
329+
.any(|d| d.verified(&self.own_identity, &self.device_owner_identity))
314330
}
315331

316332
/// Iterator over all the device ids of the user devices.

crates/matrix-sdk-crypto/src/identities/manager.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ impl IdentityManager {
134134
store: Store,
135135
device_keys: DeviceKeys,
136136
) -> StoreResult<DeviceChange> {
137-
let old_device = store
138-
.get_readonly_device_unfiltered(&device_keys.user_id, &device_keys.device_id)
139-
.await?;
137+
let old_device =
138+
store.get_readonly_device(&device_keys.user_id, &device_keys.device_id).await?;
140139

141140
if let Some(mut device) = old_device {
142141
if let Err(e) = device.update_device(&device_keys) {

crates/matrix-sdk-crypto/src/session_manager/sessions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl SessionManager {
183183
// Add the list of devices that the user wishes to establish sessions
184184
// right now.
185185
for user_id in users {
186-
let user_devices = self.store.get_readonly_devices(user_id).await?;
186+
let user_devices = self.store.get_readonly_devices_filtered(user_id).await?;
187187

188188
for (device_id, device) in user_devices.into_iter() {
189189
if !device.algorithms().contains(&EventEncryptionAlgorithm::OlmV1Curve25519AesSha2)

crates/matrix-sdk-crypto/src/store/mod.rs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -295,36 +295,18 @@ impl Store {
295295
.and_then(|d| d.display_name().map(|d| d.to_string())))
296296
}
297297

298-
/// Get the read-only version of a specific device.
299-
///
300-
/// *Note*: This doesn't return our own device.
301298
pub async fn get_readonly_device(
302299
&self,
303300
user_id: &UserId,
304301
device_id: &DeviceId,
305-
) -> Result<Option<ReadOnlyDevice>> {
306-
if user_id == self.user_id() && device_id == self.device_id() {
307-
Ok(None)
308-
} else {
309-
self.inner.get_device(user_id, device_id).await
310-
}
311-
}
312-
313-
/// Get the read-only version of a specific device.
314-
///
315-
/// *Note*: This doesn't return our own device.
316-
pub async fn get_readonly_device_unfiltered(
317-
&self,
318-
user_id: &UserId,
319-
device_id: &DeviceId,
320302
) -> Result<Option<ReadOnlyDevice>> {
321303
self.inner.get_device(user_id, device_id).await
322304
}
323305

324306
/// Get the read-only version of all the devices that the given user has.
325307
///
326308
/// *Note*: This doesn't return our own device.
327-
pub async fn get_readonly_devices(
309+
pub async fn get_readonly_devices_filtered(
328310
&self,
329311
user_id: &UserId,
330312
) -> Result<HashMap<DeviceIdBox, ReadOnlyDevice>> {
@@ -362,11 +344,7 @@ impl Store {
362344
}
363345

364346
pub async fn get_user_devices(&self, user_id: &UserId) -> Result<UserDevices> {
365-
let mut devices = self.inner.get_user_devices(user_id).await?;
366-
367-
if user_id == self.user_id() {
368-
devices.remove(self.device_id());
369-
}
347+
let devices = self.inner.get_user_devices(user_id).await?;
370348

371349
let own_identity =
372350
self.inner.get_user_identity(&self.user_id).await?.map(|i| i.own().cloned()).flatten();
@@ -385,24 +363,16 @@ impl Store {
385363
user_id: &UserId,
386364
device_id: &DeviceId,
387365
) -> Result<Option<Device>> {
388-
if user_id == self.user_id() && device_id == self.device_id() {
389-
Ok(None)
390-
} else {
391-
let own_identity = self
392-
.inner
393-
.get_user_identity(&self.user_id)
394-
.await?
395-
.map(|i| i.own().cloned())
396-
.flatten();
397-
let device_owner_identity = self.inner.get_user_identity(user_id).await?;
398-
399-
Ok(self.inner.get_device(user_id, device_id).await?.map(|d| Device {
400-
inner: d,
401-
verification_machine: self.verification_machine.clone(),
402-
own_identity,
403-
device_owner_identity,
404-
}))
405-
}
366+
let own_identity =
367+
self.inner.get_user_identity(&self.user_id).await?.map(|i| i.own().cloned()).flatten();
368+
let device_owner_identity = self.inner.get_user_identity(user_id).await?;
369+
370+
Ok(self.inner.get_device(user_id, device_id).await?.map(|d| Device {
371+
inner: d,
372+
verification_machine: self.verification_machine.clone(),
373+
own_identity,
374+
device_owner_identity,
375+
}))
406376
}
407377

408378
pub async fn get_identity(&self, user_id: &UserId) -> Result<Option<UserIdentities>> {

0 commit comments

Comments
 (0)