Skip to content

Commit

Permalink
Merge pull request #84 from a5r0n/official-upstream-update
Browse files Browse the repository at this point in the history
Official upstream update
  • Loading branch information
cavallium authored Dec 17, 2023
2 parents b3eb1ac + a35ff45 commit 8990b79
Show file tree
Hide file tree
Showing 14 changed files with 938 additions and 770 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (POLICY CMP0065)
cmake_policy(SET CMP0065 NEW)
endif()

project(TelegramBotApi VERSION 6.7.1 LANGUAGES CXX)
project(TelegramBotApi VERSION 6.9.2 LANGUAGES CXX)

if (POLICY CMP0069)
option(TELEGRAM_BOT_API_ENABLE_LTO "Use \"ON\" to enable Link Time Optimization.")
Expand Down
2 changes: 1 addition & 1 deletion td
Submodule td updated 547 files
1,391 changes: 738 additions & 653 deletions telegram-bot-api/Client.cpp

Large diffs are not rendered by default.

82 changes: 52 additions & 30 deletions telegram-bot-api/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class Client final : public WebhookActor::Callback {
struct UserInfo;
struct ChatInfo;
struct BotCommandScope;
struct BotUserIds;

enum class AccessRights { Read, ReadMembers, Edit, Write };

Expand Down Expand Up @@ -375,13 +376,16 @@ class Client final : public WebhookActor::Callback {

static bool to_bool(td::MutableSlice value);

static object_ptr<td_api::InputMessageReplyTo> get_input_message_reply_to(int64 reply_to_message_id);

static td::Result<object_ptr<td_api::keyboardButton>> get_keyboard_button(td::JsonValue &button);

td::Result<object_ptr<td_api::inlineKeyboardButton>> get_inline_keyboard_button(td::JsonValue &button);
static td::Result<object_ptr<td_api::inlineKeyboardButton>> get_inline_keyboard_button(td::JsonValue &button,
BotUserIds &bot_user_ids);

td::Result<object_ptr<td_api::ReplyMarkup>> get_reply_markup(const Query *query);
static td::Result<object_ptr<td_api::ReplyMarkup>> get_reply_markup(const Query *query, BotUserIds &bot_user_ids);

td::Result<object_ptr<td_api::ReplyMarkup>> get_reply_markup(td::JsonValue &&value);
static td::Result<object_ptr<td_api::ReplyMarkup>> get_reply_markup(td::JsonValue &&value, BotUserIds &bot_user_ids);

static td::Result<object_ptr<td_api::labeledPricePart>> get_labeled_price_part(td::JsonValue &value);

Expand Down Expand Up @@ -415,13 +419,17 @@ class Client final : public WebhookActor::Callback {
static td::Result<td_api::object_ptr<td_api::inlineQueryResultsButton>> get_inline_query_results_button(
td::MutableSlice value);

td::Result<object_ptr<td_api::InputInlineQueryResult>> get_inline_query_result(const Query *query);
static td::Result<object_ptr<td_api::InputInlineQueryResult>> get_inline_query_result(const Query *query,
BotUserIds &bot_user_ids);

td::Result<object_ptr<td_api::InputInlineQueryResult>> get_inline_query_result(td::JsonValue &&value);
static td::Result<object_ptr<td_api::InputInlineQueryResult>> get_inline_query_result(td::JsonValue &&value,
BotUserIds &bot_user_ids);

td::Result<td::vector<object_ptr<td_api::InputInlineQueryResult>>> get_inline_query_results(const Query *query);
static td::Result<td::vector<object_ptr<td_api::InputInlineQueryResult>>> get_inline_query_results(
const Query *query, BotUserIds &bot_user_ids);

td::Result<td::vector<object_ptr<td_api::InputInlineQueryResult>>> get_inline_query_results(td::JsonValue &&value);
static td::Result<td::vector<object_ptr<td_api::InputInlineQueryResult>>> get_inline_query_results(
td::JsonValue &&value, BotUserIds &bot_user_ids);

struct BotCommandScope {
object_ptr<td_api::BotCommandScope> scope_;
Expand Down Expand Up @@ -540,7 +548,7 @@ class Client final : public WebhookActor::Callback {

void decrease_yet_unsent_message_count(int64 chat_id, int32 count);

int64 extract_yet_unsent_message_query_id(int64 chat_id, int64 message_id, bool *is_reply_to_message_deleted);
int64 extract_yet_unsent_message_query_id(int64 chat_id, int64 message_id);

// start custom helper methods

Expand All @@ -552,7 +560,7 @@ class Client final : public WebhookActor::Callback {
static int64 get_int64_arg(const Query *query, td::Slice field_name, int64 default_value,
int64 min_value = std::numeric_limits<int64>::min(),
int64 max_value = std::numeric_limits<int64>::max());
static td::Result<td_api::object_ptr<td_api::ChatReportReason>> get_report_reason(const Query *query,
static td::Result<td_api::object_ptr<td_api::ReportReason>> get_report_reason(const Query *query,
td::Slice field_name = td::Slice("reason"));

static td::Result<td_api::object_ptr<td_api::SearchMessagesFilter>> get_search_messages_filter(
Expand All @@ -561,13 +569,15 @@ class Client final : public WebhookActor::Callback {
// end custom helper methods

void on_message_send_succeeded(object_ptr<td_api::message> &&message, int64 old_message_id);
void on_message_send_failed(int64 chat_id, int64 old_message_id, int64 new_message_id, td::Status result);

void on_message_send_failed(int64 chat_id, int64 old_message_id, int64 new_message_id,
object_ptr<td_api::error> &&error);

static bool init_methods();

static bool is_local_method(td::Slice method);

void on_cmd(PromisedQueryPtr query);
void on_cmd(PromisedQueryPtr query, bool force = false);

td::Status process_get_me_query(PromisedQueryPtr &query);
td::Status process_get_my_commands_query(PromisedQueryPtr &query);
Expand Down Expand Up @@ -646,6 +656,7 @@ class Client final : public WebhookActor::Callback {
td::Status process_reopen_general_forum_topic_query(PromisedQueryPtr &query);
td::Status process_hide_general_forum_topic_query(PromisedQueryPtr &query);
td::Status process_unhide_general_forum_topic_query(PromisedQueryPtr &query);
td::Status process_unpin_all_general_forum_topic_messages_query(PromisedQueryPtr &query);
td::Status process_get_chat_member_query(PromisedQueryPtr &query);
td::Status process_get_chat_administrators_query(PromisedQueryPtr &query);
td::Status process_get_chat_member_count_query(PromisedQueryPtr &query);
Expand Down Expand Up @@ -735,7 +746,10 @@ class Client final : public WebhookActor::Callback {

void on_webhook_closed(td::Status status);

void do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query);
void delete_last_send_message_time(td::int64 file_size, double max_delay);

void do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query,
bool force = false);

int64 get_send_message_query_id(PromisedQueryPtr query, bool is_multisend);

Expand Down Expand Up @@ -800,6 +814,7 @@ class Client final : public WebhookActor::Callback {
td::string editable_username;
td::string language_code;
int64 emoji_status_custom_emoji_id;
int32 emoji_status_expiration_date;

object_ptr<td_api::chatPhoto> photo;
td::string bio;
Expand Down Expand Up @@ -842,7 +857,7 @@ class Client final : public WebhookActor::Callback {
};
static void add_group(GroupInfo *group_info, object_ptr<td_api::basicGroup> &&group);
void set_group_photo(int64 group_id, object_ptr<td_api::chatPhoto> &&photo);
void set_group_description(int64 group_id, td::string &&descripton);
void set_group_description(int64 group_id, td::string &&description);
void set_group_invite_link(int64 group_id, td::string &&invite_link);
GroupInfo *add_group_info(int64 group_id);
const GroupInfo *get_group_info(int64 group_id) const;
Expand Down Expand Up @@ -875,7 +890,7 @@ class Client final : public WebhookActor::Callback {
};
static void add_supergroup(SupergroupInfo *supergroup_info, object_ptr<td_api::supergroup> &&supergroup);
void set_supergroup_photo(int64 supergroup_id, object_ptr<td_api::chatPhoto> &&photo);
void set_supergroup_description(int64 supergroup_id, td::string &&descripton);
void set_supergroup_description(int64 supergroup_id, td::string &&description);
void set_supergroup_invite_link(int64 supergroup_id, td::string &&invite_link);
void set_supergroup_sticker_set_id(int64 supergroup_id, int64 sticker_set_id);
void set_supergroup_can_set_sticker_set(int64 supergroup_id, bool can_set_sticker_set);
Expand Down Expand Up @@ -926,7 +941,7 @@ class Client final : public WebhookActor::Callback {
td::string initial_author_signature;
td::string initial_sender_name;
td::string author_signature;
int64 reply_to_message_id = 0;
object_ptr<td_api::messageReplyToMessage> reply_to_message;
int64 media_album_id = 0;
int64 via_bot_user_id = 0;
object_ptr<td_api::MessageContent> content;
Expand All @@ -943,13 +958,18 @@ class Client final : public WebhookActor::Callback {
bool can_be_saved = false;
bool is_automatic_forward = false;
bool is_topic_message = false;
mutable bool is_reply_to_message_deleted = false;
mutable bool is_content_changed = false;
};

static int64 &get_reply_to_message_id(object_ptr<td_api::message> &message);
static int64 get_same_chat_reply_to_message_id(const td_api::messageReplyToMessage *reply_to,
int64 message_thread_id);

static int64 get_same_chat_reply_to_message_id(const object_ptr<td_api::MessageReplyTo> &reply_to,
int64 message_thread_id);

void set_message_reply_to_message_id(MessageInfo *message_info, int64 reply_to_message_id);
static int64 get_same_chat_reply_to_message_id(const object_ptr<td_api::message> &message);

static void drop_internal_reply_to_message_in_another_chat(object_ptr<td_api::message> &message);

static td::Slice get_sticker_type(const object_ptr<td_api::StickerType> &type);

Expand Down Expand Up @@ -1155,17 +1175,10 @@ class Client final : public WebhookActor::Callback {
td::WaitFreeHashMap<int64, td::unique_ptr<SupergroupInfo>> supergroups_;
td::WaitFreeHashMap<int64, td::unique_ptr<ChatInfo>> chats_;

td::FlatHashMap<FullMessageId, td::FlatHashSet<int64>, FullMessageIdHash>
reply_message_ids_; // message -> replies to it
td::FlatHashMap<FullMessageId, td::FlatHashSet<int64>, FullMessageIdHash>
yet_unsent_reply_message_ids_; // message -> replies to it

td::FlatHashMap<int32, td::vector<PromisedQueryPtr>> file_download_listeners_;
td::FlatHashSet<int32> download_started_file_ids_;

struct YetUnsentMessage {
int64 reply_to_message_id = 0;
bool is_reply_to_message_deleted = false;
int64 send_message_query_id = 0;
};
td::FlatHashMap<FullMessageId, YetUnsentMessage, FullMessageIdHash> yet_unsent_messages_;
Expand Down Expand Up @@ -1206,11 +1219,15 @@ class Client final : public WebhookActor::Callback {

td::WaitFreeHashMap<int64, td::string> sticker_set_names_;

int64 cur_temp_bot_user_id_ = 1;
td::FlatHashMap<td::string, int64> bot_user_ids_;
td::FlatHashSet<td::string> unresolved_bot_usernames_;
td::FlatHashMap<int64, int64> temp_to_real_bot_user_id_;
td::FlatHashMap<td::string, td::vector<int64>> awaiting_bot_resolve_queries_;
td::WaitFreeHashMap<int64, double> last_send_message_time_;

struct BotUserIds {
int64 default_bot_user_id_ = 0;
int64 cur_temp_bot_user_id_ = 1;
td::FlatHashMap<td::string, int64> bot_user_ids_;
td::FlatHashSet<td::string> unresolved_bot_usernames_;
};
BotUserIds bot_user_ids_;

struct PendingBotResolveQuery {
std::size_t pending_resolve_count = 0;
Expand All @@ -1220,6 +1237,9 @@ class Client final : public WebhookActor::Callback {
td::FlatHashMap<int64, PendingBotResolveQuery> pending_bot_resolve_queries_;
int64 current_bot_resolve_query_id_ = 1;

td::FlatHashMap<td::string, td::vector<int64>> awaiting_bot_resolve_queries_;
td::FlatHashMap<int64, int64> temp_to_real_bot_user_id_;

td::string dir_;
td::ActorOwn<td::ClientActor> td_client_;
td::ActorContext context_;
Expand Down Expand Up @@ -1273,6 +1293,8 @@ class Client final : public WebhookActor::Callback {
double previous_get_updates_finish_time_ = 0;
double next_get_updates_conflict_time_ = 0;

int32 log_in_date_ = 0;

int32 flood_limited_query_count_ = 0;
double next_flood_limit_warning_time_ = 0;

Expand Down
20 changes: 9 additions & 11 deletions telegram-bot-api/ClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
auto top_clients = get_top_clients(50, id_filter);

if(!as_json) {
sb << stat_.get_description() << '\n';
sb << BotStatActor::get_description() << '\n';
}
if (id_filter.empty()) {
if(as_json) {
Expand Down Expand Up @@ -448,9 +448,6 @@ td::int64 ClientManager::get_tqueue_id(td::int64 user_id, bool is_test_dc) {
}

void ClientManager::start_up() {
//NB: the same scheduler as for database in Td
auto scheduler_id = 1;

// init tqueue
{
auto load_start_time = td::Time::now();
Expand Down Expand Up @@ -478,7 +475,8 @@ void ClientManager::start_up() {
}
}

auto concurrent_binlog = std::make_shared<td::ConcurrentBinlog>(std::move(binlog), scheduler_id);
auto concurrent_binlog =
std::make_shared<td::ConcurrentBinlog>(std::move(binlog), SharedData::get_binlog_scheduler_id());
auto concurrent_tqueue_binlog = td::make_unique<td::TQueueBinlog<td::BinlogInterface>>();
concurrent_tqueue_binlog->set_binlog(std::move(concurrent_binlog));
tqueue->set_callback(std::move(concurrent_tqueue_binlog));
Expand All @@ -493,18 +491,18 @@ void ClientManager::start_up() {
// init webhook_db and user_db
auto concurrent_webhook_db = td::make_unique<td::BinlogKeyValue<td::ConcurrentBinlog>>();
auto status = concurrent_webhook_db->init(parameters_->working_directory_ + "webhooks_db.binlog", td::DbKey::empty(),
scheduler_id);
SharedData::get_binlog_scheduler_id());
LOG_IF(FATAL, status.is_error()) << "Can't open webhooks_db.binlog " << status;
parameters_->shared_data_->webhook_db_ = std::move(concurrent_webhook_db);

auto concurrent_user_db = td::make_unique<td::BinlogKeyValue<td::ConcurrentBinlog>>();
status = concurrent_user_db->init(parameters_->working_directory_ + "user_db.binlog", td::DbKey::empty(), scheduler_id);
status = concurrent_user_db->init(parameters_->working_directory_ + "user_db.binlog", td::DbKey::empty(), SharedData::get_binlog_scheduler_id());
LOG_IF(FATAL, status.is_error()) << "Can't open user_db.binlog " << status.error();
parameters_->shared_data_->user_db_ = std::move(concurrent_user_db);

auto &webhook_db = *parameters_->shared_data_->webhook_db_;
auto &user_db = *parameters_->shared_data_->user_db_;
for (auto key_value : webhook_db.get_all()) {
for (const auto &key_value : webhook_db.get_all()) {
if (!token_range_(td::to_integer<td::uint64>(key_value.first))) {
LOG(WARNING) << "DROP WEBHOOK: " << key_value.first << " ---> " << key_value.second;
webhook_db.erase(key_value.first);
Expand All @@ -516,8 +514,8 @@ void ClientManager::start_up() {
}

// launch watchdog
watchdog_id_ = td::create_actor_on_scheduler<Watchdog>(
"ManagerWatchdog", td::Scheduler::instance()->sched_count() - 3, td::this_thread::get_id(), WATCHDOG_TIMEOUT);
watchdog_id_ = td::create_actor_on_scheduler<Watchdog>("ManagerWatchdog", SharedData::get_watchdog_scheduler_id(),
td::this_thread::get_id(), WATCHDOG_TIMEOUT);
set_timeout_in(600.0);
}

Expand Down Expand Up @@ -684,7 +682,7 @@ void ClientManager::raw_event(const td::Event::Raw &event) {

void ClientManager::timeout_expired() {
send_closure(watchdog_id_, &Watchdog::kick);
set_timeout_in(WATCHDOG_TIMEOUT / 2);
set_timeout_in(WATCHDOG_TIMEOUT / 10);

double now = td::Time::now();
if (now > next_tqueue_gc_time_) {
Expand Down
49 changes: 44 additions & 5 deletions telegram-bot-api/ClientParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,54 @@ struct SharedData {
return static_cast<td::int32>(result);
}

static td::int32 get_database_scheduler_id() {
// the same scheduler as for database in Td
return 1;
}

static td::int32 get_file_gc_scheduler_id() {
// the same scheduler as for file GC in Td
return 2;
}

static td::int32 get_client_scheduler_id() {
// the thread for ClientManager and all Clients
return 4;
}

static td::int32 get_watchdog_scheduler_id() {
// the thread for watchdogs
return 5;
}

static td::int32 get_slow_incoming_http_scheduler_id() {
// the thread for slow incoming HTTP connections
return 6;
}

static td::int32 get_slow_outgoing_http_scheduler_id() {
// the thread for slow outgoing HTTP connections
return 7;
}

static td::int32 get_dns_resolver_scheduler_id() {
// the thread for DNS resolving
return 8;
}

static td::int32 get_binlog_scheduler_id() {
// the thread for TQueue and webhook binlogs
return 9;
}

static td::int32 get_webhook_certificate_scheduler_id() {
// the thread for webhook certificate processing
return 10;
}

static td::int32 get_statistics_thread_id() {
// the thread for CPU usage updating
return 11;
}

static td::int32 get_thread_count() {
return 12;
}
};

struct ClientParameters {
Expand Down
9 changes: 3 additions & 6 deletions telegram-bot-api/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
#pragma once

#include "telegram-bot-api/ClientParameters.h"

#include "td/net/HttpInboundConnection.h"
#include "td/net/TcpListener.h"

Expand Down Expand Up @@ -61,13 +63,8 @@ class HttpServer final : public td::TcpListener::Callback {
}

void accept(td::SocketFd fd) final {
auto scheduler_count = td::Scheduler::instance()->sched_count();
auto scheduler_id = scheduler_count - 1;
if (scheduler_id > 0) {
scheduler_id--;
}
td::create_actor<td::HttpInboundConnection>("HttpInboundConnection", td::BufferedFd<td::SocketFd>(std::move(fd)), 0,
50, 500, creator_(), scheduler_id)
50, 500, creator_(), SharedData::get_slow_incoming_http_scheduler_id())
.release();
}

Expand Down
Loading

0 comments on commit 8990b79

Please sign in to comment.