Terms of service update support.

GitOrigin-RevId: 3c4c34def98e06e9b6da5102614f797046ff0921
This commit is contained in:
levlam 2018-06-08 01:19:34 +03:00
parent 03b674ab1c
commit 53f7e0cedf
5 changed files with 90 additions and 2 deletions

View File

@ -843,6 +843,7 @@ void AuthManager::on_authorization(tl_object_ptr<telegram_api::auth_authorizatio
}
td->updates_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();

View File

@ -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<std::pair<int32, TermsOfService>> 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<std::pair<int32, TermsOfService>> 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<td_api::updateTermsOfService>(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<GetNearestDcQuery>(Promise<string>())->send();
} else {
updates_manager_->get_difference("init");
schedule_get_terms_of_service(0);
}
complete_pending_preauthentication_requests([](int32 id) { return true; });

View File

@ -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<td_api::Object> 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<std::pair<int32, TermsOfService>> result, bool dummy);
template <class T>
friend class RequestActor; // uses send_result/send_error
friend class TestQuery; // uses send_result/send_error

View File

@ -16,6 +16,46 @@
namespace td {
class GetTermsOfServiceUpdateQuery : public Td::ResultHandler {
Promise<std::pair<int32, TermsOfService>> promise_;
public:
explicit GetTermsOfServiceUpdateQuery(Promise<std::pair<int32, TermsOfService>> &&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<telegram_api::help_getTermsOfServiceUpdate>(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<telegram_api::help_termsOfServiceUpdateEmpty>(result);
promise_.set_value(std::make_pair(update->expires_, TermsOfService()));
break;
}
case telegram_api::help_termsOfServiceUpdate::ID: {
auto update = move_tl_object_as<telegram_api::help_termsOfServiceUpdate>(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<Unit> promise_;
@ -69,8 +109,12 @@ TermsOfService::TermsOfService(telegram_api::object_ptr<telegram_api::help_terms
show_popup_ = (terms->flags_ & telegram_api::help_termsOfService::POPUP_MASK) != 0;
}
void get_terms_of_service(Td *td, Promise<std::pair<int32, TermsOfService>> promise) {
td->create_handler<GetTermsOfServiceUpdateQuery>(std::move(promise))->send();
}
void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise<Unit> &&promise) {
td->create_handler<AcceptTermsOfServiceQuery>(std::move(promise))->send(std::move(terms_of_service_id));
}
} // namespace td
} // namespace td

View File

@ -17,6 +17,8 @@
#include "td/utils/common.h"
#include "td/utils/tl_helpers.h"
#include <utility>
namespace td {
class Td;
@ -65,6 +67,8 @@ class TermsOfService {
}
};
void get_terms_of_service(Td *td, Promise<std::pair<int32, TermsOfService>> promise);
void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise<Unit> &&promise);
} // namespace td
} // namespace td