From e6fb913798e3b09e9f4cfea70bdff4f1008ecdb5 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 22 Sep 2023 14:22:24 +0300 Subject: [PATCH] Add ChangeAuthorizationSettingsOnServerLogEvent. --- td/telegram/AccountManager.cpp | 103 ++++++++++++++++++++++++++++++-- td/telegram/AccountManager.h | 16 +++++ td/telegram/Td.cpp | 2 + td/telegram/TdDb.cpp | 3 + td/telegram/TdDb.h | 3 +- td/telegram/logevent/LogEvent.h | 1 + 6 files changed, 121 insertions(+), 7 deletions(-) diff --git a/td/telegram/AccountManager.cpp b/td/telegram/AccountManager.cpp index cbd8ad2cb..2a6941a6f 100644 --- a/td/telegram/AccountManager.cpp +++ b/td/telegram/AccountManager.cpp @@ -12,12 +12,16 @@ #include "td/telegram/Global.h" #include "td/telegram/LinkManager.h" #include "td/telegram/logevent/LogEvent.h" +#include "td/telegram/logevent/LogEventHelper.h" #include "td/telegram/net/NetQueryCreator.h" #include "td/telegram/Td.h" #include "td/telegram/TdDb.h" #include "td/telegram/telegram_api.h" #include "td/telegram/UserId.h" +#include "td/db/binlog/BinlogEvent.h" +#include "td/db/binlog/BinlogHelper.h" + #include "td/utils/algorithm.h" #include "td/utils/base64.h" #include "td/utils/buffer.h" @@ -822,24 +826,89 @@ void AccountManager::terminate_all_other_sessions(Promise &&promise) { td_->create_handler(std::move(promise))->send(); } +class AccountManager::ChangeAuthorizationSettingsOnServerLogEvent { + public: + int64 hash_; + bool set_encrypted_requests_disabled_; + bool encrypted_requests_disabled_; + bool set_call_requests_disabled_; + bool call_requests_disabled_; + bool confirm_; + + template + void store(StorerT &storer) const { + BEGIN_STORE_FLAGS(); + STORE_FLAG(set_encrypted_requests_disabled_); + STORE_FLAG(encrypted_requests_disabled_); + STORE_FLAG(set_call_requests_disabled_); + STORE_FLAG(call_requests_disabled_); + STORE_FLAG(confirm_); + END_STORE_FLAGS(); + td::store(hash_, storer); + } + + template + void parse(ParserT &parser) { + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(set_encrypted_requests_disabled_); + PARSE_FLAG(encrypted_requests_disabled_); + PARSE_FLAG(set_call_requests_disabled_); + PARSE_FLAG(call_requests_disabled_); + PARSE_FLAG(confirm_); + END_PARSE_FLAGS(); + td::parse(hash_, parser); + } +}; + +uint64 AccountManager::save_change_authorization_settings_on_server_log_event( + int64 hash, bool set_encrypted_requests_disabled, bool encrypted_requests_disabled, bool set_call_requests_disabled, + bool call_requests_disabled, bool confirm) { + ChangeAuthorizationSettingsOnServerLogEvent log_event{hash, + set_encrypted_requests_disabled, + encrypted_requests_disabled, + set_call_requests_disabled, + call_requests_disabled, + confirm}; + return binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::ChangeAuthorizationSettingsOnServer, + get_log_event_storer(log_event)); +} + +void AccountManager::change_authorization_settings_on_server(int64 hash, bool set_encrypted_requests_disabled, + bool encrypted_requests_disabled, + bool set_call_requests_disabled, + bool call_requests_disabled, bool confirm, + uint64 log_event_id, Promise &&promise) { + if (log_event_id == 0) { + log_event_id = save_change_authorization_settings_on_server_log_event( + hash, set_encrypted_requests_disabled, encrypted_requests_disabled, set_call_requests_disabled, + call_requests_disabled, confirm); + } + + auto new_promise = get_erase_log_event_promise(log_event_id, std::move(promise)); + promise = std::move(new_promise); // to prevent self-move + + td_->create_handler(std::move(promise)) + ->send(hash, set_encrypted_requests_disabled, encrypted_requests_disabled, set_call_requests_disabled, + call_requests_disabled, confirm); +} + void AccountManager::confirm_session(int64 session_id, Promise &&promise) { if (!on_confirm_authorization(session_id)) { // the authorization can be from the list of active authorizations, but the update could have been lost // return promise.set_value(Unit()); } - td_->create_handler(std::move(promise)) - ->send(session_id, false, false, false, false, true); + change_authorization_settings_on_server(session_id, false, false, false, false, true, 0, std::move(promise)); } void AccountManager::toggle_session_can_accept_calls(int64 session_id, bool can_accept_calls, Promise &&promise) { - td_->create_handler(std::move(promise)) - ->send(session_id, false, false, true, !can_accept_calls, false); + change_authorization_settings_on_server(session_id, false, false, true, !can_accept_calls, false, 0, + std::move(promise)); } void AccountManager::toggle_session_can_accept_secret_chats(int64 session_id, bool can_accept_secret_chats, Promise &&promise) { - td_->create_handler(std::move(promise)) - ->send(session_id, true, !can_accept_secret_chats, false, false, false); + change_authorization_settings_on_server(session_id, true, !can_accept_secret_chats, false, false, false, 0, + std::move(promise)); } void AccountManager::set_inactive_session_ttl_days(int32 authorization_ttl_days, Promise &&promise) { @@ -976,6 +1045,28 @@ void AccountManager::send_update_unconfirmed_session() const { send_closure(G()->td(), &Td::send_update, get_update_unconfirmed_session()); } +void AccountManager::on_binlog_events(vector &&events) { + if (G()->close_flag()) { + return; + } + for (auto &event : events) { + switch (event.type_) { + case LogEvent::HandlerType::ChangeAuthorizationSettingsOnServer: { + ChangeAuthorizationSettingsOnServerLogEvent log_event; + log_event_parse(log_event, event.get_data()).ensure(); + + change_authorization_settings_on_server( + log_event.hash_, log_event.set_encrypted_requests_disabled_, log_event.encrypted_requests_disabled_, + log_event.set_call_requests_disabled_, log_event.call_requests_disabled_, log_event.confirm_, event.id_, + Auto()); + break; + } + default: + LOG(FATAL) << "Unsupported log event type " << event.type_; + } + } +} + void AccountManager::get_current_state(vector> &updates) const { if (unconfirmed_authorizations_ != nullptr) { updates.push_back(get_update_unconfirmed_session()); diff --git a/td/telegram/AccountManager.h b/td/telegram/AccountManager.h index 6aa152eff..9ee9bba02 100644 --- a/td/telegram/AccountManager.h +++ b/td/telegram/AccountManager.h @@ -15,6 +15,8 @@ namespace td { +struct BinlogEvent; + class Td; class AccountManager final : public Actor { @@ -68,12 +70,16 @@ class AccountManager final : public Actor { bool on_confirm_authorization(int64 hash); + void on_binlog_events(vector &&events); + void get_current_state(vector> &updates) const; private: class UnconfirmedAuthorization; class UnconfirmedAuthorizations; + class ChangeAuthorizationSettingsOnServerLogEvent; + void start_up() final; void timeout_expired() final; @@ -92,6 +98,16 @@ class AccountManager final : public Actor { void send_update_unconfirmed_session() const; + uint64 save_change_authorization_settings_on_server_log_event(int64 hash, bool set_encrypted_requests_disabled, + bool encrypted_requests_disabled, + bool set_call_requests_disabled, + bool call_requests_disabled, bool confirm); + + void change_authorization_settings_on_server(int64 hash, bool set_encrypted_requests_disabled, + bool encrypted_requests_disabled, bool set_call_requests_disabled, + bool call_requests_disabled, bool confirm, uint64 log_event_id, + Promise &&promise); + Td *td_; ActorShared<> parent_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 06ef8084b..cce1fbdf5 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3791,6 +3791,8 @@ void Td::init(Parameters parameters, Result r_opened_datab send_closure_later(secret_chats_manager_, &SecretChatsManager::replay_binlog_event, std::move(event)); } + send_closure_later(account_manager_actor_, &AccountManager::on_binlog_events, std::move(events.to_account_manager)); + send_closure_later(poll_manager_actor_, &PollManager::on_binlog_events, std::move(events.to_poll_manager)); send_closure_later(messages_manager_actor_, &MessagesManager::on_binlog_events, diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index da8b4914a..f09256f06 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -144,6 +144,9 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue &binlog_p case LogEvent::HandlerType::SaveAppLog: events.save_app_log_events.push_back(event.clone()); break; + case LogEvent::HandlerType::ChangeAuthorizationSettingsOnServer: + events.to_account_manager.push_back(event.clone()); + break; case LogEvent::HandlerType::BinlogPmcMagic: binlog_pmc.external_init_handle(event); break; diff --git a/td/telegram/TdDb.h b/td/telegram/TdDb.h index f3670b429..a74b6aac8 100644 --- a/td/telegram/TdDb.h +++ b/td/telegram/TdDb.h @@ -72,10 +72,11 @@ class TdDb { vector secret_chat_events; vector web_page_events; vector save_app_log_events; - vector to_poll_manager; + vector to_account_manager; vector to_messages_manager; vector to_notification_manager; vector to_notification_settings_manager; + vector to_poll_manager; vector to_story_manager; int64 since_last_open = 0; diff --git a/td/telegram/logevent/LogEvent.h b/td/telegram/logevent/LogEvent.h index e81a84dab..480e067ae 100644 --- a/td/telegram/logevent/LogEvent.h +++ b/td/telegram/logevent/LogEvent.h @@ -112,6 +112,7 @@ class LogEvent { LoadDialogExpiringStories = 0x402, SendStory = 0x403, EditStory = 0x404, + ChangeAuthorizationSettingsOnServer = 0x500, ConfigPmcMagic = 0x1f18, BinlogPmcMagic = 0x4327 };