events: Add relations accessors to event enums

This commit is contained in:
Jonas Platte 2022-09-16 11:34:54 +02:00
parent 956871cfa6
commit fa61cc1248
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
2 changed files with 25 additions and 3 deletions

View File

@ -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.

View File

@ -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
}
})
}