1// Copyright (c) 2022 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use crate::message::MessagePayload;
8use crate::util::helpers::Text;
9
10generate_element!(
11 /// Container for a set of reactions.
12 Reactions, "reactions", REACTIONS,
13 attributes: [
14 /// The id of the message these reactions apply to.
15 id: Required<String> = "id",
16 ],
17 children: [
18 /// The list of reactions.
19 reactions: Vec<Reaction> = ("reaction", REACTIONS) => Reaction,
20 ]
21);
22
23impl MessagePayload for Reactions {}
24
25generate_element!(
26 /// One emoji reaction.
27 Reaction, "reaction", REACTIONS,
28 text: (
29 /// The text of this reaction.
30 emoji: Text<String>
31 )
32);
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 use crate::Element;
38 use std::convert::{TryFrom, TryInto};
39
40 #[cfg(target_pointer_width = "32")]
41 #[test]
42 fn test_size() {
43 assert_size!(Reactions, 24);
44 assert_size!(Reaction, 12);
45 }
46
47 #[cfg(target_pointer_width = "64")]
48 #[test]
49 fn test_size() {
50 assert_size!(Reactions, 48);
51 assert_size!(Reaction, 24);
52 }
53
54 #[test]
55 fn test_empty() {
56 let elem: Element = "<reactions xmlns='urn:xmpp:reactions:0' id='foo'/>"
57 .parse()
58 .unwrap();
59 let elem2 = elem.clone();
60 let reactions = Reactions::try_from(elem2).unwrap();
61 assert_eq!(reactions.id, "foo");
62 assert_eq!(reactions.reactions.len(), 0);
63 }
64
65 #[test]
66 fn test_multi() {
67 let elem: Element =
68 "<reactions xmlns='urn:xmpp:reactions:0' id='foo'><reaction>👋</reaction><reaction>🐢</reaction></reactions>"
69 .parse()
70 .unwrap();
71 let elem2 = elem.clone();
72 let reactions = Reactions::try_from(elem2).unwrap();
73 assert_eq!(reactions.id, "foo");
74 assert_eq!(reactions.reactions.len(), 2);
75 let [hand, turtle]: [Reaction; 2] = reactions.reactions.try_into().unwrap();
76 assert_eq!(hand.emoji, "👋");
77 assert_eq!(turtle.emoji, "🐢");
78 }
79
80 #[test]
81 fn test_zwj_emoji() {
82 let elem: Element =
83 "<reactions xmlns='urn:xmpp:reactions:0' id='foo'><reaction>👩🏾❤️👩🏼</reaction></reactions>"
84 .parse()
85 .unwrap();
86 let elem2 = elem.clone();
87 let mut reactions = Reactions::try_from(elem2).unwrap();
88 assert_eq!(reactions.id, "foo");
89 assert_eq!(reactions.reactions.len(), 1);
90 let reaction = reactions.reactions.pop().unwrap();
91 assert_eq!(
92 reaction.emoji,
93 "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}"
94 );
95 }
96}