2019-04-30 11:12:30 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
namespace Storage {
|
|
|
|
class Account;
|
2020-06-18 18:04:16 +00:00
|
|
|
class Domain;
|
2020-06-15 16:25:02 +00:00
|
|
|
enum class StartResult : uchar;
|
2020-06-09 16:57:05 +00:00
|
|
|
} // namespace Storage
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
namespace MTP {
|
|
|
|
class AuthKey;
|
2020-06-17 09:36:25 +00:00
|
|
|
class Config;
|
2020-06-15 16:25:02 +00:00
|
|
|
} // namespace MTP
|
|
|
|
|
2019-04-30 11:12:30 +00:00
|
|
|
namespace Main {
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
class Domain;
|
2019-07-24 11:45:24 +00:00
|
|
|
class Session;
|
2020-06-18 12:47:09 +00:00
|
|
|
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 {
|
2019-04-30 11:12:30 +00:00
|
|
|
public:
|
2020-06-18 18:04:16 +00:00
|
|
|
Account(not_null<Domain*> domain, const QString &dataName, int index);
|
2019-06-05 20:41:51 +00:00
|
|
|
~Account();
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
[[nodiscard]] Domain &domain() const {
|
|
|
|
return *_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Storage::Domain &domainLocal() const;
|
2019-04-30 11:12:30 +00:00
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
[[nodiscard]] Storage::StartResult legacyStart(
|
|
|
|
const QByteArray &passcode);
|
2020-06-18 11:17:58 +00:00
|
|
|
[[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
|
|
|
|
2020-06-24 09:32:07 +00:00
|
|
|
[[nodiscard]] uint64 willHaveSessionUniqueId(MTP::Config *config) const;
|
2020-06-30 07:02:44 +00:00
|
|
|
void createSession(
|
|
|
|
const MTPUser &user,
|
|
|
|
std::unique_ptr<SessionSettings> settings = nullptr);
|
2019-10-02 10:46:02 +00:00
|
|
|
void createSession(
|
|
|
|
UserId id,
|
|
|
|
QByteArray serialized,
|
|
|
|
int streamVersion,
|
2020-06-18 12:47:09 +00:00
|
|
|
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
|
|
|
|
2020-06-09 16:57:05 +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
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
[[nodiscard]] Storage::Account &local() const {
|
|
|
|
return *_local;
|
|
|
|
}
|
2019-11-27 09:45:23 +00:00
|
|
|
|
2019-04-30 11:12:30 +00:00
|
|
|
[[nodiscard]] bool sessionExists() const;
|
2020-06-10 13:24:41 +00:00
|
|
|
[[nodiscard]] Session &session() const;
|
2020-06-23 13:29:02 +00:00
|
|
|
[[nodiscard]] Session *maybeSession() const;
|
2019-07-24 11:45:24 +00:00
|
|
|
[[nodiscard]] rpl::producer<Session*> sessionValue() const;
|
|
|
|
[[nodiscard]] rpl::producer<Session*> sessionChanges() const;
|
2019-04-30 11:12:30 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
[[nodiscard]] MTP::Instance &mtp() const {
|
|
|
|
return *_mtp;
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
[[nodiscard]] rpl::producer<not_null<MTP::Instance*>> mtpValue() const;
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2021-08-30 17:32:30 +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);
|
2020-06-29 18:14:16 +00:00
|
|
|
|
|
|
|
void setMtpMainDcId(MTP::DcId mainDcId);
|
2019-07-24 11:45:24 +00:00
|
|
|
void setSessionUserId(UserId userId);
|
|
|
|
void setSessionFromStorage(
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<SessionSettings> data,
|
2019-07-24 08:46:23 +00:00
|
|
|
QByteArray &&selfSerialized,
|
|
|
|
int32 selfStreamVersion);
|
2020-06-18 12:47:09 +00:00
|
|
|
[[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();
|
2019-04-30 11:12:30 +00:00
|
|
|
|
2022-12-28 23:33:57 +00:00
|
|
|
void setHandleLoginCode(Fn<void(QString)> callback);
|
|
|
|
void handleLoginCode(const QString &code) const;
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
[[nodiscard]] rpl::lifetime &lifetime() {
|
2020-06-16 09:40:43 +00:00
|
|
|
return _lifetime;
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:12:30 +00:00
|
|
|
private:
|
2020-06-18 18:04:16 +00:00
|
|
|
static constexpr auto kDefaultSaveDelay = crl::time(1000);
|
2020-06-26 10:36:22 +00:00
|
|
|
enum class DestroyReason {
|
|
|
|
Quitting,
|
|
|
|
LoggedOut,
|
|
|
|
};
|
2020-06-18 18:04:16 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
void startMtp(std::unique_ptr<MTP::Config> config);
|
2019-10-02 10:46:02 +00:00
|
|
|
void createSession(
|
|
|
|
const MTPUser &user,
|
|
|
|
QByteArray serialized,
|
|
|
|
int streamVersion,
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<SessionSettings> settings);
|
2019-07-24 08:46:23 +00:00
|
|
|
void watchProxyChanges();
|
|
|
|
void watchSessionChanges();
|
2021-03-12 10:45:13 +00:00
|
|
|
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
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
const not_null<Domain*> _domain;
|
2020-06-09 16:57:05 +00:00
|
|
|
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;
|
2019-04-30 11:12:30 +00:00
|
|
|
|
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;
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<SessionSettings> _storedSessionSettings;
|
2020-06-17 09:36:25 +00:00
|
|
|
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;
|
|
|
|
|
2019-04-30 11:12:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Main
|