diff --git a/Telegram/SourceFiles/auth_session.cpp b/Telegram/SourceFiles/auth_session.cpp index ca85137797..d62f32d7c1 100644 --- a/Telegram/SourceFiles/auth_session.cpp +++ b/Telegram/SourceFiles/auth_session.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "core/application.h" #include "core/changelogs.h" +#include "main/main_account.h" #include "storage/file_download.h" #include "storage/file_upload.h" #include "storage/localstorage.h" @@ -408,13 +409,14 @@ rpl::producer AuthSessionSettings::notifyAboutPinnedChanges() const { } AuthSession &Auth() { - const auto result = Core::App().authSession(); - Assert(result != nullptr); - return *result; + return Core::App().activeAccount().session(); } -AuthSession::AuthSession(const MTPUser &user) -: _autoLockTimer([this] { checkAutoLock(); }) +AuthSession::AuthSession( + not_null account, + const MTPUser &user) +: _account(account) +, _autoLockTimer([=] { checkAutoLock(); }) , _api(std::make_unique(this)) , _calls(std::make_unique()) , _downloader(std::make_unique(_api.get())) @@ -425,7 +427,6 @@ AuthSession::AuthSession(const MTPUser &user) , _user(_data->processUser(user)) , _changelogs(Core::Changelogs::Create(this)) , _supportHelper(Support::Helper::Create(this)) { - _saveDataTimer.setCallback([=] { Local::writeUserSettings(); }); @@ -465,8 +466,18 @@ AuthSession::AuthSession(const MTPUser &user) Window::Theme::Background()->start(); } +AuthSession::~AuthSession() { + ClickHandler::clearActive(); + ClickHandler::unpressed(); +} + +Main::Account &AuthSession::account() const { + return *_account; +} + bool AuthSession::Exists() { - return Core::IsAppLaunched() && (Core::App().authSession() != nullptr); + return Core::IsAppLaunched() + && Core::App().activeAccount().sessionExists(); } base::Observable &AuthSession::downloaderTaskFinished() { @@ -566,8 +577,3 @@ Support::Helper &AuthSession::supportHelper() const { Support::Templates& AuthSession::supportTemplates() const { return supportHelper().templates(); } - -AuthSession::~AuthSession() { - ClickHandler::clearActive(); - ClickHandler::unpressed(); -} diff --git a/Telegram/SourceFiles/auth_session.h b/Telegram/SourceFiles/auth_session.h index 19fb919ad4..ee6cd78063 100644 --- a/Telegram/SourceFiles/auth_session.h +++ b/Telegram/SourceFiles/auth_session.h @@ -16,6 +16,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL class ApiWrap; enum class SendFilesWay; +namespace Main { +class Account; +} // namespace Main + namespace Ui { enum class InputSubmitSettings; } // namespace Ui @@ -277,13 +281,16 @@ class AuthSession final : public base::has_weak_ptr , private base::Subscriber { public: - AuthSession(const MTPUser &user); + AuthSession(not_null account, const MTPUser &user); + ~AuthSession(); AuthSession(const AuthSession &other) = delete; AuthSession &operator=(const AuthSession &other) = delete; static bool Exists(); + Main::Account &account() const; + UserId userId() const; PeerId userPeerId() const; not_null user() const { @@ -340,11 +347,11 @@ public: Support::Helper &supportHelper() const; Support::Templates &supportTemplates() const; - ~AuthSession(); - private: static constexpr auto kDefaultSaveDelay = crl::time(1000); + const not_null _account; + AuthSessionSettings _settings; base::Timer _saveDataTimer; diff --git a/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp b/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp index 1908ac137a..3d673ca18f 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_keywords.cpp @@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "platform/platform_info.h" #include "ui/emoji_config.h" +#include "main/main_account.h" #include "auth_session.h" #include "apiwrap.h" @@ -486,12 +487,9 @@ void EmojiKeywords::langPackRefreshed() { } void EmojiKeywords::handleAuthSessionChanges() { - rpl::single( - rpl::empty_value() - ) | rpl::then(base::ObservableViewer( - Core::App().authSessionChanged() - )) | rpl::map([] { - return AuthSession::Exists() ? &Auth().api() : nullptr; + Core::App().activeAccount().sessionValue( + ) | rpl::map([](AuthSession *session) { + return session ? &session->api() : nullptr; }) | rpl::start_with_next([=](ApiWrap *api) { apiChanged(api); }, _lifetime); diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 46d1df5040..1141d25ae9 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -397,7 +397,9 @@ QByteArray Application::serializeMtpAuthorization() const { QDataStream stream(&result, QIODevice::WriteOnly); stream.setVersion(QDataStream::Qt_5_1); - auto currentUserId = _authSession ? _authSession->userId() : 0; + auto currentUserId = activeAccount().sessionExists() + ? activeAccount().session().userId() + : 0; stream << qint32(currentUserId) << qint32(mainDcId); writeKeys(stream, keys); writeKeys(stream, keysToDestroy); @@ -417,7 +419,7 @@ QByteArray Application::serializeMtpAuthorization() const { } void Application::setAuthSessionUserId(UserId userId) { - Expects(!authSession()); + Expects(!activeAccount().sessionExists()); _private->authSessionUserId = userId; } @@ -426,7 +428,7 @@ void Application::setAuthSessionFromStorage( std::unique_ptr data, QByteArray &&selfSerialized, int32 selfStreamVersion) { - Expects(!authSession()); + Expects(!activeAccount().sessionExists()); DEBUG_LOG(("authSessionUserSerialized set: %1" ).arg(selfSerialized.size())); @@ -441,8 +443,8 @@ AuthSessionSettings *Application::getAuthSessionSettings() { return _private->storedAuthSession ? _private->storedAuthSession.get() : nullptr; - } else if (_authSession) { - return &_authSession->settings(); + } else if (activeAccount().sessionExists()) { + return &activeAccount().session().settings(); } return nullptr; } @@ -542,8 +544,8 @@ void Application::startMtp() { base::take(_private->authSessionUserStreamVersion)); } if (_private->storedAuthSession) { - if (_authSession) { - _authSession->moveSettingsFrom( + if (activeAccount().sessionExists()) { + activeAccount().session().moveSettingsFrom( std::move(*_private->storedAuthSession)); } _private->storedAuthSession.reset(); @@ -556,7 +558,7 @@ void Application::startMtp() { UpdateChecker().setMtproto(mtp()); } - if (_authSession) { + if (activeAccount().sessionExists()) { // Skip all pending self updates so that we won't Local::writeSelf. Notify::peerUpdatedSendDelayed(); } @@ -644,11 +646,14 @@ void Application::startLocalStorage() { } } }); - subscribe(authSessionChanged(), [=] { - InvokeQueued(this, [=] { - const auto phone = AuthSession::Exists() - ? Auth().user()->phone() - : QString(); + activeAccount().sessionChanges( + ) | rpl::start_with_next([=] { + crl::on_main(this, [=] { + const auto phone = activeAccount().sessionExists() + ? activeAccount().session().user()->phone() + : QString(); + const auto support = activeAccount().sessionExists() + && activeAccount().session().supportMode(); if (cLoggedPhoneNumber() != phone) { cSetLoggedPhoneNumber(phone); if (_mtproto) { @@ -660,10 +665,9 @@ void Application::startLocalStorage() { _mtproto->requestConfig(); } Platform::SetApplicationIcon(Window::CreateIcon()); - Shortcuts::ToggleSupportShortcuts( - _authSession && _authSession->supportMode()); + Shortcuts::ToggleSupportShortcuts(support); }); - }); + }, _lifetime); } void Application::forceLogOut(const TextWithEntities &explanation) { @@ -768,7 +772,6 @@ void Application::writeInstallBetaVersionsSetting() { void Application::authSessionCreate(const MTPUser &user) { Expects(_mtproto != nullptr); - _authSession = std::make_unique(user); _mtproto->setUpdatesHandler(::rpcDone([]( const mtpPrime *from, const mtpPrime *end) { @@ -777,9 +780,13 @@ void Application::authSessionCreate(const MTPUser &user) { } })); _mtproto->setGlobalFailHandler(::rpcFail([=](const RPCError &error) { - crl::on_main(_authSession.get(), [=] { logOut(); }); + if (activeAccount().sessionExists()) { + crl::on_main(&activeAccount().session(), [=] { logOut(); }); + } return true; })); + + _authSession = std::make_unique(&activeAccount(), user); authSessionChanged().notify(true); } @@ -787,7 +794,7 @@ void Application::authSessionDestroy() { _private->storedAuthSession.reset(); _private->authSessionUserId = 0; _private->authSessionUserSerialized = {}; - if (_authSession) { + if (activeAccount().sessionExists()) { unlockTerms(); _mtproto->clearGlobalHandlers(); @@ -797,16 +804,21 @@ void Application::authSessionDestroy() { _authSession = nullptr; authSessionChanged().notify(true); + Notify::unreadCounterUpdated(); } } int Application::unreadBadge() const { - return _authSession ? _authSession->data().unreadBadge() : 0; + return activeAccount().sessionExists() + ? activeAccount().session().data().unreadBadge() + : 0; } bool Application::unreadBadgeMuted() const { - return _authSession ? _authSession->data().unreadBadgeMuted() : false; + return activeAccount().sessionExists() + ? activeAccount().session().data().unreadBadgeMuted() + : false; } void Application::setInternalLinkDomain(const QString &domain) const { @@ -1030,8 +1042,8 @@ void Application::loggedOut() { window->tempDirDelete(Local::ClearManagerAll); window->setupIntro(); } - if (const auto session = authSession()) { - session->data().clearLocalStorage(); + if (activeAccount().sessionExists()) { + activeAccount().session().data().clearLocalStorage(); authSessionDestroy(); } if (_mediaView) { diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index 682f5db07b..f0968dc124 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -153,7 +153,7 @@ public: } // Account component. - Main::Account &activeAccount() { + Main::Account &activeAccount() const { return *_account; } diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index 1d67d98c95..86c5864798 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -15,6 +15,9 @@ namespace Main { Account::Account(const QString &dataName) { } +Account::~Account() { +} + bool Account::sessionExists() const { return Core::App().authSession() != nullptr; } @@ -35,6 +38,14 @@ rpl::producer Account::sessionValue() const { }); } +rpl::producer Account::sessionChanges() const { + return base::ObservableViewer( + Core::App().authSessionChanged() + ) | rpl::map([] { + return Core::App().authSession(); + }); +} + MTP::Instance *Account::mtp() { return MTP::MainInstance(); } diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index 5ca6d47488..727f0e8c89 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -14,10 +14,15 @@ namespace Main { class Account final { public: explicit Account(const QString &dataName); + ~Account(); + + Account(const Account &other) = delete; + Account &operator=(const Account &other) = delete; [[nodiscard]] bool sessionExists() const; [[nodiscard]] AuthSession &session(); [[nodiscard]] rpl::producer sessionValue() const; + [[nodiscard]] rpl::producer sessionChanges() const; [[nodiscard]] MTP::Instance *mtp(); diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index e102a5321b..c7c39fc79a 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -34,8 +34,8 @@ constexpr auto kWaitingForAllGroupedDelay = crl::time(1000); } // namespace -System::System(AuthSession *session) -: _authSession(session) +System::System(not_null session) +: _session(session) , _waitTimer([=] { showNext(); }) , _waitForAllGroupedTimer([=] { showGrouped(); }) { createManager(); diff --git a/Telegram/SourceFiles/window/notifications_manager.h b/Telegram/SourceFiles/window/notifications_manager.h index 4e94a3cff7..78a73bf7d3 100644 --- a/Telegram/SourceFiles/window/notifications_manager.h +++ b/Telegram/SourceFiles/window/notifications_manager.h @@ -55,7 +55,7 @@ class Manager; class System final : private base::Subscriber { public: - System(AuthSession *session); + explicit System(not_null session); void createManager(); @@ -71,8 +71,8 @@ public: return _settingsChanged; } - AuthSession *authSession() { - return _authSession; + AuthSession &session() const { + return *_session; } ~System(); @@ -82,7 +82,7 @@ private: void showGrouped(); void ensureSoundCreated(); - AuthSession *_authSession = nullptr; + not_null _session; QMap> _whenMaps; @@ -112,7 +112,7 @@ private: int _lastForwardedCount = 0; FullMsgId _lastHistoryItemId; - + }; class Manager { diff --git a/Telegram/SourceFiles/window/notifications_manager_default.cpp b/Telegram/SourceFiles/window/notifications_manager_default.cpp index 9398d00cd3..1195d9af12 100644 --- a/Telegram/SourceFiles/window/notifications_manager_default.cpp +++ b/Telegram/SourceFiles/window/notifications_manager_default.cpp @@ -55,8 +55,8 @@ std::unique_ptr Create(System *system) { Manager::Manager(System *system) : Notifications::Manager(system) , _inputCheckTimer([=] { checkLastInput(); }) { - subscribe(system->authSession()->downloader().taskFinished(), [this] { - for_const (auto ¬ification, _notifications) { + subscribe(system->session().downloader().taskFinished(), [this] { + for (const auto ¬ification : _notifications) { notification->updatePeerPhoto(); } });