diff --git a/Telegram/SourceFiles/boxes/dictionaries_manager.cpp b/Telegram/SourceFiles/boxes/dictionaries_manager.cpp index 1d89402514..7712b66769 100644 --- a/Telegram/SourceFiles/boxes/dictionaries_manager.cpp +++ b/Telegram/SourceFiles/boxes/dictionaries_manager.cpp @@ -46,13 +46,18 @@ using QStringView = QString; class Inner : public Ui::RpWidget { public: - Inner(QWidget *parent, Dictionaries enabledDictionaries); + Inner( + QWidget *parent, + not_null session, + Dictionaries enabledDictionaries); Dictionaries enabledRows() const; QueryCallback queryCallback() const; private: - void setupContent(Dictionaries enabledDictionaries); + void setupContent( + not_null session, + Dictionaries enabledDictionaries); Dictionaries _enabledRows; QueryCallback _queryCallback; @@ -96,8 +101,10 @@ auto CreateMultiSelect(QWidget *parent) { Inner::Inner( QWidget *parent, - Dictionaries enabledDictionaries) : RpWidget(parent) { - setupContent(std::move(enabledDictionaries)); + not_null session, + Dictionaries enabledDictionaries) +: RpWidget(parent) { + setupContent(session, std::move(enabledDictionaries)); } QueryCallback Inner::queryCallback() const { @@ -110,6 +117,7 @@ Dictionaries Inner::enabledRows() const { auto AddButtonWithLoader( not_null content, + not_null session, const Spellchecker::Dict &dict, bool buttonEnabled, rpl::producer query) { @@ -288,6 +296,7 @@ auto AddButtonWithLoader( const auto weak = Ui::MakeWeak(button); setLocalLoader(base::make_unique_q( App::main(), + session, id, Spellchecker::GetDownloadLocation(id), Spellchecker::DictPathByLangId(id), @@ -336,7 +345,9 @@ auto AddButtonWithLoader( return button; } -void Inner::setupContent(Dictionaries enabledDictionaries) { +void Inner::setupContent( + not_null session, + Dictionaries enabledDictionaries) { const auto content = Ui::CreateChild(this); const auto queryStream = content->lifetime() @@ -346,6 +357,7 @@ void Inner::setupContent(Dictionaries enabledDictionaries) { const auto id = dict.id; const auto row = AddButtonWithLoader( content, + session, dict, ranges::contains(enabledDictionaries, id), queryStream->events()); @@ -389,6 +401,7 @@ void ManageDictionariesBox::prepare() { const auto inner = setInnerWidget( object_ptr( this, + _session, _session->settings().dictionariesEnabled()), st::boxScroll, multiSelect->height() diff --git a/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp b/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp index ea94dcb54a..b53efb2afc 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.cpp @@ -51,7 +51,7 @@ using SetState = BlobState; class Loader final : public BlobLoader { public: Loader( - QObject *parent, + not_null session, int id, MTP::DedicatedLoader::Location location, const QString &folder, @@ -67,16 +67,18 @@ private: class Inner : public Ui::RpWidget { public: - Inner(QWidget *parent); + Inner(QWidget *parent, not_null session); private: void setupContent(); + const not_null _session; + }; class Row : public Ui::RippleButton { public: - Row(QWidget *widget, const Set &set); + Row(QWidget *widget, not_null session, const Set &set); protected: void paintEvent(QPaintEvent *e) override; @@ -98,6 +100,7 @@ private: void radialAnimationCallback(crl::time now); void updateLoadingToFinished(); + const not_null _session; int _id = 0; bool _switching = false; rpl::variable _state; @@ -160,12 +163,12 @@ bool UnpackSet(const QString &path, const QString &folder) { Loader::Loader( - QObject *parent, + not_null session, int id, MTP::DedicatedLoader::Location location, const QString &folder, int size) -: BlobLoader(parent, id, location, folder, size) { +: BlobLoader(nullptr, session, id, location, folder, size) { } void Loader::unpack(const QString &path) { @@ -200,7 +203,9 @@ void Loader::fail() { BlobLoader::fail(); } -Inner::Inner(QWidget *parent) : RpWidget(parent) { +Inner::Inner(QWidget *parent, not_null session) +: RpWidget(parent) +, _session(session) { setupContent(); } @@ -208,15 +213,16 @@ void Inner::setupContent() { const auto content = Ui::CreateChild(this); for (const auto &set : kSets) { - content->add(object_ptr(content, set)); + content->add(object_ptr(content, _session, set)); } content->resizeToWidth(st::boxWidth); Ui::ResizeFitChild(this, content); } -Row::Row(QWidget *widget, const Set &set) +Row::Row(QWidget *widget, not_null session, const Set &set) : RippleButton(widget, st::contactsRipple) +, _session(session) , _id(set.id) , _state(Available{ set.size }) { setupContent(set); @@ -411,7 +417,7 @@ void Row::setupHandler() { } void Row::load() { - LoadAndSwitchTo(_id); + LoadAndSwitchTo(_session, _id); } void Row::setupLabels(const Set &set) { @@ -530,11 +536,12 @@ void Row::setupAnimation() { } // namespace -ManageSetsBox::ManageSetsBox(QWidget*) { +ManageSetsBox::ManageSetsBox(QWidget*, not_null session) +: _session(session) { } void ManageSetsBox::prepare() { - const auto inner = setInnerWidget(object_ptr(this)); + const auto inner = setInnerWidget(object_ptr(this, _session)); setTitle(tr::lng_emoji_manage_sets()); @@ -543,15 +550,13 @@ void ManageSetsBox::prepare() { setDimensionsToContent(st::boxWidth, inner); } -void LoadAndSwitchTo(int id) { - Expects(App::main() != nullptr); - +void LoadAndSwitchTo(not_null session, int id) { if (!ranges::contains(kSets, id, &Set::id)) { ClearNeedSwitchToId(); return; } SetGlobalLoader(base::make_unique_q( - App::main(), + session, id, GetDownloadLocation(id), internal::SetDataPath(id), diff --git a/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.h b/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.h index 39ea5fd350..e638f52e5f 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.h +++ b/Telegram/SourceFiles/chat_helpers/emoji_sets_manager.h @@ -9,19 +9,25 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/abstract_box.h" +namespace Main { +class Session; +} // namespace Main + namespace Ui { namespace Emoji { -class ManageSetsBox : public Ui::BoxContent { +class ManageSetsBox final : public Ui::BoxContent { public: - explicit ManageSetsBox(QWidget*); + ManageSetsBox(QWidget*, not_null session); -protected: +private: void prepare() override; + const not_null _session; + }; -void LoadAndSwitchTo(int id); +void LoadAndSwitchTo(not_null session, int id); } // namespace Emoji } // namespace Ui diff --git a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp index 97c6bc9734..d7d84f2308 100644 --- a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp +++ b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp @@ -163,6 +163,7 @@ void DownloadDictionaryInBackground( auto sharedLoader = std::make_shared>(); *sharedLoader = base::make_unique_q( App::main(), + session, id, GetDownloadLocation(id), DictPathByLangId(id), @@ -197,12 +198,13 @@ rpl::producer GlobalLoaderChanged() { DictLoader::DictLoader( QObject *parent, + not_null session, int id, MTP::DedicatedLoader::Location location, const QString &folder, int size, Fn destroyCallback) -: BlobLoader(parent, id, location, folder, size) +: BlobLoader(parent, session, id, location, folder, size) , _destroyCallback(std::move(destroyCallback)) { } diff --git a/Telegram/SourceFiles/chat_helpers/spellchecker_common.h b/Telegram/SourceFiles/chat_helpers/spellchecker_common.h index f806ceb088..4d535c363e 100644 --- a/Telegram/SourceFiles/chat_helpers/spellchecker_common.h +++ b/Telegram/SourceFiles/chat_helpers/spellchecker_common.h @@ -44,6 +44,7 @@ class DictLoader : public Storage::CloudBlob::BlobLoader { public: DictLoader( QObject *parent, + not_null session, int id, MTP::DedicatedLoader::Location location, const QString &folder, diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 3350f0c1b6..606d73d39d 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -114,25 +114,27 @@ Application::Application(not_null launcher) }, _lifetime); activeAccount().sessionChanges( - ) | rpl::start_with_next([=] { + ) | rpl::start_with_next([=](Main::Session *session) { if (_mediaView) { hideMediaView(); _mediaView->clearData(); } + if (session && !UpdaterDisabled()) { + if (const auto mtp = activeAccount().mtp()) { + UpdateChecker().setMtproto(mtp, session->userId()); + } + } }, _lifetime); activeAccount().mtpChanges( ) | rpl::filter([=](MTP::Instance *instance) { return instance != nullptr; - }) | rpl::start_with_next([=](not_null mtp) { + }) | rpl::start_with_next([=] { if (_window) { // This should be called when user settings are read. // Right now after they are read the startMtp() is called. _window->widget()->updateTrayMenu(); } - if (!UpdaterDisabled()) { - UpdateChecker().setMtproto(mtp.get()); - } }, _lifetime); } diff --git a/Telegram/SourceFiles/core/update_checker.cpp b/Telegram/SourceFiles/core/update_checker.cpp index 8e27b203ef..05f44f4811 100644 --- a/Telegram/SourceFiles/core/update_checker.cpp +++ b/Telegram/SourceFiles/core/update_checker.cpp @@ -172,7 +172,7 @@ private: class MtpChecker : public Checker { public: - MtpChecker(QPointer instance, bool testing); + MtpChecker(QPointer instance, int32 userId, bool testing); void start() override; @@ -191,6 +191,7 @@ private: const FileLocation &location) const; MTP::WeakInstance _mtp; + int32 _mtpUserId = 0; }; @@ -877,13 +878,17 @@ void HttpLoaderActor::partFailed(QNetworkReply::NetworkError e) { _parent->threadSafeFailed(); } -MtpChecker::MtpChecker(QPointer instance, bool testing) +MtpChecker::MtpChecker( + QPointer instance, + int32 userId, + bool testing) : Checker(testing) -, _mtp(instance) { +, _mtp(instance) +, _mtpUserId(userId) { } void MtpChecker::start() { - if (!_mtp.valid()) { + if (!_mtp.valid() || !_mtpUserId) { LOG(("Update Info: MTP is unavailable.")); crl::on_main(this, [=] { fail(); }); return; @@ -891,7 +896,8 @@ void MtpChecker::start() { const auto updaterVersion = Platform::AutoUpdateVersion(); const auto feed = "tdhbcfeed" + (updaterVersion > 1 ? QString::number(updaterVersion) : QString()); - MTP::ResolveChannel(&_mtp, feed, [=](const MTPInputChannel &channel) { + MTP::ResolveChannel(&_mtp, _mtpUserId, feed, [=]( + const MTPInputChannel &channel) { _mtp.send( MTPmessages_GetHistory( MTP_inputPeerChannel( @@ -925,7 +931,12 @@ void MtpChecker::gotMessage(const MTPmessages_Messages &result) { fail(); } }; - MTP::StartDedicatedLoader(&_mtp, *location, UpdatesFolder(), ready); + MTP::StartDedicatedLoader( + &_mtp, + _mtpUserId, + *location, + UpdatesFolder(), + ready); } auto MtpChecker::parseMessage(const MTPmessages_Messages &result) const @@ -1033,7 +1044,7 @@ public: int already() const; int size() const; - void setMtproto(const QPointer &mtproto); + void setMtproto(const QPointer &mtproto, int32 userId); ~Updater(); @@ -1079,6 +1090,7 @@ private: std::shared_ptr _activeLoader; bool _usingMtprotoLoader = (cAlphaVersion() != 0); QPointer _mtproto; + int32 _mtprotoUserId = 0; rpl::lifetime _lifetime; @@ -1226,7 +1238,7 @@ void Updater::start(bool forceWait) { std::make_unique(_testing)); startImplementation( &_mtpImplementation, - std::make_unique(_mtproto, _testing)); + std::make_unique(_mtproto, _mtprotoUserId, _testing)); _checking.fire({}); } else { @@ -1289,8 +1301,11 @@ void Updater::test() { start(false); } -void Updater::setMtproto(const QPointer &mtproto) { +void Updater::setMtproto( + const QPointer &mtproto, + int32 userId) { _mtproto = mtproto; + _mtprotoUserId = userId; } void Updater::handleTimeout() { @@ -1388,8 +1403,11 @@ Updater::~Updater() { UpdateChecker::UpdateChecker() : _updater(GetUpdaterInstance()) { if (IsAppLaunched()) { - if (const auto mtproto = Core::App().activeAccount().mtp()) { - _updater->setMtproto(mtproto); + const auto &account = Core::App().activeAccount(); + if (account.sessionExists()) { + if (const auto mtproto = account.mtp()) { + _updater->setMtproto(mtproto, account.session().userId()); + } } } } @@ -1423,8 +1441,10 @@ void UpdateChecker::test() { _updater->test(); } -void UpdateChecker::setMtproto(const QPointer &mtproto) { - _updater->setMtproto(mtproto); +void UpdateChecker::setMtproto( + const QPointer &mtproto, + int32 userId) { + _updater->setMtproto(mtproto, userId); } void UpdateChecker::stop() { diff --git a/Telegram/SourceFiles/core/update_checker.h b/Telegram/SourceFiles/core/update_checker.h index 4d137c8815..485db1e8dc 100644 --- a/Telegram/SourceFiles/core/update_checker.h +++ b/Telegram/SourceFiles/core/update_checker.h @@ -41,7 +41,7 @@ public: void stop(); void test(); - void setMtproto(const QPointer &mtproto); + void setMtproto(const QPointer &mtproto, int32 userId); State state() const; int already() const; diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index 6d4ad940da..6149ae3963 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -126,12 +126,13 @@ void Account::createSession( Expects(_sessionValue.current() == nullptr); _session = std::make_unique(this, user, std::move(settings)); - _sessionValue = _session.get(); if (!serialized.isEmpty()) { // For now it depends on Auth() which depends on _sessionValue. local().readSelf(serialized, streamVersion); } + + _sessionValue = _session.get(); } void Account::destroySession() { diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index ef37669d1f..1ccde7510f 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -53,7 +53,7 @@ public: [[nodiscard]] rpl::producer sessionValue() const; [[nodiscard]] rpl::producer sessionChanges() const; - [[nodiscard]] MTP::Instance *mtp() { + [[nodiscard]] MTP::Instance *mtp() const { return _mtp.get(); } [[nodiscard]] rpl::producer mtpValue() const; diff --git a/Telegram/SourceFiles/main/main_session.cpp b/Telegram/SourceFiles/main/main_session.cpp index 5de2b51c77..47d7fe071f 100644 --- a/Telegram/SourceFiles/main/main_session.cpp +++ b/Telegram/SourceFiles/main/main_session.cpp @@ -147,8 +147,6 @@ bool Session::validateSelf(const MTPUser &user) { } void Session::saveSettingsDelayed(crl::time delay) { - Expects(this == &Auth()); - _saveSettingsTimer.callOnce(delay); } @@ -184,7 +182,3 @@ void Session::saveSettingsNowIfNeeded() { } } // namespace Main - -Main::Session &Auth() { - return Core::App().activeAccount().session(); -} diff --git a/Telegram/SourceFiles/main/main_session.h b/Telegram/SourceFiles/main/main_session.h index 30f09b36c4..0671d9330c 100644 --- a/Telegram/SourceFiles/main/main_session.h +++ b/Telegram/SourceFiles/main/main_session.h @@ -169,5 +169,3 @@ private: }; } // namespace Main - -Main::Session &Auth(); diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp index c6ab7d6402..3f43ac1d1b 100644 --- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp +++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp @@ -365,6 +365,7 @@ Fn DedicatedLoader::failHandler() { void ResolveChannel( not_null mtp, + int32 userId, const QString &username, Fn done, Fn fail) { @@ -373,20 +374,20 @@ void ResolveChannel( ).arg(username)); fail(); }; - if (!Main::Session::Exists()) { + if (!userId) { failed(); return; } struct ResolveResult { - base::weak_ptr auth; + int32 userId = 0; MTPInputChannel channel; }; static std::map ResolveCache; const auto i = ResolveCache.find(username); if (i != end(ResolveCache)) { - if (i->second.auth.get() == &Auth()) { + if (i->second.userId == userId) { done(i->second.channel); return; } @@ -399,7 +400,7 @@ void ResolveChannel( if (const auto channel = ExtractChannel(result)) { ResolveCache.emplace( username, - ResolveResult { base::make_weak(&Auth()), *channel }); + ResolveResult { userId, *channel }); done(*channel); } else { failed(); @@ -429,6 +430,7 @@ std::optional GetMessagesElement( void StartDedicatedLoader( not_null mtp, + int32 userId, const DedicatedLoader::Location &location, const QString &folder, Fn)> ready) { @@ -448,7 +450,7 @@ void StartDedicatedLoader( }; const auto [username, postId] = location; - ResolveChannel(mtp, username, [=, postId = postId]( + ResolveChannel(mtp, userId, username, [=, postId = postId]( const MTPInputChannel &channel) { mtp->send( MTPchannels_GetMessages( diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.h b/Telegram/SourceFiles/mtproto/dedicated_file_loader.h index 645f7e29e8..949dfa6bc8 100644 --- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.h +++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.h @@ -143,6 +143,7 @@ private: void ResolveChannel( not_null mtp, + int32 userId, const QString &username, Fn done, Fn fail); @@ -152,6 +153,7 @@ std::optional GetMessagesElement( void StartDedicatedLoader( not_null mtp, + int32 userId, const DedicatedLoader::Location &location, const QString &folder, Fn)> ready); diff --git a/Telegram/SourceFiles/settings/settings_chat.cpp b/Telegram/SourceFiles/settings/settings_chat.cpp index e835390553..110e621548 100644 --- a/Telegram/SourceFiles/settings/settings_chat.cpp +++ b/Telegram/SourceFiles/settings/settings_chat.cpp @@ -737,8 +737,8 @@ void SetupStickersEmoji( st::settingsChatButton, &st::settingsIconEmoji, st::settingsChatIconLeft - )->addClickHandler([] { - Ui::show(Box()); + )->addClickHandler([=] { + Ui::show(Box(session)); }); AddSkip(container, st::settingsCheckboxesSkip); diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index 28cfa0c5fd..41b2ca8a61 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -582,7 +582,7 @@ public: voice->waveform[0] = -2; voice->wavemax = 0; } - Auth().data().requestDocumentViewRepaint(_doc); + _doc->owner().requestDocumentViewRepaint(_doc); } } ~CountWaveformTask() { diff --git a/Telegram/SourceFiles/storage/serialize_common.cpp b/Telegram/SourceFiles/storage/serialize_common.cpp index 5637b2cb46..9cd43a3055 100644 --- a/Telegram/SourceFiles/storage/serialize_common.cpp +++ b/Telegram/SourceFiles/storage/serialize_common.cpp @@ -200,7 +200,10 @@ void writePeer(QDataStream &stream, PeerData *peer) { } } -PeerData *readPeer(int streamAppVersion, QDataStream &stream) { +PeerData *readPeer( + not_null session, + int streamAppVersion, + QDataStream &stream) { quint64 peerId = 0, photoId = 0; stream >> peerId >> photoId; if (!peerId) { @@ -213,8 +216,8 @@ PeerData *readPeer(int streamAppVersion, QDataStream &stream) { return nullptr; } - const auto loaded = Auth().data().peerLoaded(peerId); - const auto result = loaded ? loaded : Auth().data().peer(peerId).get(); + const auto loaded = session->data().peerLoaded(peerId); + const auto result = loaded ? loaded : session->data().peer(peerId).get(); if (!loaded) { result->loadedStatus = PeerData::FullLoaded; } @@ -234,7 +237,7 @@ PeerData *readPeer(int streamAppVersion, QDataStream &stream) { userpicAccessHash = access; const auto showPhone = !user->isServiceUser() - && (user->id != Auth().userPeerId()) + && (user->id != session->userPeerId()) && (contact <= 0); const auto pname = (showPhone && !phone.isEmpty()) ? App::formatPhone(phone) @@ -253,7 +256,7 @@ PeerData *readPeer(int streamAppVersion, QDataStream &stream) { user->botInfo->inlinePlaceholder = inlinePlaceholder; } - if (user->id == Auth().userPeerId()) { + if (user->id == session->userPeerId()) { user->input = MTP_inputPeerSelf(); user->inputUser = MTP_inputUserSelf(); } else { diff --git a/Telegram/SourceFiles/storage/serialize_common.h b/Telegram/SourceFiles/storage/serialize_common.h index b0aa7867be..6db828ecf1 100644 --- a/Telegram/SourceFiles/storage/serialize_common.h +++ b/Telegram/SourceFiles/storage/serialize_common.h @@ -127,7 +127,10 @@ inline MTP::AuthKey::Data read(QDataStream &stream) { uint32 peerSize(not_null peer); void writePeer(QDataStream &stream, PeerData *peer); -PeerData *readPeer(int streamAppVersion, QDataStream &stream); +PeerData *readPeer( + not_null session, + int streamAppVersion, + QDataStream &stream); QString peekUserPhone(int streamAppVersion, QDataStream &stream); } // namespace Serialize diff --git a/Telegram/SourceFiles/storage/serialize_document.cpp b/Telegram/SourceFiles/storage/serialize_document.cpp index 0799e42416..e8fa97d61f 100644 --- a/Telegram/SourceFiles/storage/serialize_document.cpp +++ b/Telegram/SourceFiles/storage/serialize_document.cpp @@ -56,7 +56,11 @@ void Document::writeToStream(QDataStream &stream, DocumentData *document) { stream << qint32(document->videoThumbnailByteSize()); } -DocumentData *Document::readFromStreamHelper(int streamAppVersion, QDataStream &stream, const StickerSetInfo *info) { +DocumentData *Document::readFromStreamHelper( + not_null session, + int streamAppVersion, + QDataStream &stream, + const StickerSetInfo *info) { quint64 id, access; QString name, mime; qint32 date, dc, size, width, height, type, versionTag, version = 0; @@ -151,7 +155,7 @@ DocumentData *Document::readFromStreamHelper(int streamAppVersion, QDataStream & // size letter ('s' or 'm') is lost, it was not saved in legacy. return nullptr; } - return Auth().data().document( + return session->data().document( id, access, fileReference, @@ -171,12 +175,19 @@ DocumentData *Document::readFromStreamHelper(int streamAppVersion, QDataStream & size); } -DocumentData *Document::readStickerFromStream(int streamAppVersion, QDataStream &stream, const StickerSetInfo &info) { - return readFromStreamHelper(streamAppVersion, stream, &info); +DocumentData *Document::readStickerFromStream( + not_null session, + int streamAppVersion, + QDataStream &stream, + const StickerSetInfo &info) { + return readFromStreamHelper(session, streamAppVersion, stream, &info); } -DocumentData *Document::readFromStream(int streamAppVersion, QDataStream &stream) { - return readFromStreamHelper(streamAppVersion, stream, nullptr); +DocumentData *Document::readFromStream( + not_null session, + int streamAppVersion, + QDataStream &stream) { + return readFromStreamHelper(session, streamAppVersion, stream, nullptr); } int Document::sizeInStream(DocumentData *document) { diff --git a/Telegram/SourceFiles/storage/serialize_document.h b/Telegram/SourceFiles/storage/serialize_document.h index 8851e798d6..252db9544a 100644 --- a/Telegram/SourceFiles/storage/serialize_document.h +++ b/Telegram/SourceFiles/storage/serialize_document.h @@ -25,12 +25,23 @@ public: }; static void writeToStream(QDataStream &stream, DocumentData *document); - static DocumentData *readStickerFromStream(int streamAppVersion, QDataStream &stream, const StickerSetInfo &info); - static DocumentData *readFromStream(int streamAppVersion, QDataStream &stream); + static DocumentData *readStickerFromStream( + not_null session, + int streamAppVersion, + QDataStream &stream, + const StickerSetInfo &info); + static DocumentData *readFromStream( + not_null session, + int streamAppVersion, + QDataStream &stream); static int sizeInStream(DocumentData *document); private: - static DocumentData *readFromStreamHelper(int streamAppVersion, QDataStream &stream, const StickerSetInfo *info); + static DocumentData *readFromStreamHelper( + not_null session, + int streamAppVersion, + QDataStream &stream, + const StickerSetInfo *info); }; diff --git a/Telegram/SourceFiles/storage/storage_account.cpp b/Telegram/SourceFiles/storage/storage_account.cpp index f229835f02..5d11087f94 100644 --- a/Telegram/SourceFiles/storage/storage_account.cpp +++ b/Telegram/SourceFiles/storage/storage_account.cpp @@ -421,12 +421,12 @@ void Account::writeMap() { map.writeData(_passcodeKeyEncrypted); uint32 mapSize = 0; - const auto self = [] { - if (!Main::Session::Exists()) { + const auto self = [&] { + if (!_owner->sessionExists()) { DEBUG_LOG(("AuthSelf Warning: Session does not exist.")); return QByteArray(); } - const auto self = Auth().user(); + const auto self = _owner->session().user(); if (self->phone().isEmpty()) { DEBUG_LOG(("AuthSelf Error: Phone is empty.")); return QByteArray(); @@ -1343,7 +1343,7 @@ void Account::writeStickerSets( FileKey &stickersKey, CheckSet checkSet, const Data::StickersSetsOrder &order) { - const auto &sets = Auth().data().stickers().sets(); + const auto &sets = _owner->session().data().stickers().sets(); if (sets.empty()) { if (stickersKey) { ClearKey(stickersKey, _basePath); @@ -1444,7 +1444,7 @@ void Account::readStickerSets( stickersKey = 0; }; - auto &sets = Auth().data().stickers().setsRef(); + auto &sets = _owner->session().data().stickers().setsRef(); if (outOrder) outOrder->clear(); quint32 versionTag = 0; @@ -1519,7 +1519,7 @@ void Account::readStickerSets( // We will set this flags from order lists when reading those stickers. setFlags &= ~(MTPDstickerSet::Flag::f_installed_date | MTPDstickerSet_ClientFlag::f_featured); it = sets.emplace(setId, std::make_unique( - &Auth().data(), + &_owner->session().data(), setId, setAccess, setTitle, @@ -1550,7 +1550,10 @@ void Account::readStickerSets( Serialize::Document::StickerSetInfo info(setId, setAccess, setShortName); base::flat_set read; for (int32 j = 0; j < scnt; ++j) { - auto document = Serialize::Document::readStickerFromStream(stickers.version, stickers.stream, info); + auto document = Serialize::Document::readStickerFromStream( + &_owner->session(), + stickers.version, + stickers.stream, info); if (!CheckStreamStatus(stickers.stream)) { return failed(); } else if (!document @@ -1605,7 +1608,7 @@ void Account::readStickerSets( for (int32 k = 0; k < stickersCount; ++k) { quint64 id; stickers.stream >> id; - const auto doc = Auth().data().document(id); + const auto doc = _owner->session().data().document(id); if (!doc->sticker()) continue; pack.push_back(doc); @@ -1673,7 +1676,7 @@ void Account::writeInstalledStickers() { return StickerSetCheckResult::Skip; } return StickerSetCheckResult::Write; - }, Auth().data().stickers().setsOrder()); + }, _owner->session().data().stickers().setsOrder()); } void Account::writeFeaturedStickers() { @@ -1691,7 +1694,7 @@ void Account::writeFeaturedStickers() { return StickerSetCheckResult::Skip; } return StickerSetCheckResult::Write; - }, Auth().data().stickers().featuredSetsOrder()); + }, _owner->session().data().stickers().featuredSetsOrder()); } void Account::writeRecentStickers() { @@ -1718,7 +1721,7 @@ void Account::writeArchivedStickers() { return StickerSetCheckResult::Skip; } return StickerSetCheckResult::Write; - }, Auth().data().stickers().archivedSetsOrder()); + }, _owner->session().data().stickers().archivedSetsOrder()); } void Account::importOldRecentStickers() { @@ -1734,10 +1737,10 @@ void Account::importOldRecentStickers() { return; } - auto &sets = Auth().data().stickers().setsRef(); + auto &sets = _owner->session().data().stickers().setsRef(); sets.clear(); - auto &order = Auth().data().stickers().setsOrderRef(); + auto &order = _owner->session().data().stickers().setsOrderRef(); order.clear(); auto &recent = cRefRecentStickers(); @@ -1746,7 +1749,7 @@ void Account::importOldRecentStickers() { const auto def = sets.emplace( Data::Stickers::DefaultSetId, std::make_unique( - &Auth().data(), + &_owner->session().data(), Data::Stickers::DefaultSetId, uint64(0), tr::lng_stickers_default_set(tr::now), @@ -1760,7 +1763,7 @@ void Account::importOldRecentStickers() { const auto custom = sets.emplace( Data::Stickers::CustomSetId, std::make_unique( - &Auth().data(), + &_owner->session().data(), Data::Stickers::CustomSetId, uint64(0), qsl("Custom stickers"), @@ -1795,7 +1798,7 @@ void Account::importOldRecentStickers() { attributes.push_back(MTP_documentAttributeImageSize(MTP_int(width), MTP_int(height))); } - const auto doc = Auth().data().document( + const auto doc = _owner->session().data().document( id, access, QByteArray(), @@ -1844,21 +1847,21 @@ void Account::readInstalledStickers() { return importOldRecentStickers(); } - Auth().data().stickers().setsRef().clear(); + _owner->session().data().stickers().setsRef().clear(); readStickerSets( _installedStickersKey, - &Auth().data().stickers().setsOrderRef(), + &_owner->session().data().stickers().setsOrderRef(), MTPDstickerSet::Flag::f_installed_date); } void Account::readFeaturedStickers() { readStickerSets( _featuredStickersKey, - &Auth().data().stickers().featuredSetsOrderRef(), + &_owner->session().data().stickers().featuredSetsOrderRef(), MTPDstickerSet::Flags() | MTPDstickerSet_ClientFlag::f_featured); - const auto &sets = Auth().data().stickers().sets(); - const auto &order = Auth().data().stickers().featuredSetsOrder(); + const auto &sets = _owner->session().data().stickers().sets(); + const auto &order = _owner->session().data().stickers().featuredSetsOrder(); int unreadCount = 0; for (const auto setId : order) { auto it = sets.find(setId); @@ -1867,7 +1870,7 @@ void Account::readFeaturedStickers() { ++unreadCount; } } - Auth().data().stickers().setFeaturedSetsUnreadCount(unreadCount); + _owner->session().data().stickers().setFeaturedSetsUnreadCount(unreadCount); } void Account::readRecentStickers() { @@ -1881,13 +1884,13 @@ void Account::readFavedStickers() { void Account::readArchivedStickers() { static bool archivedStickersRead = false; if (!archivedStickersRead) { - readStickerSets(_archivedStickersKey, &Auth().data().stickers().archivedSetsOrderRef()); + readStickerSets(_archivedStickersKey, &_owner->session().data().stickers().archivedSetsOrderRef()); archivedStickersRead = true; } } void Account::writeSavedGifs() { - auto &saved = Auth().data().stickers().savedGifs(); + auto &saved = _owner->session().data().stickers().savedGifs(); if (saved.isEmpty()) { if (_savedGifsKey) { ClearKey(_savedGifsKey, _basePath); @@ -1925,7 +1928,7 @@ void Account::readSavedGifs() { return; } - auto &saved = Auth().data().stickers().savedGifsRef(); + auto &saved = _owner->session().data().stickers().savedGifsRef(); const auto failed = [&] { ClearKey(_savedGifsKey, _basePath); _savedGifsKey = 0; @@ -1938,7 +1941,10 @@ void Account::readSavedGifs() { saved.reserve(cnt); OrderedSet read; for (uint32 i = 0; i < cnt; ++i) { - auto document = Serialize::Document::readFromStream(gifs.version, gifs.stream); + auto document = Serialize::Document::readFromStream( + &_owner->session(), + gifs.version, + gifs.stream); if (!CheckStreamStatus(gifs.stream)) { return failed(); } else if (!document || !document->isGifv()) { @@ -2229,6 +2235,7 @@ void Account::readRecentHashtagsAndBots() { bots.reserve(botsCount); for (auto i = 0; i < botsCount; ++i) { const auto peer = Serialize::readPeer( + &_owner->session(), hashtags.version, hashtags.stream); if (!peer) { @@ -2439,11 +2446,14 @@ void Account::writeSelf() { void Account::readSelf(const QByteArray &serialized, int32 streamVersion) { QDataStream stream(serialized); - const auto user = Auth().user(); + const auto user = _owner->session().user(); const auto wasLoadedStatus = std::exchange( user->loadedStatus, PeerData::NotLoaded); - const auto self = Serialize::readPeer(streamVersion, stream); + const auto self = Serialize::readPeer( + &_owner->session(), + streamVersion, + stream); if (!self || !self->isSelf() || self != user) { user->loadedStatus = wasLoadedStatus; return; diff --git a/Telegram/SourceFiles/storage/storage_cloud_blob.cpp b/Telegram/SourceFiles/storage/storage_cloud_blob.cpp index 300dffd918..83d0b56160 100644 --- a/Telegram/SourceFiles/storage/storage_cloud_blob.cpp +++ b/Telegram/SourceFiles/storage/storage_cloud_blob.cpp @@ -8,10 +8,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/storage_cloud_blob.h" #include "base/zlib_help.h" -#include "core/application.h" #include "lang/lang_keys.h" #include "layout.h" #include "main/main_account.h" +#include "main/main_session.h" namespace Storage::CloudBlob { @@ -95,6 +95,7 @@ QString StateDescription(const BlobState &state, tr::phrase<> activeText) { BlobLoader::BlobLoader( QObject *parent, + not_null session, int id, MTP::DedicatedLoader::Location location, const QString &folder, @@ -103,7 +104,8 @@ BlobLoader::BlobLoader( , _folder(folder) , _id(id) , _state(Loading{ 0, size }) -, _mtproto(Core::App().activeAccount().mtp()) { +, _mtproto(session->account().mtp()) +, _mtprotoUserId(session->userId()) { const auto ready = [=](std::unique_ptr loader) { if (loader) { setImplementation(std::move(loader)); @@ -111,7 +113,12 @@ BlobLoader::BlobLoader( fail(); } }; - MTP::StartDedicatedLoader(&_mtproto, location, _folder, ready); + MTP::StartDedicatedLoader( + &_mtproto, + _mtprotoUserId, + location, + _folder, + ready); } int BlobLoader::id() const { diff --git a/Telegram/SourceFiles/storage/storage_cloud_blob.h b/Telegram/SourceFiles/storage/storage_cloud_blob.h index af6301ca0a..489bc8a72e 100644 --- a/Telegram/SourceFiles/storage/storage_cloud_blob.h +++ b/Telegram/SourceFiles/storage/storage_cloud_blob.h @@ -14,6 +14,10 @@ template struct phrase; } // namespace tr +namespace Main { +class Session; +} // namespace Main + namespace Storage::CloudBlob { constexpr auto kCloudLocationUsername = "tdhbcfiles"_cs; @@ -79,6 +83,7 @@ class BlobLoader : public QObject { public: BlobLoader( QObject *parent, + not_null session, int id, MTP::DedicatedLoader::Location location, const QString &folder, @@ -102,6 +107,8 @@ private: rpl::variable _state; MTP::WeakInstance _mtproto; + int32 _mtprotoUserId = 0; + std::unique_ptr _implementation; }; diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp index 8252f175d4..723f715377 100644 --- a/Telegram/SourceFiles/window/window_controller.cpp +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -83,10 +83,12 @@ void Controller::setupIntro() { } void Controller::setupMain() { + Expects(_account->sessionExists()); + _widget.setupMain(); if (const auto id = Ui::Emoji::NeedToSwitchBackToId()) { - Ui::Emoji::LoadAndSwitchTo(id); + Ui::Emoji::LoadAndSwitchTo(&_account->session(), id); } }