From 61671f4440221e1c34ab566b1035d908c9740491 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 20 Jun 2021 19:04:33 +0200 Subject: [PATCH] events: Add event relation conversions --- crates/ruma-events/CHANGELOG.md | 2 ++ crates/ruma-events/src/room/encrypted.rs | 33 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index 9c053d48..997a91a7 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -13,6 +13,8 @@ Breaking changes: * Rename `relation` field in some events to `relates_to` * All events that support relations now have their own `Relation` types (the `room::relationships` module has been removed) + * The `room::encryption` relation type can represent any kind of relation and has `From` + implementations so any other relation can be converted to it Improvements: diff --git a/crates/ruma-events/src/room/encrypted.rs b/crates/ruma-events/src/room/encrypted.rs index f2675373..1dcf6d5a 100644 --- a/crates/ruma-events/src/room/encrypted.rs +++ b/crates/ruma-events/src/room/encrypted.rs @@ -10,8 +10,11 @@ use ruma_identifiers::EventId; use serde::{Deserialize, Serialize}; #[cfg(feature = "unstable-pre-spec")] -use crate::room::message::Replacement; -use crate::{room::message::InReplyTo, MessageEvent}; +use crate::{key::verification, reaction, room::message::Replacement}; +use crate::{ + room::message::{self, InReplyTo}, + MessageEvent, +}; mod relation_serde; @@ -218,6 +221,32 @@ impl From for MegolmV1AesSha2Content { } } +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(re), + } + } +} + +#[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;