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

88 lines
2.5 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-06 21:17:03 +02:00
Url getEmoteLink(QString urlTemplate, const EmoteId &id,
const QString &emoteScale)
2018-06-05 17:39:49 +02:00
{
urlTemplate.detach();
2018-08-06 21:17:03 +02:00
return {urlTemplate.replace("{{id}}", id.string)
.replace("{{image}}", emoteScale)};
2018-06-05 17:39:49 +02:00
}
2018-08-11 14:20:53 +02:00
std::pair<Outcome, EmoteMap> parseGlobalEmotes(const QJsonObject &jsonRoot,
const EmoteMap &currentEmotes)
{
auto emotes = EmoteMap();
auto jsonEmotes = jsonRoot.value("emotes").toArray();
auto urlTemplate = qS("https:") + jsonRoot.value("urlTemplate").toString();
2018-06-05 17:39:49 +02:00
2018-08-11 14:20:53 +02:00
for (auto 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}});
emotes[name] = cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
}
return {Success, std::move(emotes)};
}
2018-06-05 17:39:49 +02:00
} // namespace
2018-08-11 14:20:53 +02:00
BttvEmotes::BttvEmotes()
: global_(std::make_shared<EmoteMap>())
{
}
std::shared_ptr<const EmoteMap> BttvEmotes::global() const
2018-06-05 17:39:49 +02:00
{
2018-08-11 14:20:53 +02:00
return this->global_.get();
2018-06-05 17:39:49 +02:00
}
2018-08-11 14:20:53 +02:00
boost::optional<EmotePtr> BttvEmotes::global(const EmoteName &name) const
2018-06-05 17:39:49 +02:00
{
2018-08-11 14:20:53 +02:00
auto emotes = this->global_.get();
2018-08-02 14:23:27 +02:00
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-11 14:20:53 +02:00
void BttvEmotes::loadGlobal()
2018-08-02 14:23:27 +02:00
{
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);
2018-06-05 17:39:49 +02:00
2018-08-11 14:20:53 +02:00
request.onSuccess([this](auto result) -> Outcome {
auto emotes = this->global_.get();
auto pair = parseGlobalEmotes(result.parseJson(), *emotes);
if (pair.first)
this->global_.set(
std::make_shared<EmoteMap>(std::move(pair.second)));
2018-08-02 14:23:27 +02:00
return pair.first;
2018-06-05 17:39:49 +02:00
});
request.execute();
2018-06-05 17:39:49 +02:00
}
} // namespace chatterino