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

167 lines
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
2019-09-22 16:59:51 +02:00
#include <QJsonArray>
#include <QThread>
#include "common/Common.hpp"
2018-07-15 14:11:46 +02:00
#include "common/NetworkRequest.hpp"
#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"
2018-06-05 17:39:49 +02:00
namespace chatterino {
namespace {
QString emoteLinkFormat("https://betterttv.com/emotes/%1");
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
}
2019-09-03 11:27:30 +02:00
Url getEmoteLinkV3(const EmoteId &id, const QString &emoteScale)
{
static const QString urlTemplate(
"https://cdn.betterttv.net/emote/%1/%2");
return {urlTemplate.arg(id.string, emoteScale)};
}
2019-09-03 23:32:22 +02:00
EmotePtr cachedOrMake(Emote &&emote, const EmoteId &id)
{
static std::unordered_map<EmoteId, std::weak_ptr<const Emote>> cache;
static std::mutex mutex;
return cachedOrMakeEmotePtr(std::move(emote), cache, mutex, id);
}
2018-08-15 22:46:20 +02:00
std::pair<Outcome, EmoteMap> parseGlobalEmotes(
2019-09-03 23:32:22 +02:00
const QJsonArray &jsonEmotes, const EmoteMap &currentEmotes)
2018-08-15 22:46:20 +02:00
{
auto emotes = EmoteMap();
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(getEmoteLinkV3(id, "1x"), 1),
Image::fromUrl(getEmoteLinkV3(id, "2x"), 0.5),
Image::fromUrl(getEmoteLinkV3(id, "3x"), 0.25)},
Tooltip{name.string + "<br />Global BetterTTV Emote"},
Url{emoteLinkFormat.arg(id.string)},
});
2018-08-15 22:46:20 +02:00
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
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot)
{
auto emotes = EmoteMap();
2019-09-03 11:27:30 +02:00
auto innerParse = [&jsonRoot, &emotes](const char *key) {
auto jsonEmotes = jsonRoot.value(key).toArray();
for (auto jsonEmote_ : jsonEmotes)
{
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(getEmoteLinkV3(id, "1x"), 1),
Image::fromUrl(getEmoteLinkV3(id, "2x"), 0.5),
Image::fromUrl(getEmoteLinkV3(id, "3x"), 0.25),
},
Tooltip{name.string + "<br />Channel BetterTTV Emote"},
Url{emoteLinkFormat.arg(id.string)},
});
2019-09-03 11:27:30 +02:00
emotes[name] = cachedOrMake(std::move(emote), id);
}
};
innerParse("channelEmotes");
innerParse("sharedEmotes");
2018-08-15 22:46:20 +02:00
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>())
{
}
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
}
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
void BttvEmotes::loadEmotes()
2018-08-02 14:23:27 +02:00
{
2019-08-20 21:50:36 +02:00
NetworkRequest(QString(globalEmoteApiUrl))
.timeout(30000)
.onSuccess([this](auto result) -> Outcome {
auto emotes = this->global_.get();
2019-09-03 23:32:22 +02:00
auto pair = parseGlobalEmotes(result.parseJsonArray(), *emotes);
2019-08-20 21:50:36 +02:00
if (pair.first)
this->global_.set(
std::make_shared<EmoteMap>(std::move(pair.second)));
return pair.first;
})
.execute();
2018-06-05 17:39:49 +02:00
}
2019-09-03 11:27:30 +02:00
void BttvEmotes::loadChannel(const QString &channelId,
2018-08-11 17:15:17 +02:00
std::function<void(EmoteMap &&)> callback)
{
2019-09-03 11:27:30 +02:00
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelId)
2019-08-20 21:50:36 +02:00
.timeout(3000)
.onSuccess([callback = std::move(callback)](auto result) -> Outcome {
auto pair = parseChannelEmotes(result.parseJson());
if (pair.first)
callback(std::move(pair.second));
return pair.first;
})
.execute();
2018-08-11 17:15:17 +02:00
}
2019-08-20 21:50:36 +02:00
/*
2018-08-11 17:15:17 +02:00
static Url getEmoteLink(QString urlTemplate, const EmoteId &id,
const QString &emoteScale)
{
urlTemplate.detach();
return {urlTemplate.replace("{{id}}", id.string)
.replace("{{image}}", emoteScale)};
}
2019-08-20 21:50:36 +02:00
*/
2018-08-11 17:15:17 +02:00
2018-06-05 17:39:49 +02:00
} // namespace chatterino