Support country code restrictions for giveaways.

This commit is contained in:
levlam 2023-10-12 18:58:40 +03:00
parent 5d80e3db72
commit f31635e9a8
6 changed files with 42 additions and 16 deletions

View File

@ -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<int53> 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<int53> date:int32 only_new_subscribers:Bool country_codes:vector<string> = PremiumGiveawayParameters;
//@description File with the date it was uploaded @file The file @date Point in time (Unix timestamp) when the file was uploaded

View File

@ -47,8 +47,13 @@ Result<GiveawayParameters> 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<string>(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<telegram_api::inputStorePaymentPremiumGiveaway>(
flags, false /*ignored*/, std::move(boost_input_peer), std::move(additional_input_peers), vector<string>(),
random_id, date_, currency, amount);
flags, false /*ignored*/, std::move(boost_input_peer), std::move(additional_input_peers),
vector<string>(country_codes_), random_id, date_, currency, amount);
}
td_api::object_ptr<td_api::premiumGiveawayParameters> GiveawayParameters::get_premium_giveaway_parameters_object(
@ -100,13 +108,14 @@ td_api::object_ptr<td_api::premiumGiveawayParameters> GiveawayParameters::get_pr
td->messages_manager_->force_create_dialog(dialog_id, "premiumGiveawayParameters", true);
return td_api::make_object<td_api::premiumGiveawayParameters>(
td->messages_manager_->get_chat_id_object(dialog_id, "premiumGiveawayParameters"), std::move(chat_ids), date_,
only_new_subscribers_);
only_new_subscribers_, vector<string>(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_ << ']';
}

View File

@ -26,6 +26,7 @@ class GiveawayParameters {
vector<ChannelId> additional_channel_ids_;
bool only_new_subscribers_ = false;
int32 date_ = 0;
vector<string> country_codes_;
static Result<ChannelId> get_boosted_channel_id(Td *td, DialogId dialog_id);
@ -38,11 +39,12 @@ class GiveawayParameters {
GiveawayParameters() = default;
GiveawayParameters(ChannelId boosted_channel_id, vector<ChannelId> &&additional_channel_ids,
bool only_new_subscribers, int32 date)
bool only_new_subscribers, int32 date, vector<string> &&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<GiveawayParameters> get_giveaway_parameters(Td *td,

View File

@ -15,29 +15,39 @@ namespace td {
template <class StorerT>
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 <class ParserT>
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

View File

@ -5236,8 +5236,9 @@ unique_ptr<MessageContent> get_message_content(Td *td, FormattedText message,
}
auto boosted_channel_id = channel_ids[0];
channel_ids.erase(channel_ids.begin());
return td::make_unique<MessageGiveaway>(GiveawayParameters{boosted_channel_id, std::move(channel_ids),
media->only_new_subscribers_, media->until_date_},
return td::make_unique<MessageGiveaway>(
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:

View File

@ -997,24 +997,26 @@ class CliClient final : public Actor {
int64 chat_id = 0;
vector<int64> additional_chat_ids;
int32 date;
vector<string> country_codes;
operator td_api::object_ptr<td_api::premiumGiveawayParameters>() const {
if (chat_id == 0) {
return nullptr;
}
return td_api::make_object<td_api::premiumGiveawayParameters>(chat_id, vector<int64>(additional_chat_ids), date,
rand_bool());
rand_bool(), vector<string>(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<int32>(parts.back());
for (size_t i = 1; i + 1 < parts.size(); i++) {
arg.date = to_integer<int32>(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]));
}
}