From 8e0f49dc62bbb90625e7b88b9c62fbe5ff8d9bd7 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 20 Sep 2018 23:08:55 +0300 Subject: [PATCH] Support updateTermsOfService in getCurrentState. GitOrigin-RevId: b12f653432e0ee5b9ed3c7a7e8cb39e6dec7800b --- td/telegram/Td.cpp | 26 ++++++++++++++++++++++---- td/telegram/Td.h | 4 ++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 57b18c12..d41993f4 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3169,17 +3169,26 @@ void Td::on_update_status_success(bool is_online) { } } +td_api::object_ptr Td::get_update_terms_of_service_object() const { + auto terms_of_service = pending_terms_of_service_.get_terms_of_service_object(); + if (terms_of_service == nullptr) { + return nullptr; + } + return td_api::make_object(pending_terms_of_service_.get_id().str(), + std::move(terms_of_service)); +} + void Td::on_get_terms_of_service(Result> result, bool dummy) { int32 expires_in = 0; if (result.is_error()) { expires_in = Random::fast(10, 60); } else { - auto terms = std::move(result.ok().second); - if (terms.get_id().empty()) { + pending_terms_of_service_ = std::move(result.ok().second); + auto update = get_update_terms_of_service_object(); + if (update == nullptr) { expires_in = min(max(result.ok().first, G()->unix_time() + 60) - G()->unix_time(), 86400); } else { - send_update( - make_tl_object(terms.get_id().str(), terms.get_terms_of_service_object())); + send_update(std::move(update)); } } if (expires_in > 0) { @@ -3188,6 +3197,10 @@ void Td::on_get_terms_of_service(Result> result } void Td::schedule_get_terms_of_service(int32 expires_in) { + if (expires_in == 0) { + // drop pending Terms of Service after successful accept + pending_terms_of_service_ = TermsOfService(); + } if (!close_flag_ && !auth_manager_->is_bot()) { alarm_timeout_.set_timeout_in(TERMS_OF_SERVICE_ALARM_ID, expires_in); } @@ -4605,6 +4618,11 @@ void Td::on_request(uint64 id, const td_api::getCurrentState &request) { */ } + auto update_terms_of_service = get_update_terms_of_service_object(); + if (update_terms_of_service != nullptr) { + updates.push_back(std::move(update_terms_of_service)); + } + // send response synchronously to prevent "Request aborted" or other changes of the current state send_result(id, td_api::make_object(std::move(updates))); } diff --git a/td/telegram/Td.h b/td/telegram/Td.h index eb858964..368b341d 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -255,6 +255,8 @@ class Td final : public NetQueryCallback { std::unordered_map pending_alarms_; MultiTimeout alarm_timeout_{"AlarmTimeout"}; + TermsOfService pending_terms_of_service_; + vector>> pending_preauthentication_requests_; template @@ -265,6 +267,8 @@ class Td final : public NetQueryCallback { static void on_alarm_timeout_callback(void *td_ptr, int64 alarm_id); void on_alarm_timeout(int64 alarm_id); + td_api::object_ptr get_update_terms_of_service_object() const; + void on_get_terms_of_service(Result> result, bool dummy); template