Load filters before creating session on log in.

This commit is contained in:
John Preston 2020-06-30 11:02:44 +04:00
parent 90af3d295b
commit 87eaab15b5
6 changed files with 83 additions and 33 deletions

View File

@ -216,7 +216,7 @@ bool ChatFilter::contains(not_null<History*> history) const {
}
ChatFilters::ChatFilters(not_null<Session*> owner) : _owner(owner) {
load();
crl::on_main(&owner->session(), [=] { load(); });
}
ChatFilters::~ChatFilters() = default;
@ -232,6 +232,16 @@ not_null<Dialogs::MainList*> ChatFilters::chatsList(FilterId filterId) {
return pointer.get();
}
void ChatFilters::setPreloaded(const QVector<MTPDialogFilter> &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<MTPDialogFilter> &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<MTPDialogFilter> &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()) {

View File

@ -95,6 +95,8 @@ public:
explicit ChatFilters(not_null<Session*> owner);
~ChatFilters();
void setPreloaded(const QVector<MTPDialogFilter> &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<MTPDialogFilter> &list);
bool applyOrder(const QVector<MTPint> &order);
bool applyChange(ChatFilter &filter, ChatFilter &&updated);
void applyInsert(ChatFilter filter, int position);

View File

@ -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<MTPDialogFilter> &result) {
createSession(user, photo, result.v);
}).fail([=](const RPCError &error) {
createSession(user, photo, QVector<MTPDialogFilter>());
}).send();
}
void Step::createSession(
const MTPUser &user,
QImage photo,
const QVector<MTPDialogFilter> &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<Main::SessionSettings>();
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));
}

View File

@ -106,6 +106,10 @@ protected:
return _data;
}
void finish(const MTPUser &user, QImage &&photo = QImage());
void createSession(
const MTPUser &user,
QImage photo,
const QVector<MTPDialogFilter> &filters);
void goBack();

View File

@ -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<SessionSettings>());
void Account::createSession(
const MTPUser &user,
std::unique_ptr<SessionSettings> settings) {
createSession(
user,
QByteArray(),
0,
settings ? std::move(settings) : std::make_unique<SessionSettings>());
}
void Account::createSession(

View File

@ -49,7 +49,9 @@ public:
void start(std::unique_ptr<MTP::Config> config);
[[nodiscard]] uint64 willHaveSessionUniqueId(MTP::Config *config) const;
void createSession(const MTPUser &user);
void createSession(
const MTPUser &user,
std::unique_ptr<SessionSettings> settings = nullptr);
void createSession(
UserId id,
QByteArray serialized,