mirror-chatterino2/src/providers/LinkResolver.cpp

58 lines
1.7 KiB
C++
Raw Normal View History

2018-08-25 20:38:14 +02:00
#include "providers/LinkResolver.hpp"
2018-08-24 11:56:42 +02:00
#include "common/Common.hpp"
#include "common/Env.hpp"
2018-08-24 11:56:42 +02:00
#include "common/NetworkRequest.hpp"
2018-09-06 12:54:28 +02:00
#include "messages/Link.hpp"
#include "singletons/Settings.hpp"
2018-08-24 11:56:42 +02:00
#include <QString>
namespace chatterino {
2018-09-30 18:55:41 +02:00
void LinkResolver::getLinkInfo(
2019-08-20 23:30:39 +02:00
const QString url, QObject *caller,
std::function<void(QString, Link)> successCallback)
2018-08-24 11:56:42 +02:00
{
if (!getSettings()->linkInfoTooltip)
{
successCallback("No link info loaded", Link(Link::Url, url));
return;
}
// Uncomment to test crashes
// QTimer::singleShot(3000, [=]() {
2019-08-20 21:50:36 +02:00
NetworkRequest(Env::get().linkResolverUrl.arg(QString::fromUtf8(
QUrl::toPercentEncoding(url, "", "/:"))))
2019-08-20 23:30:39 +02:00
.caller(caller)
2019-08-20 21:50:36 +02:00
.timeout(30000)
.onSuccess([successCallback, url](auto result) mutable -> Outcome {
auto root = result.parseJson();
auto statusCode = root.value("status").toInt();
QString response = QString();
QString linkString = url;
if (statusCode == 200)
2018-10-21 13:43:02 +02:00
{
2019-08-20 21:50:36 +02:00
response = root.value("tooltip").toString();
if (getSettings()->unshortLinks)
{
linkString = root.value("link").toString();
}
}
2019-08-20 21:50:36 +02:00
else
{
response = root.value("message").toString();
}
successCallback(QUrl::fromPercentEncoding(response.toUtf8()),
Link(Link::Url, linkString));
2018-08-24 11:56:42 +02:00
2019-08-20 21:50:36 +02:00
return Success;
})
.onError([successCallback, url](auto /*result*/) {
successCallback("No link info found", Link(Link::Url, url));
})
.execute();
// });
2018-08-24 11:56:42 +02:00
}
} // namespace chatterino