mirror-chatterino2/src/providers/bttv/BttvEmotes.cpp

110 lines
3.2 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/bttv/BttvEmotes.hpp"
2018-06-05 17:39:49 +02:00
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-08-02 14:23:27 +02:00
#include "messages/ImageSet.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include <QJsonArray>
#include <QThread>
2018-06-05 17:39:49 +02:00
namespace chatterino {
namespace {
2018-08-02 14:23:27 +02:00
Url getEmoteLink(QString urlTemplate, const EmoteId &id, const QString &emoteScale)
2018-06-05 17:39:49 +02:00
{
urlTemplate.detach();
2018-08-02 14:23:27 +02:00
return {urlTemplate.replace("{{id}}", id.string).replace("{{image}}", emoteScale)};
2018-06-05 17:39:49 +02:00
}
} // namespace
2018-08-02 14:23:27 +02:00
AccessGuard<const EmoteMap> BttvEmotes::accessGlobalEmotes() const
2018-06-05 17:39:49 +02:00
{
2018-08-02 14:23:27 +02:00
return this->globalEmotes_.accessConst();
2018-06-05 17:39:49 +02:00
}
2018-08-02 14:23:27 +02:00
boost::optional<EmotePtr> BttvEmotes::getGlobalEmote(const EmoteName &name)
2018-06-05 17:39:49 +02:00
{
2018-08-02 14:23:27 +02:00
auto emotes = this->globalEmotes_.access();
auto it = emotes->find(name);
2018-06-05 17:39:49 +02:00
2018-08-02 14:23:27 +02:00
if (it == emotes->end()) return boost::none;
return it->second;
}
2018-06-05 17:39:49 +02:00
2018-08-02 14:23:27 +02:00
// FOURTF: never returns anything
// boost::optional<EmotePtr> BttvEmotes::getEmote(const EmoteId &id)
//{
// auto cache = this->channelEmoteCache_.access();
// auto it = cache->find(id);
//
// if (it != cache->end()) {
// auto shared = it->second.lock();
// if (shared) {
// return shared;
// }
// }
//
// return boost::none;
//}
void BttvEmotes::loadGlobalEmotes()
{
auto request = NetworkRequest(QString(globalEmoteApiUrl));
2018-06-05 17:39:49 +02:00
request.setCaller(QThread::currentThread());
2018-08-02 14:23:27 +02:00
request.setTimeout(30000);
request.onSuccess([this](auto result) -> Outcome {
// if (auto shared = weak.lock()) {
auto currentEmotes = this->globalEmotes_.access();
2018-06-05 17:39:49 +02:00
2018-08-02 14:23:27 +02:00
auto pair = this->parseGlobalEmotes(result.parseJson(), *currentEmotes);
2018-06-05 17:39:49 +02:00
2018-08-02 14:23:27 +02:00
if (pair.first) {
*currentEmotes = std::move(pair.second);
2018-06-05 17:39:49 +02:00
}
2018-08-02 14:23:27 +02:00
return pair.first;
// }
return Failure;
2018-06-05 17:39:49 +02:00
});
request.execute();
2018-06-05 17:39:49 +02:00
}
2018-08-02 14:23:27 +02:00
std::pair<Outcome, EmoteMap> BttvEmotes::parseGlobalEmotes(const QJsonObject &jsonRoot,
const EmoteMap &currentEmotes)
{
auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate = QString("https:" + jsonRoot.value("urlTemplate").toString());
for (const QJsonValue &jsonEmote : jsonEmotes) {
auto id = EmoteId{jsonEmote.toObject().value("id").toString()};
auto name = EmoteName{jsonEmote.toObject().value("code").toString()};
auto emote = Emote({name,
ImageSet{Image::fromUrl(getEmoteLink(urlTemplate, id, "1x"), 1),
Image::fromUrl(getEmoteLink(urlTemplate, id, "2x"), 0.5),
Image::fromUrl(getEmoteLink(urlTemplate, id, "3x"), 0.25)},
Tooltip{name.string + "<br />Global Bttv Emote"},
Url{"https://manage.betterttv.net/emotes/" + id.string}});
auto it = currentEmotes.find(name);
if (it != currentEmotes.end() && *it->second == emote) {
// reuse old shared_ptr if nothing changed
emotes[name] = it->second;
} else {
emotes[name] = std::make_shared<Emote>(std::move(emote));
}
}
return {Success, std::move(emotes)};
}
2018-06-05 17:39:49 +02:00
} // namespace chatterino