// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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" #include "td/telegram/ContactsManager.h" #include "td/telegram/Dependencies.h" #include "td/telegram/DialogId.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/OptionManager.h" #include "td/telegram/Td.h" #include "td/utils/Random.h" namespace td { Result GiveawayParameters::get_boosted_channel_id(Td *td, DialogId dialog_id) { if (!td->messages_manager_->have_dialog_force(dialog_id, "get_boosted_channel_id")) { 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::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 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); } if (additional_channel_ids.size() > td->option_manager_->get_option_integer("giveaway_additional_chat_count_max")) { return Status::Error(400, "Too many additional chats specified"); } if (parameters->winners_selection_date_ < G()->unix_time()) { return Status::Error(400, "Giveaway date is in the past"); } 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"); } } if (parameters->country_codes_.size() > td->option_manager_->get_option_integer("giveaway_country_count_max")) { return Status::Error(400, "Too many countries specified"); } return GiveawayParameters(boosted_channel_id, std::move(additional_channel_ids), parameters->only_new_subscribers_, parameters->winners_selection_date_, vector(parameters->country_codes_)); } 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 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); auto boost_input_peer = td->messages_manager_->get_input_peer(DialogId(boosted_channel_id_), AccessRights::Write); CHECK(boost_input_peer != nullptr); vector> additional_input_peers; for (auto additional_channel_id : additional_channel_ids_) { auto input_peer = td->messages_manager_->get_input_peer(DialogId(additional_channel_id), AccessRights::Write); 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; } if (!additional_input_peers.empty()) { flags |= telegram_api::inputStorePaymentPremiumGiveaway::ADDITIONAL_PEERS_MASK; } if (!country_codes_.empty()) { flags |= telegram_api::inputStorePaymentPremiumGiveaway::COUNTRIES_ISO2_MASK; } return telegram_api::make_object( flags, false /*ignored*/, std::move(boost_input_peer), std::move(additional_input_peers), vector(country_codes_), random_id, date_, currency, amount); } td_api::object_ptr GiveawayParameters::get_premium_giveaway_parameters_object( Td *td) const { CHECK(is_valid()); vector chat_ids; for (auto channel_id : additional_channel_ids_) { DialogId dialog_id(channel_id); td->messages_manager_->force_create_dialog(dialog_id, "premiumGiveawayParameters", true); chat_ids.push_back(td->messages_manager_->get_chat_id_object(dialog_id, "premiumGiveawayParameters")); } DialogId dialog_id(boosted_channel_id_); td->messages_manager_->force_create_dialog(dialog_id, "premiumGiveawayParameters", true); return td_api::make_object( td->messages_manager_->get_chat_id_object(dialog_id, "premiumGiveawayParameters"), std::move(chat_ids), date_, only_new_subscribers_, vector(country_codes_)); } 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_ && lhs.only_new_subscribers_ == rhs.only_new_subscribers_ && lhs.date_ == rhs.date_ && lhs.country_codes_ == rhs.country_codes_; } 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_ << (giveaway_parameters.only_new_subscribers_ ? " only for new subscribes" : "") << " for countries " << giveaway_parameters.country_codes_ << " at " << giveaway_parameters.date_ << ']'; } } // namespace td