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

363 lines
12 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"
#include "common/NetworkResult.hpp"
#include "common/QLogging.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 "messages/MessageBuilder.hpp"
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
2018-08-02 14:23:27 +02:00
#include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Settings.hpp"
2018-08-02 14:23:27 +02:00
#include <QJsonArray>
#include <QThread>
2018-06-05 17:39:49 +02:00
namespace chatterino {
namespace {
const QString CHANNEL_HAS_NO_EMOTES(
"This channel has no BetterTTV channel emotes.");
QString emoteLinkFormat("https://betterttv.com/emotes/%1");
struct CreateEmoteResult {
EmoteId id;
EmoteName name;
Emote emote;
};
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
}
CreateEmoteResult createChannelEmote(const QString &channelDisplayName,
const QJsonObject &jsonEmote)
{
auto id = EmoteId{jsonEmote.value("id").toString()};
auto name = EmoteName{jsonEmote.value("code").toString()};
auto author = EmoteAuthor{
jsonEmote.value("user").toObject().value("displayName").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{
QString("%1<br>%2 BetterTTV Emote<br>By: %3")
.arg(name.string)
// when author is empty, it is a channel emote created by the broadcaster
.arg(author.string.isEmpty() ? "Channel" : "Shared")
.arg(author.string.isEmpty() ? channelDisplayName
: author.string)},
Url{emoteLinkFormat.arg(id.string)},
false,
id,
});
return {id, name, emote};
}
bool updateChannelEmote(Emote &emote, const QString &channelDisplayName,
const QJsonObject &jsonEmote)
{
bool anyModifications = false;
if (jsonEmote.contains("code"))
{
emote.name = EmoteName{jsonEmote.value("code").toString()};
anyModifications = true;
}
if (jsonEmote.contains("user"))
{
emote.author = EmoteAuthor{jsonEmote.value("user")
.toObject()
.value("displayName")
.toString()};
anyModifications = true;
}
if (anyModifications)
{
emote.tooltip = Tooltip{
QString("%1<br>%2 BetterTTV Emote<br>By: %3")
.arg(emote.name.string)
// when author is empty, it is a channel emote created by the broadcaster
.arg(emote.author.string.isEmpty() ? "Channel" : "Shared")
.arg(emote.author.string.isEmpty() ? channelDisplayName
: emote.author.string)};
}
return anyModifications;
}
std::pair<Outcome, EmoteMap> parseChannelEmotes(
const QJsonObject &jsonRoot, const QString &channelDisplayName)
2018-08-15 22:46:20 +02:00
{
auto emotes = EmoteMap();
auto innerParse = [&jsonRoot, &emotes,
&channelDisplayName](const char *key) {
2019-09-03 11:27:30 +02:00
auto jsonEmotes = jsonRoot.value(key).toArray();
for (auto jsonEmote_ : jsonEmotes)
{
auto emote = createChannelEmote(channelDisplayName,
jsonEmote_.toObject());
emotes[emote.name] =
cachedOrMake(std::move(emote.emote), emote.id);
2019-09-03 11:27:30 +02:00
}
};
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
{
if (!Settings::instance().enableBTTVGlobalEmotes)
{
this->global_.set(EMPTY_EMOTE_MAP);
return;
}
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
}
void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
const QString &channelId,
const QString &channelDisplayName,
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)
.timeout(20000)
.onSuccess([callback = std::move(callback), channel, channelDisplayName,
manualRefresh](auto result) -> Outcome {
auto pair =
parseChannelEmotes(result.parseJson(), channelDisplayName);
bool hasEmotes = false;
2019-08-20 21:50:36 +02:00
if (pair.first)
{
hasEmotes = !pair.second.empty();
2019-08-20 21:50:36 +02:00
callback(std::move(pair.second));
}
if (auto shared = channel.lock(); manualRefresh)
{
if (hasEmotes)
{
shared->addMessage(makeSystemMessage(
"BetterTTV channel emotes reloaded."));
}
else
{
shared->addMessage(
makeSystemMessage(CHANNEL_HAS_NO_EMOTES));
}
}
2019-08-20 21:50:36 +02:00
return pair.first;
})
.onError([channelId, channel, manualRefresh](auto result) {
auto shared = channel.lock();
if (!shared)
return;
if (result.status() == 404)
{
// User does not have any BTTV emotes
if (manualRefresh)
shared->addMessage(
makeSystemMessage(CHANNEL_HAS_NO_EMOTES));
}
else if (result.status() == NetworkResult::timedoutStatus)
{
// TODO: Auto retry in case of a timeout, with a delay
qCWarning(chatterinoBttv)
<< "Fetching BTTV emotes for channel" << channelId
<< "failed due to timeout";
shared->addMessage(makeSystemMessage(
"Failed to fetch BetterTTV channel emotes. (timed out)"));
}
else
{
qCWarning(chatterinoBttv)
<< "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
}
EmotePtr BttvEmotes::addEmote(
const QString &channelDisplayName,
Atomic<std::shared_ptr<const EmoteMap>> &channelEmoteMap,
const BttvLiveUpdateEmoteUpdateAddMessage &message)
{
// This copies the map.
EmoteMap updatedMap = *channelEmoteMap.get();
auto result = createChannelEmote(channelDisplayName, message.jsonEmote);
auto emote = std::make_shared<const Emote>(std::move(result.emote));
updatedMap[result.name] = emote;
channelEmoteMap.set(std::make_shared<EmoteMap>(std::move(updatedMap)));
return emote;
}
boost::optional<std::pair<EmotePtr, EmotePtr>> BttvEmotes::updateEmote(
const QString &channelDisplayName,
Atomic<std::shared_ptr<const EmoteMap>> &channelEmoteMap,
const BttvLiveUpdateEmoteUpdateAddMessage &message)
{
// This copies the map.
EmoteMap updatedMap = *channelEmoteMap.get();
// Step 1: remove the existing emote
auto it = updatedMap.findEmote(QString(), message.emoteID);
if (it == updatedMap.end())
{
// We already copied the map at this point and are now discarding the copy.
// This is fine, because this case should be really rare.
return boost::none;
}
auto oldEmotePtr = it->second;
// copy the existing emote, to not change the original one
auto emote = *oldEmotePtr;
updatedMap.erase(it);
// Step 2: update the emote
if (!updateChannelEmote(emote, channelDisplayName, message.jsonEmote))
{
// The emote wasn't actually updated
return boost::none;
}
auto name = emote.name;
auto emotePtr = std::make_shared<const Emote>(std::move(emote));
updatedMap[name] = emotePtr;
channelEmoteMap.set(std::make_shared<EmoteMap>(std::move(updatedMap)));
return std::make_pair(oldEmotePtr, emotePtr);
}
boost::optional<EmotePtr> BttvEmotes::removeEmote(
Atomic<std::shared_ptr<const EmoteMap>> &channelEmoteMap,
const BttvLiveUpdateEmoteRemoveMessage &message)
{
// This copies the map.
EmoteMap updatedMap = *channelEmoteMap.get();
auto it = updatedMap.findEmote(QString(), message.emoteID);
if (it == updatedMap.end())
{
// We already copied the map at this point and are now discarding the copy.
// This is fine, because this case should be really rare.
return boost::none;
}
auto emote = it->second;
updatedMap.erase(it);
channelEmoteMap.set(std::make_shared<EmoteMap>(std::move(updatedMap)));
return emote;
}
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