Skip to content

Commit e3105bf

Browse files
committed
crypto: define new WithheldCode for MSC4268
1 parent 5a5b8af commit e3105bf

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

crates/matrix-sdk-common/src/deserialized_responses.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,13 @@ pub enum WithheldCode {
12001200
#[ruma_enum(rename = "m.no_olm")]
12011201
NoOlm,
12021202

1203+
/// Normally used when sharing history, per [MSC4268]: indicates
1204+
/// that the session was not marked as "shared_history".
1205+
///
1206+
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
1207+
#[ruma_enum(rename = "io.element.msc4268.history_not_shared", alias = "m.history_not_shared")]
1208+
HistoryNotShared,
1209+
12031210
#[doc(hidden)]
12041211
_Custom(PrivOwnedStr),
12051212
}
@@ -1212,6 +1219,7 @@ impl fmt::Display for WithheldCode {
12121219
WithheldCode::Unauthorised => "You are not authorised to read the message.",
12131220
WithheldCode::Unavailable => "The requested key was not found.",
12141221
WithheldCode::NoOlm => "Unable to establish a secure channel.",
1222+
WithheldCode::HistoryNotShared => "The sender disabled sharing encrypted history.",
12151223
_ => self.as_str(),
12161224
};
12171225

crates/matrix-sdk-crypto/src/types/events/room_key_withheld.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ macro_rules! construct_withheld_content {
6565
WithheldCode::Blacklisted
6666
| WithheldCode::Unverified
6767
| WithheldCode::Unauthorised
68-
| WithheldCode::Unavailable => {
68+
| WithheldCode::Unavailable
69+
| WithheldCode::HistoryNotShared => {
6970
let content = CommonWithheldCodeContent {
7071
$room_id,
7172
$session_id,
@@ -84,7 +85,9 @@ macro_rules! construct_withheld_content {
8485
.into(),
8586
))
8687
}
87-
_ => unreachable!("Can't create an unknown withheld code content"),
88+
WithheldCode::_Custom(_) => {
89+
unreachable!("Can't create an unknown withheld code content")
90+
}
8891
}
8992
};
9093
}
@@ -180,6 +183,9 @@ pub enum MegolmV1AesSha2WithheldContent {
180183
Unauthorised(Box<CommonWithheldCodeContent>),
181184
/// The `m.unavailable` variant of the withheld code content.
182185
Unavailable(Box<CommonWithheldCodeContent>),
186+
/// The `m.history_not_shared` variant of the withheld code content (cf
187+
/// [MSC4268](https://github.com/matrix-org/matrix-spec-proposals/pull/4268)).
188+
HistoryNotShared(Box<CommonWithheldCodeContent>),
183189
/// The `m.no_olm` variant of the withheld code content.
184190
NoOlm(Box<NoOlmWithheldContent>),
185191
}
@@ -231,7 +237,10 @@ impl MegolmV1AesSha2WithheldContent {
231237
MegolmV1AesSha2WithheldContent::BlackListed(content)
232238
| MegolmV1AesSha2WithheldContent::Unverified(content)
233239
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
234-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => Some(&content.session_id),
240+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
241+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => {
242+
Some(&content.session_id)
243+
}
235244
MegolmV1AesSha2WithheldContent::NoOlm(_) => None,
236245
}
237246
}
@@ -242,7 +251,8 @@ impl MegolmV1AesSha2WithheldContent {
242251
MegolmV1AesSha2WithheldContent::BlackListed(content)
243252
| MegolmV1AesSha2WithheldContent::Unverified(content)
244253
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
245-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => Some(&content.room_id),
254+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
255+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => Some(&content.room_id),
246256
MegolmV1AesSha2WithheldContent::NoOlm(_) => None,
247257
}
248258
}
@@ -254,6 +264,7 @@ impl MegolmV1AesSha2WithheldContent {
254264
MegolmV1AesSha2WithheldContent::Unverified(_) => WithheldCode::Unverified,
255265
MegolmV1AesSha2WithheldContent::Unauthorised(_) => WithheldCode::Unauthorised,
256266
MegolmV1AesSha2WithheldContent::Unavailable(_) => WithheldCode::Unavailable,
267+
MegolmV1AesSha2WithheldContent::HistoryNotShared(_) => WithheldCode::HistoryNotShared,
257268
MegolmV1AesSha2WithheldContent::NoOlm(_) => WithheldCode::NoOlm,
258269
}
259270
}
@@ -266,7 +277,10 @@ impl MegolmV1AesSha2WithheldContent {
266277
WithheldCode::Unverified => Self::Unverified(content),
267278
WithheldCode::Unauthorised => Self::Unauthorised(content),
268279
WithheldCode::Unavailable => Self::Unavailable(content),
269-
_ => unreachable!("This constructor requires one of the common withheld codes"),
280+
WithheldCode::HistoryNotShared => Self::HistoryNotShared(content),
281+
WithheldCode::NoOlm | WithheldCode::_Custom(_) => {
282+
unreachable!("This constructor requires one of the common withheld codes")
283+
}
270284
}
271285
}
272286
}
@@ -349,14 +363,15 @@ impl TryFrom<WithheldHelper> for RoomKeyWithheldContent {
349363
WithheldCode::Blacklisted
350364
| WithheldCode::Unverified
351365
| WithheldCode::Unauthorised
352-
| WithheldCode::Unavailable => {
366+
| WithheldCode::Unavailable
367+
| WithheldCode::HistoryNotShared => {
353368
let content: CommonWithheldCodeContent = serde_json::from_value(value.other)?;
354369

355370
Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::from_code_and_content(
356371
value.code, content,
357372
))
358373
}
359-
_ => unknown(value)?,
374+
WithheldCode::_Custom(_) => unknown(value)?,
360375
},
361376
#[cfg(feature = "experimental-algorithms")]
362377
EventEncryptionAlgorithm::MegolmV2AesSha2 => match value.code {
@@ -367,14 +382,15 @@ impl TryFrom<WithheldHelper> for RoomKeyWithheldContent {
367382
WithheldCode::Blacklisted
368383
| WithheldCode::Unverified
369384
| WithheldCode::Unauthorised
370-
| WithheldCode::Unavailable => {
385+
| WithheldCode::Unavailable
386+
| WithheldCode::HistoryNotShared => {
371387
let content: CommonWithheldCodeContent = serde_json::from_value(value.other)?;
372388

373389
Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::from_code_and_content(
374390
value.code, content,
375391
))
376392
}
377-
_ => unknown(value)?,
393+
WithheldCode::_Custom(_) => unknown(value)?,
378394
},
379395
_ => unknown(value)?,
380396
})
@@ -397,7 +413,8 @@ impl Serialize for RoomKeyWithheldContent {
397413
MegolmV1AesSha2WithheldContent::BlackListed(content)
398414
| MegolmV1AesSha2WithheldContent::Unverified(content)
399415
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
400-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => WithheldHelper {
416+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
417+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => WithheldHelper {
401418
algorithm,
402419
code,
403420
reason,
@@ -420,7 +437,8 @@ impl Serialize for RoomKeyWithheldContent {
420437
MegolmV1AesSha2WithheldContent::BlackListed(content)
421438
| MegolmV1AesSha2WithheldContent::Unverified(content)
422439
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
423-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => WithheldHelper {
440+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
441+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => WithheldHelper {
424442
algorithm,
425443
code,
426444
reason,
@@ -534,6 +552,7 @@ pub(super) mod tests {
534552
WithheldCode::Blacklisted,
535553
WithheldCode::Unauthorised,
536554
WithheldCode::Unavailable,
555+
WithheldCode::HistoryNotShared,
537556
];
538557
for code in codes {
539558
let json = json(&code);

crates/matrix-sdk-crypto/src/types/events/utd_cause.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl UtdCause {
150150
WithheldCode::Blacklisted
151151
| WithheldCode::Unauthorised
152152
| WithheldCode::Unavailable
153+
| WithheldCode::HistoryNotShared
153154
| WithheldCode::NoOlm
154155
| WithheldCode::_Custom(_) => UtdCause::WithheldBySender,
155156
}

0 commit comments

Comments
 (0)