Merge version 6.0.1

This commit is contained in:
Giuseppe Marino 2022-05-01 10:45:29 +02:00
commit edcb8529e6
No known key found for this signature in database
GPG Key ID: 2BC70C5463357449
12 changed files with 949 additions and 431 deletions

View File

@ -6,7 +6,7 @@ if (POLICY CMP0065)
cmake_policy(SET CMP0065 NEW)
endif()
project(TelegramBotApi VERSION 5.7 LANGUAGES CXX)
project(TelegramBotApi VERSION 6.0.1 LANGUAGES CXX)
if (POLICY CMP0069)
option(TELEGRAM_BOT_API_ENABLE_LTO "Use \"ON\" to enable Link Time Optimization.")

2
td

@ -1 +1 @@
Subproject commit 1e1ab5d1b0e4811e6d9e1584a82da08448d0cada
Subproject commit 782670c7dbf278e0ba07fc7e168f39ac154c7238

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,8 @@
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
@ -27,8 +29,6 @@
#include <limits>
#include <memory>
#include <queue>
#include <unordered_map>
#include <unordered_set>
namespace telegram_bot_api {
@ -125,6 +125,7 @@ class Client final : public WebhookActor::Callback {
class JsonEntity;
class JsonVectorEntities;
class JsonCallbackGame;
class JsonWebAppInfo;
class JsonInlineKeyboardButton;
class JsonInlineKeyboard;
class JsonReplyMarkup;
@ -139,6 +140,8 @@ class Client final : public WebhookActor::Callback {
class JsonShippingQuery;
class JsonPreCheckoutQuery;
class JsonBotCommand;
class JsonBotMenuButton;
class JsonChatAdministratorRights;
class JsonChatPhotos;
class JsonChatMember;
class JsonChatMembers;
@ -151,6 +154,7 @@ class Client final : public WebhookActor::Callback {
class JsonEncryptedPassportElement;
class JsonEncryptedCredentials;
class JsonPassportData;
class JsonWebAppData;
class JsonProximityAlertTriggered;
class JsonVideoChatScheduled;
class JsonVideoChatStarted;
@ -160,6 +164,7 @@ class Client final : public WebhookActor::Callback {
class JsonUpdateTypes;
class JsonWebhookInfo;
class JsonStickerSet;
class JsonSentWebAppMessage;
class JsonCustomJson;
//start custom Json objects
@ -188,7 +193,9 @@ class Client final : public WebhookActor::Callback {
class TdOnGetEditedMessageCallback;
class TdOnGetCallbackQueryMessageCallback;
class TdOnGetStickerSetCallback;
class TdOnGetMenuButtonCallback;
class TdOnGetMyCommandsCallback;
class TdOnGetMyDefaultAdministratorRightsCallback;
class TdOnGetChatFullInfoCallback;
class TdOnGetChatStickerSetCallback;
class TdOnGetChatPinnedMessageCallback;
@ -199,6 +206,7 @@ class Client final : public WebhookActor::Callback {
class TdOnReplacePrimaryChatInviteLinkCallback;
class TdOnGetChatInviteLinkCallback;
class TdOnGetGameHighScoresCallback;
class TdOnAnswerWebAppQueryCallback;
class TdOnReturnFileCallback;
class TdOnReturnStickerSetCallback;
class TdOnDownloadFileCallback;
@ -323,8 +331,8 @@ class Client final : public WebhookActor::Callback {
template <class OnSuccess>
void get_chat_member(int64 chat_id, int64 user_id, PromisedQueryPtr query, OnSuccess on_success);
void send_request(object_ptr<td_api::Function> &&f, std::unique_ptr<TdQueryCallback> handler);
void do_send_request(object_ptr<td_api::Function> &&f, std::unique_ptr<TdQueryCallback> handler);
void send_request(object_ptr<td_api::Function> &&f, td::unique_ptr<TdQueryCallback> handler);
void do_send_request(object_ptr<td_api::Function> &&f, td::unique_ptr<TdQueryCallback> handler);
static object_ptr<td_api::Object> execute(object_ptr<td_api::Function> &&f);
void on_update(object_ptr<td_api::Object> result);
void on_result(td::uint64 id, object_ptr<td_api::Object> result);
@ -378,6 +386,8 @@ class Client final : public WebhookActor::Callback {
object_ptr<td_api::inputThumbnail> get_input_thumbnail(const Query *query, Slice field_name) const;
td::Result<object_ptr<td_api::InputInlineQueryResult>> get_inline_query_result(const Query *query);
td::Result<object_ptr<td_api::InputInlineQueryResult>> get_inline_query_result(td::JsonValue &&value);
td::Result<td::vector<object_ptr<td_api::InputInlineQueryResult>>> get_inline_query_results(const Query *query);
@ -403,6 +413,14 @@ class Client final : public WebhookActor::Callback {
static td::Result<td::vector<object_ptr<td_api::botCommand>>> get_bot_commands(const Query *query);
static td::Result<object_ptr<td_api::botMenuButton>> get_bot_menu_button(const Query *query);
static td::Result<object_ptr<td_api::botMenuButton>> get_bot_menu_button(td::JsonValue &&value);
static td::Result<object_ptr<td_api::chatAdministratorRights>> get_chat_administrator_rights(td::JsonValue &&value);
static td::Result<object_ptr<td_api::chatAdministratorRights>> get_chat_administrator_rights(const Query *query);
static td::Result<object_ptr<td_api::maskPosition>> get_mask_position(const Query *query, Slice field_name);
static td::Result<object_ptr<td_api::maskPosition>> get_mask_position(td::JsonValue &&value);
@ -506,6 +524,10 @@ class Client final : public WebhookActor::Callback {
Status process_get_my_commands_query(PromisedQueryPtr &query);
Status process_set_my_commands_query(PromisedQueryPtr &query);
Status process_delete_my_commands_query(PromisedQueryPtr &query);
Status process_get_my_default_administrator_rights_query(PromisedQueryPtr &query);
Status process_set_my_default_administrator_rights_query(PromisedQueryPtr &query);
Status process_get_chat_menu_button_query(PromisedQueryPtr &query);
Status process_set_chat_menu_button_query(PromisedQueryPtr &query);
Status process_get_user_profile_photos_query(PromisedQueryPtr &query);
Status process_send_message_query(PromisedQueryPtr &query);
Status process_send_animation_query(PromisedQueryPtr &query);
@ -536,6 +558,7 @@ class Client final : public WebhookActor::Callback {
Status process_delete_message_query(PromisedQueryPtr &query);
Status process_set_game_score_query(PromisedQueryPtr &query);
Status process_get_game_high_scores_query(PromisedQueryPtr &query);
Status process_answer_web_app_query_query(PromisedQueryPtr &query);
Status process_answer_inline_query_query(PromisedQueryPtr &query);
Status process_answer_callback_query_query(PromisedQueryPtr &query);
Status process_answer_shipping_query_query(PromisedQueryPtr &query);
@ -682,6 +705,7 @@ class Client final : public WebhookActor::Callback {
td::string username;
td::string language_code;
object_ptr<td_api::chatPhoto> photo;
td::string bio;
// start custom properties
@ -695,12 +719,15 @@ class Client final : public WebhookActor::Callback {
bool is_inline_bot = false;
bool has_private_forwards = false;
};
static void add_user(std::unordered_map<int64, UserInfo> &users, object_ptr<td_api::user> &&user);
static void add_user(UserInfo *user_info, object_ptr<td_api::user> &&user);
void set_user_photo(int64 user_id, object_ptr<td_api::chatPhoto> &&photo);
void set_user_bio(int64 user_id, td::string &&bio);
void set_user_has_private_forwards(int64 user_id, bool has_private_forwards);
UserInfo *add_user_info(int64 user_id);
const UserInfo *get_user_info(int64 user_id) const;
struct GroupInfo {
object_ptr<td_api::chatPhoto> photo;
td::string description;
td::string invite_link;
int32 member_count = 0;
@ -709,13 +736,16 @@ class Client final : public WebhookActor::Callback {
bool is_active = false;
int64 upgraded_to_supergroup_id = 0;
};
static void add_group(std::unordered_map<int64, GroupInfo> &groups, object_ptr<td_api::basicGroup> &&group);
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_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;
struct SupergroupInfo {
td::string username;
object_ptr<td_api::chatPhoto> photo;
td::string description;
td::string invite_link;
int64 sticker_set_id = 0;
@ -733,8 +763,8 @@ class Client final : public WebhookActor::Callback {
bool is_scam = false;
// end custom properties
};
static void add_supergroup(std::unordered_map<int64, SupergroupInfo> &supergroups,
object_ptr<td_api::supergroup> &&supergroup);
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_invite_link(int64 supergroup_id, td::string &&invite_link);
void set_supergroup_sticker_set_id(int64 supergroup_id, int64 sticker_set_id);
@ -742,6 +772,7 @@ class Client final : public WebhookActor::Callback {
void set_supergroup_slow_mode_delay(int64 supergroup_id, int32 slow_mode_delay);
void set_supergroup_linked_chat_id(int64 supergroup_id, int64 linked_chat_id);
void set_supergroup_location(int64 supergroup_id, object_ptr<td_api::chatLocation> location);
SupergroupInfo *add_supergroup_info(int64 supergroup_id);
const SupergroupInfo *get_supergroup_info(int64 supergroup_id) const;
struct ChatInfo {
@ -750,7 +781,7 @@ class Client final : public WebhookActor::Callback {
td::string title;
int32 message_auto_delete_time = 0;
bool has_protected_content = false;
object_ptr<td_api::chatPhotoInfo> photo;
object_ptr<td_api::chatPhotoInfo> photo_info;
object_ptr<td_api::chatPermissions> permissions;
union {
int64 user_id;
@ -834,6 +865,9 @@ class Client final : public WebhookActor::Callback {
static void json_store_callback_query_payload(td::JsonObjectScope &object,
const td_api::CallbackQueryPayload *payload);
static void json_store_administrator_rights(td::JsonObjectScope &object,
const td_api::chatAdministratorRights *rights, ChatType chat_type);
static void json_store_permissions(td::JsonObjectScope &object, const td_api::chatPermissions *permissions);
void remove_replies_to_message(int64 chat_id, int64 reply_to_message_id, bool only_from_cache);
@ -947,6 +981,8 @@ class Client final : public WebhookActor::Callback {
std::size_t get_pending_update_count() const;
void update_last_synchronization_error_date();
static bool is_chat_member(const object_ptr<td_api::ChatMemberStatus> &status);
static td::string get_chat_member_status(const object_ptr<td_api::ChatMemberStatus> &status);
@ -986,30 +1022,30 @@ class Client final : public WebhookActor::Callback {
int64 channel_bot_user_id_ = 0;
int64 service_notifications_user_id_ = 0;
static std::unordered_map<td::string, Status (Client::*)(PromisedQueryPtr &query)> methods_;
static td::FlatHashMap<td::string, Status (Client::*)(PromisedQueryPtr &query)> methods_;
std::unordered_map<FullMessageId, std::unique_ptr<MessageInfo>, FullMessageIdHash> messages_; // message cache
std::unordered_map<int64, UserInfo> users_; // user info cache
std::unordered_map<int64, GroupInfo> groups_; // group info cache
std::unordered_map<int64, SupergroupInfo> supergroups_; // supergroup info cache
std::unordered_map<int64, ChatInfo> chats_; // chat info cache
td::FlatHashMap<FullMessageId, td::unique_ptr<MessageInfo>, FullMessageIdHash> messages_; // message cache
td::FlatHashMap<int64, td::unique_ptr<UserInfo>> users_; // user info cache
td::FlatHashMap<int64, td::unique_ptr<GroupInfo>> groups_; // group info cache
td::FlatHashMap<int64, td::unique_ptr<SupergroupInfo>> supergroups_; // supergroup info cache
td::FlatHashMap<int64, td::unique_ptr<ChatInfo>> chats_; // chat info cache
std::unordered_map<FullMessageId, std::unordered_set<int64>, FullMessageIdHash>
td::FlatHashMap<FullMessageId, td::FlatHashSet<int64>, FullMessageIdHash>
reply_message_ids_; // message -> replies to it
std::unordered_map<FullMessageId, std::unordered_set<int64>, FullMessageIdHash>
td::FlatHashMap<FullMessageId, td::FlatHashSet<int64>, FullMessageIdHash>
yet_unsent_reply_message_ids_; // message -> replies to it
std::unordered_map<int32, td::vector<PromisedQueryPtr>> file_download_listeners_;
std::unordered_set<int32> download_started_file_ids_;
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;
};
std::unordered_map<FullMessageId, YetUnsentMessage, FullMessageIdHash> yet_unsent_messages_;
td::FlatHashMap<FullMessageId, YetUnsentMessage, FullMessageIdHash> yet_unsent_messages_;
std::unordered_map<int64, int32> yet_unsent_message_count_;
td::FlatHashMap<int64, int32> yet_unsent_message_count_; // chat_id -> count
struct PendingSendMessageQuery {
PromisedQueryPtr query;
@ -1018,7 +1054,7 @@ class Client final : public WebhookActor::Callback {
td::vector<td::string> messages;
object_ptr<td_api::error> error;
};
std::unordered_map<int64, PendingSendMessageQuery>
td::FlatHashMap<int64, td::unique_ptr<PendingSendMessageQuery>>
pending_send_message_queries_; // query_id -> PendingSendMessageQuery
int64 current_send_message_query_id_ = 1;
@ -1034,28 +1070,28 @@ class Client final : public WebhookActor::Callback {
std::queue<NewMessage> queue_;
bool has_active_request_ = false;
};
std::unordered_map<int64, NewMessageQueue> new_message_queues_; // chat_id -> queue
td::FlatHashMap<int64, NewMessageQueue> new_message_queues_; // chat_id -> queue
struct NewCallbackQueryQueue {
std::queue<object_ptr<td_api::updateNewCallbackQuery>> queue_;
bool has_active_request_ = false;
};
std::unordered_map<int64, NewCallbackQueryQueue> new_callback_query_queues_; // sender_user_id -> queue
td::FlatHashMap<int64, NewCallbackQueryQueue> new_callback_query_queues_; // sender_user_id -> queue
std::unordered_map<int64, td::string> sticker_set_names_;
td::FlatHashMap<int64, td::string> sticker_set_names_;
int64 cur_temp_bot_user_id_ = 1;
std::unordered_map<td::string, int64> bot_user_ids_;
std::unordered_set<td::string> unresolved_bot_usernames_;
std::unordered_map<int64, int64> temp_to_real_bot_user_id_;
std::unordered_map<td::string, td::vector<int64>> awaiting_bot_resolve_queries_;
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_;
struct PendingBotResolveQuery {
std::size_t pending_resolve_count = 0;
PromisedQueryPtr query;
td::Promise<PromisedQueryPtr> on_success;
};
std::unordered_map<int64, PendingBotResolveQuery> pending_bot_resolve_queries_;
td::FlatHashMap<int64, PendingBotResolveQuery> pending_bot_resolve_queries_;
int64 current_bot_resolve_query_id_ = 1;
td::string dir_;
@ -1063,11 +1099,11 @@ class Client final : public WebhookActor::Callback {
td::ActorContext context_;
std::queue<PromisedQueryPtr> cmd_queue_;
td::vector<object_ptr<td_api::Object>> pending_updates_;
td::Container<std::unique_ptr<TdQueryCallback>> handlers_;
td::Container<td::unique_ptr<TdQueryCallback>> handlers_;
static constexpr int32 LONG_POLL_MAX_TIMEOUT = 50;
static constexpr double LONG_POLL_MAX_DELAY = 0.01;
static constexpr double LONG_POLL_WAIT_AFTER = 0.002;
static constexpr double LONG_POLL_MAX_DELAY = 0.002;
static constexpr double LONG_POLL_WAIT_AFTER = 0.001;
int32 long_poll_limit_ = 0;
int32 long_poll_offset_ = 0;
bool long_poll_was_wakeup_ = false;
@ -1100,6 +1136,10 @@ class Client final : public WebhookActor::Callback {
double local_unix_time_difference_ = 0; // Unix time - now()
double disconnection_time_ = 0; // the time when Connection state changed from "Ready", or 0 if it is "Ready"
double last_update_creation_time_ = 0; // the time when the last update was added
int32 last_synchronization_error_date_ = 0; // the date of the last connection error
int32 previous_get_updates_offset_ = -1;
double previous_get_updates_start_time_ = 0;
double previous_get_updates_finish_time_ = 0;

View File

@ -76,9 +76,13 @@ void ClientManager::send(PromisedQueryPtr query) {
return fail_query(401, "Unauthorized: invalid token specified", std::move(query));
}
auto r_user_id = td::to_integer_safe<td::int64>(query->token().substr(0, token.find(':')));
if (r_user_id.is_error() || r_user_id.ok() < 0 || !token_range_(r_user_id.ok())) {
if (r_user_id.is_error() || !token_range_(r_user_id.ok())) {
return fail_query(421, "Misdirected Request: unallowed token specified", std::move(query));
}
auto user_id = r_user_id.ok();
if (user_id <= 0 || user_id >= (static_cast<td::int64>(1) << 54)) {
return fail_query(401, "Unauthorized: invalid token specified", std::move(query));
}
if (query->is_test_dc()) {
token += "/test";
@ -532,7 +536,7 @@ PromisedQueryPtr ClientManager::get_webhook_restore_query(td::Slice token, bool
args.emplace_back(add_string("url"), add_string(parser.read_all()));
const auto method = add_string("setwebhook");
auto query = std::make_unique<Query>(std::move(containers), token, is_user, is_test_dc, method, std::move(args),
auto query = td::make_unique<Query>(std::move(containers), token, is_user, is_test_dc, method, std::move(args),
td::vector<std::pair<td::MutableSlice, td::MutableSlice>>(),
td::vector<td::HttpFile>(), std::move(shared_data), td::IPAddress(), true);
return PromisedQueryPtr(query.release(), PromiseDeleter(td::PromiseActor<td::unique_ptr<Query>>()));
@ -542,6 +546,7 @@ void ClientManager::raw_event(const td::Event::Raw &event) {
auto id = get_link_token();
auto *info = clients_.get(id);
CHECK(info != nullptr);
CHECK(info->tqueue_id_ != 0);
auto &value = active_client_count_[info->tqueue_id_];
if (event.ptr != nullptr) {
value++;

View File

@ -16,11 +16,11 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FloodControlFast.h"
#include "td/utils/Slice.h"
#include <memory>
#include <unordered_map>
#include <utility>
namespace telegram_bot_api {
@ -64,9 +64,9 @@ class ClientManager final : public td::Actor {
std::shared_ptr<const ClientParameters> parameters_;
TokenRange token_range_;
std::unordered_map<td::string, td::uint64> token_to_id_;
std::unordered_map<td::string, td::FloodControlFast> flood_controls_;
std::unordered_map<td::int64, td::uint64> active_client_count_;
td::FlatHashMap<td::string, td::uint64> token_to_id_;
td::FlatHashMap<td::string, td::FloodControlFast> flood_controls_;
td::FlatHashMap<td::int64, td::uint64> active_client_count_;
bool close_flag_ = false;
td::vector<td::Promise<td::Unit>> close_promises_;

View File

@ -59,7 +59,7 @@ void HttpConnection::handle(td::unique_ptr<td::HttpQuery> http_query,
}
auto query = std::make_unique<Query>(std::move(http_query->container_), token, is_user, is_test_dc, method,
auto query = td::make_unique<Query>(std::move(http_query->container_), token, is_user, is_test_dc, method,
std::move(http_query->args_), std::move(http_query->headers_),
std::move(http_query->files_), shared_data_, http_query->peer_address_, false);

View File

@ -21,7 +21,7 @@
namespace telegram_bot_api {
std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
Query::Query(td::vector<td::BufferSlice> &&container, td::Slice token, bool is_user, bool is_test_dc, td::MutableSlice method,
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&args,
@ -97,8 +97,8 @@ void Query::set_error(int http_status_code, td::BufferSlice result) {
void Query::set_retry_after_error(int retry_after) {
retry_after_ = retry_after;
std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> parameters;
parameters.emplace("retry_after", std::make_unique<td::VirtuallyJsonableLong>(retry_after));
td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> parameters;
parameters.emplace("retry_after", td::make_unique<td::VirtuallyJsonableLong>(retry_after));
set_error(429, td::json_encode<td::BufferSlice>(
JsonQueryError(429, PSLICE() << "Too Many Requests: retry after " << retry_after, parameters)));
}

View File

@ -15,6 +15,7 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/List.h"
#include "td/utils/port/IPAddress.h"
@ -23,7 +24,6 @@
#include <algorithm>
#include <memory>
#include <unordered_map>
#include <utility>
namespace telegram_bot_api {
@ -168,11 +168,11 @@ td::StringBuilder &operator<<(td::StringBuilder &sb, const Query &query);
// fix for outdated C++14 libraries
// https://stackoverflow.com/questions/26947704/implicit-conversion-failure-from-initializer-list
extern std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
extern td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
class JsonParameters final : public td::Jsonable {
public:
explicit JsonParameters(const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters)
explicit JsonParameters(const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters)
: parameters_(parameters) {
}
void store(td::JsonValueScope *scope) const {
@ -184,7 +184,7 @@ class JsonParameters final : public td::Jsonable {
}
private:
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters_;
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters_;
};
template <class T>
@ -210,7 +210,7 @@ class JsonQueryError final : public td::Jsonable {
public:
JsonQueryError(
int error_code, td::Slice description,
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters)
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters)
: error_code_(error_code), description_(description), parameters_(parameters) {
}
void store(td::JsonValueScope *scope) const {
@ -226,7 +226,7 @@ class JsonQueryError final : public td::Jsonable {
private:
int error_code_;
td::Slice description_;
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters_;
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters_;
};
class PromiseDeleter {
@ -265,7 +265,7 @@ void answer_query(const Jsonable &result, PromisedQueryPtr query, td::Slice desc
inline void fail_query(
int http_status_code, td::Slice description, PromisedQueryPtr query,
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters) {
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters) {
query->set_error(http_status_code,
td::json_encode<td::BufferSlice>(JsonQueryError(http_status_code, description, parameters)));
query.reset(); // send query into promise explicitly

View File

@ -379,11 +379,14 @@ void WebhookActor::load_updates() {
for (auto &update : updates) {
VLOG(webhook) << "Load update " << update.id;
if (update_map_.find(update.id) != update_map_.end()) {
LOG(ERROR) << "Receive duplicated event " << update.id << " from tqueue";
CHECK(update.id.is_valid());
auto &dest_ptr = update_map_[update.id];
if (dest_ptr != nullptr) {
LOG(ERROR) << "Receive duplicated event " << update.id << " from TQueue";
continue;
}
auto &dest = update_map_[update.id];
dest_ptr = td::make_unique<Update>();
auto &dest = *dest_ptr;
dest.id_ = update.id;
dest.json_ = update.data.str();
dest.delay_ = 1;
@ -437,7 +440,7 @@ void WebhookActor::load_updates() {
void WebhookActor::drop_event(td::TQueue::EventId event_id) {
auto it = update_map_.find(event_id);
CHECK(it != update_map_.end());
auto queue_id = it->second.queue_id_;
auto queue_id = it->second->queue_id_;
update_map_.erase(it);
auto queue_updates_it = queue_updates_.find(queue_id);
@ -448,8 +451,10 @@ void WebhookActor::drop_event(td::TQueue::EventId event_id) {
if (queue_updates_it->second.event_ids.empty()) {
queue_updates_.erase(queue_updates_it);
} else {
auto &update = update_map_[queue_updates_it->second.event_ids.front()];
queues_.emplace(update.wakeup_at_, update.queue_id_);
auto update_id = queue_updates_it->second.event_ids.front();
CHECK(update_id.is_valid());
auto &update = update_map_[update_id];
queues_.emplace(update->wakeup_at_, update->queue_id_);
}
parameters_->shared_data_->tqueue_->forget(tqueue_id_, event_id);
@ -462,7 +467,7 @@ void WebhookActor::on_update_ok(td::TQueue::EventId event_id) {
auto it = update_map_.find(event_id);
CHECK(it != update_map_.end());
VLOG(webhook) << "Receive ok for update " << event_id << " in " << (last_success_time_ - it->second.last_send_time_)
VLOG(webhook) << "Receive ok for update " << event_id << " in " << (last_success_time_ - it->second->last_send_time_)
<< " seconds";
drop_event(event_id);
@ -474,21 +479,22 @@ void WebhookActor::on_update_error(td::TQueue::EventId event_id, td::Slice error
auto it = update_map_.find(event_id);
CHECK(it != update_map_.end());
CHECK(it->second != nullptr);
auto &update = *it->second;
const int MAX_RETRY_AFTER = 3600;
retry_after = td::clamp(retry_after, 0, MAX_RETRY_AFTER);
int next_delay = it->second.delay_;
int next_delay = update.delay_;
int next_effective_delay = retry_after;
if (retry_after == 0 && it->second.fail_count_ > 0) {
if (retry_after == 0 && update.fail_count_ > 0) {
next_delay = td::min(WEBHOOK_MAX_RESEND_TIMEOUT, next_delay * 2);
next_effective_delay = next_delay;
}
if (parameters_->shared_data_->get_unix_time(now) + next_effective_delay > it->second.expires_at_) {
if (parameters_->shared_data_->get_unix_time(now) + next_effective_delay > update.expires_at_) {
LOG(WARNING) << "Drop update " << event_id << ": " << error;
drop_event(event_id);
return;
}
auto &update = it->second;
update.delay_ = next_delay;
update.wakeup_at_ = now + next_effective_delay;
update.fail_count_++;
@ -514,10 +520,15 @@ td::Status WebhookActor::send_update() {
}
auto queue_id = it->id;
CHECK(queue_id != 0);
queues_.erase(it);
auto event_id = queue_updates_[queue_id].event_ids.front();
CHECK(event_id.is_valid());
auto &update = update_map_[event_id];
auto update_map_it = update_map_.find(event_id);
CHECK(update_map_it != update_map_.end());
CHECK(update_map_it->second != nullptr);
auto &update = *update_map_it->second;
update.last_send_time_ = now;
auto body = td::json_encode<td::BufferSlice>(JsonUpdate(update.id_.value(), update.json_));
@ -586,7 +597,7 @@ void WebhookActor::handle(td::unique_ptr<td::HttpQuery> response) {
if (!method.empty() && method != "deletewebhook" && method != "setwebhook" && method != "close" &&
method != "logout" && !td::begins_with(method, "get")) {
VLOG(webhook) << "Receive request " << method << " in response to webhook";
auto query = std::make_unique<Query>(std::move(response->container_), td::MutableSlice(), false, false,
auto query = td::make_unique<Query>(std::move(response->container_), td::MutableSlice(), false, false,
td::MutableSlice(), std::move(response->args_),
std::move(response->headers_), std::move(response->files_),
parameters_->shared_data_, response->peer_address_, false);

View File

@ -20,6 +20,7 @@
#include "td/utils/BufferedFd.h"
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FloodControlFast.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/JsonBuilder.h"
@ -35,7 +36,6 @@
#include <memory>
#include <set>
#include <tuple>
#include <unordered_map>
namespace telegram_bot_api {
@ -127,8 +127,8 @@ class WebhookActor final : public td::HttpOutboundConnection::Callback {
return std::hash<td::int32>()(event_id.value());
}
};
std::unordered_map<td::TQueue::EventId, Update, EventIdHash> update_map_;
std::unordered_map<td::int64, QueueUpdates> queue_updates_;
td::FlatHashMap<td::TQueue::EventId, td::unique_ptr<Update>, EventIdHash> update_map_;
td::FlatHashMap<td::int64, QueueUpdates> queue_updates_;
std::set<Queue> queues_;
td::int64 unique_queue_id_ = static_cast<td::int64>(1) << 60;

View File

@ -200,7 +200,7 @@ int main(int argc, char *argv[]) {
auto start_time = td::Time::now();
auto shared_data = std::make_shared<SharedData>();
auto parameters = std::make_unique<ClientParameters>();
parameters->version_ = "5.7";
parameters->version_ = "6.0.1";
parameters->shared_data_ = shared_data;
parameters->start_time_ = start_time;
auto net_query_stats = td::create_net_query_stats();
@ -230,11 +230,11 @@ int main(int argc, char *argv[]) {
}
return 0;
}(std::getenv("TELEGRAM_API_ID"));
parameters->api_hash_ = [](auto x) -> std::string {
parameters->api_hash_ = [](auto x) -> td::string {
if (x) {
return x;
}
return std::string();
return td::string();
}(std::getenv("TELEGRAM_API_HASH"));
options.set_usage(td::Slice(argv[0]), "--api-id=<arg> --api-hash=<arg> [--local] [OPTION]...");