events: Add transaction_id accessor to applicable event enums

This commit is contained in:
Jonas Platte 2022-04-13 16:45:15 +02:00
parent dd24d6be77
commit 1db716f643
No known key found for this signature in database
GPG Key ID: BBA95679259D342F
2 changed files with 38 additions and 1 deletions

View File

@ -8,7 +8,8 @@ use super::{
Redact, Redact,
}; };
use crate::{ use crate::{
serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UserId, serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId,
TransactionId, UserId,
}; };
event_enum! { event_enum! {
@ -153,6 +154,14 @@ impl AnyRoomEvent {
room_ev_accessor!(room_id: &RoomId); room_ev_accessor!(room_id: &RoomId);
room_ev_accessor!(event_id: &EventId); room_ev_accessor!(event_id: &EventId);
room_ev_accessor!(sender: &UserId); room_ev_accessor!(sender: &UserId);
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&TransactionId> {
match self {
Self::MessageLike(ev) => ev.transaction_id(),
Self::State(ev) => ev.transaction_id(),
}
}
} }
/// Any sync room event. /// Any sync room event.
@ -173,6 +182,14 @@ impl AnySyncRoomEvent {
room_ev_accessor!(event_id: &EventId); room_ev_accessor!(event_id: &EventId);
room_ev_accessor!(sender: &UserId); room_ev_accessor!(sender: &UserId);
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&TransactionId> {
match self {
Self::MessageLike(ev) => ev.transaction_id(),
Self::State(ev) => ev.transaction_id(),
}
}
/// Converts `self` to an `AnyRoomEvent` by adding the given a room ID. /// Converts `self` to an `AnyRoomEvent` by adding the given a room ID.
pub fn into_full_event(self, room_id: Box<RoomId>) -> AnyRoomEvent { pub fn into_full_event(self, room_id: Box<RoomId>) -> AnyRoomEvent {
match self { match self {

View File

@ -487,6 +487,25 @@ fn expand_accessor_methods(
}) })
}); });
let txn_id_accessor = maybe_redacted.then(|| {
let variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
quote! {
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&#ruma_common::TransactionId> {
match self {
#(
#variants(event) => {
event.as_original().and_then(|ev| ev.unsigned.transaction_id.as_deref())
}
)*
Self::_Custom(event) => {
event.as_original().and_then(|ev| ev.unsigned.transaction_id.as_deref())
}
}
}
}
});
Ok(quote! { Ok(quote! {
#[automatically_derived] #[automatically_derived]
impl #ident { impl #ident {
@ -497,6 +516,7 @@ fn expand_accessor_methods(
#content_accessors #content_accessors
#( #methods )* #( #methods )*
#txn_id_accessor
} }
}) })
} }