From f31635e9a83ea78a8b49cc4d1ddfac51b0547234 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 12 Oct 2023 18:58:40 +0300 Subject: [PATCH] Support country code restrictions for giveaways. --- td/generate/scheme/td_api.tl | 3 ++- td/telegram/GiveawayParameters.cpp | 22 ++++++++++++++++------ td/telegram/GiveawayParameters.h | 6 ++++-- td/telegram/GiveawayParameters.hpp | 10 ++++++++++ td/telegram/MessageContent.cpp | 7 ++++--- td/telegram/cli.cpp | 10 ++++++---- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index fe5a31edc..0812922c7 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2298,7 +2298,8 @@ messageExtendedMediaUnsupported caption:formattedText = MessageExtendedMedia; //@additional_chat_ids Identifiers of other channel chats that must be subscribed by the users to be eligible for the giveaway //@date Point in time (Unix timestamp) when the giveaway will be performed; must be from 1 minute to 365 days in the future in scheduled giveaways //@only_new_subscribers True, if only new subscribers of the chats will be eligible for the giveaway -premiumGiveawayParameters boosted_chat_id:int53 additional_chat_ids:vector date:int32 only_new_subscribers:Bool = PremiumGiveawayParameters; +//@country_codes The list of two-letter ISO 3166-1 alpha-2 codes of countries, only users from which will be eligible for the giveaway +premiumGiveawayParameters boosted_chat_id:int53 additional_chat_ids:vector date:int32 only_new_subscribers:Bool country_codes:vector = PremiumGiveawayParameters; //@description File with the date it was uploaded @file The file @date Point in time (Unix timestamp) when the file was uploaded diff --git a/td/telegram/GiveawayParameters.cpp b/td/telegram/GiveawayParameters.cpp index 1f34df059..e5a79f40a 100644 --- a/td/telegram/GiveawayParameters.cpp +++ b/td/telegram/GiveawayParameters.cpp @@ -47,8 +47,13 @@ Result GiveawayParameters::get_giveaway_parameters( if (parameters->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"); + } + } return GiveawayParameters(boosted_channel_id, std::move(additional_channel_ids), parameters->only_new_subscribers_, - parameters->date_); + parameters->date_, vector(parameters->country_codes_)); } void GiveawayParameters::add_dependencies(Dependencies &dependencies) const { @@ -82,9 +87,12 @@ GiveawayParameters::get_input_store_payment_premium_giveaway(Td *td, const strin 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(), - random_id, date_, currency, amount); + 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( @@ -100,13 +108,14 @@ td_api::object_ptr GiveawayParameters::get_pr 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_); + 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.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) { @@ -116,7 +125,8 @@ bool operator!=(const GiveawayParameters &lhs, const GiveawayParameters &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" : "") << " at " + << (giveaway_parameters.only_new_subscribers_ ? " only for new subscribes" : "") + << " for countries " << giveaway_parameters.country_codes_ << " at " << giveaway_parameters.date_ << ']'; } diff --git a/td/telegram/GiveawayParameters.h b/td/telegram/GiveawayParameters.h index 87cd9a99f..52dd9e348 100644 --- a/td/telegram/GiveawayParameters.h +++ b/td/telegram/GiveawayParameters.h @@ -26,6 +26,7 @@ class GiveawayParameters { vector additional_channel_ids_; bool only_new_subscribers_ = false; int32 date_ = 0; + vector country_codes_; static Result get_boosted_channel_id(Td *td, DialogId dialog_id); @@ -38,11 +39,12 @@ class GiveawayParameters { GiveawayParameters() = default; GiveawayParameters(ChannelId boosted_channel_id, vector &&additional_channel_ids, - bool only_new_subscribers, int32 date) + bool only_new_subscribers, int32 date, vector &&country_codes) : boosted_channel_id_(boosted_channel_id) , additional_channel_ids_(std::move(additional_channel_ids)) , only_new_subscribers_(only_new_subscribers) - , date_(date) { + , date_(date) + , country_codes_(std::move(country_codes)) { } static Result get_giveaway_parameters(Td *td, diff --git a/td/telegram/GiveawayParameters.hpp b/td/telegram/GiveawayParameters.hpp index cbe52b07e..64a21a4df 100644 --- a/td/telegram/GiveawayParameters.hpp +++ b/td/telegram/GiveawayParameters.hpp @@ -15,29 +15,39 @@ namespace td { template void GiveawayParameters::store(StorerT &storer) const { bool has_additional_channel_ids = !additional_channel_ids_.empty(); + bool has_country_codes = !country_codes_.empty(); BEGIN_STORE_FLAGS(); STORE_FLAG(only_new_subscribers_); STORE_FLAG(has_additional_channel_ids); + STORE_FLAG(has_country_codes); END_STORE_FLAGS(); td::store(boosted_channel_id_, storer); if (has_additional_channel_ids) { td::store(additional_channel_ids_, storer); } td::store(date_, storer); + if (has_country_codes) { + td::store(country_codes_, storer); + } } template void GiveawayParameters::parse(ParserT &parser) { bool has_additional_channel_ids; + bool has_country_codes; BEGIN_PARSE_FLAGS(); PARSE_FLAG(only_new_subscribers_); PARSE_FLAG(has_additional_channel_ids); + PARSE_FLAG(has_country_codes); END_PARSE_FLAGS(); td::parse(boosted_channel_id_, parser); if (has_additional_channel_ids) { td::parse(additional_channel_ids_, parser); } td::parse(date_, parser); + if (has_country_codes) { + td::parse(country_codes_, parser); + } } } // namespace td diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 78fb24840..fd3b96cbe 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -5236,9 +5236,10 @@ unique_ptr get_message_content(Td *td, FormattedText message, } auto boosted_channel_id = channel_ids[0]; channel_ids.erase(channel_ids.begin()); - return td::make_unique(GiveawayParameters{boosted_channel_id, std::move(channel_ids), - media->only_new_subscribers_, media->until_date_}, - media->quantity_, media->months_); + return td::make_unique( + GiveawayParameters{boosted_channel_id, std::move(channel_ids), media->only_new_subscribers_, + media->until_date_, std::move(media->countries_iso2_)}, + media->quantity_, media->months_); } case telegram_api::messageMediaUnsupported::ID: return make_unique(); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 2e892611e..d4dbc13bf 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -997,24 +997,26 @@ class CliClient final : public Actor { int64 chat_id = 0; vector additional_chat_ids; int32 date; + vector country_codes; operator td_api::object_ptr() const { if (chat_id == 0) { return nullptr; } return td_api::make_object(chat_id, vector(additional_chat_ids), date, - rand_bool()); + rand_bool(), vector(country_codes)); } }; void get_args(string &args, PremiumGiveawayParameters &arg) const { auto parts = autosplit(args); - if (args.size() < 2) { + if (args.size() < 3) { return; } arg.chat_id = as_chat_id(parts[0]); - arg.date = to_integer(parts.back()); - for (size_t i = 1; i + 1 < parts.size(); i++) { + arg.date = to_integer(parts[parts.size() - 2]); + arg.country_codes.push_back(parts[parts.size() - 1].str()); + for (size_t i = 1; i + 2 < parts.size(); i++) { arg.additional_chat_ids.push_back(as_chat_id(parts[i])); } }