Skip to content

Commit 6d2dd06

Browse files
committed
data-model: make the device emit both stable and unstable scope tokens
1 parent 8658e8d commit 6d2dd06

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

crates/data-model/src/compat/device.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use serde::{Deserialize, Serialize};
1313
use thiserror::Error;
1414

1515
static GENERATED_DEVICE_ID_LENGTH: usize = 10;
16-
static DEVICE_SCOPE_PREFIX: &str = "urn:matrix:org.matrix.msc2967.client:device:";
16+
static UNSTABLE_DEVICE_SCOPE_PREFIX: &str = "urn:matrix:org.matrix.msc2967.client:device:";
17+
static STABLE_DEVICE_SCOPE_PREFIX: &str = "urn:matrix:client:device:";
1718

1819
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1920
#[serde(transparent)]
@@ -28,24 +29,31 @@ pub enum ToScopeTokenError {
2829
}
2930

3031
impl Device {
31-
/// Get the corresponding [`ScopeToken`] for that device
32+
/// Get the corresponding stable and unstable [`ScopeToken`] for that device
3233
///
3334
/// # Errors
3435
///
3536
/// Returns an error if the device ID contains characters that can't be
3637
/// encoded in a scope
37-
pub fn to_scope_token(&self) -> Result<ScopeToken, ToScopeTokenError> {
38-
format!("{DEVICE_SCOPE_PREFIX}{}", self.id)
39-
.parse()
40-
.map_err(|_| ToScopeTokenError::InvalidCharacters)
38+
pub fn to_scope_token(&self) -> Result<[ScopeToken; 2], ToScopeTokenError> {
39+
Ok([
40+
format!("{STABLE_DEVICE_SCOPE_PREFIX}{}", self.id)
41+
.parse()
42+
.map_err(|_| ToScopeTokenError::InvalidCharacters)?,
43+
format!("{UNSTABLE_DEVICE_SCOPE_PREFIX}{}", self.id)
44+
.parse()
45+
.map_err(|_| ToScopeTokenError::InvalidCharacters)?,
46+
])
4147
}
4248

4349
/// Get the corresponding [`Device`] from a [`ScopeToken`]
4450
///
4551
/// Returns `None` if the [`ScopeToken`] is not a device scope
4652
#[must_use]
4753
pub fn from_scope_token(token: &ScopeToken) -> Option<Self> {
48-
let id = token.as_str().strip_prefix(DEVICE_SCOPE_PREFIX)?;
54+
let stable = token.as_str().strip_prefix(STABLE_DEVICE_SCOPE_PREFIX);
55+
let unstable = token.as_str().strip_prefix(UNSTABLE_DEVICE_SCOPE_PREFIX);
56+
let id = stable.or(unstable)?;
4957
Some(Device::from(id.to_owned()))
5058
}
5159

@@ -89,12 +97,23 @@ mod test {
8997
#[test]
9098
fn test_device_id_to_from_scope_token() {
9199
let device = Device::from("AABBCCDDEE".to_owned());
92-
let scope_token = device.to_scope_token().unwrap();
100+
let [stable_scope_token, unstable_scope_token] = device.to_scope_token().unwrap();
93101
assert_eq!(
94-
scope_token.as_str(),
102+
stable_scope_token.as_str(),
103+
"urn:matrix:client:device:AABBCCDDEE"
104+
);
105+
assert_eq!(
106+
unstable_scope_token.as_str(),
95107
"urn:matrix:org.matrix.msc2967.client:device:AABBCCDDEE"
96108
);
97-
assert_eq!(Device::from_scope_token(&scope_token), Some(device));
109+
assert_eq!(
110+
Device::from_scope_token(&unstable_scope_token).as_ref(),
111+
Some(&device)
112+
);
113+
assert_eq!(
114+
Device::from_scope_token(&stable_scope_token).as_ref(),
115+
Some(&device)
116+
);
98117
assert_eq!(Device::from_scope_token(&OPENID), None);
99118
}
100119
}

0 commit comments

Comments
 (0)