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/NetworkRequest.hpp"
|
2018-09-06 12:54:28 +02:00
|
|
|
#include "messages/Link.hpp"
|
2018-09-06 16:59:49 +02:00
|
|
|
#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(
|
|
|
|
const QString url, std::function<void(QString, Link)> successCallback)
|
2018-08-24 11:56:42 +02:00
|
|
|
{
|
2019-03-28 19:09:09 +01:00
|
|
|
if (!getSettings()->linkInfoTooltip) {
|
|
|
|
successCallback("No link info loaded", Link(Link::Url, url));
|
|
|
|
return;
|
|
|
|
}
|
2018-09-30 18:55:41 +02:00
|
|
|
QString requestUrl("https://braize.pajlada.com/chatterino/link_resolver/" +
|
|
|
|
QUrl::toPercentEncoding(url, "", "/:"));
|
2018-10-24 11:36:36 +02:00
|
|
|
// Uncomment to test crashes
|
|
|
|
// QTimer::singleShot(3000, [=]() {
|
2018-08-24 11:56:42 +02:00
|
|
|
NetworkRequest request(requestUrl);
|
|
|
|
request.setCaller(QThread::currentThread());
|
|
|
|
request.setTimeout(30000);
|
2018-09-06 12:54:28 +02:00
|
|
|
request.onSuccess([successCallback, url](auto result) mutable -> Outcome {
|
2018-08-24 11:56:42 +02:00
|
|
|
auto root = result.parseJson();
|
2018-08-26 15:25:51 +02:00
|
|
|
auto statusCode = root.value("status").toInt();
|
|
|
|
QString response = QString();
|
2018-09-06 12:54:28 +02:00
|
|
|
QString linkString = url;
|
2018-10-21 13:43:02 +02:00
|
|
|
if (statusCode == 200)
|
|
|
|
{
|
2018-08-26 15:25:51 +02:00
|
|
|
response = root.value("tooltip").toString();
|
2018-10-31 19:45:51 +01:00
|
|
|
if (getSettings()->unshortLinks)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-09-06 16:59:49 +02:00
|
|
|
linkString = root.value("link").toString();
|
|
|
|
}
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-26 15:25:51 +02:00
|
|
|
response = root.value("message").toString();
|
|
|
|
}
|
2018-09-30 18:55:41 +02:00
|
|
|
successCallback(QUrl::fromPercentEncoding(response.toUtf8()),
|
|
|
|
Link(Link::Url, linkString));
|
2018-08-24 11:56:42 +02:00
|
|
|
|
|
|
|
return Success;
|
|
|
|
});
|
|
|
|
|
2018-09-06 12:54:28 +02:00
|
|
|
request.onError([successCallback, url](auto result) {
|
|
|
|
successCallback("No link info found", Link(Link::Url, url));
|
2018-08-24 11:56:42 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
request.execute();
|
2018-10-24 11:36:36 +02:00
|
|
|
// });
|
2018-08-24 11:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|