tdesktop/Telegram/SourceFiles/main/main_account.h

173 lines
4.6 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2019-11-18 09:28:14 +00:00
#include "mtproto/mtproto_auth_key.h"
2019-11-13 08:31:12 +00:00
#include "mtproto/mtp_instance.h"
2019-07-24 08:46:23 +00:00
#include "base/weak_ptr.h"
namespace Storage {
class Account;
class Domain;
2020-06-15 16:25:02 +00:00
enum class StartResult : uchar;
} // namespace Storage
2020-06-15 16:25:02 +00:00
namespace MTP {
class AuthKey;
class Config;
2020-06-15 16:25:02 +00:00
} // namespace MTP
namespace Main {
class Domain;
2019-07-24 11:45:24 +00:00
class Session;
class SessionSettings;
2019-11-27 09:45:23 +00:00
class AppConfig;
2019-07-24 11:45:24 +00:00
2019-07-24 08:46:23 +00:00
class Account final : public base::has_weak_ptr {
public:
Account(not_null<Domain*> domain, const QString &dataName, int index);
~Account();
[[nodiscard]] Domain &domain() const {
return *_domain;
}
[[nodiscard]] Storage::Domain &domainLocal() const;
2020-06-15 16:25:02 +00:00
[[nodiscard]] Storage::StartResult legacyStart(
const QByteArray &passcode);
[[nodiscard]] std::unique_ptr<MTP::Config> prepareToStart(
std::shared_ptr<MTP::AuthKey> localKey);
void prepareToStartAdded(
std::shared_ptr<MTP::AuthKey> localKey);
void start(std::unique_ptr<MTP::Config> config);
2020-06-15 16:25:02 +00:00
[[nodiscard]] uint64 willHaveSessionUniqueId(MTP::Config *config) const;
void createSession(
const MTPUser &user,
std::unique_ptr<SessionSettings> settings = nullptr);
void createSession(
UserId id,
QByteArray serialized,
int streamVersion,
std::unique_ptr<SessionSettings> settings);
2019-06-06 09:37:12 +00:00
2019-07-24 08:46:23 +00:00
void logOut();
void forcedLogOut();
2020-06-16 09:40:43 +00:00
[[nodiscard]] bool loggingOut() const;
2019-07-24 08:46:23 +00:00
[[nodiscard]] AppConfig &appConfig() const {
2020-06-15 16:25:02 +00:00
Expects(_appConfig != nullptr);
2019-11-27 09:45:23 +00:00
return *_appConfig;
}
2020-06-15 16:25:02 +00:00
[[nodiscard]] Storage::Account &local() const {
return *_local;
}
2019-11-27 09:45:23 +00:00
[[nodiscard]] bool sessionExists() const;
[[nodiscard]] Session &session() const;
[[nodiscard]] Session *maybeSession() const;
2019-07-24 11:45:24 +00:00
[[nodiscard]] rpl::producer<Session*> sessionValue() const;
[[nodiscard]] rpl::producer<Session*> sessionChanges() const;
[[nodiscard]] MTP::Instance &mtp() const {
return *_mtp;
2019-07-24 08:46:23 +00:00
}
[[nodiscard]] rpl::producer<not_null<MTP::Instance*>> mtpValue() const;
2019-07-24 08:46:23 +00:00
// Each time the main session changes a new copy of the pointer is fired.
// This allows to resend the requests that were not requiring auth, and
// which could be forgotten without calling .done() or .fail() because
// of the main dc changing.
[[nodiscard]] auto mtpMainSessionValue() const
-> rpl::producer<not_null<MTP::Instance*>>;
2019-07-24 08:46:23 +00:00
// Set from legacy storage.
2020-06-10 14:52:44 +00:00
void setLegacyMtpKey(std::shared_ptr<MTP::AuthKey> key);
void setMtpMainDcId(MTP::DcId mainDcId);
2019-07-24 11:45:24 +00:00
void setSessionUserId(UserId userId);
void setSessionFromStorage(
std::unique_ptr<SessionSettings> data,
2019-07-24 08:46:23 +00:00
QByteArray &&selfSerialized,
int32 selfStreamVersion);
[[nodiscard]] SessionSettings *getSessionSettings();
2019-11-22 09:40:52 +00:00
[[nodiscard]] rpl::producer<> mtpNewSessionCreated() const;
[[nodiscard]] rpl::producer<MTPUpdates> mtpUpdates() const;
2019-07-24 08:46:23 +00:00
// Serialization.
[[nodiscard]] QByteArray serializeMtpAuthorization() const;
void setMtpAuthorization(const QByteArray &serialized);
void suggestMainDcId(MTP::DcId mainDcId);
void destroyStaleAuthorizationKeys();
2022-12-28 23:33:57 +00:00
void setHandleLoginCode(Fn<void(QString)> callback);
void handleLoginCode(const QString &code) const;
[[nodiscard]] rpl::lifetime &lifetime() {
2020-06-16 09:40:43 +00:00
return _lifetime;
}
private:
static constexpr auto kDefaultSaveDelay = crl::time(1000);
2020-06-26 10:36:22 +00:00
enum class DestroyReason {
Quitting,
LoggedOut,
};
void startMtp(std::unique_ptr<MTP::Config> config);
void createSession(
const MTPUser &user,
QByteArray serialized,
int streamVersion,
std::unique_ptr<SessionSettings> settings);
2019-07-24 08:46:23 +00:00
void watchProxyChanges();
void watchSessionChanges();
bool checkForUpdates(const MTP::Response &message);
bool checkForNewSession(const MTP::Response &message);
2019-07-24 08:46:23 +00:00
void destroyMtpKeys(MTP::AuthKeysList &&keys);
void resetAuthorizationKeys();
void loggedOut();
2020-06-26 10:36:22 +00:00
void destroySession(DestroyReason reason);
2019-07-24 08:46:23 +00:00
const not_null<Domain*> _domain;
const std::unique_ptr<Storage::Account> _local;
2019-07-24 08:46:23 +00:00
std::unique_ptr<MTP::Instance> _mtp;
rpl::variable<MTP::Instance*> _mtpValue;
std::unique_ptr<MTP::Instance> _mtpForKeysDestroy;
2019-11-22 09:40:52 +00:00
rpl::event_stream<MTPUpdates> _mtpUpdates;
rpl::event_stream<> _mtpNewSessionCreated;
2019-07-24 08:46:23 +00:00
2019-11-27 09:45:23 +00:00
std::unique_ptr<AppConfig> _appConfig;
2019-07-24 11:45:24 +00:00
std::unique_ptr<Session> _session;
rpl::variable<Session*> _sessionValue;
2022-12-28 23:33:57 +00:00
Fn<void(QString)> _handleLoginCode = nullptr;
2019-07-24 11:45:24 +00:00
UserId _sessionUserId = 0;
QByteArray _sessionUserSerialized;
int32 _sessionUserStreamVersion = 0;
std::unique_ptr<SessionSettings> _storedSessionSettings;
MTP::Instance::Fields _mtpFields;
2019-07-24 08:46:23 +00:00
MTP::AuthKeysList _mtpKeysToDestroy;
2019-11-20 13:33:45 +00:00
bool _loggingOut = false;
2019-07-24 08:46:23 +00:00
rpl::lifetime _lifetime;
};
} // namespace Main