diff --git a/Telegram/SourceFiles/data/data_chat_filters.cpp b/Telegram/SourceFiles/data/data_chat_filters.cpp index 00f56ed1cd..8d5e01e97d 100644 --- a/Telegram/SourceFiles/data/data_chat_filters.cpp +++ b/Telegram/SourceFiles/data/data_chat_filters.cpp @@ -216,7 +216,7 @@ bool ChatFilter::contains(not_null history) const { } ChatFilters::ChatFilters(not_null owner) : _owner(owner) { - load(); + crl::on_main(&owner->session(), [=] { load(); }); } ChatFilters::~ChatFilters() = default; @@ -232,6 +232,16 @@ not_null ChatFilters::chatsList(FilterId filterId) { return pointer.get(); } +void ChatFilters::setPreloaded(const QVector &result) { + _loadRequestId = -1; + received(result); + crl::on_main(&_owner->session(), [=] { + if (_loadRequestId == -1) { + _loadRequestId = 0; + } + }); +} + void ChatFilters::load() { load(false); } @@ -244,40 +254,44 @@ void ChatFilters::load(bool force) { api.request(_loadRequestId).cancel(); _loadRequestId = api.request(MTPmessages_GetDialogFilters( )).done([=](const MTPVector &result) { - auto position = 0; - auto changed = false; - for (const auto &filter : result.v) { - auto parsed = ChatFilter::FromTL(filter, _owner); - const auto b = begin(_list) + position, e = end(_list); - const auto i = ranges::find(b, e, parsed.id(), &ChatFilter::id); - if (i == e) { - applyInsert(std::move(parsed), position); - changed = true; - } else if (i == b) { - if (applyChange(*b, std::move(parsed))) { - changed = true; - } - } else { - std::swap(*i, *b); - applyChange(*b, std::move(parsed)); - changed = true; - } - ++position; - } - while (position < _list.size()) { - applyRemove(position); - changed = true; - } - if (changed || !_loaded) { - _loaded = true; - _listChanged.fire({}); - } + received(result.v); _loadRequestId = 0; }).fail([=](const RPCError &error) { _loadRequestId = 0; }).send(); } +void ChatFilters::received(const QVector &list) { + auto position = 0; + auto changed = false; + for (const auto &filter : list) { + auto parsed = ChatFilter::FromTL(filter, _owner); + const auto b = begin(_list) + position, e = end(_list); + const auto i = ranges::find(b, e, parsed.id(), &ChatFilter::id); + if (i == e) { + applyInsert(std::move(parsed), position); + changed = true; + } else if (i == b) { + if (applyChange(*b, std::move(parsed))) { + changed = true; + } + } else { + std::swap(*i, *b); + applyChange(*b, std::move(parsed)); + changed = true; + } + ++position; + } + while (position < _list.size()) { + applyRemove(position); + changed = true; + } + if (changed || !_loaded) { + _loaded = true; + _listChanged.fire({}); + } +} + void ChatFilters::apply(const MTPUpdate &update) { update.match([&](const MTPDupdateDialogFilter &data) { if (const auto filter = data.vfilter()) { diff --git a/Telegram/SourceFiles/data/data_chat_filters.h b/Telegram/SourceFiles/data/data_chat_filters.h index 04ced87a56..43cb0d5817 100644 --- a/Telegram/SourceFiles/data/data_chat_filters.h +++ b/Telegram/SourceFiles/data/data_chat_filters.h @@ -95,6 +95,8 @@ public: explicit ChatFilters(not_null owner); ~ChatFilters(); + void setPreloaded(const QVector &result); + void load(); void apply(const MTPUpdate &update); void set(ChatFilter filter); @@ -125,6 +127,7 @@ public: private: void load(bool force); + void received(const QVector &list); bool applyOrder(const QVector &order); bool applyChange(ChatFilter &filter, ChatFilter &&updated); void applyInsert(ChatFilter filter, int position); diff --git a/Telegram/SourceFiles/intro/intro_step.cpp b/Telegram/SourceFiles/intro/intro_step.cpp index b87370bbcb..ad5b818a2b 100644 --- a/Telegram/SourceFiles/intro/intro_step.cpp +++ b/Telegram/SourceFiles/intro/intro_step.cpp @@ -27,6 +27,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/effects/slide_animation.h" #include "data/data_user.h" #include "data/data_auto_download.h" +#include "data/data_session.h" +#include "data/data_chat_filters.h" #include "window/window_controller.h" #include "window/themes/window_theme.h" #include "app.h" @@ -160,6 +162,18 @@ void Step::finish(const MTPUser &user, QImage &&photo) { } } + api().request(MTPmessages_GetDialogFilters( + )).done([=](const MTPVector &result) { + createSession(user, photo, result.v); + }).fail([=](const RPCError &error) { + createSession(user, photo, QVector()); + }).send(); +} + +void Step::createSession( + const MTPUser &user, + QImage photo, + const QVector &filters) { // Save the default language if we've suggested some other and user ignored it. const auto currentId = Lang::Current().id(); const auto defaultId = Lang::DefaultLanguageId(); @@ -168,12 +182,19 @@ void Step::finish(const MTPUser &user, QImage &&photo) { Lang::Current().switchToId(Lang::DefaultLanguage()); Local::writeLangPack(); } + + auto settings = std::make_unique(); + settings->setDialogsFiltersEnabled(!filters.isEmpty()); + const auto account = _account; - account->createSession(user); + account->createSession(user, std::move(settings)); // "this" is already deleted here by creating the main widget. account->local().writeMtpData(); auto &session = account->session(); + if (!filters.isEmpty()) { + session.data().chatsFilters().setPreloaded(filters); + } if (!photo.isNull()) { session.api().uploadPeerPhoto(session.user(), std::move(photo)); } diff --git a/Telegram/SourceFiles/intro/intro_step.h b/Telegram/SourceFiles/intro/intro_step.h index bc95488002..b98349a449 100644 --- a/Telegram/SourceFiles/intro/intro_step.h +++ b/Telegram/SourceFiles/intro/intro_step.h @@ -106,6 +106,10 @@ protected: return _data; } void finish(const MTPUser &user, QImage &&photo = QImage()); + void createSession( + const MTPUser &user, + QImage photo, + const QVector &filters); void goBack(); diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index c2e9958d4d..a4b2eb8fbf 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -131,8 +131,14 @@ uint64 Account::willHaveSessionUniqueId(MTP::Config *config) const { | (config && config->isTestMode() ? 0x0100'0000'0000'0000ULL : 0ULL); } -void Account::createSession(const MTPUser &user) { - createSession(user, QByteArray(), 0, std::make_unique()); +void Account::createSession( + const MTPUser &user, + std::unique_ptr settings) { + createSession( + user, + QByteArray(), + 0, + settings ? std::move(settings) : std::make_unique()); } void Account::createSession( diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index d63eab11a2..8c14b1793b 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -49,7 +49,9 @@ public: void start(std::unique_ptr config); [[nodiscard]] uint64 willHaveSessionUniqueId(MTP::Config *config) const; - void createSession(const MTPUser &user); + void createSession( + const MTPUser &user, + std::unique_ptr settings = nullptr); void createSession( UserId id, QByteArray serialized,