From e9b0b26f77b9bc709fe3b3285d618973d012f59c Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 10 Jul 2024 15:27:55 +0300 Subject: [PATCH] Add td_api::messageGiftedStars. --- td/generate/scheme/td_api.tl | 11 +++ td/telegram/DialogAction.cpp | 1 + td/telegram/MessageContent.cpp | 138 ++++++++++++++++++++++++++++- td/telegram/MessageContentType.cpp | 8 ++ td/telegram/MessageContentType.h | 3 +- 5 files changed, 156 insertions(+), 5 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index d680510b0..0056fa7f4 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3518,6 +3518,17 @@ messagePremiumGiveawayCompleted giveaway_message_id:int53 winner_count:int32 unc //@unclaimed_prize_count Number of undistributed prizes messagePremiumGiveawayWinners boosted_chat_id:int53 giveaway_message_id:int53 additional_chat_count:int32 actual_winners_selection_date:int32 only_new_members:Bool was_refunded:Bool month_count:int32 prize_description:string winner_count:int32 winner_user_ids:vector unclaimed_prize_count:int32 = MessageContent; +//@description Telegram Stars were gifted to a user +//@gifter_user_id The identifier of a user that gifted Telegram Stars; 0 if the gift was anonymous +//@receiver_user_id The identifier of a user that received Telegram Stars +//@currency Currency for the paid amount +//@amount The paid amount, in the smallest units of the currency +//@cryptocurrency Cryptocurrency used to pay for the gift; may be empty if none +//@cryptocurrency_amount The paid amount, in the smallest units of the cryptocurrency; 0 if none +//@star_count Number of Telegram Stars that were gifted +//@transaction_id Identifier of the transaction for Telegram Stars purchase; for receiver only +messageGiftedStars gifter_user_id:int53 receiver_user_id:int53 currency:string amount:int53 cryptocurrency:string cryptocurrency_amount:int64 star_count:int53 transaction_id:string = MessageContent; + //@description A contact has registered with Telegram messageContactRegistered = MessageContent; diff --git a/td/telegram/DialogAction.cpp b/td/telegram/DialogAction.cpp index 02805e624..3baccbf66 100644 --- a/td/telegram/DialogAction.cpp +++ b/td/telegram/DialogAction.cpp @@ -424,6 +424,7 @@ bool DialogAction::is_canceled_by_message_of_type(MessageContentType message_con case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; default: UNREACHABLE(); diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index e494b7b30..2f41464b2 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -504,7 +504,7 @@ class MessageChatSetTtl final : public MessageContent { class MessageUnsupported final : public MessageContent { public: - static constexpr int32 CURRENT_VERSION = 32; + static constexpr int32 CURRENT_VERSION = 33; int32 version = CURRENT_VERSION; MessageUnsupported() = default; @@ -1163,6 +1163,31 @@ class MessagePaymentRefunded final : public MessageContent { } }; +class MessageGiftStars final : public MessageContent { + public: + string currency; + int64 amount = 0; + string crypto_currency; + int64 crypto_amount = 0; + int64 star_count = 0; + string transaction_id; + + MessageGiftStars() = default; + MessageGiftStars(string &¤cy, int64 amount, string &&crypto_currency, int64 crypto_amount, int64 star_count, + string &&transaction_id) + : currency(std::move(currency)) + , amount(amount) + , crypto_currency(std::move(crypto_currency)) + , crypto_amount(crypto_amount) + , star_count(star_count) + , transaction_id(std::move(transaction_id)) { + } + + MessageContentType get_type() const final { + return MessageContentType::GiftStars; + } +}; + template static void store(const MessageContent *content, StorerT &storer) { CHECK(content != nullptr); @@ -1756,6 +1781,26 @@ static void store(const MessageContent *content, StorerT &storer) { store(m->provider_payment_charge_id, storer); break; } + case MessageContentType::GiftStars: { + const auto *m = static_cast(content); + bool has_crypto_amount = !m->crypto_currency.empty(); + bool has_transaction_id = !m->transaction_id.empty(); + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_crypto_amount); + STORE_FLAG(has_transaction_id); + END_STORE_FLAGS(); + store(m->currency, storer); + store(m->amount, storer); + store(m->star_count, storer); + if (has_crypto_amount) { + store(m->crypto_currency, storer); + store(m->crypto_amount, storer); + } + if (has_transaction_id) { + store(m->transaction_id, storer); + } + break; + } default: UNREACHABLE(); } @@ -2542,6 +2587,27 @@ static void parse(unique_ptr &content, ParserT &parser) { content = std::move(m); break; } + case MessageContentType::GiftStars: { + auto m = make_unique(); + bool has_crypto_amount; + bool has_transaction_id; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_crypto_amount); + PARSE_FLAG(has_transaction_id); + END_PARSE_FLAGS(); + parse(m->currency, parser); + parse(m->amount, parser); + parse(m->star_count, parser); + if (has_crypto_amount) { + parse(m->crypto_currency, parser); + parse(m->crypto_amount, parser); + } + if (has_transaction_id) { + parse(m->transaction_id, parser); + } + content = std::move(m); + break; + } default: is_bad = true; @@ -3297,6 +3363,7 @@ bool can_message_content_have_input_media(const Td *td, const MessageContent *co case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; case MessageContentType::Animation: case MessageContentType::Audio: @@ -3441,6 +3508,7 @@ SecretInputMedia get_message_content_secret_input_media( case MessageContentType::DialogShared: case MessageContentType::PaidMedia: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: break; default: UNREACHABLE(); @@ -3608,6 +3676,7 @@ static telegram_api::object_ptr get_message_content_in case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: break; default: UNREACHABLE(); @@ -3860,6 +3929,7 @@ void delete_message_content_thumbnail(MessageContent *content, Td *td, int32 med case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: break; default: UNREACHABLE(); @@ -4099,6 +4169,7 @@ Status can_send_message_content(DialogId dialog_id, const MessageContent *conten case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: UNREACHABLE(); } return Status::OK(); @@ -4249,6 +4320,7 @@ static int32 get_message_content_media_index_mask(const MessageContent *content, case MessageContentType::DialogShared: case MessageContentType::PaidMedia: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return 0; default: UNREACHABLE(); @@ -4539,6 +4611,8 @@ vector get_message_content_min_user_ids(const Td *td, const MessageConte case MessageContentType::PaymentRefunded: // private chats only break; + case MessageContentType::GiftStars: + break; default: UNREACHABLE(); break; @@ -4960,6 +5034,7 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: break; default: UNREACHABLE(); @@ -5113,6 +5188,7 @@ bool merge_message_content_file_id(Td *td, MessageContent *message_content, File case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: LOG(ERROR) << "Receive new file " << new_file_id << " in a sent message of the type " << content_type; break; default: @@ -5683,6 +5759,16 @@ void compare_message_contents(Td *td, const MessageContent *old_content, const M } break; } + case MessageContentType::GiftStars: { + const auto *lhs = static_cast(old_content); + const auto *rhs = static_cast(new_content); + if (lhs->currency != rhs->currency || lhs->amount != rhs->amount || + lhs->crypto_currency != rhs->crypto_currency || lhs->crypto_amount != rhs->crypto_amount || + lhs->star_count != rhs->star_count || lhs->transaction_id != rhs->transaction_id) { + need_update = true; + } + break; + } default: UNREACHABLE(); break; @@ -6885,6 +6971,7 @@ unique_ptr dup_message_content(Td *td, DialogId dialog_id, const case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return nullptr; default: UNREACHABLE(); @@ -7242,7 +7329,7 @@ unique_ptr get_action_message_content(Td *td, tl_object_ptrcrypto_amount_ = 0; } } else if (action->crypto_amount_ <= 0) { - LOG(ERROR) << "Receive invalid premium gift crypto price " << action->crypto_amount_; + LOG(ERROR) << "Receive invalid premium gift crypto amount " << action->crypto_amount_; action->crypto_amount_ = 0; } return td::make_unique(std::move(action->currency_), action->amount_, @@ -7375,8 +7462,25 @@ unique_ptr get_action_message_content(Td *td, tl_object_ptrcharge_->id_), std::move(action->charge_->provider_charge_id_)); } - case telegram_api::messageActionGiftStars::ID: - return make_unique(); + case telegram_api::messageActionGiftStars::ID: { + auto action = move_tl_object_as(action_ptr); + if (action->amount_ <= 0 || !check_currency_amount(action->amount_)) { + LOG(ERROR) << "Receive invalid gifted stars price " << action->amount_; + action->amount_ = 0; + } + if (action->crypto_currency_.empty()) { + if (action->crypto_amount_ != 0) { + LOG(ERROR) << "Receive gifted stars crypto price " << action->crypto_amount_ << " without currency"; + action->crypto_amount_ = 0; + } + } else if (action->crypto_amount_ <= 0) { + LOG(ERROR) << "Receive invalid gifted stars crypto amount " << action->crypto_amount_; + action->crypto_amount_ = 0; + } + return td::make_unique( + std::move(action->currency_), action->amount_, std::move(action->crypto_currency_), action->crypto_amount_, + StarManager::get_star_count(action->stars_), std::move(action->transaction_id_)); + } default: UNREACHABLE(); } @@ -7818,6 +7922,29 @@ tl_object_ptr get_message_content_object(const MessageCo get_message_sender_object(td, m->dialog_id, "messagePaymentRefunded"), m->currency, m->total_amount, m->invoice_payload, m->telegram_payment_charge_id, m->provider_payment_charge_id); } + case MessageContentType::GiftStars: { + const auto *m = static_cast(content); + int64 gifter_user_id = 0; + int64 receiver_user_id = + td->user_manager_->get_user_id_object(td->user_manager_->get_my_id(), "MessageGiftStars 1"); + if (dialog_id.get_type() == DialogType::User) { + auto user_id = dialog_id.get_user_id(); + if (is_outgoing) { + gifter_user_id = receiver_user_id; + receiver_user_id = td->user_manager_->get_user_id_object(user_id, "MessageGiftStars 2"); + } else { + if (user_id != UserManager::get_service_notifications_user_id() && !td->user_manager_->is_user_bot(user_id) && + !td->user_manager_->is_user_support(user_id)) { + gifter_user_id = td->user_manager_->get_user_id_object(user_id, "MessageGiftStars 3"); + } + } + } else { + LOG(ERROR) << "Receive gifted stars in " << dialog_id; + } + return td_api::make_object(gifter_user_id, receiver_user_id, m->currency, m->amount, + m->crypto_currency, m->crypto_amount, m->star_count, + m->transaction_id); + } default: UNREACHABLE(); return nullptr; @@ -8371,6 +8498,7 @@ string get_message_content_search_text(const Td *td, const MessageContent *conte case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return string(); default: UNREACHABLE(); @@ -8771,6 +8899,8 @@ void add_message_content_dependencies(Dependencies &dependencies, const MessageC dependencies.add_message_sender_dependencies(content->dialog_id); break; } + case MessageContentType::GiftStars: + break; default: UNREACHABLE(); break; diff --git a/td/telegram/MessageContentType.cpp b/td/telegram/MessageContentType.cpp index b329076cd..554b4d862 100644 --- a/td/telegram/MessageContentType.cpp +++ b/td/telegram/MessageContentType.cpp @@ -150,6 +150,8 @@ StringBuilder &operator<<(StringBuilder &string_builder, MessageContentType cont return string_builder << "PaidMedia"; case MessageContentType::PaymentRefunded: return string_builder << "PaymentRefunded"; + case MessageContentType::GiftStars: + return string_builder << "GiftStars"; default: return string_builder << "Invalid type " << static_cast(content_type); } @@ -239,6 +241,7 @@ bool is_allowed_media_group_content(MessageContentType content_type) { case MessageContentType::DialogShared: case MessageContentType::PaidMedia: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; default: UNREACHABLE(); @@ -322,6 +325,7 @@ bool can_be_secret_message_content(MessageContentType content_type) { case MessageContentType::DialogShared: case MessageContentType::PaidMedia: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; default: UNREACHABLE(); @@ -401,6 +405,7 @@ bool can_be_local_message_content(MessageContentType content_type) { case MessageContentType::DialogShared: case MessageContentType::PaidMedia: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; default: UNREACHABLE(); @@ -480,6 +485,7 @@ bool is_service_message_content(MessageContentType content_type) { case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return true; default: UNREACHABLE(); @@ -559,6 +565,7 @@ bool is_editable_message_content(MessageContentType content_type) { case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; default: UNREACHABLE(); @@ -702,6 +709,7 @@ bool can_have_message_content_caption(MessageContentType content_type) { case MessageContentType::BoostApply: case MessageContentType::DialogShared: case MessageContentType::PaymentRefunded: + case MessageContentType::GiftStars: return false; default: UNREACHABLE(); diff --git a/td/telegram/MessageContentType.h b/td/telegram/MessageContentType.h index 007c9ad10..f2465f1d1 100644 --- a/td/telegram/MessageContentType.h +++ b/td/telegram/MessageContentType.h @@ -82,7 +82,8 @@ enum class MessageContentType : int32 { BoostApply, DialogShared, PaidMedia, - PaymentRefunded + PaymentRefunded, + GiftStars }; // increase MessageUnsupported::CURRENT_VERSION each time a new message content type is added