events: Fix deserialization of AnyGlobalAccountDataEvent for variants with type fragment

This commit is contained in:
Kévin Commaille 2024-06-19 19:48:55 +02:00 committed by Kévin Commaille
parent ceba4586d6
commit 00a17dbe9d
3 changed files with 45 additions and 5 deletions

View File

@ -1,5 +1,10 @@
# [unreleased] # [unreleased]
Bug fixes:
- Fix deserialization of `AnyGlobalAccountDataEvent` for variants with a type
fragment.
Improvements: Improvements:
- Add support for encrypted stickers as sent by several bridges under the flag `compat-encrypted-stickers` - Add support for encrypted stickers as sent by several bridges under the flag `compat-encrypted-stickers`

View File

@ -182,7 +182,7 @@ mod tests {
PassPhrase, SecretStorageEncryptionAlgorithm, SecretStorageKeyEventContent, PassPhrase, SecretStorageEncryptionAlgorithm, SecretStorageKeyEventContent,
SecretStorageV1AesHmacSha2Properties, SecretStorageV1AesHmacSha2Properties,
}; };
use crate::{EventContentFromType, GlobalAccountDataEvent}; use crate::{AnyGlobalAccountDataEvent, EventContentFromType, GlobalAccountDataEvent};
#[test] #[test]
fn key_description_serialization() { fn key_description_serialization() {
@ -326,7 +326,7 @@ mod tests {
} }
#[test] #[test]
fn event_serialization() { fn event_content_serialization() {
let mut content = SecretStorageKeyEventContent::new( let mut content = SecretStorageKeyEventContent::new(
"my_key_id".into(), "my_key_id".into(),
SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties { SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
@ -346,6 +346,31 @@ mod tests {
assert_eq!(to_json_value(&content).unwrap(), json); assert_eq!(to_json_value(&content).unwrap(), json);
} }
#[test]
fn event_serialization() {
let mut content = SecretStorageKeyEventContent::new(
"my_key_id".into(),
SecretStorageEncryptionAlgorithm::V1AesHmacSha2(SecretStorageV1AesHmacSha2Properties {
iv: Some(Base64::parse("YWJjZGVmZ2hpamtsbW5vcA").unwrap()),
mac: Some(Base64::parse("aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U").unwrap()),
}),
);
content.name = Some("my_key".to_owned());
let event = GlobalAccountDataEvent { content };
let json = json!({
"type": "m.secret_storage.key.my_key_id",
"content": {
"name": "my_key",
"algorithm": "m.secret_storage.v1.aes-hmac-sha2",
"iv": "YWJjZGVmZ2hpamtsbW5vcA",
"mac": "aWRvbnRrbm93d2hhdGFtYWNsb29rc2xpa2U"
}
});
assert_eq!(to_json_value(&event).unwrap(), json);
}
#[test] #[test]
fn event_deserialization() { fn event_deserialization() {
let json = json!({ let json = json!({
@ -358,8 +383,8 @@ mod tests {
} }
}); });
let ev = let any_ev = from_json_value::<AnyGlobalAccountDataEvent>(json).unwrap();
from_json_value::<GlobalAccountDataEvent<SecretStorageKeyEventContent>>(json).unwrap(); assert_matches!(any_ev, AnyGlobalAccountDataEvent::SecretStorageKey(ev));
assert_eq!(ev.content.key_id, "my_key_id"); assert_eq!(ev.content.key_id, "my_key_id");
assert_eq!(ev.content.name.unwrap(), "my_key"); assert_eq!(ev.content.name.unwrap(), "my_key");
assert_matches!(ev.content.passphrase, None); assert_matches!(ev.content.passphrase, None);

View File

@ -170,7 +170,17 @@ fn expand_deserialize_impl(
}; };
let self_variant = variant.ctor(quote! { Self }); let self_variant = variant.ctor(quote! { Self });
let content = event.to_event_path(kind, var); let content = event.to_event_path(kind, var);
let ev_types = event.aliases.iter().chain([&event.ev_type]); let ev_types = event.aliases.iter().chain([&event.ev_type]).map(|ev_type| {
if event.has_type_fragment() {
let ev_type = ev_type.value();
let prefix = ev_type
.strip_suffix('*')
.expect("event type with type fragment must end with *");
quote! { t if t.starts_with(#prefix) }
} else {
quote! { #ev_type }
}
});
Ok(quote! { Ok(quote! {
#variant_attrs #(#ev_types)|* => { #variant_attrs #(#ev_types)|* => {