Skip to main content

layer_client/
reactions.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// NOTE:
5// The "Layer" project is no longer maintained or supported.
6// Its original purpose for personal SDK/APK experimentation and learning
7// has been fulfilled.
8//
9// Please use Ferogram instead:
10// https://github.com/ankit-chaubey/ferogram
11// Ferogram will receive future updates and development, although progress
12// may be slower.
13//
14// Ferogram is an async Telegram MTProto client library written in Rust.
15// Its implementation follows the behaviour of the official Telegram clients,
16// particularly Telegram Desktop and TDLib, and aims to provide a clean and
17// modern async interface for building Telegram clients and tools.
18
19//! [`InputReactions`]: typed parameter for reacting to messages.
20//!
21//!  API.
22//!
23//! # Examples
24//!
25//! ```rust,no_run
26//! use layer_client::reactions::InputReactions;
27//!
28//! // Simple emoji
29//! InputReactions::emoticon("👍");
30//!
31//! // Custom emoji (premium)
32//! InputReactions::custom_emoji(1234567890);
33//!
34//! // Remove all reactions
35//! InputReactions::remove();
36//!
37//! // Multi-reaction
38//! use layer_tl_types::enums::Reaction;
39//! InputReactions::from(vec![
40//! Reaction::Emoji(layer_tl_types::types::ReactionEmoji { emoticon: "👍".into() }),
41//! Reaction::Emoji(layer_tl_types::types::ReactionEmoji { emoticon: "❤️".into() }),
42//! ]);
43//!
44//! // Chained modifiers
45//! InputReactions::emoticon("🔥").big().add_to_recent();
46//! ```
47
48use layer_tl_types::{self as tl, enums::Reaction};
49
50/// A set of reactions to apply to a message.
51///
52/// Construct with [`InputReactions::emoticon`], [`InputReactions::custom_emoji`],
53/// [`InputReactions::remove`], or `From<Vec<Reaction>>`.
54#[derive(Clone, Debug, Default)]
55pub struct InputReactions {
56    pub(crate) reactions: Vec<Reaction>,
57    pub(crate) add_to_recent: bool,
58    pub(crate) big: bool,
59}
60
61impl InputReactions {
62    // Constructors
63
64    /// React with a standard Unicode emoji (e.g. `"👍"`).
65    pub fn emoticon<S: Into<String>>(emoticon: S) -> Self {
66        Self {
67            reactions: vec![Reaction::Emoji(tl::types::ReactionEmoji {
68                emoticon: emoticon.into(),
69            })],
70            ..Self::default()
71        }
72    }
73
74    /// React with a custom (premium) emoji identified by its `document_id`.
75    pub fn custom_emoji(document_id: i64) -> Self {
76        Self {
77            reactions: vec![Reaction::CustomEmoji(tl::types::ReactionCustomEmoji {
78                document_id,
79            })],
80            ..Self::default()
81        }
82    }
83
84    /// Remove all reactions from the message.
85    pub fn remove() -> Self {
86        Self::default()
87    }
88
89    // Modifiers
90
91    /// Play the reaction with a large animated effect.
92    pub fn big(mut self) -> Self {
93        self.big = true;
94        self
95    }
96
97    /// Add this reaction to the user's recent reactions list.
98    pub fn add_to_recent(mut self) -> Self {
99        self.add_to_recent = true;
100        self
101    }
102}
103
104// From impls
105
106impl From<&str> for InputReactions {
107    fn from(s: &str) -> Self {
108        InputReactions::emoticon(s)
109    }
110}
111
112impl From<String> for InputReactions {
113    fn from(s: String) -> Self {
114        InputReactions::emoticon(s)
115    }
116}
117
118impl From<Vec<Reaction>> for InputReactions {
119    fn from(reactions: Vec<Reaction>) -> Self {
120        Self {
121            reactions,
122            ..Self::default()
123        }
124    }
125}
126
127impl From<InputReactions> for Vec<Reaction> {
128    fn from(r: InputReactions) -> Self {
129        r.reactions
130    }
131}