From 9913390bfe30fa76667028d36975516b146cc178 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 20 Mar 2024 19:12:35 +0300 Subject: [PATCH] Support businessRecipients.excluded_chat_ids. --- td/generate/scheme/td_api.tl | 3 +- td/telegram/BusinessAwayMessage.cpp | 2 +- td/telegram/BusinessConnectedBot.cpp | 2 +- td/telegram/BusinessGreetingMessage.cpp | 2 +- td/telegram/BusinessRecipients.cpp | 57 +++++++++++++++++++------ td/telegram/BusinessRecipients.h | 3 +- td/telegram/BusinessRecipients.hpp | 10 +++++ td/telegram/cli.cpp | 4 +- 8 files changed, 64 insertions(+), 19 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index d0d142691..14489c8d8 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -595,12 +595,13 @@ businessLocation location:location address:string = BusinessLocation; //@description Describes private chats chosen for automatic interaction with a business //@chat_ids Identifiers of selected private chats +//@excluded_chat_ids Identifiers of private chats that are always excluded; for businessConnectedBot only //@select_existing_chats True, if all existing private chats are selected //@select_new_chats True, if all new private chats are selected //@select_contacts True, if all private chats with contacts are selected //@select_non_contacts True, if all private chats with non-contacts are selected //@exclude_selected If true, then all private chats except the selected are chosen. Otherwise, only the selected chats are chosen -businessRecipients chat_ids:vector select_existing_chats:Bool select_new_chats:Bool select_contacts:Bool select_non_contacts:Bool exclude_selected:Bool = BusinessRecipients; +businessRecipients chat_ids:vector excluded_chat_ids:vector select_existing_chats:Bool select_new_chats:Bool select_contacts:Bool select_non_contacts:Bool exclude_selected:Bool = BusinessRecipients; //@description Describes settings for messages that are automatically sent by a Telegram Business account when it is away //@shortcut_id Unique quick reply shortcut identifier for the away messages diff --git a/td/telegram/BusinessAwayMessage.cpp b/td/telegram/BusinessAwayMessage.cpp index 63ffe920e..fc057d907 100644 --- a/td/telegram/BusinessAwayMessage.cpp +++ b/td/telegram/BusinessAwayMessage.cpp @@ -23,7 +23,7 @@ BusinessAwayMessage::BusinessAwayMessage(td_api::object_ptrshortcut_id_); - recipients_ = BusinessRecipients(std::move(away_message->recipients_)); + recipients_ = BusinessRecipients(std::move(away_message->recipients_), false); schedule_ = BusinessAwayMessageSchedule(std::move(away_message->schedule_)); offline_only_ = away_message->offline_only_; } diff --git a/td/telegram/BusinessConnectedBot.cpp b/td/telegram/BusinessConnectedBot.cpp index 308705fb5..7578e5614 100644 --- a/td/telegram/BusinessConnectedBot.cpp +++ b/td/telegram/BusinessConnectedBot.cpp @@ -23,7 +23,7 @@ BusinessConnectedBot::BusinessConnectedBot(td_api::object_ptrbot_user_id_); - recipients_ = BusinessRecipients(std::move(connected_bot->recipients_)); + recipients_ = BusinessRecipients(std::move(connected_bot->recipients_), true); can_reply_ = connected_bot->can_reply_; } diff --git a/td/telegram/BusinessGreetingMessage.cpp b/td/telegram/BusinessGreetingMessage.cpp index 59393d96a..f37f446fe 100644 --- a/td/telegram/BusinessGreetingMessage.cpp +++ b/td/telegram/BusinessGreetingMessage.cpp @@ -30,7 +30,7 @@ BusinessGreetingMessage::BusinessGreetingMessage( return; } shortcut_id_ = QuickReplyShortcutId(greeting_message->shortcut_id_); - recipients_ = BusinessRecipients(std::move(greeting_message->recipients_)); + recipients_ = BusinessRecipients(std::move(greeting_message->recipients_), false); inactivity_days_ = inactivity_days; } diff --git a/td/telegram/BusinessRecipients.cpp b/td/telegram/BusinessRecipients.cpp index 5b7a76100..a4fbce75d 100644 --- a/td/telegram/BusinessRecipients.cpp +++ b/td/telegram/BusinessRecipients.cpp @@ -27,15 +27,17 @@ BusinessRecipients::BusinessRecipients(telegram_api::object_ptr recipients) : user_ids_(UserId::get_user_ids(recipients->users_)) + , excluded_user_ids_(UserId::get_user_ids(recipients->exclude_users_)) , existing_chats_(recipients->existing_chats_) , new_chats_(recipients->new_chats_) , contacts_(recipients->contacts_) , non_contacts_(recipients->non_contacts_) , exclude_selected_(recipients->exclude_selected_) { td::remove_if(user_ids_, [](UserId user_id) { return !user_id.is_valid(); }); + td::remove_if(excluded_user_ids_, [](UserId user_id) { return !user_id.is_valid(); }); } -BusinessRecipients::BusinessRecipients(td_api::object_ptr recipients) { +BusinessRecipients::BusinessRecipients(td_api::object_ptr recipients, bool allow_excluded) { if (recipients == nullptr) { return; } @@ -45,6 +47,18 @@ BusinessRecipients::BusinessRecipients(td_api::object_ptrexcluded_chat_ids_) { + DialogId dialog_id(chat_id); + if (dialog_id.get_type() == DialogType::User) { + excluded_user_ids_.push_back(dialog_id.get_user_id()); + } + } + if (recipients->exclude_selected_) { + append(user_ids_, std::move(excluded_user_ids_)); + reset_to_empty(excluded_user_ids_); + } + } existing_chats_ = recipients->select_existing_chats_; new_chats_ = recipients->select_new_chats_; contacts_ = recipients->select_contacts_; @@ -60,8 +74,16 @@ td_api::object_ptr BusinessRecipients::get_business_ CHECK(td->dialog_manager_->have_dialog_force(dialog_id, "get_business_recipients_object")); chat_ids.push_back(td->dialog_manager_->get_chat_id_object(dialog_id, "businessRecipients")); } - return td_api::make_object(std::move(chat_ids), existing_chats_, new_chats_, contacts_, - non_contacts_, exclude_selected_); + vector excluded_chat_ids; + for (auto user_id : excluded_user_ids_) { + DialogId dialog_id(user_id); + td->dialog_manager_->force_create_dialog(dialog_id, "get_business_recipients_object", true); + CHECK(td->dialog_manager_->have_dialog_force(dialog_id, "get_business_recipients_object")); + excluded_chat_ids.push_back(td->dialog_manager_->get_chat_id_object(dialog_id, "businessRecipients")); + } + return td_api::make_object(std::move(chat_ids), std::move(excluded_chat_ids), + existing_chats_, new_chats_, contacts_, non_contacts_, + exclude_selected_); } telegram_api::object_ptr BusinessRecipients::get_input_business_recipients( @@ -101,19 +123,19 @@ telegram_api::object_ptr BusinessRecipients::get_input_business_bot_recipients(Td *td) const { int32 flags = 0; if (existing_chats_) { - flags |= telegram_api::inputBusinessRecipients::EXISTING_CHATS_MASK; + flags |= telegram_api::inputBusinessBotRecipients::EXISTING_CHATS_MASK; } if (new_chats_) { - flags |= telegram_api::inputBusinessRecipients::NEW_CHATS_MASK; + flags |= telegram_api::inputBusinessBotRecipients::NEW_CHATS_MASK; } if (contacts_) { - flags |= telegram_api::inputBusinessRecipients::CONTACTS_MASK; + flags |= telegram_api::inputBusinessBotRecipients::CONTACTS_MASK; } if (non_contacts_) { - flags |= telegram_api::inputBusinessRecipients::NON_CONTACTS_MASK; + flags |= telegram_api::inputBusinessBotRecipients::NON_CONTACTS_MASK; } if (exclude_selected_) { - flags |= telegram_api::inputBusinessRecipients::EXCLUDE_SELECTED_MASK; + flags |= telegram_api::inputBusinessBotRecipients::EXCLUDE_SELECTED_MASK; } vector> input_users; for (auto user_id : user_ids_) { @@ -123,16 +145,27 @@ BusinessRecipients::get_input_business_bot_recipients(Td *td) const { } } if (!input_users.empty()) { - flags |= telegram_api::inputBusinessRecipients::USERS_MASK; + flags |= telegram_api::inputBusinessBotRecipients::USERS_MASK; + } + vector> excluded_input_users; + for (auto user_id : excluded_user_ids_) { + auto r_input_user = td->contacts_manager_->get_input_user(user_id); + if (r_input_user.is_ok()) { + excluded_input_users.push_back(r_input_user.move_as_ok()); + } + } + if (!excluded_input_users.empty()) { + flags |= telegram_api::inputBusinessBotRecipients::EXCLUDE_USERS_MASK; } return telegram_api::make_object( flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, - std::move(input_users), Auto()); + std::move(input_users), std::move(excluded_input_users)); } bool operator==(const BusinessRecipients &lhs, const BusinessRecipients &rhs) { - return lhs.user_ids_ == rhs.user_ids_ && lhs.existing_chats_ == rhs.existing_chats_ && - lhs.new_chats_ == rhs.new_chats_ && lhs.contacts_ == rhs.contacts_ && lhs.non_contacts_ == rhs.non_contacts_ && + return lhs.user_ids_ == rhs.user_ids_ && lhs.excluded_user_ids_ == rhs.excluded_user_ids_ && + lhs.existing_chats_ == rhs.existing_chats_ && lhs.new_chats_ == rhs.new_chats_ && + lhs.contacts_ == rhs.contacts_ && lhs.non_contacts_ == rhs.non_contacts_ && lhs.exclude_selected_ == rhs.exclude_selected_; } diff --git a/td/telegram/BusinessRecipients.h b/td/telegram/BusinessRecipients.h index 979051f7f..e427a2f92 100644 --- a/td/telegram/BusinessRecipients.h +++ b/td/telegram/BusinessRecipients.h @@ -25,7 +25,7 @@ class BusinessRecipients { explicit BusinessRecipients(telegram_api::object_ptr recipients); - explicit BusinessRecipients(td_api::object_ptr recipients); + BusinessRecipients(td_api::object_ptr recipients, bool allow_excluded); td_api::object_ptr get_business_recipients_object(Td *td) const; @@ -41,6 +41,7 @@ class BusinessRecipients { private: vector user_ids_; + vector excluded_user_ids_; bool existing_chats_ = false; bool new_chats_ = false; bool contacts_ = false; diff --git a/td/telegram/BusinessRecipients.hpp b/td/telegram/BusinessRecipients.hpp index f073a6fe5..e37ee4447 100644 --- a/td/telegram/BusinessRecipients.hpp +++ b/td/telegram/BusinessRecipients.hpp @@ -16,6 +16,7 @@ namespace td { template void BusinessRecipients::store(StorerT &storer) const { bool has_user_ids = !user_ids_.empty(); + bool has_excluded_user_ids = !excluded_user_ids_.empty(); BEGIN_STORE_FLAGS(); STORE_FLAG(existing_chats_); STORE_FLAG(new_chats_); @@ -23,15 +24,20 @@ void BusinessRecipients::store(StorerT &storer) const { STORE_FLAG(non_contacts_); STORE_FLAG(exclude_selected_); STORE_FLAG(has_user_ids); + STORE_FLAG(has_excluded_user_ids); END_STORE_FLAGS(); if (has_user_ids) { td::store(user_ids_, storer); } + if (has_excluded_user_ids) { + td::store(excluded_user_ids_, storer); + } } template void BusinessRecipients::parse(ParserT &parser) { bool has_user_ids; + bool has_excluded_user_ids; BEGIN_PARSE_FLAGS(); PARSE_FLAG(existing_chats_); PARSE_FLAG(new_chats_); @@ -39,10 +45,14 @@ void BusinessRecipients::parse(ParserT &parser) { PARSE_FLAG(non_contacts_); PARSE_FLAG(exclude_selected_); PARSE_FLAG(has_user_ids); + PARSE_FLAG(has_excluded_user_ids); END_PARSE_FLAGS(); if (has_user_ids) { td::parse(user_ids_, parser); } + if (has_excluded_user_ids) { + td::parse(excluded_user_ids_, parser); + } } } // namespace td diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index f7f86e30b..504deab46 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -585,8 +585,8 @@ class CliClient final : public Actor { } td_api::object_ptr as_business_recipients(string chat_ids) const { - return td_api::make_object(as_chat_ids(chat_ids), rand_bool(), rand_bool(), rand_bool(), - rand_bool(), rand_bool()); + return td_api::make_object(as_chat_ids(chat_ids), Auto(), rand_bool(), rand_bool(), + rand_bool(), rand_bool(), rand_bool()); } static td_api::object_ptr as_sticker_format(string sticker_format) {