Skip to main content

ruma_events/
reaction.rs

1//! Types for the [`m.reaction`] event.
2//!
3//! [`m.reaction`]: https://spec.matrix.org/v1.19/client-server-api/#mreaction
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::relation::Annotation;
9
10/// The payload for a `m.reaction` event.
11///
12/// A reaction to another event.
13#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
14#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
15#[ruma_event(type = "m.reaction", kind = MessageLike)]
16pub struct ReactionEventContent {
17    /// Information about the related event.
18    #[serde(rename = "m.relates_to")]
19    pub relates_to: Annotation,
20}
21
22impl ReactionEventContent {
23    /// Creates a new `ReactionEventContent` from the given annotation.
24    ///
25    /// You can also construct a `ReactionEventContent` from an annotation using `From` / `Into`.
26    pub fn new(relates_to: Annotation) -> Self {
27        Self { relates_to }
28    }
29}
30
31impl From<Annotation> for ReactionEventContent {
32    fn from(relates_to: Annotation) -> Self {
33        Self::new(relates_to)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use ruma_common::{canonical_json::assert_to_canonical_json_eq, owned_event_id, serde::Raw};
40    use serde_json::{from_value as from_json_value, json};
41    use strass::assert_let;
42
43    use super::ReactionEventContent;
44    use crate::relation::Annotation;
45
46    #[test]
47    fn deserialize() {
48        let json = json!({
49            "m.relates_to": {
50                "rel_type": "m.annotation",
51                "event_id": "$1598361704261elfgc:localhost",
52                "key": "🦛",
53            }
54        });
55
56        assert_let!(
57            Ok(ReactionEventContent { relates_to }) = from_json_value::<ReactionEventContent>(json)
58        );
59        assert_eq!(relates_to.event_id, "$1598361704261elfgc:localhost");
60        assert_eq!(relates_to.key, "🦛");
61    }
62
63    #[test]
64    fn serialize() {
65        let content = ReactionEventContent::new(Annotation::new(
66            owned_event_id!("$my_reaction"),
67            "🏠".to_owned(),
68        ));
69
70        assert_to_canonical_json_eq!(
71            content,
72            json!({
73                "m.relates_to": {
74                    "rel_type": "m.annotation",
75                    "event_id": "$my_reaction",
76                    "key": "🏠"
77                }
78            })
79        );
80    }
81
82    #[test]
83    fn serialization_roundtrip() {
84        let content = ReactionEventContent::new(Annotation::new(
85            owned_event_id!("$my_reaction"),
86            "🏠".to_owned(),
87        ));
88
89        let json_content = Raw::new(&content).unwrap();
90        let deser_content = json_content.deserialize().unwrap();
91
92        assert_eq!(deser_content.relates_to.event_id, content.relates_to.event_id);
93        assert_eq!(deser_content.relates_to.key, content.relates_to.key);
94    }
95}