Access _authSession through activeAccount().

This commit is contained in:
John Preston 2019-06-05 23:41:51 +03:00
parent bd2e1ceb02
commit 94c4ea6174
10 changed files with 93 additions and 54 deletions

View File

@ -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<bool> 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<Main::Account*> account,
const MTPUser &user)
: _account(account)
, _autoLockTimer([=] { checkAutoLock(); })
, _api(std::make_unique<ApiWrap>(this))
, _calls(std::make_unique<Calls::Instance>())
, _downloader(std::make_unique<Storage::Downloader>(_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<void> &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();
}

View File

@ -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<Main::Account*> 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<UserData*> 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<Main::Account*> _account;
AuthSessionSettings _settings;
base::Timer _saveDataTimer;

View File

@ -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);

View File

@ -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<AuthSessionSettings> 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<AuthSession>(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<AuthSession>(&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) {

View File

@ -153,7 +153,7 @@ public:
}
// Account component.
Main::Account &activeAccount() {
Main::Account &activeAccount() const {
return *_account;
}

View File

@ -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<AuthSession*> Account::sessionValue() const {
});
}
rpl::producer<AuthSession*> Account::sessionChanges() const {
return base::ObservableViewer(
Core::App().authSessionChanged()
) | rpl::map([] {
return Core::App().authSession();
});
}
MTP::Instance *Account::mtp() {
return MTP::MainInstance();
}

View File

@ -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<AuthSession*> sessionValue() const;
[[nodiscard]] rpl::producer<AuthSession*> sessionChanges() const;
[[nodiscard]] MTP::Instance *mtp();

View File

@ -34,8 +34,8 @@ constexpr auto kWaitingForAllGroupedDelay = crl::time(1000);
} // namespace
System::System(AuthSession *session)
: _authSession(session)
System::System(not_null<AuthSession*> session)
: _session(session)
, _waitTimer([=] { showNext(); })
, _waitForAllGroupedTimer([=] { showGrouped(); }) {
createManager();

View File

@ -55,7 +55,7 @@ class Manager;
class System final : private base::Subscriber {
public:
System(AuthSession *session);
explicit System(not_null<AuthSession*> 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<AuthSession*> _session;
QMap<History*, QMap<MsgId, crl::time>> _whenMaps;
@ -112,7 +112,7 @@ private:
int _lastForwardedCount = 0;
FullMsgId _lastHistoryItemId;
};
class Manager {

View File

@ -55,8 +55,8 @@ std::unique_ptr<Manager> Create(System *system) {
Manager::Manager(System *system)
: Notifications::Manager(system)
, _inputCheckTimer([=] { checkLastInput(); }) {
subscribe(system->authSession()->downloader().taskFinished(), [this] {
for_const (auto &notification, _notifications) {
subscribe(system->session().downloader().taskFinished(), [this] {
for (const auto &notification : _notifications) {
notification->updatePeerPhoto();
}
});