From 33073a50225074b08467734909b737b3d1715a0d Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 8 Apr 2024 20:18:48 +0300 Subject: [PATCH] Add td_api::updateSpeedLimitNotification. --- td/generate/scheme/td_api.tl | 5 +++++ td/telegram/Global.cpp | 5 +++++ td/telegram/Global.h | 2 ++ td/telegram/Td.cpp | 1 + td/telegram/UpdatesManager.cpp | 9 +++++++++ td/telegram/UpdatesManager.h | 4 ++++ td/telegram/net/NetQueryDelayer.cpp | 20 ++++++++++++++++++-- td/telegram/net/NetQueryDispatcher.cpp | 2 ++ 8 files changed, 46 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 504018777..a4f8158cc 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7151,6 +7151,11 @@ updateAnimationSearchParameters provider:string emojis:vector = Update; //@description The list of suggested to the user actions has changed @added_actions Added suggested actions @removed_actions Removed suggested actions updateSuggestedActions added_actions:vector removed_actions:vector = Update; +//@description Download or upload file speed for the user was limited, but it can be restored by subscription to Telegram Premium. +//-Use getOption("premium_download_speedup") or getOption("premium_upload_speedup") to get expected speedup after subscription to Telegram Premium +//@is_upload True, if upload speed was limited; false, if download speed was limited +updateSpeedLimitNotification is_upload:Bool = Update; + //@description The list of contacts that had birthdays recently or will have birthday soon has changed @close_birthday_users List of contact users with close birthday updateContactCloseBirthdays close_birthday_users:vector = Update; diff --git a/td/telegram/Global.cpp b/td/telegram/Global.cpp index 587bed200..269f633f3 100644 --- a/td/telegram/Global.cpp +++ b/td/telegram/Global.cpp @@ -13,6 +13,7 @@ #include "td/telegram/OptionManager.h" #include "td/telegram/StateManager.h" #include "td/telegram/TdDb.h" +#include "td/telegram/UpdatesManager.h" #include "td/utils/format.h" #include "td/utils/logging.h" @@ -343,6 +344,10 @@ void Global::add_location_access_hash(double latitude, double longitude, int64 a location_access_hashes_[get_location_key(latitude, longitude)] = access_hash; } +void Global::notify_speed_limited(bool is_upload) { + send_closure(updates_manager_, &UpdatesManager::notify_speed_limited, is_upload); +} + double get_global_server_time() { return G()->server_time(); } diff --git a/td/telegram/Global.h b/td/telegram/Global.h index ac5965010..0947b7125 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -626,6 +626,8 @@ class Global final : public ActorContext { store_all_files_in_files_directory_ = flag; } + void notify_speed_limited(bool is_upload); + private: std::shared_ptr dh_config_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 73b1f938e..56f375551 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4054,6 +4054,7 @@ void Td::send_update(tl_object_ptr &&object) { VLOG(td_requests) << "Sending update: " << to_string(object); } break; + case td_api::updateSpeedLimitNotification::ID: case td_api::updateDefaultReactionType::ID / 2: LOG(ERROR) << "Sending update: " << oneline(to_string(object)); break; diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 98a102306..43cda1eef 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -1784,6 +1784,15 @@ void UpdatesManager::on_server_pong(tl_object_ptr & } } +void UpdatesManager::notify_speed_limited(bool is_upload) { + if (Time::now() < next_notify_speed_limited_[is_upload]) { + return; + } + next_notify_speed_limited_[is_upload] = + Time::now() + td_->option_manager_->get_option_integer("upload_premium_speedup_notify_period"); + send_closure(G()->td(), &Td::send_update, td_api::make_object(is_upload)); +} + void UpdatesManager::process_get_difference_updates( vector> &&new_messages, vector> &&new_encrypted_messages, diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index 78c657445..2a7736d13 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -131,6 +131,8 @@ class UpdatesManager final : public Actor { void ping_server(); + void notify_speed_limited(bool is_upload); + bool running_get_difference() const { return running_get_difference_; } @@ -280,6 +282,8 @@ class UpdatesManager final : public Actor { }; FlatHashMap session_infos_; + double next_notify_speed_limited_[2] = {0.0, 0.0}; + void start_up() final; void tear_down() final; diff --git a/td/telegram/net/NetQueryDelayer.cpp b/td/telegram/net/NetQueryDelayer.cpp index 94145396a..837517768 100644 --- a/td/telegram/net/NetQueryDelayer.cpp +++ b/td/telegram/net/NetQueryDelayer.cpp @@ -34,10 +34,26 @@ void NetQueryDelayer::delay(NetQueryPtr query) { } } else if (code == 420) { auto error_message = query->error().message(); - for (auto prefix : - {Slice("FLOOD_WAIT_"), Slice("SLOWMODE_WAIT_"), Slice("2FA_CONFIRM_WAIT_"), Slice("TAKEOUT_INIT_DELAY_")}) { + for (auto prefix : {Slice("FLOOD_WAIT_"), Slice("SLOWMODE_WAIT_"), Slice("2FA_CONFIRM_WAIT_"), + Slice("TAKEOUT_INIT_DELAY_"), Slice("FLOOD_PREMIUM_WAIT_")}) { if (begins_with(error_message, prefix)) { timeout = clamp(to_integer(error_message.substr(prefix.size())), 1, 14 * 24 * 60 * 60); + if (prefix == "FLOOD_PREMIUM_WAIT_") { + switch (query->type()) { + case NetQuery::Type::Common: + LOG(ERROR) << "Receive " << error_message << " for " << query; + break; + case NetQuery::Type::Upload: + G()->notify_speed_limited(true); + break; + case NetQuery::Type::Download: + case NetQuery::Type::DownloadSmall: + G()->notify_speed_limited(false); + break; + default: + UNREACHABLE(); + } + } break; } } diff --git a/td/telegram/net/NetQueryDispatcher.cpp b/td/telegram/net/NetQueryDispatcher.cpp index e4d5d9c50..78fcf0791 100644 --- a/td/telegram/net/NetQueryDispatcher.cpp +++ b/td/telegram/net/NetQueryDispatcher.cpp @@ -121,6 +121,8 @@ void NetQueryDispatcher::dispatch(NetQueryPtr net_query) { net_query->debug(PSTRING() << "sent to download small session multi proxy " << dest_dc_id); send_closure_later(dcs_[dc_pos].download_small_session_, &SessionMultiProxy::send, std::move(net_query)); break; + default: + UNREACHABLE(); } }