mirror-chatterino2/src/providers/ffz/FfzEmotes.cpp

143 lines
4.1 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/ffz/FfzEmotes.hpp"
2018-06-05 18:07:17 +02:00
2018-06-26 17:20:03 +02:00
#include "common/UrlFetch.hpp"
2018-06-26 14:09:39 +02:00
#include "debug/Log.hpp"
#include "messages/Image.hpp"
2018-06-05 18:07:17 +02:00
namespace chatterino {
namespace {
QString getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{
auto emote = urls.value(emoteScale);
if (emote.isUndefined()) {
return "";
}
assert(emote.isString());
return "https:" + emote.toString();
}
void fillInEmoteData(const QJsonObject &urls, const QString &code, const QString &tooltip,
2018-06-26 17:06:17 +02:00
EmoteData &emoteData)
2018-06-05 18:07:17 +02:00
{
QString url1x = getEmoteLink(urls, "1");
QString url2x = getEmoteLink(urls, "2");
QString url3x = getEmoteLink(urls, "4");
assert(!url1x.isEmpty());
emoteData.image1x = new Image(url1x, 1, code, tooltip);
2018-06-05 18:07:17 +02:00
if (!url2x.isEmpty()) {
emoteData.image2x = new Image(url2x, 0.5, code, tooltip);
2018-06-05 18:07:17 +02:00
}
if (!url3x.isEmpty()) {
emoteData.image3x = new Image(url3x, 0.25, code, tooltip);
2018-06-05 18:07:17 +02:00
}
}
} // namespace
2018-06-07 12:25:52 +02:00
void FFZEmotes::loadGlobalEmotes()
{
QString url("https://api.frankerfacez.com/v1/set/global");
NetworkRequest request(url);
request.setCaller(QThread::currentThread());
request.setTimeout(30000);
request.onSuccess([this](auto result) {
auto root = result.parseJson();
2018-06-07 12:25:52 +02:00
auto sets = root.value("sets").toObject();
std::vector<QString> codes;
2018-06-07 12:25:52 +02:00
for (const QJsonValue &set : sets) {
auto emoticons = set.toObject().value("emoticons").toArray();
for (const QJsonValue &emote : emoticons) {
QJsonObject object = emote.toObject();
QString code = object.value("name").toString();
int id = object.value("id").toInt();
QJsonObject urls = object.value("urls").toObject();
2018-06-26 17:06:17 +02:00
EmoteData emoteData;
2018-06-07 12:25:52 +02:00
fillInEmoteData(urls, code, code + "<br/>Global FFZ Emote", emoteData);
emoteData.pageLink =
QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
this->globalEmotes.insert(code, emoteData);
codes.push_back(code);
2018-06-07 12:25:52 +02:00
}
this->globalEmoteCodes = codes;
}
return true;
2018-06-07 12:25:52 +02:00
});
request.execute();
2018-06-07 12:25:52 +02:00
}
2018-06-26 17:06:17 +02:00
void FFZEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
2018-06-05 18:07:17 +02:00
{
printf("[FFZEmotes] Reload FFZ Channel Emotes for channel %s\n", qPrintable(channelName));
QString url("https://api.frankerfacez.com/v1/room/" + channelName);
NetworkRequest request(url);
request.setCaller(QThread::currentThread());
request.setTimeout(3000);
request.onSuccess([this, channelName, _map](auto result) {
auto rootNode = result.parseJson();
2018-06-05 18:07:17 +02:00
auto map = _map.lock();
if (_map.expired()) {
return false;
2018-06-05 18:07:17 +02:00
}
map->clear();
auto setsNode = rootNode.value("sets").toObject();
std::vector<QString> codes;
2018-06-05 18:07:17 +02:00
for (const QJsonValue &setNode : setsNode) {
auto emotesNode = setNode.toObject().value("emoticons").toArray();
for (const QJsonValue &emoteNode : emotesNode) {
QJsonObject emoteObject = emoteNode.toObject();
// margins
int id = emoteObject.value("id").toInt();
QString code = emoteObject.value("name").toString();
QJsonObject urls = emoteObject.value("urls").toObject();
2018-07-06 19:23:47 +02:00
auto emote = this->channelEmoteCache_.getOrAdd(id, [id, &code, &urls] {
2018-06-26 17:06:17 +02:00
EmoteData emoteData;
2018-06-05 18:07:17 +02:00
fillInEmoteData(urls, code, code + "<br/>Channel FFZ Emote", emoteData);
emoteData.pageLink =
QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
return emoteData;
});
this->channelEmotes.insert(code, emote);
map->insert(code, emote);
codes.push_back(code);
2018-06-05 18:07:17 +02:00
}
this->channelEmoteCodes[channelName] = codes;
2018-06-05 18:07:17 +02:00
}
return true;
2018-06-05 18:07:17 +02:00
});
request.execute();
2018-06-05 18:07:17 +02:00
}
} // namespace chatterino