From 53f7e0cedf31bd42426b9445506c4f81ffd2122d Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 8 Jun 2018 01:19:34 +0300 Subject: [PATCH] Terms of service update support. GitOrigin-RevId: 3c4c34def98e06e9b6da5102614f797046ff0921 --- td/telegram/AuthManager.cpp | 1 + td/telegram/Td.cpp | 33 ++++++++++++++++++++++++ td/telegram/Td.h | 6 +++++ td/telegram/TermsOfService.cpp | 46 +++++++++++++++++++++++++++++++++- td/telegram/TermsOfService.h | 6 ++++- 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 305194f9..122f5f2a 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -843,6 +843,7 @@ void AuthManager::on_authorization(tl_object_ptrupdates_manager_->get_difference("on_authorization"); td->on_online_updated(true, true); + td->schedule_get_terms_of_service(0); send_closure(G()->config_manager(), &ConfigManager::request_config); if (query_id_ != 0) { on_query_ok(); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 190de0b5..c3434619 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3831,6 +3831,13 @@ void Td::on_alarm_timeout(int64 alarm_id) { alarm_timeout_.set_timeout_in(PING_SERVER_ALARM_ID, PING_SERVER_TIMEOUT + Random::fast(0, PING_SERVER_TIMEOUT / 5)); return; } + if (alarm_id == TERMS_OF_SERVICE_ALARM_ID && !close_flag_) { + get_terms_of_service( + this, PromiseCreator::lambda([actor_id = actor_id(this)](Result> result) { + send_closure(actor_id, &Td::on_get_terms_of_service, std::move(result), false); + })); + return; + } auto it = pending_alarms_.find(alarm_id); CHECK(it != pending_alarms_.end()); uint64 request_id = it->second; @@ -3866,6 +3873,30 @@ void Td::on_update_status_success(bool is_online) { } } +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()) { + 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())); + } + } + if (expires_in > 0) { + schedule_get_terms_of_service(expires_in); + } +} + +void Td::schedule_get_terms_of_service(int32 expires_in) { + if (!close_flag_) { + alarm_timeout_.set_timeout_in(TERMS_OF_SERVICE_ALARM_ID, expires_in); + } +} + void Td::on_channel_unban_timeout(int64 channel_id_long) { if (close_flag_ >= 2) { return; @@ -4407,6 +4438,7 @@ void Td::clear() { alarm_timeout_.cancel_timeout(ONLINE_ALARM_ID); } alarm_timeout_.cancel_timeout(PING_SERVER_ALARM_ID); + alarm_timeout_.cancel_timeout(TERMS_OF_SERVICE_ALARM_ID); LOG(DEBUG) << "Requests was answered " << timer; // close all pure actors @@ -4797,6 +4829,7 @@ Status Td::init(DbKey key) { create_handler(Promise())->send(); } else { updates_manager_->get_difference("init"); + schedule_get_terms_of_service(0); } complete_pending_preauthentication_requests([](int32 id) { return true; }); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 5294b118..c72df5ad 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -11,6 +11,7 @@ #include "td/telegram/TdCallback.h" #include "td/telegram/TdDb.h" #include "td/telegram/TdParameters.h" +#include "td/telegram/TermsOfService.h" #include "td/telegram/td_api.h" @@ -92,6 +93,8 @@ class Td final : public NetQueryCallback { void force_get_difference(); + void schedule_get_terms_of_service(int32 expires_in); + void on_result(NetQueryPtr query) override; void on_connection_state_changed(StateManager::State new_state); void on_authorization_lost(); @@ -201,6 +204,7 @@ class Td final : public NetQueryCallback { static constexpr int32 ONLINE_TIMEOUT = 240; static constexpr int64 PING_SERVER_ALARM_ID = -1; static constexpr int32 PING_SERVER_TIMEOUT = 300; + static constexpr int64 TERMS_OF_SERVICE_ALARM_ID = -2; void send_result(uint64 id, tl_object_ptr object); void send_error(uint64 id, Status error); @@ -253,6 +257,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); + void on_get_terms_of_service(Result> result, bool dummy); + template friend class RequestActor; // uses send_result/send_error friend class TestQuery; // uses send_result/send_error diff --git a/td/telegram/TermsOfService.cpp b/td/telegram/TermsOfService.cpp index df0022d3..bc203f9b 100644 --- a/td/telegram/TermsOfService.cpp +++ b/td/telegram/TermsOfService.cpp @@ -16,6 +16,46 @@ namespace td { +class GetTermsOfServiceUpdateQuery : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetTermsOfServiceUpdateQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create(create_storer(telegram_api::help_getTermsOfServiceUpdate()))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + auto result = result_ptr.move_as_ok(); + switch (result->get_id()) { + case telegram_api::help_termsOfServiceUpdateEmpty::ID: { + auto update = move_tl_object_as(result); + promise_.set_value(std::make_pair(update->expires_, TermsOfService())); + break; + } + case telegram_api::help_termsOfServiceUpdate::ID: { + auto update = move_tl_object_as(result); + promise_.set_value(std::make_pair(update->expires_, TermsOfService(std::move(update->terms_of_service_)))); + break; + } + default: + UNREACHABLE(); + } + } + + void on_error(uint64 id, Status status) override { + promise_.set_error(std::move(status)); + } +}; + class AcceptTermsOfServiceQuery : public Td::ResultHandler { Promise promise_; @@ -69,8 +109,12 @@ TermsOfService::TermsOfService(telegram_api::object_ptrflags_ & telegram_api::help_termsOfService::POPUP_MASK) != 0; } +void get_terms_of_service(Td *td, Promise> promise) { + td->create_handler(std::move(promise))->send(); +} + void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise &&promise) { td->create_handler(std::move(promise))->send(std::move(terms_of_service_id)); } -} // namespace td \ No newline at end of file +} // namespace td diff --git a/td/telegram/TermsOfService.h b/td/telegram/TermsOfService.h index 8d167ef2..613100d2 100644 --- a/td/telegram/TermsOfService.h +++ b/td/telegram/TermsOfService.h @@ -17,6 +17,8 @@ #include "td/utils/common.h" #include "td/utils/tl_helpers.h" +#include + namespace td { class Td; @@ -65,6 +67,8 @@ class TermsOfService { } }; +void get_terms_of_service(Td *td, Promise> promise); + void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise &&promise); -} // namespace td \ No newline at end of file +} // namespace td