layer_client/reactions.rs
1//! [`InputReactions`]: typed parameter for reacting to messages.
2//!
3//! API.
4//!
5//! # Examples
6//!
7//! ```rust,no_run
8//! use layer_client::reactions::InputReactions;
9//!
10//! // Simple emoji
11//! InputReactions::emoticon("👍");
12//!
13//! // Custom emoji (premium)
14//! InputReactions::custom_emoji(1234567890);
15//!
16//! // Remove all reactions
17//! InputReactions::remove();
18//!
19//! // Multi-reaction
20//! use layer_tl_types::enums::Reaction;
21//! InputReactions::from(vec![
22//! Reaction::Emoji(layer_tl_types::types::ReactionEmoji { emoticon: "👍".into() }),
23//! Reaction::Emoji(layer_tl_types::types::ReactionEmoji { emoticon: "❤️".into() }),
24//! ]);
25//!
26//! // Chained modifiers
27//! InputReactions::emoticon("🔥").big().add_to_recent();
28//! ```
29
30use layer_tl_types::{self as tl, enums::Reaction};
31
32/// A set of reactions to apply to a message.
33///
34/// Construct with [`InputReactions::emoticon`], [`InputReactions::custom_emoji`],
35/// [`InputReactions::remove`], or `From<Vec<Reaction>>`.
36#[derive(Clone, Debug, Default)]
37pub struct InputReactions {
38 pub(crate) reactions: Vec<Reaction>,
39 pub(crate) add_to_recent: bool,
40 pub(crate) big: bool,
41}
42
43impl InputReactions {
44 // Constructors
45
46 /// React with a standard Unicode emoji (e.g. `"👍"`).
47 pub fn emoticon<S: Into<String>>(emoticon: S) -> Self {
48 Self {
49 reactions: vec![Reaction::Emoji(tl::types::ReactionEmoji {
50 emoticon: emoticon.into(),
51 })],
52 ..Self::default()
53 }
54 }
55
56 /// React with a custom (premium) emoji identified by its `document_id`.
57 pub fn custom_emoji(document_id: i64) -> Self {
58 Self {
59 reactions: vec![Reaction::CustomEmoji(tl::types::ReactionCustomEmoji {
60 document_id,
61 })],
62 ..Self::default()
63 }
64 }
65
66 /// Remove all reactions from the message.
67 pub fn remove() -> Self {
68 Self::default()
69 }
70
71 // Modifiers
72
73 /// Play the reaction with a large animated effect.
74 pub fn big(mut self) -> Self {
75 self.big = true;
76 self
77 }
78
79 /// Add this reaction to the user's recent reactions list.
80 pub fn add_to_recent(mut self) -> Self {
81 self.add_to_recent = true;
82 self
83 }
84}
85
86// From impls
87
88impl From<&str> for InputReactions {
89 fn from(s: &str) -> Self {
90 InputReactions::emoticon(s)
91 }
92}
93
94impl From<String> for InputReactions {
95 fn from(s: String) -> Self {
96 InputReactions::emoticon(s)
97 }
98}
99
100impl From<Vec<Reaction>> for InputReactions {
101 fn from(reactions: Vec<Reaction>) -> Self {
102 Self {
103 reactions,
104 ..Self::default()
105 }
106 }
107}
108
109impl From<InputReactions> for Vec<Reaction> {
110 fn from(r: InputReactions) -> Self {
111 r.reactions
112 }
113}