Skip to content

Commit 1384798

Browse files
committed
Add class SuggestedPostPrice.
1 parent f78c63c commit 1384798

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ set(TDLIB_SOURCE_PART2
625625
td/telegram/StoryViewer.cpp
626626
td/telegram/SuggestedAction.cpp
627627
td/telegram/SuggestedActionManager.cpp
628+
td/telegram/SuggestedPostPrice.cpp
628629
td/telegram/Support.cpp
629630
td/telegram/SynchronousRequests.cpp
630631
td/telegram/TargetDialogTypes.cpp
@@ -1013,6 +1014,7 @@ set(TDLIB_SOURCE_PART2
10131014
td/telegram/StoryViewer.h
10141015
td/telegram/SuggestedAction.h
10151016
td/telegram/SuggestedActionManager.h
1017+
td/telegram/SuggestedPostPrice.h
10161018
td/telegram/Support.h
10171019
td/telegram/SynchronousRequests.h
10181020
td/telegram/TargetDialogTypes.h
@@ -1144,6 +1146,7 @@ set(TDLIB_SOURCE_PART2
11441146
td/telegram/StoryInteractionInfo.hpp
11451147
td/telegram/StoryStealthMode.hpp
11461148
td/telegram/SuggestedAction.hpp
1149+
td/telegram/SuggestedPostPrice.hpp
11471150
td/telegram/TermsOfService.hpp
11481151
td/telegram/ThemeSettings.hpp
11491152
td/telegram/ToDoCompletion.hpp

SplitSource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ function ($matches) use ($needed_std_headers) {
446446
'story_manager[_(-](?![.]get[(][)])|StoryManager' => 'StoryManager',
447447
'SuggestedAction|[a-z_]*_suggested_action' => 'SuggestedAction',
448448
'suggested_action_manager[_(-](?![.]get[(][)])|SuggestedActionManager' => 'SuggestedActionManager',
449+
'SuggestedPostPrice' => 'SuggestedPostPrice',
449450
'SynchronousRequests' => 'SynchronousRequests',
450451
'TargetDialogTypes' => 'TargetDialogTypes',
451452
'td_api' => 'td_api',

td/telegram/SuggestedPostPrice.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

td/telegram/SuggestedPostPrice.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#pragma once
8+
9+
#include "td/telegram/telegram_api.h"
10+
11+
#include "td/utils/common.h"
12+
#include "td/utils/StringBuilder.h"
13+
14+
namespace td {
15+
16+
class SuggestedPostPrice {
17+
enum class Type : int32 { None, Star, Ton };
18+
Type type_ = Type::None;
19+
int64 amount_ = 0;
20+
21+
static constexpr int64 TON_MULTIPLIER = 10000000;
22+
23+
friend bool operator==(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs);
24+
25+
friend StringBuilder &operator<<(StringBuilder &string_builder, const SuggestedPostPrice &amount);
26+
27+
public:
28+
SuggestedPostPrice() = default;
29+
30+
explicit SuggestedPostPrice(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr);
31+
32+
telegram_api::object_ptr<telegram_api::StarsAmount> get_input_stars_amount() const;
33+
34+
template <class StorerT>
35+
void store(StorerT &storer) const;
36+
37+
template <class ParserT>
38+
void parse(ParserT &parser);
39+
};
40+
41+
bool operator==(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs);
42+
bool operator!=(const SuggestedPostPrice &lhs, const SuggestedPostPrice &rhs);
43+
44+
StringBuilder &operator<<(StringBuilder &string_builder, const SuggestedPostPrice &amount);
45+
46+
} // namespace td

td/telegram/SuggestedPostPrice.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#pragma once
8+
9+
#include "td/telegram/SuggestedPostPrice.h"
10+
11+
#include "td/utils/tl_helpers.h"
12+
13+
namespace td {
14+
15+
template <class StorerT>
16+
void SuggestedPostPrice::store(StorerT &storer) const {
17+
bool has_amount = amount_ != 0;
18+
BEGIN_STORE_FLAGS();
19+
STORE_FLAG(has_amount);
20+
END_STORE_FLAGS();
21+
store(type_, storer);
22+
if (has_amount) {
23+
store(amount_, storer);
24+
}
25+
}
26+
27+
template <class ParserT>
28+
void SuggestedPostPrice::parse(ParserT &parser) {
29+
bool has_amount;
30+
BEGIN_PARSE_FLAGS();
31+
PARSE_FLAG(has_amount);
32+
END_PARSE_FLAGS();
33+
parse(type_, parser);
34+
if (has_amount) {
35+
parse(amount_, parser);
36+
}
37+
}
38+
39+
} // namespace td

0 commit comments

Comments
 (0)