Add class BusinessConnectionId.

This commit is contained in:
levlam 2024-03-07 20:18:04 +03:00
parent 0a703fb8c2
commit 967829d14b
6 changed files with 92 additions and 17 deletions

View File

@ -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

View File

@ -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',

View File

@ -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 <class StorerT>
void store(StorerT &storer) const {
storer.store_string(business_connection_id_);
}
template <class ParserT>
void parse(ParserT &parser) {
business_connection_id_ = parser.fetch_string<string>();
}
};
struct BusinessConnectionIdHash {
uint32 operator()(BusinessConnectionId business_connection_id) const {
return Hash<string>()(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

View File

@ -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<td_api::businessConnection> get_business_connection_object(Td *td) const {
return td_api::make_object<td_api::businessConnection>(
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<td_api::object_ptr<td_api::businessConnection>> &&promise) {
const BusinessConnectionId &connection_id, Promise<td_api::object_ptr<td_api::businessConnection>> &&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<telegram_api::object_ptr<telegram_api::Updates>> r_updates) {
const BusinessConnectionId &connection_id, Result<telegram_api::object_ptr<telegram_api::Updates>> 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());

View File

@ -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<telegram_api::botBusinessConnection> &&connection);
void get_business_connection(const string &connection_id,
void get_business_connection(const BusinessConnectionId &connection_id,
Promise<td_api::object_ptr<td_api::businessConnection>> &&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<telegram_api::object_ptr<telegram_api::Updates>> r_updates);
WaitFreeHashMap<string, unique_ptr<BusinessConnection>> business_connections_;
WaitFreeHashMap<BusinessConnectionId, unique_ptr<BusinessConnection>, BusinessConnectionIdHash> business_connections_;
FlatHashMap<string, vector<Promise<td_api::object_ptr<td_api::businessConnection>>>> get_business_connection_queries_;
FlatHashMap<BusinessConnectionId, vector<Promise<td_api::object_ptr<td_api::businessConnection>>>,
BusinessConnectionIdHash>
get_business_connection_queries_;
Td *td_;
ActorShared<> parent_;

View File

@ -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) {