2017-02-23 09:32:28 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-02-23 09:32:28 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-02-23 09:32:28 +00:00
|
|
|
*/
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2017-02-23 09:32:28 +00:00
|
|
|
|
2017-04-06 19:02:40 +00:00
|
|
|
#include "apiwrap.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2017-12-29 18:17:07 +00:00
|
|
|
#include "core/changelogs.h"
|
2019-06-05 20:41:51 +00:00
|
|
|
#include "main/main_account.h"
|
2019-08-23 13:52:59 +00:00
|
|
|
#include "chat_helpers/stickers_emoji_pack.h"
|
2020-03-04 09:21:19 +00:00
|
|
|
#include "chat_helpers/stickers_dice_pack.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "storage/file_download.h"
|
2019-12-05 08:32:33 +00:00
|
|
|
#include "storage/download_manager_mtproto.h"
|
2017-08-04 14:54:32 +00:00
|
|
|
#include "storage/file_upload.h"
|
2017-04-09 18:06:06 +00:00
|
|
|
#include "storage/localstorage.h"
|
2017-09-04 11:40:02 +00:00
|
|
|
#include "storage/storage_facade.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2017-12-29 18:17:07 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2019-08-23 13:52:59 +00:00
|
|
|
//#include "platform/platform_specific.h"
|
2017-04-19 09:44:07 +00:00
|
|
|
#include "calls/calls_instance.h"
|
2018-11-15 15:36:04 +00:00
|
|
|
#include "support/support_helper.h"
|
2018-09-11 14:36:09 +00:00
|
|
|
#include "observer_peer.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2017-04-15 18:48:54 +00:00
|
|
|
|
2020-02-07 22:42:01 +00:00
|
|
|
#ifndef TDESKTOP_DISABLE_SPELLCHECK
|
|
|
|
#include "chat_helpers/spellchecker_common.h"
|
|
|
|
#endif // TDESKTOP_DISABLE_SPELLCHECK
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
namespace Main {
|
2017-04-15 18:48:54 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-11-04 16:51:59 +00:00
|
|
|
constexpr auto kLegacyCallsPeerToPeerNobody = 4;
|
2017-04-15 18:48:54 +00:00
|
|
|
|
|
|
|
} // namespace
|
2017-02-23 09:32:28 +00:00
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Session::Session(
|
2019-06-05 20:41:51 +00:00
|
|
|
not_null<Main::Account*> account,
|
2019-10-02 10:46:02 +00:00
|
|
|
const MTPUser &user,
|
|
|
|
Settings &&settings)
|
2019-06-05 20:41:51 +00:00
|
|
|
: _account(account)
|
2019-10-02 10:46:02 +00:00
|
|
|
, _settings(std::move(settings))
|
2019-08-23 13:52:59 +00:00
|
|
|
, _saveSettingsTimer([=] { Local::writeUserSettings(); })
|
2017-06-05 13:33:45 +00:00
|
|
|
, _api(std::make_unique<ApiWrap>(this))
|
2019-08-06 16:40:08 +00:00
|
|
|
, _calls(std::make_unique<Calls::Instance>(this))
|
2019-12-05 08:32:33 +00:00
|
|
|
, _downloader(std::make_unique<Storage::DownloadManagerMtproto>(_api.get()))
|
2019-04-12 10:50:41 +00:00
|
|
|
, _uploader(std::make_unique<Storage::Uploader>(_api.get()))
|
2017-09-04 11:40:02 +00:00
|
|
|
, _storage(std::make_unique<Storage::Facade>())
|
2017-12-29 18:17:07 +00:00
|
|
|
, _notifications(std::make_unique<Window::Notifications::System>(this))
|
2018-02-18 13:26:28 +00:00
|
|
|
, _data(std::make_unique<Data::Session>(this))
|
2019-01-18 12:27:37 +00:00
|
|
|
, _user(_data->processUser(user))
|
2019-08-01 14:13:02 +00:00
|
|
|
, _emojiStickersPack(std::make_unique<Stickers::EmojiPack>(this))
|
2020-04-06 09:07:49 +00:00
|
|
|
, _diceStickersPacks(std::make_unique<Stickers::DicePacks>(this))
|
2018-10-02 20:39:54 +00:00
|
|
|
, _changelogs(Core::Changelogs::Create(this))
|
2019-01-03 12:36:01 +00:00
|
|
|
, _supportHelper(Support::Helper::Create(this)) {
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().lockChanges(
|
2018-06-03 13:30:40 +00:00
|
|
|
) | rpl::start_with_next([=] {
|
2017-04-15 18:48:54 +00:00
|
|
|
notifications().updateAll();
|
2018-06-03 13:30:40 +00:00
|
|
|
}, _lifetime);
|
2018-05-11 14:03:53 +00:00
|
|
|
subscribe(Global::RefConnectionTypeChanged(), [=] {
|
2020-04-24 10:31:28 +00:00
|
|
|
_api->refreshTopPromotion();
|
2018-05-11 14:03:53 +00:00
|
|
|
});
|
2020-04-24 10:31:28 +00:00
|
|
|
_api->refreshTopPromotion();
|
2018-06-03 13:30:40 +00:00
|
|
|
_api->requestTermsUpdate();
|
2018-09-11 12:50:40 +00:00
|
|
|
_api->requestFullPeer(_user);
|
2018-05-15 16:38:27 +00:00
|
|
|
|
2018-09-11 14:36:09 +00:00
|
|
|
crl::on_main(this, [=] {
|
|
|
|
using Flag = Notify::PeerUpdate::Flag;
|
|
|
|
const auto events = Flag::NameChanged
|
|
|
|
| Flag::UsernameChanged
|
|
|
|
| Flag::PhotoChanged
|
|
|
|
| Flag::AboutChanged
|
|
|
|
| Flag::UserPhoneChanged;
|
|
|
|
subscribe(
|
|
|
|
Notify::PeerUpdated(),
|
|
|
|
Notify::PeerUpdatedHandler(
|
|
|
|
events,
|
|
|
|
[=](const Notify::PeerUpdate &update) {
|
|
|
|
if (update.peer == _user) {
|
|
|
|
Local::writeSelf();
|
|
|
|
}
|
|
|
|
}));
|
2019-10-02 10:46:02 +00:00
|
|
|
|
|
|
|
if (_settings.hadLegacyCallsPeerToPeerNobody()) {
|
|
|
|
api().savePrivacy(
|
|
|
|
MTP_inputPrivacyKeyPhoneP2P(),
|
|
|
|
QVector<MTPInputPrivacyRule>(
|
|
|
|
1,
|
|
|
|
MTP_inputPrivacyValueDisallowAll()));
|
|
|
|
saveSettingsDelayed();
|
|
|
|
}
|
2018-09-11 14:36:09 +00:00
|
|
|
});
|
|
|
|
|
2017-12-29 18:17:07 +00:00
|
|
|
Window::Theme::Background()->start();
|
2020-02-07 22:42:01 +00:00
|
|
|
|
|
|
|
#ifndef TDESKTOP_DISABLE_SPELLCHECK
|
|
|
|
Spellchecker::Start(this);
|
|
|
|
#endif // TDESKTOP_DISABLE_SPELLCHECK
|
2017-02-23 09:32:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Session::~Session() {
|
2019-06-05 20:41:51 +00:00
|
|
|
ClickHandler::clearActive();
|
|
|
|
ClickHandler::unpressed();
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Main::Account &Session::account() const {
|
2019-06-05 20:41:51 +00:00
|
|
|
return *_account;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
bool Session::Exists() {
|
2019-06-05 20:41:51 +00:00
|
|
|
return Core::IsAppLaunched()
|
|
|
|
&& Core::App().activeAccount().sessionExists();
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
base::Observable<void> &Session::downloaderTaskFinished() {
|
2017-08-04 14:54:32 +00:00
|
|
|
return downloader().taskFinished();
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
UserId Session::userId() const {
|
2019-01-04 11:09:48 +00:00
|
|
|
return _user->bareId();
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
PeerId Session::userPeerId() const {
|
2019-01-04 11:09:48 +00:00
|
|
|
return _user->id;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
bool Session::validateSelf(const MTPUser &user) {
|
2018-05-13 08:42:25 +00:00
|
|
|
if (user.type() != mtpc_user || !user.c_user().is_self()) {
|
|
|
|
LOG(("API Error: bad self user received."));
|
|
|
|
return false;
|
2019-07-05 13:38:38 +00:00
|
|
|
} else if (user.c_user().vid().v != userId()) {
|
2017-03-04 19:36:59 +00:00
|
|
|
LOG(("Auth Error: wrong self user received."));
|
2019-07-24 08:46:23 +00:00
|
|
|
crl::on_main(this, [=] { _account->logOut(); });
|
2017-03-04 19:36:59 +00:00
|
|
|
return false;
|
2017-02-23 09:32:28 +00:00
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
return true;
|
2017-02-23 09:32:28 +00:00
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
void Session::saveSettingsDelayed(crl::time delay) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Expects(this == &Auth());
|
2018-05-13 08:42:25 +00:00
|
|
|
|
2019-08-23 13:52:59 +00:00
|
|
|
_saveSettingsTimer.callOnce(delay);
|
2017-04-09 18:06:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
not_null<MTP::Instance*> Session::mtp() {
|
2019-07-24 08:46:23 +00:00
|
|
|
return _account->mtp();
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
void Session::termsDeleteNow() {
|
2019-03-27 08:37:25 +00:00
|
|
|
api().request(MTPaccount_DeleteAccount(
|
|
|
|
MTP_string("Decline ToS update")
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
bool Session::supportMode() const {
|
2018-11-15 15:36:04 +00:00
|
|
|
return (_supportHelper != nullptr);
|
2018-10-02 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Support::Helper &Session::supportHelper() const {
|
2018-10-02 20:39:54 +00:00
|
|
|
Expects(supportMode());
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
return *_supportHelper;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Support::Templates& Session::supportTemplates() const {
|
2018-11-15 15:36:04 +00:00
|
|
|
return supportHelper().templates();
|
2018-09-28 11:56:21 +00:00
|
|
|
}
|
2019-07-24 11:45:24 +00:00
|
|
|
|
2019-12-27 14:09:55 +00:00
|
|
|
void Session::saveSettingsNowIfNeeded() {
|
|
|
|
if (_saveSettingsTimer.isActive()) {
|
|
|
|
_saveSettingsTimer.cancel();
|
|
|
|
Local::writeUserSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
} // namespace Main
|
|
|
|
|
|
|
|
Main::Session &Auth() {
|
|
|
|
return Core::App().activeAccount().session();
|
|
|
|
}
|