From e22d3b7c28d0e6e5a23d60e85786a2db4f74e923 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 25 Feb 2019 04:16:41 +0300 Subject: [PATCH] Return result through Promise from GetFull*Query. GitOrigin-RevId: 40849a7f4a1febaa35c36b811717b48932da2bff --- td/telegram/ContactsManager.cpp | 47 ++++++++++++++++++++++----------- td/telegram/ContactsManager.h | 12 ++++----- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 5a8977ba..16d47fae 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1850,11 +1850,13 @@ class GetUsersQuery : public Td::ResultHandler { }; class GetFullUserQuery : public Td::ResultHandler { - UserId user_id_; + Promise promise_; public: - void send(UserId user_id, tl_object_ptr &&input_user) { - user_id_ = user_id; + explicit GetFullUserQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(tl_object_ptr &&input_user) { send_query(G()->net_query_creator().create(create_storer(telegram_api::users_getFullUser(std::move(input_user))))); } @@ -1866,11 +1868,11 @@ class GetFullUserQuery : public Td::ResultHandler { LOG(DEBUG) << "Receive result for getFullUser " << to_string(result_ptr.ok()); td->contacts_manager_->on_get_user_full(result_ptr.move_as_ok()); - td->contacts_manager_->on_get_user_full_result(user_id_, Unit()); + promise_.set_value(Unit()); } void on_error(uint64 id, Status status) override { - td->contacts_manager_->on_get_user_full_result(user_id_, std::move(status)); + promise_.set_error(std::move(status)); } }; @@ -1971,11 +1973,13 @@ class GetChatsQuery : public Td::ResultHandler { }; class GetFullChatQuery : public Td::ResultHandler { - ChatId chat_id_; + Promise promise_; public: + explicit GetFullChatQuery(Promise &&promise) : promise_(std::move(promise)) { + } + void send(ChatId chat_id) { - chat_id_ = chat_id; LOG(INFO) << "Send getFullChat query to get " << chat_id; send_query(G()->net_query_creator().create(create_storer(telegram_api::messages_getFullChat(chat_id.get())))); } @@ -1987,16 +1991,15 @@ class GetFullChatQuery : public Td::ResultHandler { } auto ptr = result_ptr.move_as_ok(); - // LOG(INFO) << "Receive result for getFullChat query: " << to_string(ptr); td->contacts_manager_->on_get_users(std::move(ptr->users_), "GetFullChatQuery"); td->contacts_manager_->on_get_chats(std::move(ptr->chats_), "GetFullChatQuery"); td->contacts_manager_->on_get_chat_full(std::move(ptr->full_chat_)); - td->contacts_manager_->on_get_chat_full_result(chat_id_, Unit()); + promise_.set_value(Unit()); } void on_error(uint64 id, Status status) override { - td->contacts_manager_->on_get_chat_full_result(chat_id_, std::move(status)); + promise_.set_error(std::move(status)); } }; @@ -2055,9 +2058,13 @@ class GetChannelsQuery : public Td::ResultHandler { }; class GetFullChannelQuery : public Td::ResultHandler { + Promise promise_; ChannelId channel_id_; public: + explicit GetFullChannelQuery(Promise &&promise) : promise_(std::move(promise)) { + } + void send(ChannelId channel_id, tl_object_ptr &&input_channel) { channel_id_ = channel_id; send_query(G()->net_query_creator().create( @@ -2071,17 +2078,16 @@ class GetFullChannelQuery : public Td::ResultHandler { } auto ptr = result_ptr.move_as_ok(); - // LOG(INFO) << "Receive result for getFullChannel query: " << to_string(ptr); td->contacts_manager_->on_get_users(std::move(ptr->users_), "GetFullChannelQuery"); td->contacts_manager_->on_get_chats(std::move(ptr->chats_), "GetFullChannelQuery"); td->contacts_manager_->on_get_chat_full(std::move(ptr->full_chat_)); - td->contacts_manager_->on_get_channel_full_result(channel_id_, Unit()); + promise_.set_value(Unit()); } void on_error(uint64 id, Status status) override { td->contacts_manager_->on_get_channel_error(channel_id_, status, "GetFullChannelQuery"); - td->contacts_manager_->on_get_channel_full_result(channel_id_, std::move(status)); + promise_.set_error(std::move(status)); } }; @@ -8379,7 +8385,10 @@ void ContactsManager::send_get_user_full_query(UserId user_id, tl_object_ptrcreate_handler()->send(user_id, std::move(input_user)); + auto request_promise = PromiseCreator::lambda([actor_id = actor_id(this), user_id](Result &&result) { + send_closure(actor_id, &ContactsManager::on_get_user_full_result, user_id, std::move(result)); + }); + td_->create_handler(std::move(request_promise))->send(std::move(input_user)); } void ContactsManager::on_get_user_full_result(UserId user_id, Result &&result) { @@ -8651,7 +8660,10 @@ void ContactsManager::send_get_chat_full_query(ChatId chat_id, Promise &&p return; } - td_->create_handler()->send(chat_id); + auto request_promise = PromiseCreator::lambda([actor_id = actor_id(this), chat_id](Result &&result) { + send_closure(actor_id, &ContactsManager::on_get_chat_full_result, chat_id, std::move(result)); + }); + td_->create_handler(std::move(request_promise))->send(chat_id); } void ContactsManager::on_get_chat_full_result(ChatId chat_id, Result &&result) { @@ -8929,7 +8941,10 @@ void ContactsManager::send_get_channel_full_query(ChannelId channel_id, return; } - td_->create_handler()->send(channel_id, std::move(input_channel)); + auto request_promise = PromiseCreator::lambda([actor_id = actor_id(this), channel_id](Result &&result) { + send_closure(actor_id, &ContactsManager::on_get_channel_full_result, channel_id, std::move(result)); + }); + td_->create_handler(std::move(request_promise))->send(channel_id, std::move(input_channel)); } void ContactsManager::on_get_channel_full_result(ChannelId channel_id, Result &&result) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index c5201668..e1a2873a 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -200,12 +200,6 @@ class ContactsManager : public Actor { void on_get_created_public_channels(vector> &&chats); - void on_get_user_full_result(UserId user_id, Result &&result); - - void on_get_chat_full_result(ChatId chat_id, Result &&result); - - void on_get_channel_full_result(ChannelId channel_id, Result &&result); - UserId get_my_id() const; void set_my_online_status(bool is_online, bool send_update, bool is_local); @@ -949,6 +943,12 @@ class ContactsManager : public Actor { void update_chat_full(ChatFull *chat_full, ChatId chat_id); void update_channel_full(ChannelFull *channel_full, ChannelId channel_id); + void on_get_user_full_result(UserId user_id, Result &&result); + + void on_get_chat_full_result(ChatId chat_id, Result &&result); + + void on_get_channel_full_result(ChannelId channel_id, Result &&result); + bool is_chat_full_outdated(ChatFull *chat_full, Chat *c, ChatId chat_id); int32 get_contacts_hash();