tgbot/types/definitions/chat/
boost.rs

1use serde::{Deserialize, Deserializer, Serialize, Serializer};
2
3use crate::{
4    api::{Method, Payload},
5    types::{Chat, ChatId, Integer, User},
6};
7
8/// Contains information about a chat boost.
9#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
10pub struct ChatBoost {
11    /// Point in time (Unix timestamp) when the chat was boosted.
12    pub add_date: Integer,
13    /// Unique identifier of the boost.
14    pub boost_id: String,
15    /// Point in time (Unix timestamp) when the boost will automatically expire,
16    /// unless the booster's Telegram Premium subscription is prolonged.
17    pub expiration_date: Integer,
18    /// Source of the added boost.
19    pub source: ChatBoostSource,
20}
21
22impl ChatBoost {
23    /// Creates a new `ChatBoost`.
24    ///
25    /// # Arguments
26    ///
27    /// * `add_date` - Point in time (Unix timestamp) when the chat was boosted.
28    /// * `boost_id` - Unique identifier of the boost.
29    /// * `expiration_date` - Point in time (Unix timestamp) when the boost will automatically expire,
30    ///   unless the booster's Telegram Premium subscription is prolonged.
31    /// * `source` - Source of the added boost.
32    pub fn new<T>(add_date: Integer, boost_id: T, expiration_date: Integer, source: ChatBoostSource) -> Self
33    where
34        T: Into<String>,
35    {
36        Self {
37            add_date,
38            boost_id: boost_id.into(),
39            expiration_date,
40            source,
41        }
42    }
43}
44
45/// Represents a boost removed from a chat.
46#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
47pub struct ChatBoostRemoved {
48    /// Unique identifier of the boost.
49    pub boost_id: String,
50    /// Chat which was boosted.
51    pub chat: Chat,
52    /// Point in time (Unix timestamp) when the boost was removed.
53    pub remove_date: Integer,
54    /// Source of the removed boost.
55    pub source: ChatBoostSource,
56}
57
58impl ChatBoostRemoved {
59    /// Creates a new `ChatBoostRemoved`.
60    ///
61    /// # Arguments
62    ///
63    /// * `boost_id` - Unique identifier of the boost.
64    /// * `chat` - Chat which was boosted.
65    /// * `remove_date` - Point in time (Unix timestamp) when the boost was removed.
66    /// * `source` - Source of the removed boost.
67    pub fn new<A, B>(boost_id: A, chat: B, remove_date: Integer, source: ChatBoostSource) -> Self
68    where
69        A: Into<String>,
70        B: Into<Chat>,
71    {
72        Self {
73            boost_id: boost_id.into(),
74            chat: chat.into(),
75            remove_date,
76            source,
77        }
78    }
79}
80
81/// Describes the source of a chat boost.
82#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
83#[serde(rename_all = "snake_case", tag = "source")]
84pub enum ChatBoostSource {
85    /// The boost was obtained by the creation of Telegram Premium gift codes to boost a chat.
86    ///
87    /// Each such code boosts the chat 4 times for the duration of the corresponding Telegram Premium subscription.
88    /// Contains a user for which the gift code was created.
89    #[serde(
90        deserialize_with = "ChatBoostSourceUser::deserialize_value",
91        serialize_with = "ChatBoostSourceUser::serialize_value"
92    )]
93    GiftCode(User),
94    /// The boost was obtained by the creation of a Telegram Premium giveaway.
95    Giveaway(ChatBoostSourceGiveaway),
96    /// The boost was obtained by subscribing to Telegram Premium
97    /// or by gifting a Telegram Premium subscription to another user.
98    ///
99    /// Contains a user that boosted the chat
100    #[serde(
101        deserialize_with = "ChatBoostSourceUser::deserialize_value",
102        serialize_with = "ChatBoostSourceUser::serialize_value"
103    )]
104    Premium(User),
105}
106
107#[derive(Deserialize, Serialize)]
108struct ChatBoostSourceUser {
109    user: User,
110}
111
112impl ChatBoostSourceUser {
113    pub(super) fn deserialize_value<'de, D>(deserializer: D) -> Result<User, D::Error>
114    where
115        D: Deserializer<'de>,
116    {
117        Self::deserialize(deserializer).map(|x| x.user)
118    }
119
120    pub(super) fn serialize_value<S>(value: &User, serializer: S) -> Result<S::Ok, S::Error>
121    where
122        S: Serializer,
123    {
124        Self { user: value.clone() }.serialize(serializer)
125    }
126}
127
128/// The boost was obtained by the creation of a Telegram Premium giveaway.
129///
130/// This boosts the chat 4 times for the duration of the corresponding Telegram Premium subscription.
131#[serde_with::skip_serializing_none]
132#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
133pub struct ChatBoostSourceGiveaway {
134    /// Identifier of a message in the chat with the giveaway;
135    /// the message could have been deleted already.
136    /// May be 0 if the message isn't sent yet.
137    pub giveaway_message_id: Integer,
138    /// Whether the giveaway was completed, but there was no user to win the prize.
139    pub is_unclaimed: Option<bool>,
140    /// The number of Telegram Stars to be split between giveaway winners;
141    /// for Telegram Star giveaways only.
142    pub prize_star_count: Option<Integer>,
143    /// User that won the prize in the giveaway if any.
144    pub user: Option<User>,
145}
146
147impl ChatBoostSourceGiveaway {
148    /// Creates a new `ChatBoostSourceGiveaway`.
149    ///
150    /// # Arguments
151    ///
152    /// * `giveaway_message_id` - Identifier of a message in the chat with the giveaway.
153    pub fn new(giveaway_message_id: Integer) -> Self {
154        Self {
155            giveaway_message_id,
156            is_unclaimed: None,
157            prize_star_count: None,
158            user: None,
159        }
160    }
161
162    /// Sets a new value for the `is_unclaimed` flag.
163    ///
164    /// # Arguments
165    ///
166    /// * `value` - Whether the giveaway was completed, but there was no user to win the prize.
167    pub fn with_is_unclaimed(mut self, value: bool) -> Self {
168        self.is_unclaimed = Some(value);
169        self
170    }
171
172    /// Sets a new prize star count.
173    ///
174    /// # Arguments
175    ///
176    /// * `value` - The number of Telegram Stars to be split between giveaway winners;
177    ///   for Telegram Star giveaways only.
178    pub fn with_prize_star_count(mut self, value: Integer) -> Self {
179        self.prize_star_count = Some(value);
180        self
181    }
182
183    /// Sets a new user
184    ///
185    /// # Arguments
186    ///
187    /// * `value` - User that won the prize in the giveaway.
188    pub fn with_user(mut self, value: User) -> Self {
189        self.user = Some(value);
190        self
191    }
192}
193
194/// Represents a boost added to a chat or changed.
195#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
196pub struct ChatBoostUpdated {
197    /// Infomation about the chat boost.
198    pub boost: ChatBoost,
199    /// Chat which was boosted.
200    pub chat: Chat,
201}
202
203impl ChatBoostUpdated {
204    /// Creates a new `ChatBoostUpdated`.
205    ///
206    /// # Arguments
207    ///
208    /// * `boost` - Infomation about the chat boost.
209    /// * `chat` - Chat which was boosted.
210    pub fn new<T>(boost: ChatBoost, chat: T) -> Self
211    where
212        T: Into<Chat>,
213    {
214        Self {
215            boost,
216            chat: chat.into(),
217        }
218    }
219}
220
221/// Represents a list of boosts added to a chat by a user.
222#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
223pub struct UserChatBoosts {
224    /// The list of boosts added to the chat by the user.
225    pub boosts: Vec<ChatBoost>,
226}
227
228impl<T> From<T> for UserChatBoosts
229where
230    T: IntoIterator<Item = ChatBoost>,
231{
232    fn from(value: T) -> Self {
233        Self {
234            boosts: value.into_iter().collect(),
235        }
236    }
237}
238
239/// Returns the list of boosts added to a chat by a user.
240///
241/// Requires administrator rights in the chat.
242#[derive(Clone, Debug, Serialize)]
243pub struct GetUserChatBoosts {
244    chat_id: ChatId,
245    user_id: Integer,
246}
247
248impl GetUserChatBoosts {
249    /// Creates a new `GetUserChatBoosts`.
250    ///
251    /// # Arguments
252    ///
253    /// * `chat_id` - Unique identifier for the chat.
254    /// * `user_id` - Unique identifier of the target user.
255    pub fn new<T>(chat_id: T, user_id: Integer) -> Self
256    where
257        T: Into<ChatId>,
258    {
259        Self {
260            chat_id: chat_id.into(),
261            user_id,
262        }
263    }
264}
265
266impl Method for GetUserChatBoosts {
267    type Response = UserChatBoosts;
268
269    fn into_payload(self) -> Payload {
270        Payload::json("getUserChatBoosts", self)
271    }
272}