mirror of https://github.com/procxx/kepka.git
Cache updater username resolve results.
This commit is contained in:
parent
267293d21b
commit
8f63fa71c9
|
@ -247,7 +247,9 @@ private:
|
||||||
using Checker::fail;
|
using Checker::fail;
|
||||||
base::lambda<void(const RPCError &error)> failHandler();
|
base::lambda<void(const RPCError &error)> failHandler();
|
||||||
|
|
||||||
void gotFeed(const MTPcontacts_ResolvedPeer &result);
|
void resolveChannel(
|
||||||
|
const QString &username,
|
||||||
|
base::lambda<void(const MTPInputChannel &channel)> callback);
|
||||||
void gotMessage(const MTPmessages_Messages &result);
|
void gotMessage(const MTPmessages_Messages &result);
|
||||||
base::optional<FileLocation> parseMessage(
|
base::optional<FileLocation> parseMessage(
|
||||||
const MTPmessages_Messages &result) const;
|
const MTPmessages_Messages &result) const;
|
||||||
|
@ -255,9 +257,6 @@ private:
|
||||||
FileLocation validateLatestLocation(
|
FileLocation validateLatestLocation(
|
||||||
uint64 availableVersion,
|
uint64 availableVersion,
|
||||||
const FileLocation &location) const;
|
const FileLocation &location) const;
|
||||||
void resolvedFiles(
|
|
||||||
const MTPcontacts_ResolvedPeer &result,
|
|
||||||
const FileLocation &location);
|
|
||||||
void gotFile(const MTPmessages_Messages &result);
|
void gotFile(const MTPmessages_Messages &result);
|
||||||
base::optional<ParsedFile> parseFile(
|
base::optional<ParsedFile> parseFile(
|
||||||
const MTPmessages_Messages &result) const;
|
const MTPmessages_Messages &result) const;
|
||||||
|
@ -1255,22 +1254,12 @@ void MtpChecker::start() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
constexpr auto kFeedUsername = "tdhbcfeed";
|
constexpr auto kFeedUsername = "tdhbcfeed";
|
||||||
|
resolveChannel(kFeedUsername, [=](const MTPInputChannel &channel) {
|
||||||
_mtp.send(
|
|
||||||
MTPcontacts_ResolveUsername(MTP_string(kFeedUsername)),
|
|
||||||
[=](const MTPcontacts_ResolvedPeer &result) { gotFeed(result); },
|
|
||||||
failHandler());
|
|
||||||
}
|
|
||||||
|
|
||||||
void MtpChecker::gotFeed(const MTPcontacts_ResolvedPeer &result) {
|
|
||||||
Expects(result.type() == mtpc_contacts_resolvedPeer);
|
|
||||||
|
|
||||||
if (const auto channel = ExtractChannel(result)) {
|
|
||||||
_mtp.send(
|
_mtp.send(
|
||||||
MTPmessages_GetHistory(
|
MTPmessages_GetHistory(
|
||||||
MTP_inputPeerChannel(
|
MTP_inputPeerChannel(
|
||||||
channel->c_inputChannel().vchannel_id,
|
channel.c_inputChannel().vchannel_id,
|
||||||
channel->c_inputChannel().vaccess_hash),
|
channel.c_inputChannel().vaccess_hash),
|
||||||
MTP_int(0), // offset_id
|
MTP_int(0), // offset_id
|
||||||
MTP_int(0), // offset_date
|
MTP_int(0), // offset_date
|
||||||
MTP_int(0), // add_offset
|
MTP_int(0), // add_offset
|
||||||
|
@ -1280,28 +1269,74 @@ void MtpChecker::gotFeed(const MTPcontacts_ResolvedPeer &result) {
|
||||||
MTP_int(0)), // hash
|
MTP_int(0)), // hash
|
||||||
[=](const MTPmessages_Messages &result) { gotMessage(result); },
|
[=](const MTPmessages_Messages &result) { gotMessage(result); },
|
||||||
failHandler());
|
failHandler());
|
||||||
} else {
|
});
|
||||||
LOG(("Update Error: MTP feed channel not found."));
|
}
|
||||||
|
|
||||||
|
void MtpChecker::resolveChannel(
|
||||||
|
const QString &username,
|
||||||
|
base::lambda<void(const MTPInputChannel &channel)> callback) {
|
||||||
|
const auto failed = [&] {
|
||||||
|
LOG(("Update Error: MTP channel '%1' resolve failed."
|
||||||
|
).arg(username));
|
||||||
fail();
|
fail();
|
||||||
|
};
|
||||||
|
if (!AuthSession::Exists()) {
|
||||||
|
failed();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ResolveResult {
|
||||||
|
base::weak_ptr<AuthSession> auth;
|
||||||
|
MTPInputChannel channel;
|
||||||
|
};
|
||||||
|
static std::map<QString, ResolveResult> ResolveCache;
|
||||||
|
|
||||||
|
const auto i = ResolveCache.find(username);
|
||||||
|
if (i != end(ResolveCache)) {
|
||||||
|
if (i->second.auth.get() == &Auth()) {
|
||||||
|
callback(i->second.channel);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ResolveCache.erase(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto doneHandler = [=](const MTPcontacts_ResolvedPeer &result) {
|
||||||
|
Expects(result.type() == mtpc_contacts_resolvedPeer);
|
||||||
|
|
||||||
|
if (const auto channel = ExtractChannel(result)) {
|
||||||
|
ResolveCache.emplace(
|
||||||
|
username,
|
||||||
|
ResolveResult { base::make_weak(&Auth()), *channel });
|
||||||
|
callback(*channel);
|
||||||
|
} else {
|
||||||
|
failed();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
_mtp.send(
|
||||||
|
MTPcontacts_ResolveUsername(MTP_string(username)),
|
||||||
|
doneHandler,
|
||||||
|
failHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MtpChecker::gotMessage(const MTPmessages_Messages &result) {
|
void MtpChecker::gotMessage(const MTPmessages_Messages &result) {
|
||||||
if (const auto location = parseMessage(result)) {
|
const auto location = parseMessage(result);
|
||||||
if (location->username.isEmpty()) {
|
if (!location) {
|
||||||
done(nullptr);
|
|
||||||
} else {
|
|
||||||
const auto got = [=](const MTPcontacts_ResolvedPeer &result) {
|
|
||||||
resolvedFiles(result, *location);
|
|
||||||
};
|
|
||||||
_mtp.send(
|
|
||||||
MTPcontacts_ResolveUsername(MTP_string(location->username)),
|
|
||||||
got,
|
|
||||||
failHandler());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fail();
|
fail();
|
||||||
|
return;
|
||||||
|
} else if (location->username.isEmpty()) {
|
||||||
|
done(nullptr);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
resolveChannel(location->username, [=](const MTPInputChannel &channel) {
|
||||||
|
_mtp.send(
|
||||||
|
MTPchannels_GetMessages(
|
||||||
|
channel,
|
||||||
|
MTP_vector<MTPInputMessage>(
|
||||||
|
1,
|
||||||
|
MTP_inputMessageID(MTP_int(location->postId)))),
|
||||||
|
[=](const MTPmessages_Messages &result) { gotFile(result); },
|
||||||
|
failHandler());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
auto MtpChecker::parseMessage(const MTPmessages_Messages &result) const
|
auto MtpChecker::parseMessage(const MTPmessages_Messages &result) const
|
||||||
|
@ -1371,27 +1406,6 @@ auto MtpChecker::validateLatestLocation(
|
||||||
return (availableVersion <= myVersion) ? FileLocation() : location;
|
return (availableVersion <= myVersion) ? FileLocation() : location;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MtpChecker::resolvedFiles(
|
|
||||||
const MTPcontacts_ResolvedPeer &result,
|
|
||||||
const FileLocation &location) {
|
|
||||||
Expects(result.type() == mtpc_contacts_resolvedPeer);
|
|
||||||
|
|
||||||
if (const auto channel = ExtractChannel(result)) {
|
|
||||||
_mtp.send(
|
|
||||||
MTPchannels_GetMessages(
|
|
||||||
*channel,
|
|
||||||
MTP_vector<MTPInputMessage>(
|
|
||||||
1,
|
|
||||||
MTP_inputMessageID(MTP_int(location.postId)))),
|
|
||||||
[=](const MTPmessages_Messages &result) { gotFile(result); },
|
|
||||||
failHandler());
|
|
||||||
} else {
|
|
||||||
LOG(("Update Error: MTP files channel not found: '%1'."
|
|
||||||
).arg(location.username));
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MtpChecker::gotFile(const MTPmessages_Messages &result) {
|
void MtpChecker::gotFile(const MTPmessages_Messages &result) {
|
||||||
if (const auto file = parseFile(result)) {
|
if (const auto file = parseFile(result)) {
|
||||||
done(std::make_shared<MtpLoader>(
|
done(std::make_shared<MtpLoader>(
|
||||||
|
@ -1576,7 +1590,7 @@ private:
|
||||||
void startImplementation(
|
void startImplementation(
|
||||||
not_null<Implementation*> which,
|
not_null<Implementation*> which,
|
||||||
std::unique_ptr<Checker> checker);
|
std::unique_ptr<Checker> checker);
|
||||||
void tryLoaders();
|
bool tryLoaders();
|
||||||
void handleTimeout();
|
void handleTimeout();
|
||||||
void checkerDone(
|
void checkerDone(
|
||||||
not_null<Implementation*> which,
|
not_null<Implementation*> which,
|
||||||
|
@ -1830,20 +1844,19 @@ void Updater::handleTimeout() {
|
||||||
};
|
};
|
||||||
reset(_httpImplementation);
|
reset(_httpImplementation);
|
||||||
reset(_mtpImplementation);
|
reset(_mtpImplementation);
|
||||||
tryLoaders();
|
if (!tryLoaders()) {
|
||||||
|
cSetLastUpdateCheck(0);
|
||||||
|
_timer.callOnce(kUpdaterTimeout);
|
||||||
|
}
|
||||||
} else if (_action == Action::Loading) {
|
} else if (_action == Action::Loading) {
|
||||||
_failed.fire({});
|
_failed.fire({});
|
||||||
}
|
}
|
||||||
if (_action == Action::Waiting) {
|
|
||||||
cSetLastUpdateCheck(0);
|
|
||||||
_timer.callOnce(kUpdaterTimeout);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Updater::tryLoaders() {
|
bool Updater::tryLoaders() {
|
||||||
if (_httpImplementation.checker || _mtpImplementation.checker) {
|
if (_httpImplementation.checker || _mtpImplementation.checker) {
|
||||||
// Some checkers didn't finish yet.
|
// Some checkers didn't finish yet.
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
_retryTimer.cancel();
|
_retryTimer.cancel();
|
||||||
|
|
||||||
|
@ -1871,6 +1884,7 @@ void Updater::tryLoaders() {
|
||||||
};
|
};
|
||||||
if (_mtpImplementation.failed && _httpImplementation.failed) {
|
if (_mtpImplementation.failed && _httpImplementation.failed) {
|
||||||
_failed.fire({});
|
_failed.fire({});
|
||||||
|
return false;
|
||||||
} else if (!_mtpImplementation.loader) {
|
} else if (!_mtpImplementation.loader) {
|
||||||
tryOne(_httpImplementation);
|
tryOne(_httpImplementation);
|
||||||
} else if (!_httpImplementation.loader) {
|
} else if (!_httpImplementation.loader) {
|
||||||
|
@ -1881,6 +1895,7 @@ void Updater::tryLoaders() {
|
||||||
: _httpImplementation);
|
: _httpImplementation);
|
||||||
_usingMtprotoLoader = !_usingMtprotoLoader;
|
_usingMtprotoLoader = !_usingMtprotoLoader;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Updater::finalize(QString filepath) {
|
void Updater::finalize(QString filepath) {
|
||||||
|
|
Loading…
Reference in New Issue