2020-06-15 16:25:02 +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
|
|
|
|
*/
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "main/main_domain.h"
|
2020-06-15 16:25:02 +00:00
|
|
|
|
|
|
|
#include "core/application.h"
|
2022-06-11 00:07:22 +00:00
|
|
|
#include "core/core_settings.h"
|
2020-06-15 16:25:02 +00:00
|
|
|
#include "core/shortcuts.h"
|
2020-06-30 16:41:25 +00:00
|
|
|
#include "core/crash_reports.h"
|
2020-06-15 16:25:02 +00:00
|
|
|
#include "main/main_account.h"
|
|
|
|
#include "main/main_session.h"
|
2020-06-18 11:17:58 +00:00
|
|
|
#include "data/data_session.h"
|
2020-06-30 16:41:25 +00:00
|
|
|
#include "data/data_changes.h"
|
|
|
|
#include "data/data_user.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
|
|
|
#include "mtproto/mtproto_dc_options.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "storage/storage_domain.h"
|
2020-06-25 07:14:05 +00:00
|
|
|
#include "storage/storage_account.h"
|
2020-06-15 16:25:02 +00:00
|
|
|
#include "storage/localstorage.h"
|
2020-06-25 07:14:05 +00:00
|
|
|
#include "export/export_settings.h"
|
2020-06-30 07:44:32 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2022-06-02 10:54:10 +00:00
|
|
|
#include "data/data_peer_values.h" // Data::AmPremiumValue.
|
2020-06-16 14:19:23 +00:00
|
|
|
#include "facades.h"
|
2020-06-15 16:25:02 +00:00
|
|
|
|
|
|
|
namespace Main {
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
Domain::Domain(const QString &dataName)
|
2020-06-15 16:25:02 +00:00
|
|
|
: _dataName(dataName)
|
2020-06-18 18:04:16 +00:00
|
|
|
, _local(std::make_unique<Storage::Domain>(this, dataName)) {
|
2020-06-29 16:07:56 +00:00
|
|
|
_active.changes(
|
|
|
|
) | rpl::take(1) | rpl::start_with_next([] {
|
2020-07-22 12:10:17 +00:00
|
|
|
// In case we had a legacy passcoded app we start settings here.
|
|
|
|
Core::App().startSettingsAndBackground();
|
|
|
|
|
2020-06-30 07:44:32 +00:00
|
|
|
Core::App().notifications().createManager();
|
2020-06-29 16:07:56 +00:00
|
|
|
}, _lifetime);
|
2020-06-30 16:41:25 +00:00
|
|
|
|
|
|
|
_active.changes(
|
|
|
|
) | rpl::map([](Main::Account *account) {
|
|
|
|
return account ? account->sessionValue() : rpl::never<Session*>();
|
|
|
|
}) | rpl::flatten_latest(
|
|
|
|
) | rpl::map([](Main::Session *session) {
|
|
|
|
return session
|
|
|
|
? session->changes().peerFlagsValue(
|
|
|
|
session->user(),
|
|
|
|
Data::PeerUpdate::Flag::Username)
|
|
|
|
: rpl::never<Data::PeerUpdate>();
|
|
|
|
}) | rpl::flatten_latest(
|
|
|
|
) | rpl::start_with_next([](const Data::PeerUpdate &update) {
|
|
|
|
CrashReports::SetAnnotation("Username", update.peer->userName());
|
|
|
|
}, _lifetime);
|
2020-06-15 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
Domain::~Domain() = default;
|
2020-06-15 16:25:02 +00:00
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
bool Domain::started() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return !_accounts.empty();
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
Storage::StartResult Domain::start(const QByteArray &passcode) {
|
2020-06-15 16:25:02 +00:00
|
|
|
Expects(!started());
|
|
|
|
|
2020-06-16 09:40:43 +00:00
|
|
|
const auto result = _local->start(passcode);
|
2020-06-15 16:25:02 +00:00
|
|
|
if (result == Storage::StartResult::Success) {
|
2020-06-18 11:17:58 +00:00
|
|
|
activateAfterStarting();
|
2020-06-29 16:07:56 +00:00
|
|
|
crl::on_main(&Core::App(), [=] { suggestExportIfNeeded(); });
|
2020-06-15 16:25:02 +00:00
|
|
|
} else {
|
|
|
|
Assert(!started());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::finish() {
|
2020-06-19 12:59:31 +00:00
|
|
|
_accountToActivate = -1;
|
2020-06-17 09:36:25 +00:00
|
|
|
_active = nullptr;
|
|
|
|
base::take(_accounts);
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:07:56 +00:00
|
|
|
void Domain::suggestExportIfNeeded() {
|
|
|
|
Expects(started());
|
|
|
|
|
|
|
|
for (const auto &[index, account] : _accounts) {
|
|
|
|
if (const auto session = account->maybeSession()) {
|
|
|
|
const auto settings = session->local().readExportSettings();
|
|
|
|
if (const auto availableAt = settings.availableAt) {
|
|
|
|
session->data().suggestStartExport(availableAt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Domain::accountAddedInStorage(AccountWithIndex accountWithIndex) {
|
|
|
|
Expects(accountWithIndex.account != nullptr);
|
2020-06-16 09:40:43 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
for (const auto &[index, _] : _accounts) {
|
|
|
|
if (index == accountWithIndex.index) {
|
|
|
|
Unexpected("Repeated account index.");
|
|
|
|
}
|
2020-06-16 09:40:43 +00:00
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
_accounts.push_back(std::move(accountWithIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Domain::activateFromStorage(int index) {
|
|
|
|
_accountToActivate = index;
|
|
|
|
}
|
2020-06-16 09:40:43 +00:00
|
|
|
|
2020-06-19 17:13:43 +00:00
|
|
|
int Domain::activeForStorage() const {
|
|
|
|
return _accountToActivate;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::resetWithForgottenPasscode() {
|
2020-06-16 09:40:43 +00:00
|
|
|
if (_accounts.empty()) {
|
|
|
|
_local->startFromScratch();
|
|
|
|
activateAfterStarting();
|
|
|
|
} else {
|
|
|
|
for (const auto &[index, account] : _accounts) {
|
|
|
|
account->logOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::activateAfterStarting() {
|
2020-06-16 09:40:43 +00:00
|
|
|
Expects(started());
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
auto toActivate = _accounts.front().account.get();
|
2020-06-16 09:40:43 +00:00
|
|
|
for (const auto &[index, account] : _accounts) {
|
2020-06-19 12:59:31 +00:00
|
|
|
if (index == _accountToActivate) {
|
|
|
|
toActivate = account.get();
|
|
|
|
}
|
2020-06-16 09:40:43 +00:00
|
|
|
watchSession(account.get());
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
activate(toActivate);
|
2020-06-16 14:19:23 +00:00
|
|
|
removePasscodeIfEmpty();
|
2020-06-16 09:40:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
const std::vector<Domain::AccountWithIndex> &Domain::accounts() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return _accounts;
|
|
|
|
}
|
|
|
|
|
2022-06-02 10:47:57 +00:00
|
|
|
std::vector<not_null<Account*>> Domain::orderedAccounts() const {
|
|
|
|
const auto order = Core::App().settings().accountsOrder();
|
|
|
|
auto accounts = ranges::views::all(
|
|
|
|
_accounts
|
|
|
|
) | ranges::views::transform([](const Domain::AccountWithIndex &a) {
|
|
|
|
return not_null{ a.account.get() };
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
ranges::stable_sort(accounts, [&](
|
|
|
|
not_null<Account*> a,
|
|
|
|
not_null<Account*> b) {
|
|
|
|
const auto aIt = a->sessionExists()
|
|
|
|
? ranges::find(order, a->session().uniqueId())
|
|
|
|
: end(order);
|
|
|
|
const auto bIt = b->sessionExists()
|
|
|
|
? ranges::find(order, b->session().uniqueId())
|
|
|
|
: end(order);
|
|
|
|
return aIt < bIt;
|
|
|
|
});
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:01:41 +00:00
|
|
|
rpl::producer<> Domain::accountsChanges() const {
|
|
|
|
return _accountsChanges.events();
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:55:08 +00:00
|
|
|
Account *Domain::maybeLastOrSomeAuthedAccount() {
|
|
|
|
auto result = (Account*)nullptr;
|
|
|
|
for (const auto &[index, account] : _accounts) {
|
|
|
|
if (!account->sessionExists()) {
|
|
|
|
continue;
|
|
|
|
} else if (index == _lastActiveIndex) {
|
|
|
|
return account.get();
|
|
|
|
} else if (!result) {
|
|
|
|
result = account.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
rpl::producer<Account*> Domain::activeValue() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return _active.value();
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
Account &Domain::active() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
Expects(!_accounts.empty());
|
|
|
|
|
|
|
|
Ensures(_active.current() != nullptr);
|
|
|
|
return *_active.current();
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:55:08 +00:00
|
|
|
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
rpl::producer<not_null<Account*>> Domain::activeChanges() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return _active.changes() | rpl::map([](Account *value) {
|
|
|
|
return not_null{ value };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
rpl::producer<Session*> Domain::activeSessionChanges() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return _activeSessions.events();
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
rpl::producer<Session*> Domain::activeSessionValue() const {
|
2020-06-23 13:29:02 +00:00
|
|
|
const auto current = _accounts.empty()
|
2020-06-15 16:25:02 +00:00
|
|
|
? nullptr
|
2020-06-23 13:29:02 +00:00
|
|
|
: active().maybeSession();
|
2020-06-15 16:25:02 +00:00
|
|
|
return rpl::single(current) | rpl::then(_activeSessions.events());
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
int Domain::unreadBadge() const {
|
2020-06-18 11:17:58 +00:00
|
|
|
return _unreadBadge;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
bool Domain::unreadBadgeMuted() const {
|
2020-06-18 11:17:58 +00:00
|
|
|
return _unreadBadgeMuted;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
rpl::producer<> Domain::unreadBadgeChanges() const {
|
2020-06-18 11:17:58 +00:00
|
|
|
return _unreadBadgeChanges.events();
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::notifyUnreadBadgeChanged() {
|
2020-06-18 11:17:58 +00:00
|
|
|
for (const auto &[index, account] : _accounts) {
|
2020-06-23 13:29:02 +00:00
|
|
|
if (const auto session = account->maybeSession()) {
|
|
|
|
session->data().notifyUnreadBadgeChanged();
|
2020-06-18 11:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::updateUnreadBadge() {
|
2020-06-18 11:17:58 +00:00
|
|
|
_unreadBadge = 0;
|
|
|
|
_unreadBadgeMuted = true;
|
|
|
|
for (const auto &[index, account] : _accounts) {
|
2020-06-23 13:29:02 +00:00
|
|
|
if (const auto session = account->maybeSession()) {
|
|
|
|
const auto data = &session->data();
|
2020-06-18 11:17:58 +00:00
|
|
|
_unreadBadge += data->unreadBadge();
|
|
|
|
if (!data->unreadBadgeMuted()) {
|
|
|
|
_unreadBadgeMuted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_unreadBadgeChanges.fire({});
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::scheduleUpdateUnreadBadge() {
|
2020-06-18 11:17:58 +00:00
|
|
|
if (_unreadBadgeUpdateScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_unreadBadgeUpdateScheduled = true;
|
|
|
|
Core::App().postponeCall(crl::guard(&Core::App(), [=] {
|
|
|
|
_unreadBadgeUpdateScheduled = false;
|
|
|
|
updateUnreadBadge();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
not_null<Main::Account*> Domain::add(MTP::Environment environment) {
|
|
|
|
Expects(started());
|
2022-05-31 12:32:41 +00:00
|
|
|
Expects(_accounts.size() < kPremiumMaxAccounts);
|
2020-06-17 09:36:25 +00:00
|
|
|
|
|
|
|
static const auto cloneConfig = [](const MTP::Config &config) {
|
|
|
|
return std::make_unique<MTP::Config>(config);
|
|
|
|
};
|
2020-06-29 18:14:16 +00:00
|
|
|
auto mainDcId = MTP::Instance::Fields::kNotSetMainDc;
|
|
|
|
const auto accountConfig = [&](not_null<Account*> account) {
|
|
|
|
mainDcId = account->mtp().mainDcId();
|
2020-06-17 09:36:25 +00:00
|
|
|
return cloneConfig(account->mtp().config());
|
|
|
|
};
|
|
|
|
auto config = [&] {
|
|
|
|
if (_active.current()->mtp().environment() == environment) {
|
|
|
|
return accountConfig(_active.current());
|
|
|
|
}
|
2020-06-18 18:04:16 +00:00
|
|
|
for (const auto &[index, account] : _accounts) {
|
2020-06-17 09:36:25 +00:00
|
|
|
if (account->mtp().environment() == environment) {
|
|
|
|
return accountConfig(account.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (environment == MTP::Environment::Production)
|
|
|
|
? cloneConfig(Core::App().fallbackProductionConfig())
|
|
|
|
: std::make_unique<MTP::Config>(environment);
|
|
|
|
}();
|
2020-06-15 16:25:02 +00:00
|
|
|
auto index = 0;
|
2020-06-19 12:59:31 +00:00
|
|
|
while (ranges::contains(_accounts, index, &AccountWithIndex::index)) {
|
2020-06-15 16:25:02 +00:00
|
|
|
++index;
|
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
_accounts.push_back(AccountWithIndex{
|
|
|
|
.index = index,
|
|
|
|
.account = std::make_unique<Account>(this, _dataName, index)
|
|
|
|
});
|
|
|
|
const auto account = _accounts.back().account.get();
|
2020-06-29 18:14:16 +00:00
|
|
|
account->setMtpMainDcId(mainDcId);
|
2020-06-17 09:36:25 +00:00
|
|
|
_local->startAdded(account, std::move(config));
|
2020-06-16 09:40:43 +00:00
|
|
|
watchSession(account);
|
2020-06-22 13:01:41 +00:00
|
|
|
_accountsChanges.fire({});
|
2020-07-02 18:11:45 +00:00
|
|
|
|
|
|
|
auto &settings = Core::App().settings();
|
|
|
|
if (_accounts.size() == 2 && !settings.mainMenuAccountsShown()) {
|
|
|
|
settings.setMainMenuAccountsShown(true);
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
return account;
|
2020-06-15 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:16:05 +00:00
|
|
|
void Domain::addActivated(MTP::Environment environment) {
|
2022-05-31 12:32:41 +00:00
|
|
|
if (accounts().size() < maxAccounts()) {
|
2020-06-30 17:16:05 +00:00
|
|
|
activate(add(environment));
|
|
|
|
} else {
|
|
|
|
for (auto &[index, account] : accounts()) {
|
|
|
|
if (!account->sessionExists()
|
|
|
|
&& account->mtp().environment() == environment) {
|
|
|
|
activate(account.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::watchSession(not_null<Account*> account) {
|
2020-06-18 11:17:58 +00:00
|
|
|
account->sessionValue(
|
|
|
|
) | rpl::filter([=](Session *session) {
|
|
|
|
return session != nullptr;
|
|
|
|
}) | rpl::start_with_next([=](Session *session) {
|
|
|
|
session->data().unreadBadgeChanges(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
scheduleUpdateUnreadBadge();
|
|
|
|
}, session->lifetime());
|
2022-06-02 10:54:10 +00:00
|
|
|
|
|
|
|
Data::AmPremiumValue(
|
|
|
|
session
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_lastMaxAccounts = maxAccounts();
|
|
|
|
}, session->lifetime());
|
2020-06-18 11:17:58 +00:00
|
|
|
}, account->lifetime());
|
|
|
|
|
2020-06-16 09:40:43 +00:00
|
|
|
account->sessionChanges(
|
|
|
|
) | rpl::filter([=](Session *session) {
|
2020-06-18 11:17:58 +00:00
|
|
|
return !session;
|
2020-06-17 09:36:25 +00:00
|
|
|
}) | rpl::start_with_next([=] {
|
2020-06-18 11:17:58 +00:00
|
|
|
scheduleUpdateUnreadBadge();
|
2020-06-16 09:40:43 +00:00
|
|
|
if (account == _active.current()) {
|
|
|
|
activateAuthedAccount();
|
|
|
|
}
|
|
|
|
crl::on_main(&Core::App(), [=] {
|
|
|
|
removeRedundantAccounts();
|
|
|
|
});
|
|
|
|
}, account->lifetime());
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::activateAuthedAccount() {
|
2020-06-16 09:40:43 +00:00
|
|
|
Expects(started());
|
|
|
|
|
|
|
|
if (_active.current()->sessionExists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto i = _accounts.begin(); i != _accounts.end(); ++i) {
|
2020-06-19 12:59:31 +00:00
|
|
|
if (i->account->sessionExists()) {
|
|
|
|
activate(i->account.get());
|
2020-06-16 09:40:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
bool Domain::removePasscodeIfEmpty() {
|
2020-06-16 14:19:23 +00:00
|
|
|
if (_accounts.size() != 1 || _active.current()->sessionExists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Local::reset();
|
2021-09-30 15:37:42 +00:00
|
|
|
|
|
|
|
// We completely logged out, remove the passcode if it was there.
|
|
|
|
if (Core::App().passcodeLocked()) {
|
|
|
|
Core::App().unlockPasscode();
|
|
|
|
}
|
2021-05-25 20:38:01 +00:00
|
|
|
if (!_local->hasLocalPasscode()) {
|
2020-06-16 14:19:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_local->setPasscode(QByteArray());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::removeRedundantAccounts() {
|
2020-06-16 09:40:43 +00:00
|
|
|
Expects(started());
|
|
|
|
|
|
|
|
const auto was = _accounts.size();
|
|
|
|
activateAuthedAccount();
|
|
|
|
for (auto i = _accounts.begin(); i != _accounts.end();) {
|
2020-06-19 12:59:31 +00:00
|
|
|
if (i->account.get() == _active.current()
|
|
|
|
|| i->account->sessionExists()) {
|
2020-06-16 09:40:43 +00:00
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
checkForLastProductionConfig(i->account.get());
|
2020-06-16 09:40:43 +00:00
|
|
|
i = _accounts.erase(i);
|
|
|
|
}
|
2020-06-16 14:19:23 +00:00
|
|
|
|
|
|
|
if (!removePasscodeIfEmpty() && _accounts.size() != was) {
|
2020-06-16 09:40:43 +00:00
|
|
|
scheduleWriteAccounts();
|
2020-06-22 13:01:41 +00:00
|
|
|
_accountsChanges.fire({});
|
2020-06-16 09:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::checkForLastProductionConfig(
|
2020-06-17 09:36:25 +00:00
|
|
|
not_null<Main::Account*> account) {
|
|
|
|
const auto mtp = &account->mtp();
|
|
|
|
if (mtp->environment() != MTP::Environment::Production) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto &[index, other] : _accounts) {
|
|
|
|
if (other.get() != account
|
|
|
|
&& other->mtp().environment() == MTP::Environment::Production) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Core::App().refreshFallbackProductionConfig(mtp->config());
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:08:16 +00:00
|
|
|
void Domain::maybeActivate(not_null<Main::Account*> account) {
|
|
|
|
Core::App().preventOrInvoke(crl::guard(account, [=] {
|
|
|
|
activate(account);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Domain::activate(not_null<Main::Account*> account) {
|
|
|
|
if (_active.current() == account.get()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto i = ranges::find(_accounts, account.get(), [](
|
|
|
|
const AccountWithIndex &value) {
|
|
|
|
return value.account.get();
|
|
|
|
});
|
|
|
|
Assert(i != end(_accounts));
|
|
|
|
const auto changed = (_accountToActivate != i->index);
|
2020-06-26 10:36:52 +00:00
|
|
|
auto wasAuthed = false;
|
2020-06-15 16:25:02 +00:00
|
|
|
|
|
|
|
_activeLifetime.destroy();
|
2020-06-23 14:55:08 +00:00
|
|
|
if (_active.current()) {
|
|
|
|
_lastActiveIndex = _accountToActivate;
|
2020-06-26 10:36:52 +00:00
|
|
|
wasAuthed = _active.current()->sessionExists();
|
2020-06-23 14:55:08 +00:00
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
_accountToActivate = i->index;
|
|
|
|
_active = account.get();
|
2020-06-15 16:25:02 +00:00
|
|
|
_active.current()->sessionValue(
|
|
|
|
) | rpl::start_to_stream(_activeSessions, _activeLifetime);
|
2020-06-16 06:42:47 +00:00
|
|
|
|
2020-06-18 11:17:58 +00:00
|
|
|
if (changed) {
|
2020-06-26 10:36:52 +00:00
|
|
|
if (wasAuthed) {
|
|
|
|
scheduleWriteAccounts();
|
|
|
|
} else {
|
|
|
|
crl::on_main(&Core::App(), [=] {
|
|
|
|
removeRedundantAccounts();
|
|
|
|
});
|
|
|
|
}
|
2020-06-18 11:17:58 +00:00
|
|
|
}
|
2020-06-16 09:40:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
void Domain::scheduleWriteAccounts() {
|
2020-06-16 09:40:43 +00:00
|
|
|
if (_writeAccountsScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_writeAccountsScheduled = true;
|
2020-06-16 06:42:47 +00:00
|
|
|
crl::on_main(&Core::App(), [=] {
|
2020-06-16 09:40:43 +00:00
|
|
|
_writeAccountsScheduled = false;
|
2020-06-16 06:42:47 +00:00
|
|
|
_local->writeAccounts();
|
|
|
|
});
|
2020-06-15 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 12:32:41 +00:00
|
|
|
int Domain::maxAccounts() const {
|
2022-06-02 08:27:00 +00:00
|
|
|
const auto premiumCount = ranges::count_if(accounts(), [](
|
2022-05-31 12:32:41 +00:00
|
|
|
const Main::Domain::AccountWithIndex &d) {
|
2022-06-02 08:27:00 +00:00
|
|
|
return d.account->sessionExists() && d.account->session().premium();
|
2022-05-31 12:32:41 +00:00
|
|
|
});
|
2022-06-02 08:27:00 +00:00
|
|
|
return std::min(int(premiumCount) + kMaxAccounts, kPremiumMaxAccounts);
|
2022-05-31 12:32:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 10:54:10 +00:00
|
|
|
rpl::producer<int> Domain::maxAccountsChanges() const {
|
|
|
|
return _lastMaxAccounts.changes();
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
} // namespace Main
|