From 90fe9edc3234c7fa0b4dcaf9169f13c4f6521b56 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 31 Jan 2022 19:30:34 +0100 Subject: [PATCH] events: Remove questionable trait implementations They weren't really useful for user code. --- crates/ruma-events/src/enums.rs | 51 +++++++++++++++++++----- crates/ruma-events/src/room/encrypted.rs | 34 +--------------- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/crates/ruma-events/src/enums.rs b/crates/ruma-events/src/enums.rs index 02e7195a..67d8fd5c 100644 --- a/crates/ruma-events/src/enums.rs +++ b/crates/ruma-events/src/enums.rs @@ -5,7 +5,9 @@ use serde::{de, Deserialize, Serialize}; use serde_json::value::RawValue as RawJsonValue; use crate::{ - from_raw_json_value, room::redaction::SyncRoomRedactionEvent, Redact, UnsignedDeHelper, + from_raw_json_value, + room::{encrypted, message, redaction::SyncRoomRedactionEvent}, + Redact, UnsignedDeHelper, }; event_enum! { @@ -328,13 +330,20 @@ impl AnyMessageEventContent { /// /// This is a helper function intended for encryption. There should not be a reason to access /// `m.relates_to` without first destructuring an `AnyMessageEventContent` otherwise. - pub fn relation(&self) -> Option { + pub fn relation(&self) -> Option { #[cfg(feature = "unstable-pre-spec")] - use crate::key::verification::{ - accept::KeyVerificationAcceptEventContent, cancel::KeyVerificationCancelEventContent, - done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent, - mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent, - start::KeyVerificationStartEventContent, + use crate::{ + key::{ + self, + verification::{ + accept::KeyVerificationAcceptEventContent, + cancel::KeyVerificationCancelEventContent, + done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent, + mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent, + start::KeyVerificationStartEventContent, + }, + }, + reaction, }; match self { @@ -347,12 +356,32 @@ impl AnyMessageEventContent { | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. }) | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. }) | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => { - Some(relates_to.clone().into()) - }, + let key::verification::Relation { event_id } = relates_to; + Some(encrypted::Relation::Reference(encrypted::Reference { + event_id: event_id.clone(), + })) + } #[cfg(feature = "unstable-pre-spec")] - Self::Reaction(ev) => Some(ev.relates_to.clone().into()), + Self::Reaction(ev) => { + let reaction::Relation { event_id, emoji } = &ev.relates_to; + Some(encrypted::Relation::Annotation(encrypted::Annotation { + event_id: event_id.clone(), + key: emoji.clone(), + })) + } Self::RoomEncrypted(ev) => ev.relates_to.clone(), - Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into), + Self::RoomMessage(ev) => ev.relates_to.clone().map(|rel| match rel { + message::Relation::Reply { in_reply_to } => { + encrypted::Relation::Reply { in_reply_to } + } + #[cfg(feature = "unstable-pre-spec")] + message::Relation::Replacement(re) => { + encrypted::Relation::Replacement(encrypted::Replacement { + event_id: re.event_id, + }) + } + message::Relation::_Custom => encrypted::Relation::_Custom, + }), Self::CallAnswer(_) | Self::CallInvite(_) | Self::CallHangup(_) diff --git a/crates/ruma-events/src/room/encrypted.rs b/crates/ruma-events/src/room/encrypted.rs index 1a912df6..c719f087 100644 --- a/crates/ruma-events/src/room/encrypted.rs +++ b/crates/ruma-events/src/room/encrypted.rs @@ -11,9 +11,7 @@ use ruma_identifiers::DeviceId; use ruma_identifiers::EventId; use serde::{Deserialize, Serialize}; -use crate::room::message::{self, InReplyTo}; -#[cfg(feature = "unstable-pre-spec")] -use crate::{key::verification, reaction}; +use crate::room::message::InReplyTo; mod relation_serde; @@ -248,36 +246,6 @@ impl From for MegolmV1AesSha2Content { } } -// FIXME: Remove on next breaking change release -impl From for Relation { - fn from(rel: message::Relation) -> Self { - match rel { - message::Relation::Reply { in_reply_to } => Self::Reply { in_reply_to }, - #[cfg(feature = "unstable-pre-spec")] - message::Relation::Replacement(re) => { - Self::Replacement(Replacement { event_id: re.event_id }) - } - message::Relation::_Custom => Self::_Custom, - } - } -} - -#[cfg(feature = "unstable-pre-spec")] -impl From for Relation { - fn from(rel: reaction::Relation) -> Self { - let reaction::Relation { event_id, emoji } = rel; - Self::Annotation(Annotation { event_id, key: emoji }) - } -} - -#[cfg(feature = "unstable-pre-spec")] -impl From for Relation { - fn from(rel: verification::Relation) -> Self { - let verification::Relation { event_id } = rel; - Self::Reference(Reference { event_id }) - } -} - #[cfg(test)] mod tests { use matches::assert_matches;