From fa61cc12483c44fce741ba824ca24bf3492a82aa Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 16 Sep 2022 11:34:54 +0200 Subject: [PATCH] events: Add relations accessors to event enums --- crates/ruma-common/src/events/enums.rs | 8 +++++++- crates/ruma-macros/src/events/event_enum.rs | 20 ++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/crates/ruma-common/src/events/enums.rs b/crates/ruma-common/src/events/enums.rs index 67ac32a7..d27c9414 100644 --- a/crates/ruma-common/src/events/enums.rs +++ b/crates/ruma-common/src/events/enums.rs @@ -5,7 +5,7 @@ use serde_json::value::RawValue as RawJsonValue; use super::{ key, room::{encrypted, redaction::SyncRoomRedactionEvent}, - Redact, + Redact, Relations, }; use crate::{ serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, @@ -180,6 +180,9 @@ impl AnyTimelineEvent { /// Returns this event's `transaction_id` from inside `unsigned`, if there is one. pub fn transaction_id(&self) -> Option<&TransactionId>; + + /// Returns this event's `relations` from inside `unsigned`, if that field exists. + pub fn relations(&self) -> Option<&Relations>; } } @@ -209,6 +212,9 @@ impl AnySyncTimelineEvent { /// Returns this event's `transaction_id` from inside `unsigned`, if there is one. pub fn transaction_id(&self) -> Option<&TransactionId>; + + /// Returns this event's `relations` from inside `unsigned`, if that field exists. + pub fn relations(&self) -> Option<&Relations>; } /// Converts `self` to an `AnyTimelineEvent` by adding the given a room ID. diff --git a/crates/ruma-macros/src/events/event_enum.rs b/crates/ruma-macros/src/events/event_enum.rs index 20c3e82f..7240efcd 100644 --- a/crates/ruma-macros/src/events/event_enum.rs +++ b/crates/ruma-macros/src/events/event_enum.rs @@ -575,8 +575,10 @@ fn expand_accessor_methods( } }); - let txn_id_accessor = maybe_redacted.then(|| { + let maybe_redacted_accessors = maybe_redacted.then(|| { let variants = variants.iter().map(|v| v.match_arm(quote! { Self })); + let variants2 = variants.clone(); + quote! { /// Returns this event's `transaction_id` from inside `unsigned`, if there is one. pub fn transaction_id(&self) -> Option<&#ruma_common::TransactionId> { @@ -591,6 +593,20 @@ fn expand_accessor_methods( } } } + + /// Returns this event's `relations` from inside `unsigned`, if that field exists. + pub fn relations(&self) -> Option<&#ruma_common::events::Relations> { + match self { + #( + #variants2(event) => { + event.as_original().and_then(|ev| ev.unsigned.relations.as_ref()) + } + )* + Self::_Custom(event) => { + event.as_original().and_then(|ev| ev.unsigned.relations.as_ref()) + } + } + } } }); @@ -605,7 +621,7 @@ fn expand_accessor_methods( #content_accessor #( #methods )* #state_key_accessor - #txn_id_accessor + #maybe_redacted_accessors } }) }