1
0
Fork 0
mirror of https://github.com/teloxide/teloxide.git synced 2025-01-13 21:23:45 +01:00

Add ChatBoost and ChatBoostSource structs

This commit is contained in:
Andrey Brusnik 2024-07-19 14:37:12 +04:00
parent e5edaceb22
commit 65b46972f9
No known key found for this signature in database
GPG key ID: D33232F28CFF442C
3 changed files with 188 additions and 0 deletions
crates/teloxide-core/src

View file

@ -13,6 +13,8 @@ pub use callback_query::*;
pub use chat::*;
pub use chat_action::*;
pub use chat_administrator_rights::*;
pub use chat_boost::*;
pub use chat_boost_source::*;
pub use chat_full_info::*;
pub use chat_invite_link::*;
pub use chat_join_request::*;
@ -156,6 +158,8 @@ mod callback_query;
mod chat;
mod chat_action;
mod chat_administrator_rights;
mod chat_boost;
mod chat_boost_source;
mod chat_full_info;
mod chat_invite_link;
mod chat_join_request;
@ -309,6 +313,7 @@ pub use user_id::*;
use serde_with::with_prefix;
// Deserialization prefix for giveaway_message_id field used in GiveawayWinners
// and ChatBoostSourceGiveaway
with_prefix!(prefix_giveaway_message_id "giveaway_");
/// Converts an `i64` timestamp to a `choro::DateTime`, producing serde error

View file

@ -0,0 +1,52 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::types::ChatBoostSource;
/// This object contains information about a chat boost.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChatBoost {
/// Unique identifier of the boost.
pub boost_id: String,
/// Point in time (Unix timestamp) when the chat was boosted.
#[serde(with = "crate::types::serde_date_from_unix_timestamp")]
pub add_date: DateTime<Utc>,
/// Point in time (Unix timestamp) when the boost will automatically expire,
/// unless the booster's Telegram Premium subscription is prolonged.
#[serde(with = "crate::types::serde_date_from_unix_timestamp")]
pub expiration_date: DateTime<Utc>,
/// Source of the added boost.
pub source: ChatBoostSource,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let data = r#"
{
"boost_id": "4506e1b7e866e33fcbde78fe1746ec3a",
"add_date": 1721399621,
"expiration_date": 1745088963,
"source": {
"source": "premium",
"user": {
"id": 1459074222,
"is_bot": false,
"first_name": "shadowchain",
"username": "shdwchn10",
"language_code": "en",
"is_premium": true
}
}
}
"#;
serde_json::from_str::<ChatBoost>(data).unwrap();
}
}

View file

@ -0,0 +1,131 @@
use serde::{Deserialize, Serialize};
use crate::types::{MessageId, User};
/// This object describes the source of a chat boost.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChatBoostSource {
#[serde(flatten)]
pub kind: ChatBoostSourceKind,
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "source")]
pub enum ChatBoostSourceKind {
Premium(ChatBoostSourcePremium),
GiftCode(ChatBoostSourceGiftCode),
Giveaway(ChatBoostSourceGiveaway),
}
/// The boost was obtained by subscribing to Telegram Premium or by gifting a
/// Telegram Premium subscription to another user.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChatBoostSourcePremium {
/// User that boosted the chat.
pub user: User,
}
/// The boost was obtained by the creation of Telegram Premium gift codes to
/// boost a chat. Each such code boosts the chat 4 times for the duration of the
/// corresponding Telegram Premium subscription.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChatBoostSourceGiftCode {
/// User for which the gift code was created.
pub user: User,
}
/// The boost was obtained by the creation of a Telegram Premium giveaway. This
/// boosts the chat 4 times for the duration of the corresponding Telegram
/// Premium subscription.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChatBoostSourceGiveaway {
/// Identifier of a message in the chat with the giveaway; the message could
/// have been deleted already. May be 0 if the message isn't sent yet.
#[serde(flatten, with = "crate::types::prefix_giveaway_message_id")]
pub giveaway_message_id: MessageId,
/// User that won the prize in the giveaway if any.
pub user: Option<User>,
/// `true`, if the giveaway was completed, but there was no user to win the
/// prize.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub is_unclaimed: bool,
}
impl ChatBoostSource {
#[must_use]
pub fn user(&self) -> Option<&User> {
Some(match &self.kind {
ChatBoostSourceKind::Premium(premium) => &premium.user,
ChatBoostSourceKind::GiftCode(gift_code) => &gift_code.user,
ChatBoostSourceKind::Giveaway(giveaway) => return giveaway.user.as_ref(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize_premium() {
let data = r#"
{
"source": "premium",
"user": {
"id": 1459074222,
"is_bot": false,
"first_name": "shadowchain",
"username": "shdwchn10",
"language_code": "en",
"is_premium": true
}
}
"#;
serde_json::from_str::<ChatBoostSource>(data).unwrap();
}
#[test]
fn deserialize_gift_code() {
let data = r#"
{
"source": "gift_code",
"user": {
"id": 1459074222,
"is_bot": false,
"first_name": "shadowchain",
"username": "shdwchn10",
"language_code": "en",
"is_premium": false
}
}
"#;
serde_json::from_str::<ChatBoostSource>(data).unwrap();
}
#[test]
fn deserialize_giveaway() {
let data = r#"
{
"source": "giveaway",
"giveaway_message_id": 420,
"user": {
"id": 1459074222,
"is_bot": false,
"first_name": "shadowchain",
"username": "shdwchn10",
"language_code": "en",
"is_premium": false
}
}
"#;
serde_json::from_str::<ChatBoostSource>(data).unwrap();
}
}