2018-08-25 20:38:14 +02:00
|
|
|
#include "providers/LinkResolver.hpp"
|
2018-08-24 11:56:42 +02:00
|
|
|
|
|
|
|
#include "common/Common.hpp"
|
2019-05-26 21:44:13 +02:00
|
|
|
#include "common/Env.hpp"
|
2018-08-24 11:56:42 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2020-05-10 12:11:10 +02:00
|
|
|
#include "messages/Image.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(
|
2019-08-20 23:30:39 +02:00
|
|
|
const QString url, QObject *caller,
|
2020-05-10 12:11:10 +02:00
|
|
|
std::function<void(QString, Link, ImagePtr)> successCallback)
|
2018-08-24 11:56:42 +02:00
|
|
|
{
|
2019-05-10 23:31:10 +02:00
|
|
|
if (!getSettings()->linkInfoTooltip)
|
|
|
|
{
|
2020-05-10 12:11:10 +02:00
|
|
|
successCallback("No link info loaded", Link(Link::Url, url), nullptr);
|
2019-03-28 19:09:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-10-24 11:36:36 +02:00
|
|
|
// 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)
|
2022-05-22 13:16:16 +02:00
|
|
|
.onSuccess([successCallback,
|
|
|
|
url](NetworkResult result) mutable -> Outcome {
|
|
|
|
auto root = result.parseJson();
|
|
|
|
auto statusCode = root.value("status").toInt();
|
|
|
|
QString response;
|
|
|
|
QString linkString = url;
|
|
|
|
ImagePtr thumbnail = nullptr;
|
|
|
|
if (statusCode == 200)
|
|
|
|
{
|
|
|
|
response = root.value("tooltip").toString();
|
|
|
|
|
|
|
|
if (root.contains("thumbnail"))
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
2020-12-05 15:03:13 +01:00
|
|
|
thumbnail =
|
|
|
|
Image::fromUrl({root.value("thumbnail").toString()});
|
2019-08-20 21:50:36 +02:00
|
|
|
}
|
2022-05-22 13:16:16 +02:00
|
|
|
if (getSettings()->unshortLinks)
|
2020-05-10 12:11:10 +02:00
|
|
|
{
|
2022-05-22 13:16:16 +02:00
|
|
|
linkString = root.value("link").toString();
|
2020-05-10 12:11:10 +02:00
|
|
|
}
|
2022-05-22 13:16:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = root.value("message").toString();
|
|
|
|
}
|
|
|
|
successCallback(QUrl::fromPercentEncoding(response.toUtf8()),
|
|
|
|
Link(Link::Url, linkString), thumbnail);
|
2018-08-24 11:56:42 +02:00
|
|
|
|
2022-05-22 13:16:16 +02:00
|
|
|
return Success;
|
|
|
|
})
|
2019-08-20 21:50:36 +02:00
|
|
|
.onError([successCallback, url](auto /*result*/) {
|
2020-05-10 12:11:10 +02:00
|
|
|
successCallback("No link info found", Link(Link::Url, url),
|
|
|
|
nullptr);
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.execute();
|
2018-10-24 11:36:36 +02:00
|
|
|
// });
|
2018-08-24 11:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|