diff options
| author | levlam <levlam@telegram.org> | 2025-07-11 20:23:44 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2025-07-11 20:23:44 +0300 |
| commit | 9db58ff18280e8782953a01d0fc11622499fb6af (patch) | |
| tree | ff2586964021b1e5c931271a54f2e4075d24b140 | |
| parent | 3c4b74c635a0b754736c4ec0b0eb976d83abbedc (diff) | |
Add class CurrencyAmount.
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | SplitSource.php | 1 | ||||
| -rw-r--r-- | td/generate/scheme/td_api.tl | 4 | ||||
| -rw-r--r-- | td/telegram/CurrencyAmount.cpp | 65 | ||||
| -rw-r--r-- | td/telegram/CurrencyAmount.h | 54 | ||||
| -rw-r--r-- | td/telegram/CurrencyAmount.hpp | 51 | ||||
| -rw-r--r-- | td/telegram/SuggestedPostPrice.cpp | 4 |
7 files changed, 178 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0000e6a20..14532386d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -393,6 +393,7 @@ set(TDLIB_SOURCE_PART1 td/telegram/ConnectionStateManager.cpp td/telegram/Contact.cpp td/telegram/CountryInfoManager.cpp + td/telegram/CurrencyAmount.cpp td/telegram/DelayDispatcher.cpp td/telegram/Dependencies.cpp td/telegram/DeviceTokenManager.cpp @@ -726,6 +727,7 @@ set(TDLIB_SOURCE_PART2 td/telegram/ConnectionStateManager.h td/telegram/Contact.h td/telegram/CountryInfoManager.h + td/telegram/CurrencyAmount.h td/telegram/CustomEmojiId.h td/telegram/DelayDispatcher.h td/telegram/Dependencies.h @@ -1076,6 +1078,7 @@ set(TDLIB_SOURCE_PART2 td/telegram/BusinessRecipients.hpp td/telegram/BusinessWorkHours.hpp td/telegram/ChatReactions.hpp + td/telegram/CurrencyAmount.hpp td/telegram/DialogFilter.hpp td/telegram/DialogInviteLink.hpp td/telegram/DialogNotificationSettings.hpp diff --git a/SplitSource.php b/SplitSource.php index 0b3b84861..08c2c8c1d 100644 --- a/SplitSource.php +++ b/SplitSource.php @@ -337,6 +337,7 @@ function split_file($file, $chunks, $undo) { 'common_dialog_manager[_(-](?![.]get[(][)])|CommonDialogManager' => 'CommonDialogManager', 'connection_state_manager[_(-](?![.]get[(][)])|ConnectionStateManager' => 'ConnectionStateManager', 'country_info_manager[_(-](?![.]get[(][)])|CountryInfoManager' => 'CountryInfoManager', + 'CurrencyAmount' => 'CurrencyAmount', 'CustomEmojiId' => 'CustomEmojiId', 'device_token_manager[_(-](?![.]get[(][)])|DeviceTokenManager' => 'DeviceTokenManager', 'DialogAction[^M]' => 'DialogAction', diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index d3cb33361..f54ed51c3 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2304,7 +2304,7 @@ factCheck text:formattedText country_code:string = FactCheck; //@has_timestamped_media True, if media timestamp entities refers to a media in this message as opposed to a media in the replied message //@is_channel_post True, if the message is a channel post. All messages to channels are channel posts, all other messages are not channel posts //@is_paid_star_suggested_post True, if the message is a suggested channel post which was paid in Telegram Stars; a warning must be shown if the message is deleted in less then 24 hours after sending -//@is_paid_ton_suggested_post True, if the message is a suggested channel post which was paid in toncoins; a warning must be shown if the message is deleted in less then 24 hours after sending +//@is_paid_ton_suggested_post True, if the message is a suggested channel post which was paid in Toncoins; a warning must be shown if the message is deleted in less then 24 hours after sending //@contains_unread_mention True, if the message contains an unread mention for the current user //@date Point in time (Unix timestamp) when the message was sent; 0 for scheduled messages //@edit_date Point in time (Unix timestamp) when the message was last edited; 0 for scheduled messages @@ -4469,7 +4469,7 @@ messageChecklistTasksAdded checklist_message_id:int53 tasks:vector<checklistTask //@description A suggested post was published for 24 hours and payment for the post was received //@suggested_post_message_id Identifier of the message with the checklist; can be 0 if the message was deleted //@star_amount The amount of received Telegram Stars -//@ton_amount The amount of received toncoins; in the smallest units of the cryptocurrency +//@ton_amount The amount of received Toncoins; in the smallest units of the cryptocurrency messageSuggestedPostPaid suggested_post_message_id:int53 star_amount:starAmount ton_amount:int64 = MessageContent; //@description A contact has registered with Telegram diff --git a/td/telegram/CurrencyAmount.cpp b/td/telegram/CurrencyAmount.cpp new file mode 100644 index 000000000..adc9dadf0 --- /dev/null +++ b/td/telegram/CurrencyAmount.cpp @@ -0,0 +1,65 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/CurrencyAmount.h" + +#include "td/utils/logging.h" + +namespace td { + +CurrencyAmount::CurrencyAmount(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr, bool allow_negative) { + if (amount_ptr == nullptr) { + return; + } + switch (amount_ptr->get_id()) { + case telegram_api::starsAmount::ID: { + auto star_amount = + StarAmount(telegram_api::move_object_as<telegram_api::starsAmount>(amount_ptr), allow_negative); + if (star_amount == StarAmount()) { + return; + } + type_ = Type::Star; + star_amount_ = star_amount; + break; + } + case telegram_api::starsTonAmount::ID: { + auto ton_amount = + TonAmount(telegram_api::move_object_as<telegram_api::starsTonAmount>(amount_ptr), allow_negative); + if (ton_amount == TonAmount()) { + return; + } + type_ = Type::Ton; + ton_amount_ = ton_amount; + break; + } + default: + UNREACHABLE(); + } +} + +bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs) { + return lhs.type_ == rhs.type_ && lhs.star_amount_ == rhs.star_amount_ && lhs.ton_amount_ == rhs.ton_amount_; +} + +bool operator!=(const CurrencyAmount &lhs, const CurrencyAmount &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount) { + switch (amount.type_) { + case CurrencyAmount::Type::None: + return string_builder << "[Free]"; + case CurrencyAmount::Type::Star: + return string_builder << '[' << amount.star_amount_ << " Stars]"; + case CurrencyAmount::Type::Ton: + return string_builder << '[' << amount.ton_amount_ << " nanotoncoins]"; + default: + UNREACHABLE(); + return string_builder; + } +} + +} // namespace td diff --git a/td/telegram/CurrencyAmount.h b/td/telegram/CurrencyAmount.h new file mode 100644 index 000000000..907e0a934 --- /dev/null +++ b/td/telegram/CurrencyAmount.h @@ -0,0 +1,54 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/StarAmount.h" +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" +#include "td/telegram/TonAmount.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class CurrencyAmount { + enum class Type : int32 { None, Star, Ton }; + Type type_ = Type::None; + StarAmount star_amount_; + TonAmount ton_amount_; + + friend bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs); + + friend StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount); + + public: + CurrencyAmount() = default; + + CurrencyAmount(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr, bool allow_negative); + + StarAmount get_star_amount() const { + return star_amount_; + } + + TonAmount get_ton_amount() const { + return ton_amount_; + } + + template <class StorerT> + void store(StorerT &storer) const; + + template <class ParserT> + void parse(ParserT &parser); +}; + +bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs); +bool operator!=(const CurrencyAmount &lhs, const CurrencyAmount &rhs); + +StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount); + +} // namespace td diff --git a/td/telegram/CurrencyAmount.hpp b/td/telegram/CurrencyAmount.hpp new file mode 100644 index 000000000..4206d760b --- /dev/null +++ b/td/telegram/CurrencyAmount.hpp @@ -0,0 +1,51 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/CurrencyAmount.h" +#include "td/telegram/StarAmount.hpp" +#include "td/telegram/TonAmount.hpp" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template <class StorerT> +void CurrencyAmount::store(StorerT &storer) const { + bool has_star_amount = star_amount_ != StarAmount(); + bool has_ton_amount = ton_amount_ != TonAmount(); + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_star_amount); + STORE_FLAG(has_ton_amount); + END_STORE_FLAGS(); + td::store(type_, storer); + if (has_star_amount) { + td::store(star_amount_, storer); + } + if (has_ton_amount) { + td::store(ton_amount_, storer); + } +} + +template <class ParserT> +void CurrencyAmount::parse(ParserT &parser) { + bool has_star_amount; + bool has_ton_amount; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_star_amount); + PARSE_FLAG(has_ton_amount); + END_PARSE_FLAGS(); + td::parse(type_, parser); + if (has_star_amount) { + td::parse(star_amount_, parser); + } + if (has_ton_amount) { + td::parse(ton_amount_, parser); + } +} + +} // namespace td diff --git a/td/telegram/SuggestedPostPrice.cpp b/td/telegram/SuggestedPostPrice.cpp index 9cc20b621..e8211b951 100644 --- a/td/telegram/SuggestedPostPrice.cpp +++ b/td/telegram/SuggestedPostPrice.cpp @@ -78,7 +78,7 @@ Result<SuggestedPostPrice> SuggestedPostPrice::get_suggested_post_price( } if (amount < td->option_manager_->get_option_integer("suggested_post_toncoin_cent_count_min") || amount > td->option_manager_->get_option_integer("suggested_post_toncoin_cent_count_max")) { - return Status::Error(400, "Invalid amount of toncoin cents specified"); + return Status::Error(400, "Invalid amount of Toncoin cents specified"); } SuggestedPostPrice result; result.type_ = Type::Ton; @@ -133,7 +133,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const SuggestedPostPric case SuggestedPostPrice::Type::Star: return string_builder << '[' << amount.amount_ << " Stars]"; case SuggestedPostPrice::Type::Ton: - return string_builder << '[' << amount.amount_ << " toncoin cents]"; + return string_builder << '[' << amount.amount_ << " Toncoin cents]"; default: UNREACHABLE(); return string_builder; |
