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>
|
|
|
|
|
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-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"
|
2020-05-16 12:43:44 +02:00
|
|
|
#include "messages/MessageBuilder.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
|
|
|
|
2018-06-05 17:39:49 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace {
|
2019-12-31 21:21:53 +01:00
|
|
|
|
|
|
|
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 ¤tEmotes)
|
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()};
|
|
|
|
|
2019-12-31 21:21:53 +01:00
|
|
|
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
|
|
|
}
|
2020-06-13 14:15:22 +02:00
|
|
|
std::pair<Outcome, EmoteMap> parseChannelEmotes(const QJsonObject &jsonRoot,
|
|
|
|
const QString &userName)
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
|
|
|
auto emotes = EmoteMap();
|
|
|
|
|
2020-06-13 14:15:22 +02:00
|
|
|
auto innerParse = [&jsonRoot, &emotes, &userName](const char *key) {
|
2019-09-03 11:27:30 +02:00
|
|
|
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()};
|
2020-06-13 14:15:22 +02:00
|
|
|
auto author = EmoteAuthor{jsonEmote.value("user")
|
|
|
|
.toObject()
|
|
|
|
.value("name")
|
|
|
|
.toString()};
|
2019-09-03 11:27:30 +02:00
|
|
|
// emoteObject.value("imageType").toString();
|
|
|
|
|
2019-12-31 21:21:53 +01:00
|
|
|
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),
|
|
|
|
},
|
2020-06-13 14:15:22 +02:00
|
|
|
Tooltip{name.string + "<br>Channel BetterTTV Emote" +
|
|
|
|
((author.string.isEmpty())
|
|
|
|
? "<br>By: " + userName.toUtf8()
|
|
|
|
: "<br>By: " + author.string)},
|
2019-12-31 21:21:53 +01:00
|
|
|
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>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-05-16 12:43:44 +02:00
|
|
|
void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
|
2020-06-13 14:15:22 +02:00
|
|
|
const QString &channelId, const QString &userName,
|
2020-05-16 12:43:44 +02:00
|
|
|
std::function<void(EmoteMap &&)> callback,
|
|
|
|
bool manualRefresh)
|
2018-08-11 17:15:17 +02:00
|
|
|
{
|
2019-09-03 11:27:30 +02:00
|
|
|
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelId)
|
2019-08-20 21:50:36 +02:00
|
|
|
.timeout(3000)
|
2020-06-13 14:15:22 +02:00
|
|
|
.onSuccess([callback = std::move(callback), channel, &userName,
|
2020-05-16 12:43:44 +02:00
|
|
|
manualRefresh](auto result) -> Outcome {
|
2020-06-13 14:15:22 +02:00
|
|
|
auto pair = parseChannelEmotes(result.parseJson(), userName);
|
2019-08-20 21:50:36 +02:00
|
|
|
if (pair.first)
|
|
|
|
callback(std::move(pair.second));
|
2020-05-16 12:43:44 +02:00
|
|
|
if (auto shared = channel.lock(); manualRefresh)
|
|
|
|
shared->addMessage(
|
|
|
|
makeSystemMessage("BetterTTV channel emotes reloaded."));
|
2019-08-20 21:50:36 +02:00
|
|
|
return pair.first;
|
|
|
|
})
|
2020-05-16 12:43:44 +02:00
|
|
|
.onError([channelId, channel, manualRefresh](auto result) {
|
|
|
|
auto shared = channel.lock();
|
|
|
|
if (!shared)
|
|
|
|
return;
|
|
|
|
if (result.status() == 203)
|
|
|
|
{
|
|
|
|
// User does not have any BTTV emotes
|
|
|
|
if (manualRefresh)
|
|
|
|
shared->addMessage(makeSystemMessage(
|
|
|
|
"This channel has no BetterTTV channel emotes."));
|
|
|
|
}
|
|
|
|
else if (result.status() == NetworkResult::timedoutStatus)
|
|
|
|
{
|
|
|
|
// TODO: Auto retry in case of a timeout, with a delay
|
|
|
|
qDebug() << "Fetching BTTV emotes for channel" << channelId
|
|
|
|
<< "failed due to timeout";
|
|
|
|
shared->addMessage(makeSystemMessage(
|
|
|
|
"Failed to fetch BetterTTV channel emotes. (timed out)"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Error fetching BTTV emotes for channel"
|
|
|
|
<< channelId << ", error" << result.status();
|
|
|
|
shared->addMessage(
|
|
|
|
makeSystemMessage("Failed to fetch BetterTTV channel "
|
|
|
|
"emotes. (unknown error)"));
|
|
|
|
}
|
|
|
|
})
|
2019-08-20 21:50:36 +02:00
|
|
|
.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
|