2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/bttv/BttvEmotes.hpp"
|
2018-06-05 17:39:49 +02:00
|
|
|
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Common.hpp"
|
2018-07-15 14:11:46 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-08-12 00:01:37 +02:00
|
|
|
#include "messages/Emote.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#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-15 22:46:20 +02:00
|
|
|
Url getEmoteLink(QString urlTemplate, const EmoteId &id,
|
|
|
|
const QString &emoteScale)
|
|
|
|
{
|
|
|
|
urlTemplate.detach();
|
2018-06-05 17:39:49 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
return {urlTemplate.replace("{{id}}", id.string)
|
|
|
|
.replace("{{image}}", emoteScale)};
|
2018-08-11 14:20:53 +02:00
|
|
|
}
|
2018-08-15 22:46:20 +02:00
|
|
|
std::pair<Outcome, EmoteMap> parseGlobalEmotes(
|
|
|
|
const QJsonObject &jsonRoot, const EmoteMap ¤tEmotes)
|
|
|
|
{
|
|
|
|
auto emotes = EmoteMap();
|
|
|
|
auto jsonEmotes = jsonRoot.value("emotes").toArray();
|
|
|
|
auto urlTemplate =
|
|
|
|
qS("https:") + jsonRoot.value("urlTemplate").toString();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto jsonEmote : jsonEmotes)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
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)},
|
2018-12-06 15:51:44 +01:00
|
|
|
Tooltip{name.string + "<br />Global BetterTTV Emote"},
|
2018-08-15 22:46:20 +02:00
|
|
|
Url{"https://manage.betterttv.net/emotes/" + id.string}});
|
|
|
|
|
|
|
|
emotes[name] =
|
|
|
|
cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {Success, std::move(emotes)};
|
2018-08-11 17:15:17 +02:00
|
|
|
}
|
2018-08-15 22:46:20 +02:00
|
|
|
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
|
|
|
|
{
|
|
|
|
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
|
|
|
|
static std::mutex mutex;
|
2018-08-11 17:15:17 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
|
|
|
|
}
|
|
|
|
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot)
|
|
|
|
{
|
|
|
|
auto emotes = EmoteMap();
|
|
|
|
auto jsonEmotes = jsonRoot.value("emotes").toArray();
|
|
|
|
auto urlTemplate = "https:" + jsonRoot.value("urlTemplate").toString();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto jsonEmote_ : jsonEmotes)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
auto jsonEmote = jsonEmote_.toObject();
|
|
|
|
|
|
|
|
auto id = EmoteId{jsonEmote.value("id").toString()};
|
|
|
|
auto name = EmoteName{jsonEmote.value("code").toString()};
|
|
|
|
// emoteObject.value("imageType").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)},
|
2018-12-06 15:51:44 +01:00
|
|
|
Tooltip{name.string + "<br />Channel BetterTTV Emote"},
|
2018-08-15 22:46:20 +02:00
|
|
|
Url{"https://manage.betterttv.net/emotes/" + id.string}});
|
|
|
|
|
|
|
|
emotes[name] = cachedOrMake(std::move(emote), id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {Success, std::move(emotes)};
|
|
|
|
}
|
2018-06-05 17:39:49 +02:00
|
|
|
} // namespace
|
|
|
|
|
2018-08-11 17:15:17 +02:00
|
|
|
//
|
|
|
|
// BttvEmotes
|
|
|
|
//
|
2018-08-11 14:20:53 +02:00
|
|
|
BttvEmotes::BttvEmotes()
|
|
|
|
: global_(std::make_shared<EmoteMap>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-12 00:01:37 +02:00
|
|
|
std::shared_ptr<const EmoteMap> BttvEmotes::emotes() 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-12 00:01:37 +02:00
|
|
|
boost::optional<EmotePtr> BttvEmotes::emote(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-10-21 13:43:02 +02:00
|
|
|
if (it == emotes->end())
|
|
|
|
return boost::none;
|
2018-08-02 14:23:27 +02:00
|
|
|
return it->second;
|
|
|
|
}
|
2018-06-05 17:39:49 +02:00
|
|
|
|
2018-08-12 00:01:37 +02:00
|
|
|
void BttvEmotes::loadEmotes()
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
auto request = NetworkRequest(QString(globalEmoteApiUrl));
|
2018-06-05 17:39:49 +02:00
|
|
|
|
2018-07-07 13:08:57 +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
|
|
|
});
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
request.execute();
|
2018-06-05 17:39:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 17:15:17 +02:00
|
|
|
void BttvEmotes::loadChannel(const QString &channelName,
|
|
|
|
std::function<void(EmoteMap &&)> callback)
|
|
|
|
{
|
|
|
|
auto request =
|
|
|
|
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelName);
|
|
|
|
|
|
|
|
request.setCaller(QThread::currentThread());
|
|
|
|
request.setTimeout(3000);
|
|
|
|
|
|
|
|
request.onSuccess([callback = std::move(callback)](auto result) -> Outcome {
|
|
|
|
auto pair = parseChannelEmotes(result.parseJson());
|
2018-10-21 13:43:02 +02:00
|
|
|
if (pair.first)
|
|
|
|
callback(std::move(pair.second));
|
2018-08-11 17:15:17 +02:00
|
|
|
return pair.first;
|
|
|
|
});
|
|
|
|
|
|
|
|
request.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Url getEmoteLink(QString urlTemplate, const EmoteId &id,
|
|
|
|
const QString &emoteScale)
|
|
|
|
{
|
|
|
|
urlTemplate.detach();
|
|
|
|
|
|
|
|
return {urlTemplate.replace("{{id}}", id.string)
|
|
|
|
.replace("{{image}}", emoteScale)};
|
|
|
|
}
|
|
|
|
|
2018-06-05 17:39:49 +02:00
|
|
|
} // namespace chatterino
|