diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index af2e2fba..37285a90 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -36,6 +36,7 @@ Breaking changes: * Move common relation structs under `events::room::encrypted` to `events::relation` and remove duplicate types * Remove `events::reaction::Relation` and use `events::relation::Annotation` instead + * Remove `events::key::verification::Relation` and use `events::relation::Reference` instead Improvements: diff --git a/crates/ruma-common/src/events/enums.rs b/crates/ruma-common/src/events/enums.rs index 1bf83e15..a2bcdf25 100644 --- a/crates/ruma-common/src/events/enums.rs +++ b/crates/ruma-common/src/events/enums.rs @@ -3,7 +3,6 @@ use serde::{de, Deserialize}; use serde_json::value::RawValue as RawJsonValue; use super::{ - key, room::{encrypted, redaction::SyncRoomRedactionEvent}, Redact, Relations, }; @@ -325,11 +324,8 @@ impl AnyMessageLikeEventContent { | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. }) | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. }) | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => { - let key::verification::Relation { event_id } = relates_to; - Some(encrypted::Relation::Reference(Reference { - event_id: event_id.clone(), - })) - } + Some(encrypted::Relation::Reference(relates_to.clone())) + }, #[cfg(feature = "unstable-msc2677")] Self::Reaction(ev) => Some(encrypted::Relation::Annotation(ev.relates_to.clone())), Self::RoomEncrypted(ev) => ev.relates_to.clone(), diff --git a/crates/ruma-common/src/events/key/verification.rs b/crates/ruma-common/src/events/key/verification.rs index 6283ef9a..16f62155 100644 --- a/crates/ruma-common/src/events/key/verification.rs +++ b/crates/ruma-common/src/events/key/verification.rs @@ -7,9 +7,7 @@ //! //! [MSC2241]: https://github.com/matrix-org/matrix-spec-proposals/pull/2241 -use serde::{Deserialize, Serialize}; - -use crate::{serde::StringEnum, OwnedEventId, PrivOwnedStr}; +use crate::{serde::StringEnum, PrivOwnedStr}; pub mod accept; pub mod cancel; @@ -86,22 +84,6 @@ pub enum ShortAuthenticationString { _Custom(PrivOwnedStr), } -/// A relation which associates an `m.key.verification.request` with another key verification event. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(tag = "rel_type", rename = "m.reference")] -pub struct Relation { - /// The event ID of a related `m.key.verification.request`. - pub event_id: OwnedEventId, -} - -impl Relation { - /// Creates a new `Relation` with the given event ID. - pub fn new(event_id: OwnedEventId) -> Self { - Self { event_id } - } -} - /// A Short Authentication String (SAS) verification method. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] diff --git a/crates/ruma-common/src/events/key/verification/accept.rs b/crates/ruma-common/src/events/key/verification/accept.rs index b3e2ddcb..764b5df9 100644 --- a/crates/ruma-common/src/events/key/verification/accept.rs +++ b/crates/ruma-common/src/events/key/verification/accept.rs @@ -9,10 +9,9 @@ use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; use super::{ - HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, Relation, - ShortAuthenticationString, + HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, }; -use crate::{serde::Base64, OwnedTransactionId}; +use crate::{events::relation::Reference, serde::Base64, OwnedTransactionId}; /// The content of a to-device `m.key.verification.accept` event. /// @@ -52,13 +51,13 @@ pub struct KeyVerificationAcceptEventContent { /// Information about the related event. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationAcceptEventContent { /// Creates a new `ToDeviceKeyVerificationAcceptEventContent` with the given method-specific - /// content and relation. - pub fn new(method: AcceptMethod, relates_to: Relation) -> Self { + /// content and reference. + pub fn new(method: AcceptMethod, relates_to: Reference) -> Self { Self { method, relates_to } } } @@ -176,7 +175,7 @@ mod tests { }; use crate::{ event_id, - events::{key::verification::Relation, ToDeviceEvent}, + events::{relation::Reference, ToDeviceEvent}, serde::Base64, user_id, }; @@ -248,7 +247,7 @@ mod tests { let event_id = event_id!("$1598361704261elfgc:localhost"); let key_verification_accept_content = KeyVerificationAcceptEventContent { - relates_to: Relation { event_id: event_id.to_owned() }, + relates_to: Reference { event_id: event_id.to_owned() }, method: AcceptMethod::SasV1(SasV1Content { hash: HashAlgorithm::Sha256, key_agreement_protocol: KeyAgreementProtocol::Curve25519, diff --git a/crates/ruma-common/src/events/key/verification/cancel.rs b/crates/ruma-common/src/events/key/verification/cancel.rs index cd2b241e..41b0f170 100644 --- a/crates/ruma-common/src/events/key/verification/cancel.rs +++ b/crates/ruma-common/src/events/key/verification/cancel.rs @@ -5,8 +5,7 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use super::Relation; -use crate::{serde::StringEnum, OwnedTransactionId, PrivOwnedStr}; +use crate::{events::relation::Reference, serde::StringEnum, OwnedTransactionId, PrivOwnedStr}; /// The content of a to-device `m.key.verification.cancel` event. /// @@ -52,12 +51,12 @@ pub struct KeyVerificationCancelEventContent { /// Information about the related event. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationCancelEventContent { - /// Creates a new `KeyVerificationCancelEventContent` with the given reason, code and relation. - pub fn new(reason: String, code: CancelCode, relates_to: Relation) -> Self { + /// Creates a new `KeyVerificationCancelEventContent` with the given reason, code and reference. + pub fn new(reason: String, code: CancelCode, relates_to: Reference) -> Self { Self { reason, code, relates_to } } } diff --git a/crates/ruma-common/src/events/key/verification/done.rs b/crates/ruma-common/src/events/key/verification/done.rs index 7cfa5f93..82bc9275 100644 --- a/crates/ruma-common/src/events/key/verification/done.rs +++ b/crates/ruma-common/src/events/key/verification/done.rs @@ -5,8 +5,7 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use super::Relation; -use crate::OwnedTransactionId; +use crate::{events::relation::Reference, OwnedTransactionId}; /// The content of a to-device `m.m.key.verification.done` event. /// @@ -37,12 +36,12 @@ impl ToDeviceKeyVerificationDoneEventContent { pub struct KeyVerificationDoneEventContent { /// Relation signaling which verification request this event is responding to. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationDoneEventContent { - /// Creates a new `KeyVerificationDoneEventContent` with the given relation. - pub fn new(relates_to: Relation) -> Self { + /// Creates a new `KeyVerificationDoneEventContent` with the given reference. + pub fn new(relates_to: Reference) -> Self { Self { relates_to } } } @@ -52,7 +51,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::KeyVerificationDoneEventContent; - use crate::{event_id, events::key::verification::Relation}; + use crate::{event_id, events::relation::Reference}; #[test] fn serialization() { @@ -65,7 +64,7 @@ mod tests { } }); - let content = KeyVerificationDoneEventContent { relates_to: Relation { event_id } }; + let content = KeyVerificationDoneEventContent { relates_to: Reference { event_id } }; assert_eq!(to_json_value(&content).unwrap(), json_data); } diff --git a/crates/ruma-common/src/events/key/verification/key.rs b/crates/ruma-common/src/events/key/verification/key.rs index e6bf2728..8fd2fa87 100644 --- a/crates/ruma-common/src/events/key/verification/key.rs +++ b/crates/ruma-common/src/events/key/verification/key.rs @@ -5,8 +5,7 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use super::Relation; -use crate::{serde::Base64, OwnedTransactionId}; +use crate::{events::relation::Reference, serde::Base64, OwnedTransactionId}; /// The content of a to-device `m.key.verification.key` event. /// @@ -44,12 +43,12 @@ pub struct KeyVerificationKeyEventContent { /// Information about the related event. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationKeyEventContent { - /// Creates a new `KeyVerificationKeyEventContent` with the given key and relation. - pub fn new(key: Base64, relates_to: Relation) -> Self { + /// Creates a new `KeyVerificationKeyEventContent` with the given key and reference. + pub fn new(key: Base64, relates_to: Reference) -> Self { Self { key, relates_to } } } diff --git a/crates/ruma-common/src/events/key/verification/mac.rs b/crates/ruma-common/src/events/key/verification/mac.rs index 0d2b283c..d48fe170 100644 --- a/crates/ruma-common/src/events/key/verification/mac.rs +++ b/crates/ruma-common/src/events/key/verification/mac.rs @@ -7,8 +7,7 @@ use std::collections::BTreeMap; use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use super::Relation; -use crate::{serde::Base64, OwnedTransactionId}; +use crate::{events::relation::Reference, serde::Base64, OwnedTransactionId}; /// The content of a to-device `m.key.verification.` event. /// @@ -62,13 +61,13 @@ pub struct KeyVerificationMacEventContent { /// Information about the related event. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationMacEventContent { /// Creates a new `KeyVerificationMacEventContent` with the given key ID to MAC map, key MAC and - /// relation. - pub fn new(mac: BTreeMap, keys: Base64, relates_to: Relation) -> Self { + /// reference. + pub fn new(mac: BTreeMap, keys: Base64, relates_to: Reference) -> Self { Self { mac, keys, relates_to } } } diff --git a/crates/ruma-common/src/events/key/verification/ready.rs b/crates/ruma-common/src/events/key/verification/ready.rs index 7c4b81cf..0430f79e 100644 --- a/crates/ruma-common/src/events/key/verification/ready.rs +++ b/crates/ruma-common/src/events/key/verification/ready.rs @@ -5,8 +5,8 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use super::{Relation, VerificationMethod}; -use crate::{OwnedDeviceId, OwnedTransactionId}; +use super::VerificationMethod; +use crate::{events::relation::Reference, OwnedDeviceId, OwnedTransactionId}; /// The content of a to-device `m.m.key.verification.ready` event. /// @@ -57,16 +57,16 @@ pub struct KeyVerificationReadyEventContent { /// Relation signaling which verification request this event is responding /// to. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationReadyEventContent { /// Creates a new `KeyVerificationReadyEventContent` with the given device ID, methods and - /// relation. + /// reference. pub fn new( from_device: OwnedDeviceId, methods: Vec, - relates_to: Relation, + relates_to: Reference, ) -> Self { Self { from_device, methods, relates_to } } @@ -79,7 +79,7 @@ mod tests { use super::{KeyVerificationReadyEventContent, ToDeviceKeyVerificationReadyEventContent}; use crate::{ event_id, - events::key::verification::{Relation, VerificationMethod}, + events::{key::verification::VerificationMethod, relation::Reference}, OwnedDeviceId, }; @@ -99,7 +99,7 @@ mod tests { let content = KeyVerificationReadyEventContent { from_device: device.clone(), - relates_to: Relation { event_id }, + relates_to: Reference { event_id }, methods: vec![VerificationMethod::SasV1], }; diff --git a/crates/ruma-common/src/events/key/verification/start.rs b/crates/ruma-common/src/events/key/verification/start.rs index a711619f..c5021744 100644 --- a/crates/ruma-common/src/events/key/verification/start.rs +++ b/crates/ruma-common/src/events/key/verification/start.rs @@ -9,10 +9,9 @@ use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; use super::{ - HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, Relation, - ShortAuthenticationString, + HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, }; -use crate::{serde::Base64, OwnedDeviceId, OwnedTransactionId}; +use crate::{events::relation::Reference, serde::Base64, OwnedDeviceId, OwnedTransactionId}; /// The content of a to-device `m.key.verification.start` event. /// @@ -64,13 +63,13 @@ pub struct KeyVerificationStartEventContent { /// Information about the related event. #[serde(rename = "m.relates_to")] - pub relates_to: Relation, + pub relates_to: Reference, } impl KeyVerificationStartEventContent { /// Creates a new `KeyVerificationStartEventContent` with the given device ID, method and - /// relation. - pub fn new(from_device: OwnedDeviceId, method: StartMethod, relates_to: Relation) -> Self { + /// reference. + pub fn new(from_device: OwnedDeviceId, method: StartMethod, relates_to: Reference) -> Self { Self { from_device, method, relates_to } } } @@ -218,7 +217,7 @@ mod tests { }; use crate::{ event_id, - events::{key::verification::Relation, ToDeviceEvent}, + events::{relation::Reference, ToDeviceEvent}, serde::Base64, user_id, }; @@ -315,7 +314,7 @@ mod tests { let key_verification_start_content = KeyVerificationStartEventContent { from_device: "123".into(), - relates_to: Relation { event_id: event_id.to_owned() }, + relates_to: Reference { event_id: event_id.to_owned() }, method: StartMethod::SasV1( SasV1ContentInit { hashes: vec![HashAlgorithm::Sha256], @@ -346,7 +345,7 @@ mod tests { let key_verification_start_content = KeyVerificationStartEventContent { from_device: "123".into(), - relates_to: Relation { event_id: event_id.to_owned() }, + relates_to: Reference { event_id: event_id.to_owned() }, method: StartMethod::ReciprocateV1(ReciprocateV1Content::new(secret.clone())), }; diff --git a/crates/ruma-common/src/events/relation.rs b/crates/ruma-common/src/events/relation.rs index 52ae83bb..4025710d 100644 --- a/crates/ruma-common/src/events/relation.rs +++ b/crates/ruma-common/src/events/relation.rs @@ -239,6 +239,7 @@ impl BundledThread { /// [reference]: https://spec.matrix.org/v1.5/client-server-api/#reference-relations #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[serde(tag = "rel_type", rename = "m.reference")] pub struct Reference { /// The ID of the event being referenced. pub event_id: OwnedEventId,