From 6e5d607b905578f557af855dc465306eaa3fd241 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 27 Aug 2024 15:55:54 +0300 Subject: [PATCH] Add and use Client::is_special_error_code. --- telegram-bot-api/Client.cpp | 42 ++++++++++++++++++++----------------- telegram-bot-api/Client.h | 2 ++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 86bd10d..87c63f3 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -39,6 +39,24 @@ namespace telegram_bot_api { using td_api::make_object; using td_api::move_object_as; +Client::Client(td::ActorShared<> parent, const td::string &bot_token, bool is_test_dc, int64 tqueue_id, + std::shared_ptr parameters, td::ActorId stat_actor) + : parent_(std::move(parent)) + , bot_token_(bot_token) + , bot_token_id_("") + , is_test_dc_(is_test_dc) + , tqueue_id_(tqueue_id) + , parameters_(std::move(parameters)) + , stat_actor_(std::move(stat_actor)) { + static auto is_inited = init_methods(); + CHECK(is_inited); +} + +Client::~Client() { + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), messages_, users_, groups_, + supergroups_, chats_, sticker_set_names_); +} + int Client::get_retry_after_time(td::Slice error_message) { td::Slice prefix = "Too Many Requests: retry after "; if (td::begins_with(error_message, prefix)) { @@ -179,22 +197,8 @@ void Client::fail_query_with_error(PromisedQueryPtr &&query, object_ptrcode_, error->message_, default_message); } -Client::Client(td::ActorShared<> parent, const td::string &bot_token, bool is_test_dc, int64 tqueue_id, - std::shared_ptr parameters, td::ActorId stat_actor) - : parent_(std::move(parent)) - , bot_token_(bot_token) - , bot_token_id_("") - , is_test_dc_(is_test_dc) - , tqueue_id_(tqueue_id) - , parameters_(std::move(parameters)) - , stat_actor_(std::move(stat_actor)) { - static auto is_inited = init_methods(); - CHECK(is_inited); -} - -Client::~Client() { - td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), messages_, users_, groups_, - supergroups_, chats_, sticker_set_names_); +bool Client::is_special_error_code(int32 error_code) { + return error_code == 401 || error_code == 429 || error_code >= 500; } bool Client::init_methods() { @@ -5281,7 +5285,7 @@ class Client::TdOnGetChatPinnedMessageCallback final : public TdQueryCallback { int64 pinned_message_id = 0; if (result->get_id() == td_api::error::ID) { auto error = move_object_as(result); - if (error->code_ == 429) { + if (is_special_error_code(error->code_)) { return fail_query_with_error(std::move(query_), std::move(error)); } else if (error->code_ != 404 && error->message_ != "CHANNEL_PRIVATE") { LOG(ERROR) << "Failed to get chat pinned message: " << to_string(error); @@ -5349,7 +5353,7 @@ class Client::TdOnGetChatPinnedMessageToUnpinCallback final : public TdQueryCall int64 pinned_message_id = 0; if (result->get_id() == td_api::error::ID) { auto error = move_object_as(result); - if (error->code_ == 429) { + if (is_special_error_code(error->code_)) { return fail_query_with_error(std::move(query_), std::move(error)); } else { return fail_query_with_error(std::move(query_), 400, "Message to unpin not found"); @@ -9959,7 +9963,7 @@ void Client::on_message_send_failed(int64 chat_id, int64 old_message_id, int64 n auto &query = *pending_send_message_queries_[query_id]; if (query.is_multisend) { if (query.error == nullptr || query.error->message_ == "Group send failed") { - if (error->code_ == 401 || error->code_ == 429 || error->code_ >= 500 || error->message_ == "Group send failed") { + if (is_special_error_code(error->code_) || error->message_ == "Group send failed") { query.error = std::move(error); } else { auto pos = (query.total_message_count - query.awaited_message_count + 1); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 3fef529..bc12bcb 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -810,6 +810,8 @@ class Client final : public WebhookActor::Callback { static void fail_query_with_error(PromisedQueryPtr &&query, object_ptr error, td::Slice default_message = td::Slice()); + static bool is_special_error_code(int32 error_code); + class JsonUpdates; void do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQueryPtr query);