|
| 1 | +// |
| 2 | +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025 |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +#include "td/telegram/SuggestedPostPrice.h" |
| 8 | + |
| 9 | +#include "td/telegram/StarAmount.h" |
| 10 | +#include "td/telegram/TonAmount.h" |
| 11 | + |
| 12 | +#include "td/utils/logging.h" |
| 13 | + |
| 14 | +namespace td { |
| 15 | + |
| 16 | +SuggestedPostPrice::SuggestedPostPrice(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr) { |
| 17 | + if (amount_ptr == nullptr) { |
| 18 | + return; |
| 19 | + } |
| 20 | + switch (amount_ptr->get_id()) { |
| 21 | + case telegram_api::starsAmount::ID: { |
| 22 | + auto star_amount = StarAmount(telegram_api::move_object_as<telegram_api::starsAmount>(amount_ptr), false); |
| 23 | + if (star_amount.get_nanostar_count() != 0) { |
| 24 | + LOG(ERROR) << "Receive suggested post price of " << star_amount << " Telegram Stars"; |
| 25 | + } |
| 26 | + type_ = Type::Star; |
| 27 | + amount_ = star_amount.get_star_count(); |
| 28 | + break; |
| 29 | + } |
| 30 | + case telegram_api::starsTonAmount::ID: { |
| 31 | + auto ton_amount = |
| 32 | + TonAmount(telegram_api::move_object_as<telegram_api::starsTonAmount>(amount_ptr), false).get_ton_amount(); |
| 33 | + if (ton_amount % TON_MULTIPLIER != 0) { |
| 34 | + LOG(ERROR) << "Receive suggested post price of " << ton_amount << " TONs"; |
| 35 | + } |
| 36 | + type_ = Type::Ton; |
| 37 | + amount_ = ton_amount / TON_MULTIPLIER; |
| 38 | + break; |
| 39 | + } |
| 40 | + default: |
| 41 | + UNREACHABLE(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +telegram_api::object_ptr<telegram_api::StarsAmount> SuggestedPostPrice::get_input_stars_amount() const { |
| 46 | + switch (type_) { |
| 47 | + case Type::None: |
| 48 | + return nullptr; |
| 49 | + case Type::Star: |
| 50 | + return telegram_api::make_object<telegram_api::starsAmount>(amount_, 0); |
| 51 | + case Type::Ton: |
| 52 | + return telegram_api::make_object<telegram_api::starsTonAmount>(amount_ * TON_MULTIPLIER); |
| 53 | + default: |
| 54 | + UNREACHABLE(); |
| 55 | + return nullptr; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +bool operator==(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs) { |
| 60 | + return lhs.type_ == rhs.type_ && lhs.amount_ == rhs.amount_; |
| 61 | +} |
| 62 | + |
| 63 | +bool operator!=(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs) { |
| 64 | + return !(lhs == rhs); |
| 65 | +} |
| 66 | + |
| 67 | +StringBuilder &operator<<(StringBuilder &string_builder, const SuggestedPostPrice &amount) { |
| 68 | + switch (amount.type_) { |
| 69 | + case SuggestedPostPrice::Type::None: |
| 70 | + return string_builder << "[Free]"; |
| 71 | + case SuggestedPostPrice::Type::Star: |
| 72 | + return string_builder << '[' << amount.amount_ << " Stars]"; |
| 73 | + case SuggestedPostPrice::Type::Ton: |
| 74 | + return string_builder << '[' << amount.amount_ << " toncoin cents]"; |
| 75 | + default: |
| 76 | + UNREACHABLE(); |
| 77 | + return string_builder; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace td |
0 commit comments