2018-06-07 20:42:17 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-06-07 20:42:17 +02:00
|
|
|
//
|
|
|
|
// 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/TermsOfService.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/misc.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/logging.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-06-07 20:42:17 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetTermsOfServiceUpdateQuery final : public Td::ResultHandler {
|
2018-06-08 00:19:34 +02:00
|
|
|
Promise<std::pair<int32, TermsOfService>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetTermsOfServiceUpdateQuery(Promise<std::pair<int32, TermsOfService>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
2019-12-13 02:00:33 +01:00
|
|
|
// we don't poll terms of service before authorization
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::help_getTermsOfServiceUpdate()));
|
2018-06-08 00:19:34 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-06-08 00:19:34 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::help_getTermsOfServiceUpdate>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-06-08 00:19:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-06-08 00:19:34 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class AcceptTermsOfServiceQuery final : public Td::ResultHandler {
|
2018-06-07 20:42:17 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AcceptTermsOfServiceQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
void send(const string &terms_of_service_id) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::help_acceptTermsOfService(
|
2021-10-19 17:11:16 +02:00
|
|
|
telegram_api::make_object<telegram_api::dataJSON>(terms_of_service_id))));
|
2018-06-07 20:42:17 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-06-07 20:42:17 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::help_acceptTermsOfService>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-06-07 20:42:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = result_ptr.ok();
|
|
|
|
if (!result) {
|
|
|
|
LOG(ERROR) << "Failed to accept terms of service";
|
|
|
|
}
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-06-07 20:42:17 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TermsOfService::TermsOfService(telegram_api::object_ptr<telegram_api::help_termsOfService> terms) {
|
|
|
|
if (terms == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
id_ = std::move(terms->id_->data_);
|
|
|
|
auto entities = get_message_entities(nullptr, std::move(terms->entities_), "TermsOfService");
|
2021-07-26 21:48:33 +02:00
|
|
|
auto status = fix_formatted_text(terms->text_, entities, true, true, true, true, false);
|
2018-06-07 20:42:17 +02:00
|
|
|
if (status.is_error()) {
|
|
|
|
if (!clean_input_string(terms->text_)) {
|
|
|
|
terms->text_.clear();
|
|
|
|
}
|
2021-08-04 03:26:41 +02:00
|
|
|
entities = find_entities(terms->text_, true, true);
|
2018-06-07 20:42:17 +02:00
|
|
|
}
|
|
|
|
if (terms->text_.empty()) {
|
|
|
|
id_.clear();
|
|
|
|
}
|
|
|
|
text_ = FormattedText{std::move(terms->text_), std::move(entities)};
|
2021-11-01 19:53:23 +01:00
|
|
|
min_user_age_ = terms->min_age_confirm_;
|
|
|
|
show_popup_ = terms->popup_;
|
2018-06-07 20:42:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-08 00:19:34 +02:00
|
|
|
void get_terms_of_service(Td *td, Promise<std::pair<int32, TermsOfService>> promise) {
|
|
|
|
td->create_handler<GetTermsOfServiceUpdateQuery>(std::move(promise))->send();
|
|
|
|
}
|
|
|
|
|
2018-06-07 20:42:17 +02:00
|
|
|
void accept_terms_of_service(Td *td, string &&terms_of_service_id, Promise<Unit> &&promise) {
|
2021-10-19 17:11:16 +02:00
|
|
|
td->create_handler<AcceptTermsOfServiceQuery>(std::move(promise))->send(terms_of_service_id);
|
2018-06-07 20:42:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-08 00:19:34 +02:00
|
|
|
} // namespace td
|