1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use crate::OwnedEventId;
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.reaction", kind = MessageLike)]
pub struct ReactionEventContent {
#[serde(rename = "m.relates_to")]
pub relates_to: Relation,
}
impl ReactionEventContent {
pub fn new(relates_to: Relation) -> Self {
Self { relates_to }
}
}
impl From<Relation> for ReactionEventContent {
fn from(relates_to: Relation) -> Self {
Self::new(relates_to)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(tag = "rel_type", rename = "m.annotation")]
pub struct Relation {
pub event_id: OwnedEventId,
pub key: String,
}
impl Relation {
pub fn new(event_id: OwnedEventId, key: String) -> Self {
Self { event_id, key }
}
}
#[cfg(test)]
mod tests {
use crate::event_id;
use matches::assert_matches;
use serde_json::{from_value as from_json_value, json};
use super::{ReactionEventContent, Relation};
#[test]
fn deserialize() {
let ev_id = event_id!("$1598361704261elfgc:localhost");
let json = json!({
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": ev_id,
"key": "🦛",
}
});
assert_matches!(
from_json_value::<ReactionEventContent>(json).unwrap(),
ReactionEventContent { relates_to: Relation { event_id, key } }
if event_id == ev_id && key == "🦛"
);
}
}