diff --git a/CMakeLists.txt b/CMakeLists.txt index 792c694f4..715d0c724 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -593,6 +593,7 @@ set(TDLIB_SOURCE td/telegram/BusinessAwayMessage.h td/telegram/BusinessAwayMessageSchedule.h td/telegram/BusinessConnectedBot.h + td/telegram/BusinessConnectionId.h td/telegram/BusinessConnectionManager.h td/telegram/BusinessGreetingMessage.h td/telegram/BusinessInfo.h diff --git a/SplitSource.php b/SplitSource.php index 0cd972c63..708b491ad 100644 --- a/SplitSource.php +++ b/SplitSource.php @@ -311,6 +311,7 @@ function split_file($file, $chunks, $undo) { 'bot_info_manager[_(-](?![.]get[(][)])|BotInfoManager' => 'BotInfoManager', 'BusinessAwayMessage' => 'BusinessAwayMessage', 'BusinessConnectedBot' => 'BusinessConnectedBot', + 'BusinessConnectionId' => 'BusinessConnectionId', 'business_connection_manager[_(-](?![.]get[(][)])|BusinessConnectionManager' => 'BusinessConnectionManager', 'BusinessGreetingMessage' => 'BusinessGreetingMessage', 'BusinessInfo|business_info' => 'BusinessInfo', diff --git a/td/telegram/BusinessConnectionId.h b/td/telegram/BusinessConnectionId.h new file mode 100644 index 000000000..a0ee0095b --- /dev/null +++ b/td/telegram/BusinessConnectionId.h @@ -0,0 +1,68 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 +// +// 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) +// +#pragma once + +#include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class BusinessConnectionId { + string business_connection_id_; + + public: + BusinessConnectionId() = default; + + explicit BusinessConnectionId(string &&business_connection_id) : business_connection_id_(std::move(business_connection_id)) { + } + + explicit BusinessConnectionId(const string &business_connection_id) : business_connection_id_(business_connection_id) { + } + + bool is_empty() const { + return business_connection_id_.empty(); + } + + bool is_valid() const { + return !business_connection_id_.empty(); + } + + const string &get() const { + return business_connection_id_; + } + + bool operator==(const BusinessConnectionId &other) const { + return business_connection_id_ == other.business_connection_id_; + } + + bool operator!=(const BusinessConnectionId &other) const { + return business_connection_id_ != other.business_connection_id_; + } + + template + void store(StorerT &storer) const { + storer.store_string(business_connection_id_); + } + + template + void parse(ParserT &parser) { + business_connection_id_ = parser.fetch_string(); + } +}; + +struct BusinessConnectionIdHash { + uint32 operator()(BusinessConnectionId business_connection_id) const { + return Hash()(business_connection_id.get()); + } +}; + +inline StringBuilder &operator<<(StringBuilder &string_builder, BusinessConnectionId business_connection_id) { + return string_builder << "business connection " << business_connection_id.get(); +} + +} // namespace td diff --git a/td/telegram/BusinessConnectionManager.cpp b/td/telegram/BusinessConnectionManager.cpp index 3f1366382..1c59240db 100644 --- a/td/telegram/BusinessConnectionManager.cpp +++ b/td/telegram/BusinessConnectionManager.cpp @@ -26,8 +26,8 @@ class GetBotBusinessConnectionQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(const string &connection_id) { - send_query(G()->net_query_creator().create(telegram_api::account_getBotBusinessConnection(connection_id))); + void send(const BusinessConnectionId &connection_id) { + send_query(G()->net_query_creator().create(telegram_api::account_getBotBusinessConnection(connection_id.get()))); } void on_result(BufferSlice packet) final { @@ -47,7 +47,7 @@ class GetBotBusinessConnectionQuery final : public Td::ResultHandler { }; struct BusinessConnectionManager::BusinessConnection { - string connection_id_; + BusinessConnectionId connection_id_; UserId user_id_; DcId dc_id_; int32 connection_date_ = 0; @@ -70,13 +70,13 @@ struct BusinessConnectionManager::BusinessConnection { ~BusinessConnection() = default; bool is_valid() const { - return !connection_id_.empty() && user_id_.is_valid() && !dc_id_.is_empty() && connection_date_ > 0; + return connection_id_.is_valid() && user_id_.is_valid() && !dc_id_.is_empty() && connection_date_ > 0; } td_api::object_ptr get_business_connection_object(Td *td) const { return td_api::make_object( - connection_id_, td->contacts_manager_->get_user_id_object(user_id_, "businessConnection"), connection_date_, - can_reply_, is_disabled_); + connection_id_.get(), td->contacts_manager_->get_user_id_object(user_id_, "businessConnection"), + connection_date_, can_reply_, is_disabled_); } }; @@ -90,8 +90,8 @@ void BusinessConnectionManager::tear_down() { parent_.reset(); } -Status BusinessConnectionManager::check_business_connection_id(const string &connection_id) const { - if (connection_id.empty() || business_connections_.count(connection_id) != 0) { +Status BusinessConnectionManager::check_business_connection_id(const BusinessConnectionId &connection_id) const { + if (connection_id.is_empty() || business_connections_.count(connection_id) != 0) { return Status::OK(); } return Status::Error(400, "Business connection not found"); @@ -114,13 +114,13 @@ void BusinessConnectionManager::on_update_bot_business_connect( } void BusinessConnectionManager::get_business_connection( - const string &connection_id, Promise> &&promise) { + const BusinessConnectionId &connection_id, Promise> &&promise) { auto connection = business_connections_.get_pointer(connection_id); if (connection != nullptr) { return promise.set_value(connection->get_business_connection_object(td_)); } - if (connection_id.empty()) { + if (connection_id.is_empty()) { return promise.set_error(Status::Error(400, "Connection iedntifier must be non-empty")); } @@ -137,7 +137,7 @@ void BusinessConnectionManager::get_business_connection( } void BusinessConnectionManager::on_get_business_connection( - const string &connection_id, Result> r_updates) { + const BusinessConnectionId &connection_id, Result> r_updates) { G()->ignore_result_if_closing(r_updates); auto queries_it = get_business_connection_queries_.find(connection_id); CHECK(queries_it != get_business_connection_queries_.end()); diff --git a/td/telegram/BusinessConnectionManager.h b/td/telegram/BusinessConnectionManager.h index 252981472..3be9e4611 100644 --- a/td/telegram/BusinessConnectionManager.h +++ b/td/telegram/BusinessConnectionManager.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/BusinessConnectionId.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" @@ -29,11 +30,11 @@ class BusinessConnectionManager final : public Actor { BusinessConnectionManager &operator=(BusinessConnectionManager &&) = delete; ~BusinessConnectionManager() final; - Status check_business_connection_id(const string &connection_id) const; + Status check_business_connection_id(const BusinessConnectionId &connection_id) const; void on_update_bot_business_connect(telegram_api::object_ptr &&connection); - void get_business_connection(const string &connection_id, + void get_business_connection(const BusinessConnectionId &connection_id, Promise> &&promise); private: @@ -41,12 +42,14 @@ class BusinessConnectionManager final : public Actor { void tear_down() final; - void on_get_business_connection(const string &connection_id, + void on_get_business_connection(const BusinessConnectionId &connection_id, Result> r_updates); - WaitFreeHashMap> business_connections_; + WaitFreeHashMap, BusinessConnectionIdHash> business_connections_; - FlatHashMap>>> get_business_connection_queries_; + FlatHashMap>>, + BusinessConnectionIdHash> + get_business_connection_queries_; Td *td_; ActorShared<> parent_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 0d77f0701..6df5cb4fb 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -23,6 +23,7 @@ #include "td/telegram/BotInfoManager.h" #include "td/telegram/BotMenuButton.h" #include "td/telegram/BusinessAwayMessage.h" +#include "td/telegram/BusinessConnectionId.h" #include "td/telegram/BusinessConnectionManager.h" #include "td/telegram/BusinessGreetingMessage.h" #include "td/telegram/BusinessManager.h" @@ -8643,7 +8644,8 @@ void Td::on_request(uint64 id, td_api::getBusinessConnection &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.connection_id_); CREATE_REQUEST_PROMISE(); - business_connection_manager_->get_business_connection(request.connection_id_, std::move(promise)); + business_connection_manager_->get_business_connection(BusinessConnectionId(std::move(request.connection_id_)), + std::move(promise)); } void Td::on_request(uint64 id, const td_api::getLoginUrlInfo &request) {