From 348b94bdeeca644eeaddc00348dbf1f9c630396f Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 1 Jan 2023 00:31:16 +0300 Subject: [PATCH 01/16] Update copyright year. --- telegram-bot-api/Client.cpp | 2 +- telegram-bot-api/Client.h | 2 +- telegram-bot-api/ClientManager.cpp | 2 +- telegram-bot-api/ClientManager.h | 2 +- telegram-bot-api/ClientParameters.h | 2 +- telegram-bot-api/HttpConnection.cpp | 2 +- telegram-bot-api/HttpConnection.h | 2 +- telegram-bot-api/HttpServer.h | 2 +- telegram-bot-api/HttpStatConnection.cpp | 2 +- telegram-bot-api/HttpStatConnection.h | 2 +- telegram-bot-api/Query.cpp | 2 +- telegram-bot-api/Query.h | 2 +- telegram-bot-api/Stats.cpp | 2 +- telegram-bot-api/Stats.h | 2 +- telegram-bot-api/Watchdog.cpp | 2 +- telegram-bot-api/Watchdog.h | 2 +- telegram-bot-api/WebhookActor.cpp | 2 +- telegram-bot-api/WebhookActor.h | 2 +- telegram-bot-api/telegram-bot-api.cpp | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 8cad2d7..d084ac0 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 166ad2c..82714aa 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 602ab12..c6cd45c 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/ClientManager.h b/telegram-bot-api/ClientManager.h index 143744a..bd82ee9 100644 --- a/telegram-bot-api/ClientManager.h +++ b/telegram-bot-api/ClientManager.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/ClientParameters.h b/telegram-bot-api/ClientParameters.h index fab4c3a..6956388 100644 --- a/telegram-bot-api/ClientParameters.h +++ b/telegram-bot-api/ClientParameters.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/HttpConnection.cpp b/telegram-bot-api/HttpConnection.cpp index a52d113..ade03d3 100644 --- a/telegram-bot-api/HttpConnection.cpp +++ b/telegram-bot-api/HttpConnection.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/HttpConnection.h b/telegram-bot-api/HttpConnection.h index d0d245c..0e3b39c 100644 --- a/telegram-bot-api/HttpConnection.h +++ b/telegram-bot-api/HttpConnection.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/HttpServer.h b/telegram-bot-api/HttpServer.h index 9699835..d5f0008 100644 --- a/telegram-bot-api/HttpServer.h +++ b/telegram-bot-api/HttpServer.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/HttpStatConnection.cpp b/telegram-bot-api/HttpStatConnection.cpp index 2606c89..d01af06 100644 --- a/telegram-bot-api/HttpStatConnection.cpp +++ b/telegram-bot-api/HttpStatConnection.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/HttpStatConnection.h b/telegram-bot-api/HttpStatConnection.h index dc1fa63..b653326 100644 --- a/telegram-bot-api/HttpStatConnection.h +++ b/telegram-bot-api/HttpStatConnection.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Query.cpp b/telegram-bot-api/Query.cpp index 1d2d7e9..3478e04 100644 --- a/telegram-bot-api/Query.cpp +++ b/telegram-bot-api/Query.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Query.h b/telegram-bot-api/Query.h index 7fdad09..8d79aba 100644 --- a/telegram-bot-api/Query.h +++ b/telegram-bot-api/Query.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Stats.cpp b/telegram-bot-api/Stats.cpp index fe504f4..5c76e7f 100644 --- a/telegram-bot-api/Stats.cpp +++ b/telegram-bot-api/Stats.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Stats.h b/telegram-bot-api/Stats.h index 661ddec..ae38426 100644 --- a/telegram-bot-api/Stats.h +++ b/telegram-bot-api/Stats.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Watchdog.cpp b/telegram-bot-api/Watchdog.cpp index 8b8105e..1bb15e6 100644 --- a/telegram-bot-api/Watchdog.cpp +++ b/telegram-bot-api/Watchdog.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/Watchdog.h b/telegram-bot-api/Watchdog.h index 5ab0115..89ac3c6 100644 --- a/telegram-bot-api/Watchdog.h +++ b/telegram-bot-api/Watchdog.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/WebhookActor.cpp b/telegram-bot-api/WebhookActor.cpp index 8d55aef..762dce4 100644 --- a/telegram-bot-api/WebhookActor.cpp +++ b/telegram-bot-api/WebhookActor.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/WebhookActor.h b/telegram-bot-api/WebhookActor.h index ab3b8af..926a914 100644 --- a/telegram-bot-api/WebhookActor.h +++ b/telegram-bot-api/WebhookActor.h @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 37ca32a..8b7b975 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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) From 0dcec25d7e131ba8902e166a6a603c3bfd02028f Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 6 Jan 2023 15:52:53 +0300 Subject: [PATCH 02/16] Add ClientManager::get_top_clients. --- telegram-bot-api/ClientManager.cpp | 67 +++++++++++++++++------------- telegram-bot-api/ClientManager.h | 6 +++ 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index c6cd45c..e07e86f 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -150,6 +150,39 @@ void ClientManager::send(PromisedQueryPtr query) { std::move(query)); // will send 429 if the client is already closed } +ClientManager::TopClients ClientManager::get_top_clients(std::size_t max_count, td::Slice token_filter) { + auto now = td::Time::now(); + TopClients result; + td::vector> top_client_ids; + for (auto id : clients_.ids()) { + auto *client_info = clients_.get(id); + CHECK(client_info); + + if (client_info->stat_.is_active(now)) { + result.active_count++; + } + + if (!td::begins_with(client_info->token_, token_filter)) { + continue; + } + + auto score = static_cast(client_info->stat_.get_score(now) * -1e9); + if (score == 0 && top_client_ids.size() >= max_count) { + continue; + } + top_client_ids.emplace_back(score, id); + } + if (top_client_ids.size() < max_count) { + max_count = top_client_ids.size(); + } + std::partial_sort(top_client_ids.begin(), top_client_ids.begin() + max_count, top_client_ids.end()); + result.top_client_ids.reserve(max_count); + for (std::size_t i = 0; i < max_count; i++) { + result.top_client_ids.push_back(top_client_ids[i].second); + } + return result; +} + void ClientManager::get_stats(td::Promise promise, td::vector> args) { if (close_flag_) { @@ -186,38 +219,12 @@ void ClientManager::get_stats(td::Promise promise, } auto now = td::Time::now(); - td::int32 active_bot_count = 0; - td::vector> top_bot_ids; - size_t max_bots = 50; - for (auto id : clients_.ids()) { - auto *client_info = clients_.get(id); - CHECK(client_info); - - if (client_info->stat_.is_active(now)) { - active_bot_count++; - } - - if (!td::begins_with(client_info->token_, id_filter)) { - continue; - } - - auto score = static_cast(client_info->stat_.get_score(now) * -1e9); - if (score == 0 && top_bot_ids.size() >= max_bots) { - continue; - } - top_bot_ids.emplace_back(score, id); - } - if (top_bot_ids.size() < max_bots) { - max_bots = top_bot_ids.size(); - } - std::partial_sort(top_bot_ids.begin(), top_bot_ids.begin() + max_bots, top_bot_ids.end()); - top_bot_ids.resize(max_bots); - + auto top_clients = get_top_clients(50, id_filter); sb << stat_.get_description() << '\n'; if (id_filter.empty()) { sb << "uptime\t" << now - parameters_->start_time_ << '\n'; sb << "bot_count\t" << clients_.size() << '\n'; - sb << "active_bot_count\t" << active_bot_count << '\n'; + sb << "active_bot_count\t" << top_clients.active_count << '\n'; auto r_mem_stat = td::mem_stat(); if (r_mem_stat.is_ok()) { auto mem_stat = r_mem_stat.move_as_ok(); @@ -245,8 +252,8 @@ void ClientManager::get_stats(td::Promise promise, } } - for (auto top_bot_id : top_bot_ids) { - auto *client_info = clients_.get(top_bot_id.second); + for (auto top_client_id : top_clients.top_client_ids) { + auto *client_info = clients_.get(top_client_id); CHECK(client_info); auto bot_info = client_info->client_.get_actor_unsafe()->get_bot_info(); diff --git a/telegram-bot-api/ClientManager.h b/telegram-bot-api/ClientManager.h index bd82ee9..33ef4c7 100644 --- a/telegram-bot-api/ClientManager.h +++ b/telegram-bot-api/ClientManager.h @@ -83,6 +83,12 @@ class ClientManager final : public td::Actor { static PromisedQueryPtr get_webhook_restore_query(td::Slice token, td::Slice webhook_info, std::shared_ptr shared_data); + struct TopClients { + td::int32 active_count = 0; + td::vector top_client_ids; + }; + TopClients get_top_clients(std::size_t max_count, td::Slice token_filter); + void start_up() final; void raw_event(const td::Event::Raw &event) final; void timeout_expired() final; From 75caf1b0e79c492f4e18f14b4c96625edaa27134 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 6 Jan 2023 16:29:09 +0300 Subject: [PATCH 03/16] Log top bots in dump_statistics. --- td | 2 +- telegram-bot-api/ClientManager.cpp | 31 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/td b/td index 5ee9c73..ac0de06 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit 5ee9c7365b9533d207887e32a9d71645e5d30fd8 +Subproject commit ac0de06b5f55e7bb5c2904ed6ef3aeb8c2df8c85 diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index e07e86f..dea4536 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -488,6 +488,37 @@ void ClientManager::dump_statistics() { } td::dump_pending_network_queries(*parameters_->net_query_stats_); + + auto now = td::Time::now(); + auto top_clients = get_top_clients(10, {}); + for (auto top_client_id : top_clients.top_client_ids) { + auto *client_info = clients_.get(top_client_id); + CHECK(client_info); + + auto bot_info = client_info->client_.get_actor_unsafe()->get_bot_info(); + td::string update_count; + td::string request_count; + auto replace_tabs = [](td::string &str) { + for (auto &c : str) { + if (c == '\t') { + c = ' '; + } + } + }; + auto stats = client_info->stat_.as_vector(now); + for (auto &stat : stats) { + if (stat.key_ == "update_count") { + replace_tabs(stat.value_); + update_count = std::move(stat.value_); + } + if (stat.key_ == "request_count") { + replace_tabs(stat.value_); + request_count = std::move(stat.value_); + } + } + LOG(WARNING) << td::tag("id", bot_info.id_) << td::tag("update_count", update_count) + << td::tag("request_count", request_count); + } } void ClientManager::raw_event(const td::Event::Raw &event) { From 452a1900942fce82a5e9e96237213f46108975c2 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 6 Jan 2023 16:30:12 +0300 Subject: [PATCH 04/16] Reduce Watchdog timeout to 0.25. --- telegram-bot-api/ClientManager.h | 2 +- telegram-bot-api/telegram-bot-api.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram-bot-api/ClientManager.h b/telegram-bot-api/ClientManager.h index 33ef4c7..ef91214 100644 --- a/telegram-bot-api/ClientManager.h +++ b/telegram-bot-api/ClientManager.h @@ -76,7 +76,7 @@ class ClientManager final : public td::Actor { td::int64 tqueue_deleted_events_ = 0; td::int64 last_tqueue_deleted_events_ = 0; - static constexpr double WATCHDOG_TIMEOUT = 0.5; + static constexpr double WATCHDOG_TIMEOUT = 0.25; static td::int64 get_tqueue_id(td::int64 user_id, bool is_test_dc); diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 8b7b975..60713ee 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -497,7 +497,7 @@ int main(int argc, char *argv[]) { .release(); } - constexpr double WATCHDOG_TIMEOUT = 0.5; + constexpr double WATCHDOG_TIMEOUT = 0.25; auto watchdog_id = sched.create_actor_unsafe(thread_count - 2, "Watchdog", td::this_thread::get_id(), WATCHDOG_TIMEOUT); From 1b741dd3b315adc966be544fd0381e8a62ebb8b5 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 6 Jan 2023 18:03:45 +0300 Subject: [PATCH 05/16] Update TDLib and asynchronously destroy deleted TQueue events. --- td | 2 +- telegram-bot-api/Client.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/td b/td index ac0de06..eeb73d4 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit ac0de06b5f55e7bb5c2904ed6ef3aeb8c2df8c85 +Subproject commit eeb73d4eb22d201655ed30e0e5b76ced3ef2323f diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index d084ac0..0e72482 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -5292,7 +5292,8 @@ void Client::timeout_expired() { void Client::clear_tqueue() { CHECK(webhook_id_.empty()); auto &tqueue = parameters_->shared_data_->tqueue_; - tqueue->clear(tqueue_id_, 0); + auto deleted_events = tqueue->clear(tqueue_id_, 0); + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), deleted_events); } bool Client::to_bool(td::MutableSlice value) { @@ -9533,7 +9534,8 @@ void Client::do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQu LOG(DEBUG) << "Queue head = " << tqueue->get_head(tqueue_id_) << ", queue tail = " << tqueue->get_tail(tqueue_id_); if (offset < 0) { - tqueue->clear(tqueue_id_, -offset); + auto deleted_events = tqueue->clear(tqueue_id_, -offset); + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), deleted_events); } if (offset <= 0) { offset = tqueue->get_head(tqueue_id_).value(); From a8f7f78fe135bd7c4d9e8ffdaa4ba84a7e72beea Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 8 Jan 2023 21:30:51 +0300 Subject: [PATCH 06/16] Rendomize maximum webhook event delay. --- telegram-bot-api/WebhookActor.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/WebhookActor.cpp b/telegram-bot-api/WebhookActor.cpp index 762dce4..3bab4ea 100644 --- a/telegram-bot-api/WebhookActor.cpp +++ b/telegram-bot-api/WebhookActor.cpp @@ -504,7 +504,8 @@ void WebhookActor::on_update_error(td::TQueue::EventId event_id, td::Slice error int next_delay = update.delay_; int next_effective_delay = retry_after; if (retry_after == 0 && update.fail_count_ > 0) { - next_delay = td::min(WEBHOOK_MAX_RESEND_TIMEOUT, next_delay * 2); + auto max_timeout = td::Random::fast(WEBHOOK_MAX_RESEND_TIMEOUT, WEBHOOK_MAX_RESEND_TIMEOUT * 2); + next_delay = td::min(max_timeout, next_delay * 2); next_effective_delay = next_delay; } if (parameters_->shared_data_->get_unix_time(now) + next_effective_delay > update.expires_at_) { From c68f6a7a8991f54ea25f3fbe5ef21d75abb5f292 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 11 Jan 2023 20:30:27 +0300 Subject: [PATCH 07/16] Update TDLib to 1.8.11. --- td | 2 +- telegram-bot-api/Client.cpp | 31 ++++++++++++++++++++++-------- telegram-bot-api/ClientManager.cpp | 2 +- telegram-bot-api/WebhookActor.cpp | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/td b/td index eeb73d4..3179d35 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit eeb73d4eb22d201655ed30e0e5b76ced3ef2323f +Subproject commit 3179d35694a28267a0b6273fc9b5bdce3b6b1235 diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 0e72482..cec3a52 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -734,10 +734,12 @@ class Client::JsonChat final : public Jsonable { } object("permissions", JsonChatPermissions(permissions)); } - auto everyone_is_administrator = permissions->can_send_messages_ && permissions->can_send_media_messages_ && - permissions->can_send_polls_ && permissions->can_send_other_messages_ && - permissions->can_add_web_page_previews_ && permissions->can_change_info_ && - permissions->can_invite_users_ && permissions->can_pin_messages_; + auto everyone_is_administrator = + permissions->can_send_messages_ && permissions->can_send_audios_ && permissions->can_send_documents_ && + permissions->can_send_photos_ && permissions->can_send_videos_ && permissions->can_send_video_notes_ && + permissions->can_send_voice_notes_ && permissions->can_send_polls_ && + permissions->can_send_other_messages_ && permissions->can_add_web_page_previews_ && + permissions->can_change_info_ && permissions->can_invite_users_ && permissions->can_pin_messages_; object("all_members_are_administrators", td::JsonBool(everyone_is_administrator)); photo = group_info->photo.get(); break; @@ -2196,6 +2198,10 @@ void Client::JsonMessage::store(JsonValueScope *scope) const { case td_api::messageBotWriteAccessAllowed::ID: object("write_access_allowed", JsonEmptyObject()); break; + case td_api::messageUserShared::ID: + break; + case td_api::messageChatShared::ID: + break; default: UNREACHABLE(); } @@ -6914,9 +6920,11 @@ td::Result> Client::get_chat_permiss if (can_send_other_messages || can_add_web_page_previews) { can_send_media_messages = true; } - return make_object(can_send_messages, can_send_media_messages, can_send_polls, - can_send_other_messages, can_add_web_page_previews, can_change_info, - can_invite_users, can_pin_messages, can_manage_topics); + return make_object(can_send_messages, can_send_media_messages, can_send_media_messages, + can_send_media_messages, can_send_media_messages, can_send_media_messages, + can_send_media_messages, can_send_polls, can_send_other_messages, + can_add_web_page_previews, can_change_info, can_invite_users, + can_pin_messages, can_manage_topics); } td::Result> Client::get_input_media(const Query *query, @@ -9975,8 +9983,11 @@ void Client::json_store_administrator_rights(td::JsonObjectScope &object, const } void Client::json_store_permissions(td::JsonObjectScope &object, const td_api::chatPermissions *permissions) { + bool can_send_media_messages = permissions->can_send_audios_ || permissions->can_send_documents_ || + permissions->can_send_photos_ || permissions->can_send_videos_ || + permissions->can_send_video_notes_ || permissions->can_send_voice_notes_; object("can_send_messages", td::JsonBool(permissions->can_send_messages_)); - object("can_send_media_messages", td::JsonBool(permissions->can_send_media_messages_)); + object("can_send_media_messages", td::JsonBool(can_send_media_messages)); object("can_send_polls", td::JsonBool(permissions->can_send_polls_)); object("can_send_other_messages", td::JsonBool(permissions->can_send_other_messages_)); object("can_add_web_page_previews", td::JsonBool(permissions->can_add_web_page_previews_)); @@ -10454,6 +10465,10 @@ bool Client::need_skip_update_message(int64 chat_id, const object_ptr containers; auto add_string = [&containers](td::Slice str) { containers.emplace_back(str); - return containers.back().as_slice(); + return containers.back().as_mutable_slice(); }; token = add_string(token); diff --git a/telegram-bot-api/WebhookActor.cpp b/telegram-bot-api/WebhookActor.cpp index 3bab4ea..318390c 100644 --- a/telegram-bot-api/WebhookActor.cpp +++ b/telegram-bot-api/WebhookActor.cpp @@ -687,7 +687,7 @@ void WebhookActor::start_up() { next_ip_address_resolve_time_ = last_success_time_ = td::Time::now() - 3600; - active_new_connection_flood_.add_limit(1, 20); + active_new_connection_flood_.add_limit(0.5, 10); pending_new_connection_flood_.add_limit(2, 1); From e636a6db45a9c86c7ccdd710effd9feb971f283a Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 11 Jan 2023 20:39:34 +0300 Subject: [PATCH 08/16] Support "request_user" keyboard buttons. --- telegram-bot-api/Client.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index cec3a52..130e36c 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -5349,6 +5349,19 @@ td::Result> Client::get_keyboard_butt return make_object(text, make_object(url)); } + if (has_json_object_field(object, "request_user")) { + TRY_RESULT(request_user, get_json_object_field(object, "request_user", JsonValue::Type::Object, false)); + auto &request_user_object = request_user.get_object(); + TRY_RESULT(id, get_json_object_int_field(request_user_object, "request_id", false)); + auto restrict_user_is_bot = has_json_object_field(request_user_object, "user_is_bot"); + TRY_RESULT(user_is_bot, get_json_object_bool_field(request_user_object, "user_is_bot")); + auto restrict_user_is_premium = has_json_object_field(request_user_object, "user_is_premium"); + TRY_RESULT(user_is_premium, get_json_object_bool_field(request_user_object, "user_is_premium")); + return make_object( + text, make_object(id, restrict_user_is_bot, user_is_bot, + restrict_user_is_premium, user_is_premium)); + } + return make_object(text, nullptr); } if (button.type() == JsonValue::Type::String) { From c3c8f112ee91ba6eeca8cd654de07cc253b15c2c Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 11 Jan 2023 20:50:43 +0300 Subject: [PATCH 09/16] Support "request_chat" keyboard buttons. --- telegram-bot-api/Client.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 130e36c..09c645c 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -5362,6 +5362,34 @@ td::Result> Client::get_keyboard_butt restrict_user_is_premium, user_is_premium)); } + if (has_json_object_field(object, "request_chat")) { + TRY_RESULT(request_chat, get_json_object_field(object, "request_chat", JsonValue::Type::Object, false)); + auto &request_chat_object = request_chat.get_object(); + TRY_RESULT(id, get_json_object_int_field(request_chat_object, "request_id", false)); + TRY_RESULT(chat_is_channel, get_json_object_bool_field(request_chat_object, "chat_is_channel")); + auto restrict_chat_is_forum = has_json_object_field(request_chat_object, "chat_is_forum"); + TRY_RESULT(chat_is_forum, get_json_object_bool_field(request_chat_object, "chat_is_forum")); + auto restrict_chat_has_username = has_json_object_field(request_chat_object, "chat_has_username"); + TRY_RESULT(chat_has_username, get_json_object_bool_field(request_chat_object, "chat_has_username")); + TRY_RESULT(chat_is_created, get_json_object_bool_field(request_chat_object, "chat_is_created")); + td_api::object_ptr user_administrator_rights; + if (has_json_object_field(request_chat_object, "user_administrator_rights")) { + TRY_RESULT_ASSIGN(user_administrator_rights, get_chat_administrator_rights(get_json_object_field_force( + request_chat_object, "user_administrator_rights"))); + } + td_api::object_ptr bot_administrator_rights; + if (has_json_object_field(request_chat_object, "bot_administrator_rights")) { + TRY_RESULT_ASSIGN(bot_administrator_rights, get_chat_administrator_rights(get_json_object_field_force( + request_chat_object, "bot_administrator_rights"))); + } + TRY_RESULT(bot_is_member, get_json_object_bool_field(request_chat_object, "bot_is_member")); + return make_object( + text, make_object( + id, chat_is_channel, restrict_chat_is_forum, chat_is_forum, restrict_chat_has_username, + chat_has_username, chat_is_created, std::move(user_administrator_rights), + std::move(bot_administrator_rights), bot_is_member)); + } + return make_object(text, nullptr); } if (button.type() == JsonValue::Type::String) { From 82d592702d05048f84c74489daf8dd0d2fb58e75 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 12 Jan 2023 14:49:26 +0300 Subject: [PATCH 10/16] Support messageUserShared and messageChatShared. --- telegram-bot-api/Client.cpp | 42 +++++++++++++++++++++++++++++++------ telegram-bot-api/Client.h | 2 ++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 09c645c..66b5dee 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -1724,6 +1724,34 @@ class Client::JsonChatSetMessageAutoDeleteTime final : public Jsonable { const td_api::messageChatSetMessageAutoDeleteTime *chat_set_message_auto_delete_time_; }; +class Client::JsonUserShared final : public Jsonable { + public: + explicit JsonUserShared(const td_api::messageUserShared *user_shared) : user_shared_(user_shared) { + } + void store(JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("user_id", user_shared_->user_id_); + object("request_id", user_shared_->button_id_); + } + + private: + const td_api::messageUserShared *user_shared_; +}; + +class Client::JsonChatShared final : public Jsonable { + public: + explicit JsonChatShared(const td_api::messageChatShared *chat_shared) : chat_shared_(chat_shared) { + } + void store(JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("chat_id", chat_shared_->chat_id_); + object("request_id", chat_shared_->button_id_); + } + + private: + const td_api::messageChatShared *chat_shared_; +}; + class Client::JsonWebAppInfo final : public Jsonable { public: explicit JsonWebAppInfo(const td::string &url) : url_(url) { @@ -2198,10 +2226,16 @@ void Client::JsonMessage::store(JsonValueScope *scope) const { case td_api::messageBotWriteAccessAllowed::ID: object("write_access_allowed", JsonEmptyObject()); break; - case td_api::messageUserShared::ID: + case td_api::messageUserShared::ID: { + auto content = static_cast(message_->content.get()); + object("user_shared", JsonUserShared(content)); break; - case td_api::messageChatShared::ID: + } + case td_api::messageChatShared::ID: { + auto content = static_cast(message_->content.get()); + object("chat_shared", JsonChatShared(content)); break; + } default: UNREACHABLE(); } @@ -10506,10 +10540,6 @@ bool Client::need_skip_update_message(int64 chat_id, const object_ptr Date: Wed, 18 Jan 2023 22:44:47 +0300 Subject: [PATCH 11/16] Improve BotInfo for bots with invalid tokens. --- telegram-bot-api/Client.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 66b5dee..e68e23d 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -4103,7 +4103,11 @@ ServerBotInfo Client::get_bot_info() const { if (user_info != nullptr) { res.username_ = user_info->editable_username; } else if (!was_authorized_) { - res.username_ = ""; + if (logging_out_) { + res.username_ = ""; + } else { + res.username_ = ""; + } } else { res.username_ = ""; } From d3846adaa21dc7e46c672979feb5316c4dc66aea Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 Jan 2023 19:27:19 +0300 Subject: [PATCH 12/16] Destroy HttpQuery on another thread. --- telegram-bot-api/Query.h | 2 ++ telegram-bot-api/WebhookActor.cpp | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/telegram-bot-api/Query.h b/telegram-bot-api/Query.h index 8d79aba..66c8e55 100644 --- a/telegram-bot-api/Query.h +++ b/telegram-bot-api/Query.h @@ -114,6 +114,8 @@ class Query final : public td::ListNode { if (!empty()) { shared_data_->query_list_size_.fetch_sub(1, std::memory_order_relaxed); } + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), container_, args_, + headers_, files_, answer_); } } diff --git a/telegram-bot-api/WebhookActor.cpp b/telegram-bot-api/WebhookActor.cpp index 318390c..e21e114 100644 --- a/telegram-bot-api/WebhookActor.cpp +++ b/telegram-bot-api/WebhookActor.cpp @@ -590,6 +590,11 @@ void WebhookActor::send_updates() { } void WebhookActor::handle(td::unique_ptr response) { + SCOPE_EXIT { + bool dummy = false; + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), response, dummy); + }; + auto connection_id = get_link_token(); if (response) { VLOG(webhook) << "Got response from connection " << connection_id; From 2c23688be6f12d28d6468400ed0437999590f3b7 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 31 Jan 2023 01:46:40 +0300 Subject: [PATCH 13/16] Support separate media permissions. --- telegram-bot-api/Client.cpp | 76 +++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index e68e23d..6819dbd 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -6931,7 +6931,12 @@ td::Result> Client::get_location(const Quer td::Result> Client::get_chat_permissions(const Query *query, bool &allow_legacy) { auto can_send_messages = false; - auto can_send_media_messages = false; + auto can_send_audios = false; + auto can_send_documents = false; + auto can_send_photos = false; + auto can_send_videos = false; + auto can_send_video_notes = false; + auto can_send_voice_notes = false; auto can_send_polls = false; auto can_send_other_messages = false; auto can_add_web_page_previews = false; @@ -6957,7 +6962,6 @@ td::Result> Client::get_chat_permiss auto status = [&] { TRY_RESULT_ASSIGN(can_send_messages, get_json_object_bool_field(object, "can_send_messages")); - TRY_RESULT_ASSIGN(can_send_media_messages, get_json_object_bool_field(object, "can_send_media_messages")); TRY_RESULT_ASSIGN(can_send_polls, get_json_object_bool_field(object, "can_send_polls")); TRY_RESULT_ASSIGN(can_send_other_messages, get_json_object_bool_field(object, "can_send_other_messages")); TRY_RESULT_ASSIGN(can_add_web_page_previews, get_json_object_bool_field(object, "can_add_web_page_previews")); @@ -6969,19 +6973,60 @@ td::Result> Client::get_chat_permiss } else { can_manage_topics = can_pin_messages; } + if (has_json_object_field(object, "can_send_audios") || has_json_object_field(object, "can_send_documents") || + has_json_object_field(object, "can_send_photos") || has_json_object_field(object, "can_send_videos") || + has_json_object_field(object, "can_send_video_notes") || + has_json_object_field(object, "can_send_voice_notes")) { + TRY_RESULT_ASSIGN(can_send_audios, get_json_object_bool_field(object, "can_send_audios")); + TRY_RESULT_ASSIGN(can_send_documents, get_json_object_bool_field(object, "can_send_documents")); + TRY_RESULT_ASSIGN(can_send_photos, get_json_object_bool_field(object, "can_send_photos")); + TRY_RESULT_ASSIGN(can_send_videos, get_json_object_bool_field(object, "can_send_videos")); + TRY_RESULT_ASSIGN(can_send_video_notes, get_json_object_bool_field(object, "can_send_video_notes")); + TRY_RESULT_ASSIGN(can_send_voice_notes, get_json_object_bool_field(object, "can_send_voice_notes")); + } else { + TRY_RESULT(can_send_media_messages, get_json_object_bool_field(object, "can_send_media_messages")); + can_send_audios = can_send_media_messages; + can_send_documents = can_send_media_messages; + can_send_photos = can_send_media_messages; + can_send_videos = can_send_media_messages; + can_send_video_notes = can_send_media_messages; + can_send_voice_notes = can_send_media_messages; + if (can_send_media_messages) { + can_send_messages = true; + } + } return Status::OK(); }(); if (status.is_error()) { return Status::Error(400, PSLICE() << "Can't parse chat permissions: " << status.message()); } + + if (can_send_other_messages || can_add_web_page_previews) { + can_send_audios = true; + can_send_documents = true; + can_send_photos = true; + can_send_videos = true; + can_send_video_notes = true; + can_send_voice_notes = true; + can_send_messages = true; + } + if (can_send_polls) { + can_send_messages = true; + } } else if (allow_legacy) { allow_legacy = false; can_send_messages = to_bool(query->arg("can_send_messages")); - can_send_media_messages = to_bool(query->arg("can_send_media_messages")); + bool can_send_media_messages = to_bool(query->arg("can_send_media_messages")); can_send_other_messages = to_bool(query->arg("can_send_other_messages")); can_add_web_page_previews = to_bool(query->arg("can_add_web_page_previews")); + if (can_send_other_messages || can_add_web_page_previews) { + can_send_media_messages = true; + } + if (can_send_media_messages) { + can_send_messages = true; + } if (can_send_messages && can_send_media_messages && can_send_other_messages && can_add_web_page_previews) { // legacy unrestrict @@ -6994,16 +7039,19 @@ td::Result> Client::get_chat_permiss query->has_arg("can_send_other_messages") || query->has_arg("can_add_web_page_previews")) { allow_legacy = true; } + + can_send_audios = can_send_media_messages; + can_send_documents = can_send_media_messages; + can_send_photos = can_send_media_messages; + can_send_videos = can_send_media_messages; + can_send_video_notes = can_send_media_messages; + can_send_voice_notes = can_send_media_messages; } - if (can_send_other_messages || can_add_web_page_previews) { - can_send_media_messages = true; - } - return make_object(can_send_messages, can_send_media_messages, can_send_media_messages, - can_send_media_messages, can_send_media_messages, can_send_media_messages, - can_send_media_messages, can_send_polls, can_send_other_messages, - can_add_web_page_previews, can_change_info, can_invite_users, - can_pin_messages, can_manage_topics); + return make_object(can_send_messages, can_send_audios, can_send_documents, can_send_photos, + can_send_videos, can_send_video_notes, can_send_voice_notes, + can_send_polls, can_send_other_messages, can_add_web_page_previews, + can_change_info, can_invite_users, can_pin_messages, can_manage_topics); } td::Result> Client::get_input_media(const Query *query, @@ -10067,6 +10115,12 @@ void Client::json_store_permissions(td::JsonObjectScope &object, const td_api::c permissions->can_send_video_notes_ || permissions->can_send_voice_notes_; object("can_send_messages", td::JsonBool(permissions->can_send_messages_)); object("can_send_media_messages", td::JsonBool(can_send_media_messages)); + object("can_send_audios", td::JsonBool(permissions->can_send_audios_)); + object("can_send_documents", td::JsonBool(permissions->can_send_documents_)); + object("can_send_photos", td::JsonBool(permissions->can_send_photos_)); + object("can_send_videos", td::JsonBool(permissions->can_send_videos_)); + object("can_send_video_notes", td::JsonBool(permissions->can_send_video_notes_)); + object("can_send_voice_notes", td::JsonBool(permissions->can_send_voice_notes_)); object("can_send_polls", td::JsonBool(permissions->can_send_polls_)); object("can_send_other_messages", td::JsonBool(permissions->can_send_other_messages_)); object("can_add_web_page_previews", td::JsonBool(permissions->can_add_web_page_previews_)); From 0bc181c216b5b1f8d1ef74477b8510b82e87cce9 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 1 Feb 2023 03:46:27 +0300 Subject: [PATCH 14/16] Add ChatJoinRequest.user_chat_id. --- telegram-bot-api/Client.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 6819dbd..5d7c686 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2725,6 +2725,7 @@ class Client::JsonChatJoinRequest final : public Jsonable { auto object = scope->enter_object(); object("chat", JsonChat(update_->chat_id_, false, client_)); object("from", JsonUser(update_->request_->user_id_, client_)); + object("user_chat_id", update_->user_chat_id_); object("date", update_->request_->date_); if (!update_->request_->bio_.empty()) { object("bio", update_->request_->bio_); From 0e6826b7e4706004f560390f2de099716f407a61 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 1 Feb 2023 04:00:16 +0300 Subject: [PATCH 15/16] Add use_independent_chat_permissions to restrictChatMember and setChatPermissions. --- telegram-bot-api/Client.cpp | 20 +++++++++++--------- telegram-bot-api/Client.h | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 5d7c686..8b62bf5 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -6929,8 +6929,8 @@ td::Result> Client::get_location(const Quer td::to_double(horizontal_accuracy)); } -td::Result> Client::get_chat_permissions(const Query *query, - bool &allow_legacy) { +td::Result> Client::get_chat_permissions( + const Query *query, bool &allow_legacy, bool use_independent_chat_permissions) { auto can_send_messages = false; auto can_send_audios = false; auto can_send_documents = false; @@ -6992,7 +6992,7 @@ td::Result> Client::get_chat_permiss can_send_videos = can_send_media_messages; can_send_video_notes = can_send_media_messages; can_send_voice_notes = can_send_media_messages; - if (can_send_media_messages) { + if (can_send_media_messages && !use_independent_chat_permissions) { can_send_messages = true; } } @@ -7003,7 +7003,7 @@ td::Result> Client::get_chat_permiss return Status::Error(400, PSLICE() << "Can't parse chat permissions: " << status.message()); } - if (can_send_other_messages || can_add_web_page_previews) { + if ((can_send_other_messages || can_add_web_page_previews) && !use_independent_chat_permissions) { can_send_audios = true; can_send_documents = true; can_send_photos = true; @@ -7012,7 +7012,7 @@ td::Result> Client::get_chat_permiss can_send_voice_notes = true; can_send_messages = true; } - if (can_send_polls) { + if (can_send_polls && !use_independent_chat_permissions) { can_send_messages = true; } } else if (allow_legacy) { @@ -7022,10 +7022,10 @@ td::Result> Client::get_chat_permiss bool can_send_media_messages = to_bool(query->arg("can_send_media_messages")); can_send_other_messages = to_bool(query->arg("can_send_other_messages")); can_add_web_page_previews = to_bool(query->arg("can_add_web_page_previews")); - if (can_send_other_messages || can_add_web_page_previews) { + if ((can_send_other_messages || can_add_web_page_previews) && !use_independent_chat_permissions) { can_send_media_messages = true; } - if (can_send_media_messages) { + if (can_send_media_messages && !use_independent_chat_permissions) { can_send_messages = true; } @@ -8395,7 +8395,8 @@ td::Status Client::process_set_chat_title_query(PromisedQueryPtr &query) { td::Status Client::process_set_chat_permissions_query(PromisedQueryPtr &query) { auto chat_id = query->arg("chat_id"); bool allow_legacy = false; - TRY_RESULT(permissions, get_chat_permissions(query.get(), allow_legacy)); + auto use_independent_chat_permissions = to_bool(query->arg("use_independent_chat_permissions")); + TRY_RESULT(permissions, get_chat_permissions(query.get(), allow_legacy, use_independent_chat_permissions)); CHECK(!allow_legacy); check_chat(chat_id, AccessRights::Write, std::move(query), @@ -8818,7 +8819,8 @@ td::Status Client::process_restrict_chat_member_query(PromisedQueryPtr &query) { TRY_RESULT(user_id, get_user_id(query.get())); int32 until_date = get_integer_arg(query.get(), "until_date", 0); bool allow_legacy = true; - TRY_RESULT(permissions, get_chat_permissions(query.get(), allow_legacy)); + auto use_independent_chat_permissions = to_bool(query->arg("use_independent_chat_permissions")); + TRY_RESULT(permissions, get_chat_permissions(query.get(), allow_legacy, use_independent_chat_permissions)); check_chat(chat_id, AccessRights::Write, std::move(query), [this, user_id, until_date, is_legacy = allow_legacy, permissions = std::move(permissions)]( diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index f8977b0..1925f87 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -433,7 +433,8 @@ class Client final : public WebhookActor::Callback { static td::Result> get_location(const Query *query); - static td::Result> get_chat_permissions(const Query *query, bool &allow_legacy); + static td::Result> get_chat_permissions(const Query *query, bool &allow_legacy, + bool use_independent_chat_permissions); td::Result> get_input_media(const Query *query, td::JsonValue &&input_media, bool for_album) const; From 81808454299921c142d3a6eac0cfddc97981591e Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 2 Feb 2023 19:21:46 +0300 Subject: [PATCH 16/16] Update version to 6.5. --- CMakeLists.txt | 2 +- telegram-bot-api/telegram-bot-api.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 894075e..8ad72e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ if (POLICY CMP0065) cmake_policy(SET CMP0065 NEW) endif() -project(TelegramBotApi VERSION 6.4.1 LANGUAGES CXX) +project(TelegramBotApi VERSION 6.5 LANGUAGES CXX) if (POLICY CMP0069) option(TELEGRAM_BOT_API_ENABLE_LTO "Use \"ON\" to enable Link Time Optimization.") diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 60713ee..c2e4581 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -165,7 +165,7 @@ int main(int argc, char *argv[]) { auto start_time = td::Time::now(); auto shared_data = std::make_shared(); auto parameters = std::make_unique(); - parameters->version_ = "6.4.1"; + parameters->version_ = "6.5"; parameters->shared_data_ = shared_data; parameters->start_time_ = start_time; auto net_query_stats = td::create_net_query_stats();