events: Add back relations accessors for message-like event enums
This commit is contained in:
parent
0bbad284f3
commit
89e398fd06
@ -145,6 +145,15 @@ pub struct OriginalSyncMessageLikeEvent<C: MessageLikeEventContent> {
|
|||||||
pub unsigned: MessageLikeUnsigned<C>,
|
pub unsigned: MessageLikeUnsigned<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<C: MessageLikeEventContent + RedactContent> OriginalSyncMessageLikeEvent<C>
|
||||||
|
where
|
||||||
|
C::Redacted: RedactedMessageLikeEventContent,
|
||||||
|
{
|
||||||
|
pub(crate) fn into_maybe_redacted(self) -> SyncMessageLikeEvent<C> {
|
||||||
|
SyncMessageLikeEvent::Original(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A redacted message-like event.
|
/// A redacted message-like event.
|
||||||
///
|
///
|
||||||
/// `RedactedMessageLikeEvent` implements the comparison traits using only the `event_id` field, a
|
/// `RedactedMessageLikeEvent` implements the comparison traits using only the `event_id` field, a
|
||||||
|
@ -240,6 +240,14 @@ impl<E> BundledMessageLikeRelations<E> {
|
|||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.replace.is_none() && self.thread.is_none() && self.reference.is_none()
|
self.replace.is_none() && self.thread.is_none() && self.reference.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Transform `BundledMessageLikeRelations<E>` to `BundledMessageLikeRelations<T>` using the
|
||||||
|
/// given closure to convert the `replace` field if it is `Some(_)`.
|
||||||
|
pub(crate) fn map_replace<T>(self, f: impl FnOnce(E) -> T) -> BundledMessageLikeRelations<T> {
|
||||||
|
let Self { replace, has_invalid_replacement, thread, reference } = self;
|
||||||
|
let replace = replace.map(|r| Box::new(f(*r)));
|
||||||
|
BundledMessageLikeRelations { replace, has_invalid_replacement, thread, reference }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> Default for BundledMessageLikeRelations<E> {
|
impl<E> Default for BundledMessageLikeRelations<E> {
|
||||||
|
@ -111,6 +111,12 @@ pub struct OriginalSyncRoomRedactionEvent {
|
|||||||
pub unsigned: RoomRedactionUnsigned,
|
pub unsigned: RoomRedactionUnsigned,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl OriginalSyncRoomRedactionEvent {
|
||||||
|
pub(crate) fn into_maybe_redacted(self) -> SyncRoomRedactionEvent {
|
||||||
|
SyncRoomRedactionEvent::Original(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Redacted redaction event without a `room_id`.
|
/// Redacted redaction event without a `room_id`.
|
||||||
#[derive(Clone, Debug, Event)]
|
#[derive(Clone, Debug, Event)]
|
||||||
#[allow(clippy::exhaustive_structs)]
|
#[allow(clippy::exhaustive_structs)]
|
||||||
|
@ -604,6 +604,34 @@ fn expand_accessor_methods(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let relations_accessor = (kind == EventKind::MessageLike).then(|| {
|
||||||
|
let variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
/// Returns this event's `relations` from inside `unsigned`.
|
||||||
|
pub fn relations(
|
||||||
|
&self,
|
||||||
|
) -> #ruma_common::events::BundledMessageLikeRelations<AnySyncMessageLikeEvent> {
|
||||||
|
match self {
|
||||||
|
#(
|
||||||
|
#variants(event) => event.as_original().map_or_else(
|
||||||
|
::std::default::Default::default,
|
||||||
|
|ev| ev.unsigned.relations.clone().map_replace(|r| {
|
||||||
|
::std::convert::From::from(r.into_maybe_redacted())
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)*
|
||||||
|
Self::_Custom(event) => event.as_original().map_or_else(
|
||||||
|
::std::default::Default::default,
|
||||||
|
|ev| ev.unsigned.relations.clone().map_replace(|r| {
|
||||||
|
AnySyncMessageLikeEvent::_Custom(r.into_maybe_redacted())
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let maybe_redacted_accessors = maybe_redacted.then(|| {
|
let maybe_redacted_accessors = maybe_redacted.then(|| {
|
||||||
let variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
|
let variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
|
||||||
|
|
||||||
@ -634,6 +662,7 @@ fn expand_accessor_methods(
|
|||||||
|
|
||||||
#content_accessor
|
#content_accessor
|
||||||
#( #methods )*
|
#( #methods )*
|
||||||
|
#relations_accessor
|
||||||
#state_key_accessor
|
#state_key_accessor
|
||||||
#maybe_redacted_accessors
|
#maybe_redacted_accessors
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user