perf: query fewer historical messages on reconnects (#5001)

Co-authored-by: Ruben Anders <ruben.anders@robotty.de>
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
iProdigy
2023-12-09 10:46:30 -08:00
committed by GitHub
parent 401e097d62
commit 13dc306506
10 changed files with 171 additions and 77 deletions
+68 -56
View File
@@ -18,72 +18,84 @@ namespace chatterino::recentmessages {
using namespace recentmessages::detail;
void load(const QString &channelName, std::weak_ptr<Channel> channelPtr,
ResultCallback onLoaded, ErrorCallback onError)
void load(
const QString &channelName, std::weak_ptr<Channel> channelPtr,
ResultCallback onLoaded, ErrorCallback onError, const int limit,
const std::optional<std::chrono::time_point<std::chrono::system_clock>>
after,
const std::optional<std::chrono::time_point<std::chrono::system_clock>>
before,
const bool jitter)
{
qCDebug(LOG) << "Loading recent messages for" << channelName;
const auto url = constructRecentMessagesUrl(channelName);
const auto url =
constructRecentMessagesUrl(channelName, limit, after, before);
NetworkRequest(url)
.onSuccess([channelPtr, onLoaded](const auto &result) {
auto shared = channelPtr.lock();
if (!shared)
{
return;
}
qCDebug(LOG) << "Successfully loaded recent messages for"
<< shared->getName();
auto root = result.parseJson();
auto parsedMessages = parseRecentMessages(root);
// build the Communi messages into chatterino messages
auto builtMessages =
buildRecentMessages(parsedMessages, shared.get());
postToThread([shared = std::move(shared), root = std::move(root),
messages = std::move(builtMessages),
onLoaded]() mutable {
// Notify user about a possible gap in logs if it returned some messages
// but isn't currently joined to a channel
const auto errorCode = root.value("error_code").toString();
if (!errorCode.isEmpty())
const long delayMs = jitter ? std::rand() % 100 : 0;
QTimer::singleShot(delayMs, [=] {
NetworkRequest(url)
.onSuccess([channelPtr, onLoaded](const auto &result) {
auto shared = channelPtr.lock();
if (!shared)
{
qCDebug(LOG)
<< QString("Got error from API: error_code=%1, "
"channel=%2")
.arg(errorCode, shared->getName());
if (errorCode == "channel_not_joined" && !messages.empty())
{
shared->addMessage(makeSystemMessage(
"Message history service recovering, there may "
"be gaps in the message history."));
}
return;
}
onLoaded(messages);
});
})
.onError([channelPtr, onError](const NetworkResult &result) {
auto shared = channelPtr.lock();
if (!shared)
{
return;
}
qCDebug(LOG) << "Successfully loaded recent messages for"
<< shared->getName();
qCDebug(LOG) << "Failed to load recent messages for"
<< shared->getName();
auto root = result.parseJson();
auto parsedMessages = parseRecentMessages(root);
shared->addMessage(makeSystemMessage(
QStringLiteral(
"Message history service unavailable (Error: %1)")
.arg(result.formatError())));
// build the Communi messages into chatterino messages
auto builtMessages =
buildRecentMessages(parsedMessages, shared.get());
onError();
})
.execute();
postToThread([shared = std::move(shared),
root = std::move(root),
messages = std::move(builtMessages),
onLoaded]() mutable {
// Notify user about a possible gap in logs if it returned some messages
// but isn't currently joined to a channel
const auto errorCode = root.value("error_code").toString();
if (!errorCode.isEmpty())
{
qCDebug(LOG)
<< QString("Got error from API: error_code=%1, "
"channel=%2")
.arg(errorCode, shared->getName());
if (errorCode == "channel_not_joined" &&
!messages.empty())
{
shared->addMessage(makeSystemMessage(
"Message history service recovering, there may "
"be gaps in the message history."));
}
}
onLoaded(messages);
});
})
.onError([channelPtr, onError](const NetworkResult &result) {
auto shared = channelPtr.lock();
if (!shared)
{
return;
}
qCDebug(LOG) << "Failed to load recent messages for"
<< shared->getName();
shared->addMessage(makeSystemMessage(
QStringLiteral(
"Message history service unavailable (Error: %1)")
.arg(result.formatError())));
onError();
})
.execute();
});
}
} // namespace chatterino::recentmessages