Skip to main content

tuwunel_core/matrix/event/
relation.rs

1use ruma::events::relation::RelationType;
2use serde::Deserialize;
3
4use super::Event;
5
6/// Compares an event's relation type with a requested relation type.
7///
8/// Implementations inspect the `m.relates_to.rel_type` field in event content.
9/// Missing or malformed relation content does not match.
10pub trait RelationTypeEqual<E: Event> {
11	/// Returns whether the event declares this relation type.
12	///
13	/// The comparison deserializes only the relation fields needed for the
14	/// check. Content that cannot be deserialized returns false.
15	fn relation_type_equal(&self, event: &E) -> bool;
16}
17
18#[derive(Clone, Debug, Deserialize)]
19struct ExtractRelatesToEventId {
20	#[serde(rename = "m.relates_to")]
21	relates_to: ExtractRelType,
22}
23
24#[derive(Clone, Debug, Deserialize)]
25struct ExtractRelType {
26	rel_type: RelationType,
27}
28
29impl<E: Event> RelationTypeEqual<E> for RelationType {
30	fn relation_type_equal(&self, event: &E) -> bool {
31		event
32			.get_content()
33			.map(|c: ExtractRelatesToEventId| c.relates_to.rel_type)
34			.is_ok_and(|r| r == *self)
35	}
36}