aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/SuggestedPostPrice.cpp
blob: 3cc23a802538979e6a64451d552d1a850e849561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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/SuggestedPostPrice.h"

#include "td/telegram/OptionManager.h"
#include "td/telegram/StarAmount.h"
#include "td/telegram/Td.h"
#include "td/telegram/TonAmount.h"

#include "td/utils/logging.h"

namespace td {

SuggestedPostPrice::SuggestedPostPrice(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr) {
  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), false);
      if (star_amount.get_nanostar_count() != 0) {
        LOG(ERROR) << "Receive price of " << star_amount << " Telegram Stars";
      }
      if (star_amount.get_star_count() == 0) {
        return;
      }
      type_ = Type::Star;
      amount_ = star_amount.get_star_count();
      break;
    }
    case telegram_api::starsTonAmount::ID: {
      auto ton_amount =
          TonAmount(telegram_api::move_object_as<telegram_api::starsTonAmount>(amount_ptr), false).get_ton_amount();
      if (ton_amount % TON_MULTIPLIER != 0) {
        LOG(ERROR) << "Receive price of " << ton_amount << " Grams";
      }
      ton_amount /= TON_MULTIPLIER;
      if (ton_amount == 0) {
        return;
      }
      type_ = Type::Ton;
      amount_ = ton_amount;
      break;
    }
    default:
      UNREACHABLE();
  }
}

Result<SuggestedPostPrice> SuggestedPostPrice::get_suggested_post_price(
    const Td *td, td_api::object_ptr<td_api::SuggestedPostPrice> &&price) {
  if (price == nullptr) {
    return SuggestedPostPrice();
  }
  switch (price->get_id()) {
    case td_api::suggestedPostPriceStar::ID: {
      auto amount = static_cast<const td_api::suggestedPostPriceStar *>(price.get())->star_count_;
      if (amount == 0) {
        return SuggestedPostPrice();
      }
      if (amount < td->option_manager_->get_option_integer("suggested_post_star_count_min") ||
          amount > td->option_manager_->get_option_integer("suggested_post_star_count_max")) {
        return Status::Error(400, "Invalid amount of Telegram Stars specified");
      }
      SuggestedPostPrice result;
      result.type_ = Type::Star;
      result.amount_ = amount;
      return result;
    }
    case td_api::suggestedPostPriceGram::ID: {
      auto amount = static_cast<const td_api::suggestedPostPriceGram *>(price.get())->gram_cent_count_;
      if (amount == 0) {
        return SuggestedPostPrice();
      }
      if (amount < td->option_manager_->get_option_integer("suggested_post_gram_cent_count_min") ||
          amount > td->option_manager_->get_option_integer("suggested_post_gram_cent_count_max")) {
        return Status::Error(400, "Invalid amount of Gram cents specified");
      }
      SuggestedPostPrice result;
      result.type_ = Type::Ton;
      result.amount_ = amount;
      return result;
    }
    default:
      UNREACHABLE();
  }
}

telegram_api::object_ptr<telegram_api::StarsAmount> SuggestedPostPrice::get_input_stars_amount() const {
  switch (type_) {
    case Type::None:
      return nullptr;
    case Type::Star:
      return telegram_api::make_object<telegram_api::starsAmount>(amount_, 0);
    case Type::Ton:
      return telegram_api::make_object<telegram_api::starsTonAmount>(amount_ * TON_MULTIPLIER);
    default:
      UNREACHABLE();
      return nullptr;
  }
}

td_api::object_ptr<td_api::SuggestedPostPrice> SuggestedPostPrice::get_suggested_post_price_object() const {
  switch (type_) {
    case Type::None:
      return nullptr;
    case Type::Star:
      return td_api::make_object<td_api::suggestedPostPriceStar>(amount_);
    case Type::Ton:
      return td_api::make_object<td_api::suggestedPostPriceGram>(amount_);
    default:
      UNREACHABLE();
      return nullptr;
  }
}

bool operator==(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs) {
  return lhs.type_ == rhs.type_ && lhs.amount_ == rhs.amount_;
}

bool operator!=(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs) {
  return !(lhs == rhs);
}

StringBuilder &operator<<(StringBuilder &string_builder, const SuggestedPostPrice &amount) {
  switch (amount.type_) {
    case SuggestedPostPrice::Type::None:
      return string_builder << "[Free]";
    case SuggestedPostPrice::Type::Star:
      return string_builder << '[' << amount.amount_ << " Stars]";
    case SuggestedPostPrice::Type::Ton:
      return string_builder << '[' << amount.amount_ << " Gram cents]";
    default:
      UNREACHABLE();
      return string_builder;
  }
}

}  // namespace td