tdesktop/Telegram/SourceFiles/main/main_domain.cpp

442 lines
11 KiB
C++
Raw Normal View History

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
*/
#include "main/main_domain.h"
2020-06-15 16:25:02 +00:00
#include "core/application.h"
#include "core/shortcuts.h"
#include "core/crash_reports.h"
2020-06-15 16:25:02 +00:00
#include "main/main_account.h"
#include "main/main_session.h"
#include "data/data_session.h"
#include "data/data_changes.h"
#include "data/data_user.h"
#include "mtproto/mtproto_config.h"
#include "mtproto/mtproto_dc_options.h"
#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"
#include "window/notifications_manager.h"
2020-06-16 14:19:23 +00:00
#include "facades.h"
2020-06-15 16:25:02 +00:00
namespace Main {
Domain::Domain(const QString &dataName)
2020-06-15 16:25:02 +00:00
: _dataName(dataName)
, _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([] {
// In case we had a legacy passcoded app we start settings here.
Core::App().startSettingsAndBackground();
Core::App().notifications().createManager();
2020-06-29 16:07:56 +00:00
}, _lifetime);
_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
}
Domain::~Domain() = default;
2020-06-15 16:25:02 +00:00
bool Domain::started() const {
2020-06-15 16:25:02 +00:00
return !_accounts.empty();
}
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) {
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;
}
void Domain::finish() {
_accountToActivate = -1;
_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);
}
}
}
}
void Domain::accountAddedInStorage(AccountWithIndex accountWithIndex) {
Expects(accountWithIndex.account != nullptr);
2020-06-16 09:40:43 +00:00
for (const auto &[index, _] : _accounts) {
if (index == accountWithIndex.index) {
Unexpected("Repeated account index.");
}
2020-06-16 09:40:43 +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;
}
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();
}
}
}
void Domain::activateAfterStarting() {
2020-06-16 09:40:43 +00:00
Expects(started());
auto toActivate = _accounts.front().account.get();
2020-06-16 09:40:43 +00:00
for (const auto &[index, account] : _accounts) {
if (index == _accountToActivate) {
toActivate = account.get();
}
2020-06-16 09:40:43 +00:00
watchSession(account.get());
}
activate(toActivate);
2020-06-16 14:19:23 +00:00
removePasscodeIfEmpty();
2020-06-16 09:40:43 +00:00
}
const std::vector<Domain::AccountWithIndex> &Domain::accounts() const {
2020-06-15 16:25:02 +00:00
return _accounts;
}
rpl::producer<> Domain::accountsChanges() const {
return _accountsChanges.events();
}
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;
}
rpl::producer<Account*> Domain::activeValue() const {
2020-06-15 16:25:02 +00:00
return _active.value();
}
Account &Domain::active() const {
2020-06-15 16:25:02 +00:00
Expects(!_accounts.empty());
Ensures(_active.current() != nullptr);
return *_active.current();
}
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 };
});
}
rpl::producer<Session*> Domain::activeSessionChanges() const {
2020-06-15 16:25:02 +00:00
return _activeSessions.events();
}
rpl::producer<Session*> Domain::activeSessionValue() const {
const auto current = _accounts.empty()
2020-06-15 16:25:02 +00:00
? nullptr
: active().maybeSession();
2020-06-15 16:25:02 +00:00
return rpl::single(current) | rpl::then(_activeSessions.events());
}
int Domain::unreadBadge() const {
return _unreadBadge;
}
bool Domain::unreadBadgeMuted() const {
return _unreadBadgeMuted;
}
rpl::producer<> Domain::unreadBadgeChanges() const {
return _unreadBadgeChanges.events();
}
void Domain::notifyUnreadBadgeChanged() {
for (const auto &[index, account] : _accounts) {
if (const auto session = account->maybeSession()) {
session->data().notifyUnreadBadgeChanged();
}
}
}
void Domain::updateUnreadBadge() {
_unreadBadge = 0;
_unreadBadgeMuted = true;
for (const auto &[index, account] : _accounts) {
if (const auto session = account->maybeSession()) {
const auto data = &session->data();
_unreadBadge += data->unreadBadge();
if (!data->unreadBadgeMuted()) {
_unreadBadgeMuted = false;
}
}
}
_unreadBadgeChanges.fire({});
}
void Domain::scheduleUpdateUnreadBadge() {
if (_unreadBadgeUpdateScheduled) {
return;
}
_unreadBadgeUpdateScheduled = true;
Core::App().postponeCall(crl::guard(&Core::App(), [=] {
_unreadBadgeUpdateScheduled = false;
updateUnreadBadge();
}));
}
not_null<Main::Account*> Domain::add(MTP::Environment environment) {
Expects(started());
Expects(_accounts.size() < kMaxAccounts);
static const auto cloneConfig = [](const MTP::Config &config) {
return std::make_unique<MTP::Config>(config);
};
auto mainDcId = MTP::Instance::Fields::kNotSetMainDc;
const auto accountConfig = [&](not_null<Account*> account) {
mainDcId = account->mtp().mainDcId();
return cloneConfig(account->mtp().config());
};
auto config = [&] {
if (_active.current()->mtp().environment() == environment) {
return accountConfig(_active.current());
}
for (const auto &[index, account] : _accounts) {
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;
while (ranges::contains(_accounts, index, &AccountWithIndex::index)) {
2020-06-15 16:25:02 +00:00
++index;
}
_accounts.push_back(AccountWithIndex{
.index = index,
.account = std::make_unique<Account>(this, _dataName, index)
});
const auto account = _accounts.back().account.get();
account->setMtpMainDcId(mainDcId);
_local->startAdded(account, std::move(config));
2020-06-16 09:40:43 +00:00
watchSession(account);
_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();
}
return account;
2020-06-15 16:25:02 +00:00
}
void Domain::addActivated(MTP::Environment environment) {
if (accounts().size() < Main::Domain::kMaxAccounts) {
activate(add(environment));
} else {
for (auto &[index, account] : accounts()) {
if (!account->sessionExists()
&& account->mtp().environment() == environment) {
activate(account.get());
break;
}
}
}
}
void Domain::watchSession(not_null<Account*> account) {
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());
}, account->lifetime());
2020-06-16 09:40:43 +00:00
account->sessionChanges(
) | rpl::filter([=](Session *session) {
return !session;
}) | rpl::start_with_next([=] {
scheduleUpdateUnreadBadge();
2020-06-16 09:40:43 +00:00
if (account == _active.current()) {
activateAuthedAccount();
}
crl::on_main(&Core::App(), [=] {
removeRedundantAccounts();
});
}, account->lifetime());
}
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) {
if (i->account->sessionExists()) {
activate(i->account.get());
2020-06-16 09:40:43 +00:00
return;
}
}
}
bool Domain::removePasscodeIfEmpty() {
2020-06-16 14:19:23 +00:00
if (_accounts.size() != 1 || _active.current()->sessionExists()) {
return false;
}
Local::reset();
// We completely logged out, remove the passcode if it was there.
if (Core::App().passcodeLocked()) {
Core::App().unlockPasscode();
}
if (!_local->hasLocalPasscode()) {
2020-06-16 14:19:23 +00:00
return false;
}
_local->setPasscode(QByteArray());
return true;
}
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();) {
if (i->account.get() == _active.current()
|| i->account->sessionExists()) {
2020-06-16 09:40:43 +00:00
++i;
continue;
}
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();
_accountsChanges.fire({});
2020-06-16 09:40:43 +00:00
}
}
void Domain::checkForLastProductionConfig(
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());
}
void Domain::maybeActivate(not_null<Main::Account*> account) {
Core::App().preventOrInvoke(crl::guard(account, [=] {
activate(account);
}));
}
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);
auto wasAuthed = false;
2020-06-15 16:25:02 +00:00
_activeLifetime.destroy();
if (_active.current()) {
_lastActiveIndex = _accountToActivate;
wasAuthed = _active.current()->sessionExists();
}
_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
if (changed) {
if (wasAuthed) {
scheduleWriteAccounts();
} else {
crl::on_main(&Core::App(), [=] {
removeRedundantAccounts();
});
}
}
2020-06-16 09:40:43 +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
}
} // namespace Main