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

175 lines
4.9 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-08-02 14:23:27 +02:00
#include <QJsonArray>
2018-07-15 14:11:46 +02:00
#include "common/NetworkRequest.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 {
2018-08-02 14:23:27 +02:00
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
2018-06-05 18:07:17 +02:00
{
auto emote = urls.value(emoteScale);
if (emote.isUndefined()) {
2018-08-02 14:23:27 +02:00
return {""};
2018-06-05 18:07:17 +02:00
}
assert(emote.isString());
2018-08-02 14:23:27 +02:00
return {"https:" + emote.toString()};
2018-06-05 18:07:17 +02:00
}
2018-08-06 21:17:03 +02:00
void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
const QString &tooltip, Emote &emoteData)
2018-06-05 18:07:17 +02:00
{
2018-08-02 14:23:27 +02:00
auto url1x = getEmoteLink(urls, "1");
auto url2x = getEmoteLink(urls, "2");
auto url3x = getEmoteLink(urls, "4");
//, code, tooltip
emoteData.name = name;
emoteData.images =
2018-08-06 21:17:03 +02:00
ImageSet{Image::fromUrl(url1x, 1), Image::fromUrl(url2x, 0.5),
Image::fromUrl(url3x, 0.25)};
2018-08-02 14:23:27 +02:00
emoteData.tooltip = {tooltip};
}
} // namespace
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
AccessGuard<const EmoteCache<EmoteName>> FfzEmotes::accessGlobalEmotes() const
{
return this->globalEmotes_.accessConst();
}
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
boost::optional<EmotePtr> FfzEmotes::getEmote(const EmoteId &id)
{
auto cache = this->channelEmoteCache_.access();
auto it = cache->find(id);
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
if (it != cache->end()) {
auto shared = it->second.lock();
if (shared) {
return shared;
}
2018-06-05 18:07:17 +02:00
}
2018-08-02 14:23:27 +02:00
return boost::none;
2018-06-05 18:07:17 +02:00
}
2018-08-02 14:23:27 +02:00
boost::optional<EmotePtr> FfzEmotes::getGlobalEmote(const EmoteName &name)
{
return this->globalEmotes_.access()->get(name);
}
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
void FfzEmotes::loadGlobalEmotes()
2018-06-07 12:25:52 +02:00
{
QString url("https://api.frankerfacez.com/v1/set/global");
NetworkRequest request(url);
request.setCaller(QThread::currentThread());
request.setTimeout(30000);
2018-08-06 21:17:03 +02:00
request.onSuccess([this](auto result) -> Outcome {
return this->parseGlobalEmotes(result.parseJson());
});
2018-08-02 14:23:27 +02:00
request.execute();
}
2018-06-07 12:25:52 +02:00
2018-08-02 14:23:27 +02:00
Outcome FfzEmotes::parseGlobalEmotes(const QJsonObject &jsonRoot)
{
auto jsonSets = jsonRoot.value("sets").toObject();
auto emotes = this->globalEmotes_.access();
auto replacement = emotes->makeReplacment();
2018-06-07 12:25:52 +02:00
2018-08-02 14:23:27 +02:00
for (auto jsonSet : jsonSets) {
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray();
2018-06-07 12:25:52 +02:00
2018-08-02 14:23:27 +02:00
for (auto jsonEmoteValue : jsonEmotes) {
auto jsonEmote = jsonEmoteValue.toObject();
2018-06-07 12:25:52 +02:00
2018-08-02 14:23:27 +02:00
auto name = EmoteName{jsonEmote.value("name").toString()};
auto id = EmoteId{jsonEmote.value("id").toString()};
auto urls = jsonEmote.value("urls").toObject();
2018-06-07 12:25:52 +02:00
2018-08-02 14:23:27 +02:00
auto emote = Emote();
2018-08-06 21:17:03 +02:00
fillInEmoteData(urls, name, name.string + "<br/>Global FFZ Emote",
emote);
emote.homePage =
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
.arg(id.string)
.arg(name.string)};
2018-06-07 12:25:52 +02:00
2018-08-02 14:23:27 +02:00
replacement.add(name, emote);
2018-06-07 12:25:52 +02:00
}
2018-08-02 14:23:27 +02:00
}
2018-08-02 14:23:27 +02:00
return Success;
2018-06-07 12:25:52 +02:00
}
2018-08-02 14:23:27 +02:00
void FfzEmotes::loadChannelEmotes(const QString &channelName,
std::function<void(EmoteMap &&)> callback)
2018-06-05 18:07:17 +02:00
{
2018-08-06 21:17:03 +02:00
// printf("[FFZEmotes] Reload FFZ Channel Emotes for channel %s\n",
// qPrintable(channelName));
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// QString url("https://api.frankerfacez.com/v1/room/" + channelName);
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// NetworkRequest request(url);
// request.setCaller(QThread::currentThread());
// request.setTimeout(3000);
// request.onSuccess([this, channelName, _map](auto result) -> Outcome {
// return this->parseChannelEmotes(result.parseJson());
//});
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// request.execute();
}
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
Outcome parseChannelEmotes(const QJsonObject &jsonRoot)
{
// auto rootNode = result.parseJson();
// auto map = _map.lock();
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// if (_map.expired()) {
// return false;
//}
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// map->clear();
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// auto setsNode = rootNode.value("sets").toObject();
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// std::vector<QString> codes;
// for (const QJsonValue &setNode : setsNode) {
// auto emotesNode = setNode.toObject().value("emoticons").toArray();
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// for (const QJsonValue &emoteNode : emotesNode) {
// QJsonObject emoteObject = emoteNode.toObject();
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// // margins
// int id = emoteObject.value("id").toInt();
// QString code = emoteObject.value("name").toString();
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// QJsonObject urls = emoteObject.value("urls").toObject();
2018-06-05 18:07:17 +02:00
2018-08-06 21:17:03 +02:00
// auto emote = this->channelEmoteCache_.getOrAdd(id, [id, &code,
// &urls] {
2018-08-02 14:23:27 +02:00
// EmoteData emoteData;
2018-08-06 21:17:03 +02:00
// fillInEmoteData(urls, code, code + "<br/>Channel FFZ Emote",
// emoteData); emoteData.pageLink =
2018-08-02 14:23:27 +02:00
// QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
2018-06-05 18:07:17 +02:00
2018-08-02 14:23:27 +02:00
// return emoteData;
// });
2018-08-02 14:23:27 +02:00
// this->channelEmotes.insert(code, emote);
// map->insert(code, emote);
// codes.push_back(code);
// }
2018-08-02 14:23:27 +02:00
// this->channelEmoteCodes[channelName] = codes;
//}
return Success;
2018-06-05 18:07:17 +02:00
}
} // namespace chatterino