From af818b40aad888a0a1d694d81705477b31afd415 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 2 Oct 2019 13:46:02 +0300 Subject: [PATCH] Version 1.8.12: Apply user settings in Session(). Fixes #6617. --- Telegram/SourceFiles/core/application.h | 1 - Telegram/SourceFiles/main/main_account.cpp | 86 +++++++++++++--------- Telegram/SourceFiles/main/main_account.h | 10 +++ Telegram/SourceFiles/main/main_session.cpp | 25 +++---- Telegram/SourceFiles/main/main_session.h | 6 +- Telegram/SourceFiles/main/main_settings.h | 3 - 6 files changed, 78 insertions(+), 53 deletions(-) diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index 4b90819bf..84b9edabe 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -123,7 +123,6 @@ public: [[nodiscard]] Settings &settings() { return _settings; } - void moveSettingsFrom(Settings &&other); void saveSettingsDelayed(crl::time delay = kDefaultSaveDelay); // Dc options and proxy. diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index 12d765491..c0185388c 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -77,13 +77,52 @@ void Account::watchSessionChanges() { } void Account::createSession(const MTPUser &user) { + createSession(user, QByteArray(), 0, Settings()); +} + +void Account::createSession( + UserId id, + QByteArray serialized, + int streamVersion, + Settings &&settings) { + DEBUG_LOG(("sessionUserSerialized.size: %1").arg(serialized.size())); + QDataStream peekStream(serialized); + const auto phone = Serialize::peekUserPhone(streamVersion, peekStream); + const auto flags = MTPDuser::Flag::f_self | (phone.isEmpty() + ? MTPDuser::Flag() + : MTPDuser::Flag::f_phone); + createSession( + MTP_user( + MTP_flags(flags), + MTP_int(base::take(_sessionUserId)), + MTPlong(), // access_hash + MTPstring(), // first_name + MTPstring(), // last_name + MTPstring(), // username + MTP_string(phone), + MTPUserProfilePhoto(), + MTPUserStatus(), + MTPint(), // bot_info_version + MTPVector(), + MTPstring(), // bot_inline_placeholder + MTPstring()), // lang_code + serialized, + streamVersion, + std::move(settings)); +} + +void Account::createSession( + const MTPUser &user, + QByteArray serialized, + int streamVersion, + Settings &&settings) { Expects(_mtp != nullptr); Expects(_session == nullptr); Expects(_sessionValue.current() == nullptr); _mtp->setUpdatesHandler(::rpcDone([]( - const mtpPrime *from, - const mtpPrime *end) { + const mtpPrime *from, + const mtpPrime *end) { if (const auto main = App::main()) { return main->updateReceived(from, end); } @@ -96,8 +135,13 @@ void Account::createSession(const MTPUser &user) { return true; })); - _session = std::make_unique(this, user); + _session = std::make_unique(this, user, std::move(settings)); _sessionValue = _session.get(); + + if (!serialized.isEmpty()) { + // For now it depends on Auth() which depends on _sessionValue. + Local::readSelf(serialized, streamVersion); + } } void Account::destroySession() { @@ -316,39 +360,13 @@ void Account::startMtp() { } if (_sessionUserId) { - DEBUG_LOG(("sessionUserSerialized.size: %1" - ).arg(_sessionUserSerialized.size())); - QDataStream peekStream(_sessionUserSerialized); - const auto phone = Serialize::peekUserPhone( - _sessionUserStreamVersion, - peekStream); - const auto flags = MTPDuser::Flag::f_self | (phone.isEmpty() - ? MTPDuser::Flag() - : MTPDuser::Flag::f_phone); - createSession(MTP_user( - MTP_flags(flags), - MTP_int(base::take(_sessionUserId)), - MTPlong(), // access_hash - MTPstring(), // first_name - MTPstring(), // last_name - MTPstring(), // username - MTP_string(phone), - MTPUserProfilePhoto(), - MTPUserStatus(), - MTPint(), // bot_info_version - MTPVector(), - MTPstring(), // bot_inline_placeholder - MTPstring())); // lang_code - Local::readSelf( + createSession( + _sessionUserId, base::take(_sessionUserSerialized), - base::take(_sessionUserStreamVersion)); - } - if (_storedSettings) { - if (sessionExists()) { - session().moveSettingsFrom(std::move(*_storedSettings)); - } - _storedSettings.reset(); + base::take(_sessionUserStreamVersion), + _storedSettings ? std::move(*_storedSettings) : Settings()); } + _storedSettings = nullptr; if (sessionExists()) { // Skip all pending self updates so that we won't Local::writeSelf. diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index 08d46b555..44765995a 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -24,6 +24,11 @@ public: Account &operator=(const Account &other) = delete; void createSession(const MTPUser &user); + void createSession( + UserId id, + QByteArray serialized, + int streamVersion, + Settings &&settings); void destroySession(); void logOut(); @@ -63,6 +68,11 @@ public: void clearMtp(); private: + void createSession( + const MTPUser &user, + QByteArray serialized, + int streamVersion, + Settings &&settings); void watchProxyChanges(); void watchSessionChanges(); diff --git a/Telegram/SourceFiles/main/main_session.cpp b/Telegram/SourceFiles/main/main_session.cpp index df7a8de7a..fe93b0352 100644 --- a/Telegram/SourceFiles/main/main_session.cpp +++ b/Telegram/SourceFiles/main/main_session.cpp @@ -37,8 +37,10 @@ constexpr auto kLegacyCallsPeerToPeerNobody = 4; Session::Session( not_null account, - const MTPUser &user) + const MTPUser &user, + Settings &&settings) : _account(account) +, _settings(std::move(settings)) , _saveSettingsTimer([=] { Local::writeUserSettings(); }) , _autoLockTimer([=] { checkAutoLock(); }) , _api(std::make_unique(this)) @@ -84,6 +86,15 @@ Session::Session( Local::writeSelf(); } })); + + if (_settings.hadLegacyCallsPeerToPeerNobody()) { + api().savePrivacy( + MTP_inputPrivacyKeyPhoneP2P(), + QVector( + 1, + MTP_inputPrivacyValueDisallowAll())); + saveSettingsDelayed(); + } }); Window::Theme::Background()->start(); @@ -127,18 +138,6 @@ bool Session::validateSelf(const MTPUser &user) { return true; } -void Session::moveSettingsFrom(Settings &&other) { - _settings.moveFrom(std::move(other)); - if (_settings.hadLegacyCallsPeerToPeerNobody()) { - api().savePrivacy( - MTP_inputPrivacyKeyPhoneP2P(), - QVector( - 1, - MTP_inputPrivacyValueDisallowAll())); - saveSettingsDelayed(); - } -} - void Session::saveSettingsDelayed(crl::time delay) { Expects(this == &Auth()); diff --git a/Telegram/SourceFiles/main/main_session.h b/Telegram/SourceFiles/main/main_session.h index 5cb8ee69b..91cf0c45b 100644 --- a/Telegram/SourceFiles/main/main_session.h +++ b/Telegram/SourceFiles/main/main_session.h @@ -57,7 +57,10 @@ class Session final : public base::has_weak_ptr , private base::Subscriber { public: - Session(not_null account, const MTPUser &user); + Session( + not_null account, + const MTPUser &user, + Settings &&other); ~Session(); Session(const Session &other) = delete; @@ -102,7 +105,6 @@ public: [[nodiscard]] Settings &settings() { return _settings; } - void moveSettingsFrom(Settings &&other); void saveSettingsDelayed(crl::time delay = kDefaultSaveDelay); [[nodiscard]] not_null mtp(); diff --git a/Telegram/SourceFiles/main/main_settings.h b/Telegram/SourceFiles/main/main_settings.h index 541995f74..c92242c82 100644 --- a/Telegram/SourceFiles/main/main_settings.h +++ b/Telegram/SourceFiles/main/main_settings.h @@ -32,9 +32,6 @@ namespace Main { class Settings final { public: - void moveFrom(Settings &&other) { - _variables = std::move(other._variables); - } [[nodiscard]] QByteArray serialize() const; void constructFromSerialized(const QByteArray &serialized);