Simplify NetQuery creation in SecretChatActor.

GitOrigin-RevId: a6d00cb5175552f9aa6dc7b38462fc00eb5b78f7
This commit is contained in:
levlam 2020-03-15 03:43:42 +03:00
parent ad3c28932b
commit 0d08895871
2 changed files with 38 additions and 39 deletions

View File

@ -68,6 +68,12 @@ SecretChatActor::SecretChatActor(int32 id, unique_ptr<Context> context, bool can
auth_state_.id = id; auth_state_.id = id;
} }
template <class T>
NetQueryPtr SecretChatActor::create_net_query(QueryType type, const T &function) {
return context_->net_query_creator().create(UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(type)),
create_storer(function));
}
void SecretChatActor::update_chat(telegram_api::object_ptr<telegram_api::EncryptedChat> chat) { void SecretChatActor::update_chat(telegram_api::object_ptr<telegram_api::EncryptedChat> chat) {
if (close_flag_) { if (close_flag_) {
return; return;
@ -358,9 +364,8 @@ void SecretChatActor::send_message_action(tl_object_ptr<secret_api::SendMessageA
} }
bool flag = action->get_id() != secret_api::sendMessageCancelAction::ID; bool flag = action->get_id() != secret_api::sendMessageCancelAction::ID;
auto net_query = context_->net_query_creator().create( auto net_query =
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::Ignore)), create_net_query(QueryType::Ignore, telegram_api::messages_setEncryptedTyping(get_input_chat(), flag));
create_storer(telegram_api::messages_setEncryptedTyping(get_input_chat(), flag)));
if (!set_typing_query_.empty()) { if (!set_typing_query_.empty()) {
LOG(INFO) << "Cancel previous set typing query"; LOG(INFO) << "Cancel previous set typing query";
cancel_query(set_typing_query_); cancel_query(set_typing_query_);
@ -390,9 +395,8 @@ void SecretChatActor::send_read_history(int32 date, Promise<> promise) {
cancel_query(read_history_query_); cancel_query(read_history_query_);
} }
auto net_query = context_->net_query_creator().create( auto net_query =
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::ReadHistory)), create_net_query(QueryType::ReadHistory, telegram_api::messages_readEncryptedHistory(get_input_chat(), date));
create_storer(telegram_api::messages_readEncryptedHistory(get_input_chat(), date)));
read_history_query_ = net_query.get_weak(); read_history_query_ = net_query.get_weak();
last_read_history_date_ = date; last_read_history_date_ = date;
read_history_promise_ = std::move(promise); read_history_promise_ = std::move(promise);
@ -566,11 +570,10 @@ Status SecretChatActor::run_auth() {
return Status::OK(); return Status::OK();
} }
// messages.requestEncryption#f64daf43 user_id:InputUser random_id:int g_a:bytes = EncryptedChat; // messages.requestEncryption#f64daf43 user_id:InputUser random_id:int g_a:bytes = EncryptedChat;
telegram_api::messages_requestEncryption tl_query(get_input_user(), auth_state_.random_id, ;
BufferSlice(auth_state_.handshake.get_g_b())); auto query = create_net_query(QueryType::EncryptedChat, telegram_api::messages_requestEncryption(
auto query = context_->net_query_creator().create( get_input_user(), auth_state_.random_id,
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::EncryptedChat)), BufferSlice(auth_state_.handshake.get_g_b())));
create_storer(tl_query));
context_->send_net_query(std::move(query), actor_shared(this), false); context_->send_net_query(std::move(query), actor_shared(this), false);
auth_state_.state = State::WaitRequestResponse; auth_state_.state = State::WaitRequestResponse;
return Status::OK(); return Status::OK();
@ -584,11 +587,10 @@ Status SecretChatActor::run_auth() {
pfs_state_.auth_key = mtproto::AuthKey(id_and_key.first, std::move(id_and_key.second)); pfs_state_.auth_key = mtproto::AuthKey(id_and_key.first, std::move(id_and_key.second));
calc_key_hash(); calc_key_hash();
// messages.acceptEncryption#3dbc0415 peer:InputEncryptedChat g_b:bytes key_fingerprint:long = EncryptedChat; // messages.acceptEncryption#3dbc0415 peer:InputEncryptedChat g_b:bytes key_fingerprint:long = EncryptedChat;
telegram_api::messages_acceptEncryption tl_query(get_input_chat(), BufferSlice(auth_state_.handshake.get_g_b()), auto query = create_net_query(
pfs_state_.auth_key.id()); QueryType::EncryptedChat,
auto query = context_->net_query_creator().create( telegram_api::messages_acceptEncryption(get_input_chat(), BufferSlice(auth_state_.handshake.get_g_b()),
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::EncryptedChat)), pfs_state_.auth_key.id()));
create_storer(tl_query));
context_->send_net_query(std::move(query), actor_shared(this), false); context_->send_net_query(std::move(query), actor_shared(this), false);
auth_state_.state = State::WaitAcceptResponse; auth_state_.state = State::WaitAcceptResponse;
return Status::OK(); return Status::OK();
@ -756,10 +758,8 @@ void SecretChatActor::do_close_chat_impl(unique_ptr<logevent::CloseSecretChat> e
context_->secret_chat_db()->erase_value(config_state_); context_->secret_chat_db()->erase_value(config_state_);
context_->secret_chat_db()->erase_value(pfs_state_); context_->secret_chat_db()->erase_value(pfs_state_);
context_->secret_chat_db()->erase_value(seq_no_state_); context_->secret_chat_db()->erase_value(seq_no_state_);
telegram_api::messages_discardEncryption tl_query(auth_state_.id); ;
auto query = context_->net_query_creator().create( auto query = create_net_query(QueryType::DiscardEncryption, telegram_api::messages_discardEncryption(auth_state_.id));
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::DiscardEncryption)),
create_storer(tl_query));
send_update_secret_chat(); send_update_secret_chat();
@ -1464,22 +1464,19 @@ NetQueryPtr SecretChatActor::create_net_query(const logevent::OutboundSecretMess
NetQueryPtr query; NetQueryPtr query;
if (message.is_service) { if (message.is_service) {
CHECK(message.file.empty()); CHECK(message.file.empty());
query = context_->net_query_creator().create( query = create_net_query(QueryType::Message,
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::Message)), telegram_api::messages_sendEncryptedService(get_input_chat(), message.random_id,
create_storer(telegram_api::messages_sendEncryptedService(get_input_chat(), message.random_id, message.encrypted_message.clone()));
message.encrypted_message.clone()))); query->total_timeout_limit = 1000000000; // inf. We will re-sent it immediately anyway
query->total_timeout_limit = 1000000000; // inf. We will re-sent it immediately anyway.
} else if (message.file.empty()) { } else if (message.file.empty()) {
query = context_->net_query_creator().create( query = create_net_query(
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::Message)), QueryType::Message,
create_storer(telegram_api::messages_sendEncrypted(get_input_chat(), message.random_id, telegram_api::messages_sendEncrypted(get_input_chat(), message.random_id, message.encrypted_message.clone()));
message.encrypted_message.clone())));
} else { } else {
query = context_->net_query_creator().create( query = create_net_query(
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::Message)), QueryType::Message,
create_storer(telegram_api::messages_sendEncryptedFile(get_input_chat(), message.random_id, telegram_api::messages_sendEncryptedFile(get_input_chat(), message.random_id, message.encrypted_message.clone(),
message.encrypted_message.clone(), message.file.as_input_encrypted_file()));
message.file.as_input_encrypted_file())));
} }
if (message.is_external && context_->get_config_option_boolean("use_quick_ack")) { if (message.is_external && context_->get_config_option_boolean("use_quick_ack")) {
query->quick_ack_promise_ = query->quick_ack_promise_ =
@ -1964,11 +1961,8 @@ void SecretChatActor::get_dh_config() {
} }
auto version = auth_state_.dh_config.version; auto version = auth_state_.dh_config.version;
int random_length = 0; int32 random_length = 0;
telegram_api::messages_getDhConfig tl_query(version, random_length); auto query = create_net_query(QueryType::DhConfig, telegram_api::messages_getDhConfig(version, random_length));
auto query = context_->net_query_creator().create(
UniqueId::next(UniqueId::Type::Default, static_cast<uint8>(QueryType::DhConfig)), create_storer(tl_query));
context_->send_net_query(std::move(query), actor_shared(this), false); context_->send_net_query(std::move(query), actor_shared(this), false);
} }

View File

@ -18,6 +18,7 @@
#include "td/telegram/DhConfig.h" #include "td/telegram/DhConfig.h"
#include "td/telegram/logevent/SecretChatEvent.h" #include "td/telegram/logevent/SecretChatEvent.h"
#include "td/telegram/MessageId.h" #include "td/telegram/MessageId.h"
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/SecretChatDb.h" #include "td/telegram/SecretChatDb.h"
#include "td/telegram/SecretChatId.h" #include "td/telegram/SecretChatId.h"
#include "td/telegram/UserId.h" #include "td/telegram/UserId.h"
@ -30,6 +31,7 @@
#include "td/utils/port/Clocks.h" #include "td/utils/port/Clocks.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
#include "td/utils/StorerBase.h"
#include "td/utils/StringBuilder.h" #include "td/utils/StringBuilder.h"
#include "td/utils/Time.h" #include "td/utils/Time.h"
#include "td/utils/tl_helpers.h" #include "td/utils/tl_helpers.h"
@ -589,6 +591,9 @@ class SecretChatActor : public NetQueryCallback {
int32 last_read_history_date_ = -1; int32 last_read_history_date_ = -1;
Promise<Unit> read_history_promise_; Promise<Unit> read_history_promise_;
template <class T>
NetQueryPtr create_net_query(QueryType type, const T &function);
enum SendFlag : int32 { enum SendFlag : int32 {
None = 0, None = 0,
External = 1, External = 1,