Skip to main content

zulip_api/message/
reaction.rs

1use serde::{Deserialize, Serialize};
2
3mod reaction_type;
4
5pub use reaction_type::ReactionType;
6
7use crate::{field_access, impl_default, impl_display};
8
9/// Represents a reaction to a Zulip message.
10#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
11pub struct Reaction {
12    emoji_name: String,
13    emoji_code: String,
14    reaction_type: ReactionType,
15    user_id: u64,
16}
17
18impl Reaction {
19    /// Creates a new [Reaction].
20    pub const fn new() -> Self {
21        Self {
22            emoji_name: String::new(),
23            emoji_code: String::new(),
24            reaction_type: ReactionType::new(),
25            user_id: 0,
26        }
27    }
28}
29
30field_access! {
31    Reaction {
32        emoji_name: cow { str },
33        emoji_code: cow { str },
34    }
35}
36
37field_access! {
38    Reaction {
39        reaction_type: ReactionType,
40        user_id: u64,
41    }
42}
43
44impl_default!(Reaction);
45impl_display!(Reaction);
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_reaction_type() {
53        [
54            ReactionType::UnicodeEmoji,
55            ReactionType::RealmEmoji,
56            ReactionType::ZulipExtraEmoji,
57        ]
58        .into_iter()
59        .for_each(|react_ty| {
60            let react_json = react_ty.to_string();
61
62            assert_eq!(serde_json::to_string(&react_ty).unwrap(), react_json);
63            assert_eq!(
64                serde_json::from_str::<ReactionType>(&react_json).unwrap(),
65                react_ty
66            );
67        });
68    }
69
70    #[test]
71    fn test_reaction() {
72        [
73            ReactionType::UnicodeEmoji,
74            ReactionType::RealmEmoji,
75            ReactionType::ZulipExtraEmoji,
76        ]
77        .into_iter()
78        .for_each(|react_ty| {
79            let emoji_name = "test emoji name";
80            let emoji_code = "test emoji code";
81            let user_id = 42;
82
83            let react = Reaction::new()
84                .with_emoji_name(emoji_name)
85                .with_emoji_code(emoji_code)
86                .with_reaction_type(react_ty)
87                .with_user_id(user_id);
88
89            let react_json = react.to_string();
90
91            assert_eq!(serde_json::to_string(&react).unwrap(), react_json);
92            assert_eq!(
93                serde_json::from_str::<Reaction>(&react_json).unwrap(),
94                react
95            );
96
97            assert_eq!(react.emoji_name(), emoji_name);
98            assert_eq!(react.emoji_code(), emoji_code);
99            assert_eq!(react.reaction_type(), react_ty);
100            assert_eq!(react.user_id(), user_id);
101        });
102    }
103}