2023-10-12 16:05:03 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2023-10-12 16:05:03 +02:00
|
|
|
//
|
|
|
|
// 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/GiveawayParameters.h"
|
|
|
|
|
2023-10-30 17:04:14 +01:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2023-10-12 16:05:03 +02:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/Dependencies.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
2024-01-03 21:07:50 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2023-10-30 17:04:14 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2023-12-05 11:45:32 +01:00
|
|
|
#include "td/telegram/misc.h"
|
2023-10-13 12:39:14 +02:00
|
|
|
#include "td/telegram/OptionManager.h"
|
2023-10-12 16:05:03 +02:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
Result<ChannelId> GiveawayParameters::get_boosted_channel_id(Td *td, DialogId dialog_id) {
|
2024-01-04 13:26:42 +01:00
|
|
|
if (!td->dialog_manager_->have_dialog_force(dialog_id, "get_boosted_channel_id")) {
|
2023-10-12 16:05:03 +02:00
|
|
|
return Status::Error(400, "Chat to boost not found");
|
|
|
|
}
|
|
|
|
if (dialog_id.get_type() != DialogType::Channel) {
|
|
|
|
return Status::Error(400, "Can't boost the chat");
|
|
|
|
}
|
|
|
|
auto channel_id = dialog_id.get_channel_id();
|
|
|
|
if (!td->contacts_manager_->is_broadcast_channel(channel_id)) {
|
|
|
|
return Status::Error(400, "Can't boost the group");
|
|
|
|
}
|
|
|
|
if (!td->contacts_manager_->get_channel_status(channel_id).can_post_messages()) {
|
|
|
|
return Status::Error(400, "Not enough rights in the chat");
|
|
|
|
}
|
|
|
|
return channel_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<GiveawayParameters> GiveawayParameters::get_giveaway_parameters(
|
|
|
|
Td *td, const td_api::premiumGiveawayParameters *parameters) {
|
|
|
|
if (parameters == nullptr) {
|
|
|
|
return Status::Error(400, "Giveaway parameters must be non-empty");
|
|
|
|
}
|
|
|
|
TRY_RESULT(boosted_channel_id, get_boosted_channel_id(td, DialogId(parameters->boosted_chat_id_)));
|
|
|
|
vector<ChannelId> additional_channel_ids;
|
|
|
|
for (auto additional_chat_id : parameters->additional_chat_ids_) {
|
|
|
|
TRY_RESULT(channel_id, get_boosted_channel_id(td, DialogId(additional_chat_id)));
|
|
|
|
additional_channel_ids.push_back(channel_id);
|
|
|
|
}
|
2023-10-21 02:20:18 +02:00
|
|
|
if (static_cast<int64>(additional_channel_ids.size()) >
|
|
|
|
td->option_manager_->get_option_integer("giveaway_additional_chat_count_max")) {
|
2023-10-13 12:39:14 +02:00
|
|
|
return Status::Error(400, "Too many additional chats specified");
|
|
|
|
}
|
2023-10-13 10:38:22 +02:00
|
|
|
if (parameters->winners_selection_date_ < G()->unix_time()) {
|
2023-10-12 16:05:03 +02:00
|
|
|
return Status::Error(400, "Giveaway date is in the past");
|
|
|
|
}
|
2023-10-12 17:58:40 +02:00
|
|
|
for (auto &country_code : parameters->country_codes_) {
|
|
|
|
if (country_code.size() != 2 || country_code[0] < 'A' || country_code[0] > 'Z') {
|
|
|
|
return Status::Error(400, "Invalid country code specified");
|
|
|
|
}
|
|
|
|
}
|
2023-10-21 02:20:18 +02:00
|
|
|
if (static_cast<int64>(parameters->country_codes_.size()) >
|
|
|
|
td->option_manager_->get_option_integer("giveaway_country_count_max")) {
|
2023-10-13 13:06:05 +02:00
|
|
|
return Status::Error(400, "Too many countries specified");
|
|
|
|
}
|
2023-12-05 11:45:32 +01:00
|
|
|
auto prize_description = parameters->prize_description_;
|
|
|
|
if (!clean_input_string(prize_description)) {
|
|
|
|
return Status::Error(400, "Strings must be encoded in UTF-8");
|
|
|
|
}
|
2023-11-06 11:51:49 +01:00
|
|
|
return GiveawayParameters(boosted_channel_id, std::move(additional_channel_ids), parameters->only_new_members_,
|
2023-12-05 11:45:32 +01:00
|
|
|
parameters->has_public_winners_, parameters->winners_selection_date_,
|
|
|
|
vector<string>(parameters->country_codes_), std::move(prize_description));
|
2023-10-12 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 23:34:33 +02:00
|
|
|
vector<ChannelId> GiveawayParameters::get_channel_ids() const {
|
|
|
|
auto result = additional_channel_ids_;
|
|
|
|
result.push_back(boosted_channel_id_);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:05:03 +02:00
|
|
|
void GiveawayParameters::add_dependencies(Dependencies &dependencies) const {
|
|
|
|
dependencies.add_dialog_and_dependencies(DialogId(boosted_channel_id_));
|
|
|
|
for (auto channel_id : additional_channel_ids_) {
|
|
|
|
dependencies.add_dialog_and_dependencies(DialogId(channel_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
telegram_api::object_ptr<telegram_api::inputStorePaymentPremiumGiveaway>
|
|
|
|
GiveawayParameters::get_input_store_payment_premium_giveaway(Td *td, const string ¤cy, int64 amount) const {
|
|
|
|
int64 random_id;
|
|
|
|
do {
|
|
|
|
random_id = Random::secure_int64();
|
|
|
|
} while (random_id == 0);
|
|
|
|
|
2024-01-03 21:07:50 +01:00
|
|
|
auto boost_input_peer = td->dialog_manager_->get_input_peer(DialogId(boosted_channel_id_), AccessRights::Write);
|
2023-10-12 16:05:03 +02:00
|
|
|
CHECK(boost_input_peer != nullptr);
|
|
|
|
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputPeer>> additional_input_peers;
|
|
|
|
for (auto additional_channel_id : additional_channel_ids_) {
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td->dialog_manager_->get_input_peer(DialogId(additional_channel_id), AccessRights::Write);
|
2023-10-12 16:05:03 +02:00
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
additional_input_peers.push_back(std::move(input_peer));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (only_new_subscribers_) {
|
|
|
|
flags |= telegram_api::inputStorePaymentPremiumGiveaway::ONLY_NEW_SUBSCRIBERS_MASK;
|
|
|
|
}
|
2023-12-05 11:45:32 +01:00
|
|
|
if (winners_are_visible_) {
|
|
|
|
flags |= telegram_api::inputStorePaymentPremiumGiveaway::WINNERS_ARE_VISIBLE_MASK;
|
|
|
|
}
|
2023-10-12 16:05:03 +02:00
|
|
|
if (!additional_input_peers.empty()) {
|
|
|
|
flags |= telegram_api::inputStorePaymentPremiumGiveaway::ADDITIONAL_PEERS_MASK;
|
|
|
|
}
|
2023-10-12 17:58:40 +02:00
|
|
|
if (!country_codes_.empty()) {
|
|
|
|
flags |= telegram_api::inputStorePaymentPremiumGiveaway::COUNTRIES_ISO2_MASK;
|
|
|
|
}
|
2023-12-05 11:45:32 +01:00
|
|
|
if (!prize_description_.empty()) {
|
|
|
|
flags |= telegram_api::inputStorePaymentPremiumGiveaway::PRIZE_DESCRIPTION_MASK;
|
|
|
|
}
|
2023-10-12 16:05:03 +02:00
|
|
|
return telegram_api::make_object<telegram_api::inputStorePaymentPremiumGiveaway>(
|
2023-12-05 10:30:12 +01:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, std::move(boost_input_peer), std::move(additional_input_peers),
|
2023-12-05 11:45:32 +01:00
|
|
|
vector<string>(country_codes_), prize_description_, random_id, date_, currency, amount);
|
2023-10-12 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::premiumGiveawayParameters> GiveawayParameters::get_premium_giveaway_parameters_object(
|
|
|
|
Td *td) const {
|
|
|
|
CHECK(is_valid());
|
|
|
|
vector<int64> chat_ids;
|
|
|
|
for (auto channel_id : additional_channel_ids_) {
|
|
|
|
DialogId dialog_id(channel_id);
|
2024-01-04 13:38:01 +01:00
|
|
|
td->dialog_manager_->force_create_dialog(dialog_id, "premiumGiveawayParameters", true);
|
2024-01-04 14:13:20 +01:00
|
|
|
chat_ids.push_back(td->dialog_manager_->get_chat_id_object(dialog_id, "premiumGiveawayParameters"));
|
2023-10-12 16:05:03 +02:00
|
|
|
}
|
|
|
|
DialogId dialog_id(boosted_channel_id_);
|
2024-01-04 13:38:01 +01:00
|
|
|
td->dialog_manager_->force_create_dialog(dialog_id, "premiumGiveawayParameters", true);
|
2023-10-12 16:05:03 +02:00
|
|
|
return td_api::make_object<td_api::premiumGiveawayParameters>(
|
2024-01-04 14:13:20 +01:00
|
|
|
td->dialog_manager_->get_chat_id_object(dialog_id, "premiumGiveawayParameters"), std::move(chat_ids), date_,
|
2023-12-05 11:45:32 +01:00
|
|
|
only_new_subscribers_, winners_are_visible_, vector<string>(country_codes_), prize_description_);
|
2023-10-12 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const GiveawayParameters &lhs, const GiveawayParameters &rhs) {
|
|
|
|
return lhs.boosted_channel_id_ == rhs.boosted_channel_id_ &&
|
|
|
|
lhs.additional_channel_ids_ == rhs.additional_channel_ids_ &&
|
2023-12-05 11:45:32 +01:00
|
|
|
lhs.only_new_subscribers_ == rhs.only_new_subscribers_ &&
|
|
|
|
lhs.winners_are_visible_ == rhs.winners_are_visible_ && lhs.date_ == rhs.date_ &&
|
|
|
|
lhs.country_codes_ == rhs.country_codes_ && lhs.prize_description_ == rhs.prize_description_;
|
2023-10-12 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const GiveawayParameters &lhs, const GiveawayParameters &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const GiveawayParameters &giveaway_parameters) {
|
|
|
|
return string_builder << "Giveaway[" << giveaway_parameters.boosted_channel_id_ << " + "
|
|
|
|
<< giveaway_parameters.additional_channel_ids_
|
2023-11-06 11:51:49 +01:00
|
|
|
<< (giveaway_parameters.only_new_subscribers_ ? " only for new members" : "")
|
2023-12-05 11:45:32 +01:00
|
|
|
<< (giveaway_parameters.winners_are_visible_ ? " with public list of winners" : "")
|
2023-10-12 17:58:40 +02:00
|
|
|
<< " for countries " << giveaway_parameters.country_codes_ << " at "
|
2023-10-12 16:05:03 +02:00
|
|
|
<< giveaway_parameters.date_ << ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|