From 261a3d0c227a3c24c8062c075c3c0386b07b2f61 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 4 Mar 2024 16:57:36 +0300 Subject: [PATCH] Move business info setters to BusinessManager. --- td/telegram/BusinessManager.cpp | 172 ++++++++++++++++++++++++++++++++ td/telegram/BusinessManager.h | 13 +++ td/telegram/ContactsManager.cpp | 160 ----------------------------- td/telegram/ContactsManager.h | 8 -- td/telegram/Td.cpp | 8 +- 5 files changed, 189 insertions(+), 172 deletions(-) diff --git a/td/telegram/BusinessManager.cpp b/td/telegram/BusinessManager.cpp index db197a97c..a37cd7908 100644 --- a/td/telegram/BusinessManager.cpp +++ b/td/telegram/BusinessManager.cpp @@ -6,8 +6,163 @@ // #include "td/telegram/BusinessManager.h" +#include "td/telegram/BusinessAwayMessage.h" +#include "td/telegram/BusinessGreetingMessage.h" +#include "td/telegram/BusinessWorkHours.h" +#include "td/telegram/ContactsManager.h" +#include "td/telegram/DialogLocation.h" +#include "td/telegram/Global.h" +#include "td/telegram/Td.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/buffer.h" +#include "td/utils/Status.h" + namespace td { +class UpdateBusinessLocationQuery final : public Td::ResultHandler { + Promise promise_; + DialogLocation location_; + + public: + explicit UpdateBusinessLocationQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(DialogLocation &&location) { + location_ = std::move(location); + int32 flags = 0; + if (!location_.empty()) { + flags |= telegram_api::account_updateBusinessLocation::GEO_POINT_MASK; + } + if (!location_.get_address().empty()) { + flags |= telegram_api::account_updateBusinessLocation::ADDRESS_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::account_updateBusinessLocation(flags, location_.get_input_geo_point(), location_.get_address()), + {{"me"}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + td_->contacts_manager_->on_update_user_location(td_->contacts_manager_->get_my_id(), std::move(location_)); + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +class UpdateBusinessWorkHoursQuery final : public Td::ResultHandler { + Promise promise_; + BusinessWorkHours work_hours_; + + public: + explicit UpdateBusinessWorkHoursQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(BusinessWorkHours &&work_hours) { + work_hours_ = std::move(work_hours); + int32 flags = 0; + if (!work_hours_.is_empty()) { + flags |= telegram_api::account_updateBusinessWorkHours::BUSINESS_WORK_HOURS_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::account_updateBusinessWorkHours(flags, work_hours_.get_input_business_work_hours()), {{"me"}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + td_->contacts_manager_->on_update_user_work_hours(td_->contacts_manager_->get_my_id(), std::move(work_hours_)); + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +class UpdateBusinessGreetingMessageQuery final : public Td::ResultHandler { + Promise promise_; + BusinessGreetingMessage greeting_message_; + + public: + explicit UpdateBusinessGreetingMessageQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(BusinessGreetingMessage &&greeting_message) { + greeting_message_ = std::move(greeting_message); + int32 flags = 0; + if (!greeting_message_.is_empty()) { + flags |= telegram_api::account_updateBusinessGreetingMessage::MESSAGE_MASK; + } + send_query(G()->net_query_creator().create(telegram_api::account_updateBusinessGreetingMessage( + flags, greeting_message_.get_input_business_greeting_message(td_)), + {{"me"}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + td_->contacts_manager_->on_update_user_greeting_message(td_->contacts_manager_->get_my_id(), + std::move(greeting_message_)); + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +class UpdateBusinessAwayMessageQuery final : public Td::ResultHandler { + Promise promise_; + BusinessAwayMessage away_message_; + + public: + explicit UpdateBusinessAwayMessageQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(BusinessAwayMessage &&away_message) { + away_message_ = std::move(away_message); + int32 flags = 0; + if (!away_message_.is_empty()) { + flags |= telegram_api::account_updateBusinessAwayMessage::MESSAGE_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::account_updateBusinessAwayMessage(flags, away_message_.get_input_business_away_message(td_)), + {{"me"}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + td_->contacts_manager_->on_update_user_away_message(td_->contacts_manager_->get_my_id(), std::move(away_message_)); + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + BusinessManager::BusinessManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { } @@ -15,4 +170,21 @@ void BusinessManager::tear_down() { parent_.reset(); } +void BusinessManager::set_business_location(DialogLocation &&location, Promise &&promise) { + td_->create_handler(std::move(promise))->send(std::move(location)); +} + +void BusinessManager::set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise) { + td_->create_handler(std::move(promise))->send(std::move(work_hours)); +} + +void BusinessManager::set_business_greeting_message(BusinessGreetingMessage &&greeting_message, + Promise &&promise) { + td_->create_handler(std::move(promise))->send(std::move(greeting_message)); +} + +void BusinessManager::set_business_away_message(BusinessAwayMessage &&away_message, Promise &&promise) { + td_->create_handler(std::move(promise))->send(std::move(away_message)); +} + } // namespace td diff --git a/td/telegram/BusinessManager.h b/td/telegram/BusinessManager.h index 122af10f7..b6e87a0cc 100644 --- a/td/telegram/BusinessManager.h +++ b/td/telegram/BusinessManager.h @@ -9,15 +9,28 @@ #include "td/actor/actor.h" #include "td/utils/common.h" +#include "td/utils/Promise.h" namespace td { +class BusinessAwayMessage; +class BusinessGreetingMessage; +class BusinessWorkHours; +class DialogLocation; class Td; class BusinessManager final : public Actor { public: BusinessManager(Td *td, ActorShared<> parent); + void set_business_location(DialogLocation &&location, Promise &&promise); + + void set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise); + + void set_business_greeting_message(BusinessGreetingMessage &&greeting_message, Promise &&promise); + + void set_business_away_message(BusinessAwayMessage &&away_message, Promise &&promise); + private: void tear_down() final; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index b11b2129b..e17a989cb 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1095,149 +1095,6 @@ class UpdateEmojiStatusQuery final : public Td::ResultHandler { } }; -class UpdateBusinessLocationQuery final : public Td::ResultHandler { - Promise promise_; - DialogLocation location_; - - public: - explicit UpdateBusinessLocationQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send(DialogLocation &&location) { - location_ = std::move(location); - int32 flags = 0; - if (!location_.empty()) { - flags |= telegram_api::account_updateBusinessLocation::GEO_POINT_MASK; - } - if (!location_.get_address().empty()) { - flags |= telegram_api::account_updateBusinessLocation::ADDRESS_MASK; - } - send_query(G()->net_query_creator().create( - telegram_api::account_updateBusinessLocation(flags, location_.get_input_geo_point(), location_.get_address()), - {{"me"}})); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - td_->contacts_manager_->on_update_user_location(td_->contacts_manager_->get_my_id(), std::move(location_)); - - promise_.set_value(Unit()); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - -class UpdateBusinessWorkHoursQuery final : public Td::ResultHandler { - Promise promise_; - BusinessWorkHours work_hours_; - - public: - explicit UpdateBusinessWorkHoursQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send(BusinessWorkHours &&work_hours) { - work_hours_ = std::move(work_hours); - int32 flags = 0; - if (!work_hours_.is_empty()) { - flags |= telegram_api::account_updateBusinessWorkHours::BUSINESS_WORK_HOURS_MASK; - } - send_query(G()->net_query_creator().create( - telegram_api::account_updateBusinessWorkHours(flags, work_hours_.get_input_business_work_hours()), {{"me"}})); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - td_->contacts_manager_->on_update_user_work_hours(td_->contacts_manager_->get_my_id(), std::move(work_hours_)); - - promise_.set_value(Unit()); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - -class UpdateBusinessGreetingMessageQuery final : public Td::ResultHandler { - Promise promise_; - BusinessGreetingMessage greeting_message_; - - public: - explicit UpdateBusinessGreetingMessageQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send(BusinessGreetingMessage &&greeting_message) { - greeting_message_ = std::move(greeting_message); - int32 flags = 0; - if (!greeting_message_.is_empty()) { - flags |= telegram_api::account_updateBusinessGreetingMessage::MESSAGE_MASK; - } - send_query(G()->net_query_creator().create(telegram_api::account_updateBusinessGreetingMessage( - flags, greeting_message_.get_input_business_greeting_message(td_)), - {{"me"}})); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - td_->contacts_manager_->on_update_user_greeting_message(td_->contacts_manager_->get_my_id(), - std::move(greeting_message_)); - - promise_.set_value(Unit()); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - -class UpdateBusinessAwayMessageQuery final : public Td::ResultHandler { - Promise promise_; - BusinessAwayMessage away_message_; - - public: - explicit UpdateBusinessAwayMessageQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send(BusinessAwayMessage &&away_message) { - away_message_ = std::move(away_message); - int32 flags = 0; - if (!away_message_.is_empty()) { - flags |= telegram_api::account_updateBusinessAwayMessage::MESSAGE_MASK; - } - send_query(G()->net_query_creator().create( - telegram_api::account_updateBusinessAwayMessage(flags, away_message_.get_input_business_away_message(td_)), - {{"me"}})); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - td_->contacts_manager_->on_update_user_away_message(td_->contacts_manager_->get_my_id(), std::move(away_message_)); - - promise_.set_value(Unit()); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - class CreateChatQuery final : public Td::ResultHandler { Promise> promise_; @@ -7092,23 +6949,6 @@ void ContactsManager::on_set_emoji_status(EmojiStatus emoji_status, Promise &&promise) { - td_->create_handler(std::move(promise))->send(std::move(location)); -} - -void ContactsManager::set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise) { - td_->create_handler(std::move(promise))->send(std::move(work_hours)); -} - -void ContactsManager::set_business_greeting_message(BusinessGreetingMessage &&greeting_message, - Promise &&promise) { - td_->create_handler(std::move(promise))->send(std::move(greeting_message)); -} - -void ContactsManager::set_business_away_message(BusinessAwayMessage &&away_message, Promise &&promise) { - td_->create_handler(std::move(promise))->send(std::move(away_message)); -} - void ContactsManager::set_chat_description(ChatId chat_id, const string &description, Promise &&promise) { auto new_description = strip_empty_characters(description, MAX_DESCRIPTION_LENGTH); auto c = get_chat(chat_id); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 5440f9932..3707c5870 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -468,14 +468,6 @@ class ContactsManager final : public Actor { void set_emoji_status(const EmojiStatus &emoji_status, Promise &&promise); - void set_business_location(DialogLocation &&location, Promise &&promise); - - void set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise); - - void set_business_greeting_message(BusinessGreetingMessage &&greeting_message, Promise &&promise); - - void set_business_away_message(BusinessAwayMessage &&away_message, Promise &&promise); - void set_chat_description(ChatId chat_id, const string &description, Promise &&promise); void set_channel_username(ChannelId channel_id, const string &username, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 99511aef8..2d8660148 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7806,26 +7806,26 @@ void Td::on_request(uint64 id, const td_api::setLocation &request) { void Td::on_request(uint64 id, td_api::setBusinessLocation &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->set_business_location(DialogLocation(std::move(request.location_)), std::move(promise)); + business_manager_->set_business_location(DialogLocation(std::move(request.location_)), std::move(promise)); } void Td::on_request(uint64 id, td_api::setBusinessOpeningHours &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->set_business_work_hours(BusinessWorkHours(std::move(request.opening_hours_)), std::move(promise)); + business_manager_->set_business_work_hours(BusinessWorkHours(std::move(request.opening_hours_)), std::move(promise)); } void Td::on_request(uint64 id, td_api::setBusinessGreetingMessageSettings &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->set_business_greeting_message( + business_manager_->set_business_greeting_message( BusinessGreetingMessage(std::move(request.greeting_message_settings_)), std::move(promise)); } void Td::on_request(uint64 id, td_api::setBusinessAwayMessageSettings &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->set_business_away_message(BusinessAwayMessage(std::move(request.away_message_settings_)), + business_manager_->set_business_away_message(BusinessAwayMessage(std::move(request.away_message_settings_)), std::move(promise)); }