From 15a11d70532ea31d91bf644b44fadae22585db2e Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 21 May 2022 23:07:28 +0300 Subject: [PATCH 1/5] Move expected logging about secret chat updates to INFO. --- td/telegram/SecretChatActor.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/td/telegram/SecretChatActor.cpp b/td/telegram/SecretChatActor.cpp index 2d70b3195..2b4ab2bd0 100644 --- a/td/telegram/SecretChatActor.cpp +++ b/td/telegram/SecretChatActor.cpp @@ -1773,7 +1773,7 @@ Status SecretChatActor::save_common_info(T &update) { Status SecretChatActor::on_update_chat(telegram_api::encryptedChatRequested &update) { if (auth_state_.state != State::Empty) { - LOG(WARNING) << "Unexpected ChatRequested ignored: " << to_string(update); + LOG(INFO) << "Unexpected encryptedChatRequested ignored: " << to_string(update); return Status::OK(); } auth_state_.state = State::SendAccept; @@ -1794,7 +1794,7 @@ Status SecretChatActor::on_update_chat(telegram_api::encryptedChatEmpty &update) } Status SecretChatActor::on_update_chat(telegram_api::encryptedChatWaiting &update) { if (auth_state_.state != State::WaitRequestResponse && auth_state_.state != State::WaitAcceptResponse) { - LOG(WARNING) << "Unexpected ChatWaiting ignored"; + LOG(INFO) << "Unexpected encryptedChatWaiting ignored"; return Status::OK(); } TRY_STATUS(save_common_info(update)); @@ -1803,7 +1803,7 @@ Status SecretChatActor::on_update_chat(telegram_api::encryptedChatWaiting &updat } Status SecretChatActor::on_update_chat(telegram_api::encryptedChat &update) { if (auth_state_.state != State::WaitRequestResponse && auth_state_.state != State::WaitAcceptResponse) { - LOG(WARNING) << "Unexpected Chat ignored"; + LOG(INFO) << "Unexpected encryptedChat ignored"; return Status::OK(); } TRY_STATUS(save_common_info(update)); From d04552045d076f3624e2ff18936c83e7f6544828 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 22 May 2022 22:09:46 +0300 Subject: [PATCH 2/5] Add warnings about long application of postponed updates. --- td/telegram/UpdatesManager.cpp | 24 ++++++++++++++++++++++-- td/telegram/UpdatesManager.h | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 4c54158ba..d3c314188 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -361,6 +361,10 @@ void UpdatesManager::before_get_difference(bool is_initial) { drop_all_pending_pts_updates(); send_closure_later(td_->notification_manager_actor_, &NotificationManager::before_get_difference); + + if (get_difference_start_time_ <= 0) { + get_difference_start_time_ = Time::now(); + } } Promise<> UpdatesManager::add_pts(int32 pts) { @@ -1537,7 +1541,9 @@ void UpdatesManager::after_get_difference() { } if (!postponed_updates_.empty()) { - VLOG(get_difference) << "Begin to apply " << postponed_updates_.size() << " postponed update chunks"; + auto begin_time = Time::now(); + auto chunk_count = postponed_updates_.size(); + VLOG(get_difference) << "Begin to apply " << chunk_count << " postponed update chunks"; size_t total_update_count = 0; while (!postponed_updates_.empty()) { auto it = postponed_updates_.begin(); @@ -1559,13 +1565,21 @@ void UpdatesManager::after_get_difference() { } total_update_count += update_count; } - VLOG(get_difference) << "Finish to apply " << total_update_count << " postponed updates"; + VLOG(get_difference) << "Finished to apply " << total_update_count << " postponed updates"; + auto passed_time = Time::now() - begin_time; + if (passed_time >= 1.0) { + LOG(WARNING) << "Applied " << total_update_count << " postponed for " + << (Time::now() - get_difference_start_time_) << " updates in " << chunk_count << " chunks in " + << passed_time; + } } if (!postponed_pts_updates_.empty()) { // must be before td_->messages_manager_->after_get_difference() auto postponed_updates = std::move(postponed_pts_updates_); postponed_pts_updates_.clear(); + auto begin_time = Time::now(); + auto update_count = postponed_updates.size(); VLOG(get_difference) << "Begin to apply " << postponed_updates.size() << " postponed pts updates with pts = " << get_pts(); for (auto &postponed_update : postponed_updates) { @@ -1577,6 +1591,11 @@ void UpdatesManager::after_get_difference() { VLOG(get_difference) << "After applying postponed pts updates have pts = " << get_pts() << ", max_pts = " << accumulated_pts_ << " and " << pending_pts_updates_.size() << " + " << postponed_pts_updates_.size() << " pending pts updates"; + auto passed_time = Time::now() - begin_time; + if (passed_time >= 1.0) { + LOG(WARNING) << "Applied " << update_count << " postponed for " << (Time::now() - get_difference_start_time_) + << " pts updates in " << passed_time; + } } td_->download_manager_->after_get_difference(); @@ -1585,6 +1604,7 @@ void UpdatesManager::after_get_difference() { td_->notification_settings_manager_->after_get_difference(); send_closure_later(td_->notification_manager_actor_, &NotificationManager::after_get_difference); send_closure(G()->state_manager(), &StateManager::on_synchronized, true); + get_difference_start_time_ = 0.0; try_reload_data(); } diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index 2ca9ea39b..54f8b1855 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -229,6 +229,7 @@ class UpdatesManager final : public Actor { int32 last_get_difference_qts_ = 0; int32 min_postponed_update_pts_ = 0; int32 min_postponed_update_qts_ = 0; + double get_difference_start_time_ = 0; // time from which we started to get difference without success void start_up() final; From d06a7ddf1cc0fad2d833a1f8ba62dd9e2d84734b Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 May 2022 02:24:52 +0300 Subject: [PATCH 3/5] Don't sync just created binlog. --- tddb/td/db/binlog/Binlog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tddb/td/db/binlog/Binlog.cpp b/tddb/td/db/binlog/Binlog.cpp index 4bc473728..5b14eed69 100644 --- a/tddb/td/db/binlog/Binlog.cpp +++ b/tddb/td/db/binlog/Binlog.cpp @@ -657,7 +657,7 @@ void Binlog::do_reindex() { event.realloc(); do_event(std::move(event)); // NB: no move is actually happens }); - need_sync_ = true; // must sync creation of the file + need_sync_ = start_size != 0; // must sync creation of the file if it is non-empty sync(); // finish_reindex From 80f3c88bdb0202dd57378d32adece3104d393922 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 24 May 2022 14:56:02 +0300 Subject: [PATCH 4/5] Move get_invite_text and save_app_log to Application.cpp. --- CMakeLists.txt | 2 + td/telegram/Application.cpp | 83 +++++++++++++++++++++++++++++++++++++ td/telegram/Application.h | 25 +++++++++++ td/telegram/Td.cpp | 64 ++-------------------------- 4 files changed, 114 insertions(+), 60 deletions(-) create mode 100644 td/telegram/Application.cpp create mode 100644 td/telegram/Application.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ae58a43fa..73c2d2aeb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -278,6 +278,7 @@ set(TDLIB_SOURCE td/telegram/Account.cpp td/telegram/AnimationsManager.cpp + td/telegram/Application.cpp td/telegram/AttachMenuManager.cpp td/telegram/AudiosManager.cpp td/telegram/AuthManager.cpp @@ -472,6 +473,7 @@ set(TDLIB_SOURCE td/telegram/Account.h td/telegram/AffectedHistory.h td/telegram/AnimationsManager.h + td/telegram/Application.h td/telegram/AttachMenuManager.h td/telegram/AudiosManager.h td/telegram/AuthManager.h diff --git a/td/telegram/Application.cpp b/td/telegram/Application.cpp new file mode 100644 index 000000000..7c33fa646 --- /dev/null +++ b/td/telegram/Application.cpp @@ -0,0 +1,83 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// 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) +// +#include "td/telegram/Application.h" + +#include "td/telegram/Global.h" +#include "td/telegram/Td.h" + +#include "td/utils/buffer.h" +#include "td/utils/logging.h" + +namespace td { + +class GetInviteTextQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit GetInviteTextQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create(telegram_api::help_getInviteText())); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto result = result_ptr.move_as_ok(); + promise_.set_value(std::move(result->message_)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +class SaveAppLogQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit SaveAppLogQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(const string &type, DialogId dialog_id, tl_object_ptr &&data) { + CHECK(data != nullptr); + vector> input_app_events; + input_app_events.push_back( + make_tl_object(G()->server_time_cached(), type, dialog_id.get(), std::move(data))); + send_query(G()->net_query_creator().create_unauth(telegram_api::help_saveAppLog(std::move(input_app_events)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + bool result = result_ptr.move_as_ok(); + LOG_IF(ERROR, !result) << "Receive false from help.saveAppLog"; + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +void get_invite_text(Td *td, Promise &&promise) { + td->create_handler(std::move(promise))->send(); +} + +void save_app_log(Td *td, const string &type, DialogId dialog_id, tl_object_ptr &&data, + Promise &&promise) { + td->create_handler(std::move(promise))->send(type, dialog_id, std::move(data)); +} + +} // namespace td diff --git a/td/telegram/Application.h b/td/telegram/Application.h new file mode 100644 index 000000000..b0d18cbd2 --- /dev/null +++ b/td/telegram/Application.h @@ -0,0 +1,25 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// 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) +// +#pragma once + +#include "td/telegram/DialogId.h" +#include "td/telegram/telegram_api.h" + +#include "td/actor/PromiseFuture.h" + +#include "td/utils/common.h" + +namespace td { + +class Td; + +void get_invite_text(Td *td, Promise &&promise); + +void save_app_log(Td *td, const string &type, DialogId dialog_id, tl_object_ptr &&data, + Promise &&promise); + +} // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c5336f71a..a171eded6 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -8,6 +8,7 @@ #include "td/telegram/Account.h" #include "td/telegram/AnimationsManager.h" +#include "td/telegram/Application.h" #include "td/telegram/AttachMenuManager.h" #include "td/telegram/AudiosManager.h" #include "td/telegram/AuthManager.h" @@ -416,63 +417,6 @@ class UpdateStatusQuery final : public Td::ResultHandler { } }; -class GetInviteTextQuery final : public Td::ResultHandler { - Promise promise_; - - public: - explicit GetInviteTextQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send() { - send_query(G()->net_query_creator().create(telegram_api::help_getInviteText())); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - auto result = result_ptr.move_as_ok(); - promise_.set_value(std::move(result->message_)); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - -class SaveAppLogQuery final : public Td::ResultHandler { - Promise promise_; - - public: - explicit SaveAppLogQuery(Promise &&promise) : promise_(std::move(promise)) { - } - - void send(const string &type, int64 peer_id, tl_object_ptr &&data) { - CHECK(data != nullptr); - vector> input_app_events; - input_app_events.push_back( - make_tl_object(G()->server_time_cached(), type, peer_id, std::move(data))); - send_query(G()->net_query_creator().create_unauth(telegram_api::help_saveAppLog(std::move(input_app_events)))); - } - - void on_result(BufferSlice packet) final { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(result_ptr.move_as_error()); - } - - bool result = result_ptr.move_as_ok(); - LOG_IF(ERROR, !result) << "Receive false from help.saveAppLog"; - promise_.set_value(Unit()); - } - - void on_error(Status status) final { - promise_.set_error(std::move(status)); - } -}; - class TestNetworkQuery final : public Td::ResultHandler { Promise promise_; @@ -7852,7 +7796,7 @@ void Td::on_request(uint64 id, const td_api::getApplicationDownloadLink &request promise.set_value(make_tl_object(result.move_as_ok())); } }); - create_handler(std::move(query_promise))->send(); + get_invite_text(this, std::move(query_promise)); } void Td::on_request(uint64 id, td_api::getDeepLinkInfo &request) { @@ -7870,9 +7814,9 @@ void Td::on_request(uint64 id, const td_api::getApplicationConfig &request) { void Td::on_request(uint64 id, td_api::saveApplicationLogEvent &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.type_); - auto result = convert_json_value(std::move(request.data_)); CREATE_OK_REQUEST_PROMISE(); - create_handler(std::move(promise))->send(request.type_, request.chat_id_, std::move(result)); + save_app_log(this, request.type_, DialogId(request.chat_id_), convert_json_value(std::move(request.data_)), + std::move(promise)); } void Td::on_request(uint64 id, td_api::addProxy &request) { From c5dfa597828e9250ac29b6e37320220411bce870 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 24 May 2022 17:41:32 +0300 Subject: [PATCH 5/5] Fix MessagesManager::can_delete_dialog. --- td/telegram/MessagesManager.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index a031cd32b..e19911dfa 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -10996,14 +10996,16 @@ void MessagesManager::on_failed_scheduled_message_deletion(DialogId dialog_id, c } MessagesManager::CanDeleteDialog MessagesManager::can_delete_dialog(const Dialog *d) const { - auto chat_source = sponsored_dialog_source_.get_chat_source_object(); - if (chat_source != nullptr) { - switch (chat_source->get_id()) { - case td_api::chatSourcePublicServiceAnnouncement::ID: - // can delete for self (but only while removing from dialog list) - return {true, false}; - default: - return {false, false}; + if (is_dialog_sponsored(d)) { + auto chat_source = sponsored_dialog_source_.get_chat_source_object(); + if (chat_source != nullptr) { + switch (chat_source->get_id()) { + case td_api::chatSourcePublicServiceAnnouncement::ID: + // can delete for self (but only while removing from dialog list) + return {true, false}; + default: + return {false, false}; + } } } if (td_->auth_manager_->is_bot() || !have_input_peer(d->dialog_id, AccessRights::Read)) {