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"
|
2022-12-31 15:41:01 +01:00
|
|
|
#include "common/NetworkResult.hpp"
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.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"
|
2023-01-21 15:06:55 +01:00
|
|
|
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
2022-08-28 12:20:47 +02:00
|
|
|
#include "singletons/Settings.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QThread>
|
|
|
|
|
2018-06-05 17:39:49 +02:00
|
|
|
namespace chatterino {
|
|
|
|
namespace {
|
2019-12-31 21:21:53 +01:00
|
|
|
|
2021-03-21 14:27:28 +01:00
|
|
|
const QString CHANNEL_HAS_NO_EMOTES(
|
|
|
|
"This channel has no BetterTTV channel emotes.");
|
|
|
|
|
2019-12-31 21:21:53 +01:00
|
|
|
QString emoteLinkFormat("https://betterttv.com/emotes/%1");
|
|
|
|
|
2023-01-21 15:06:55 +01:00
|
|
|
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 ¤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)},
|
2020-12-12 16:15:49 +01:00
|
|
|
Tooltip{name.string + "<br>Global BetterTTV Emote"},
|
2019-12-31 21:21:53 +01:00
|
|
|
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
|
|
|
}
|
2023-01-21 15:06:55 +01: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;
|
|
|
|
}
|
|
|
|
|
2020-12-12 16:15:49 +01:00
|
|
|
std::pair<Outcome, EmoteMap> parseChannelEmotes(
|
|
|
|
const QJsonObject &jsonRoot, const QString &channelDisplayName)
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
|
|
|
auto emotes = EmoteMap();
|
|
|
|
|
2020-12-12 16:15:49 +01:00
|
|
|
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)
|
|
|
|
{
|
2023-01-21 15:06:55 +01:00
|
|
|
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>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-08-28 12:20:47 +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
|
|
|
}
|
|
|
|
|
2020-05-16 12:43:44 +02:00
|
|
|
void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
|
2020-12-12 16:15:49 +01:00
|
|
|
const QString &channelId,
|
|
|
|
const QString &channelDisplayName,
|
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)
|
2021-04-25 18:17:37 +02:00
|
|
|
.timeout(20000)
|
2022-12-31 14:03:16 +01:00
|
|
|
.onSuccess([callback = std::move(callback), channel, channelDisplayName,
|
2020-05-16 12:43:44 +02:00
|
|
|
manualRefresh](auto result) -> Outcome {
|
2020-12-12 16:15:49 +01:00
|
|
|
auto pair =
|
|
|
|
parseChannelEmotes(result.parseJson(), channelDisplayName);
|
2021-03-21 14:27:28 +01:00
|
|
|
bool hasEmotes = false;
|
2019-08-20 21:50:36 +02:00
|
|
|
if (pair.first)
|
2021-03-21 14:27:28 +01:00
|
|
|
{
|
|
|
|
hasEmotes = !pair.second.empty();
|
2019-08-20 21:50:36 +02:00
|
|
|
callback(std::move(pair.second));
|
2021-03-21 14:27:28 +01:00
|
|
|
}
|
2020-05-16 12:43:44 +02:00
|
|
|
if (auto shared = channel.lock(); manualRefresh)
|
2021-03-21 14:27:28 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
})
|
2020-05-16 12:43:44 +02:00
|
|
|
.onError([channelId, channel, manualRefresh](auto result) {
|
|
|
|
auto shared = channel.lock();
|
|
|
|
if (!shared)
|
|
|
|
return;
|
2021-03-21 14:27:28 +01:00
|
|
|
if (result.status() == 404)
|
2020-05-16 12:43:44 +02:00
|
|
|
{
|
|
|
|
// User does not have any BTTV emotes
|
|
|
|
if (manualRefresh)
|
2021-03-21 14:27:28 +01:00
|
|
|
shared->addMessage(
|
|
|
|
makeSystemMessage(CHANNEL_HAS_NO_EMOTES));
|
2020-05-16 12:43:44 +02:00
|
|
|
}
|
|
|
|
else if (result.status() == NetworkResult::timedoutStatus)
|
|
|
|
{
|
|
|
|
// TODO: Auto retry in case of a timeout, with a delay
|
2020-11-21 16:20:10 +01:00
|
|
|
qCWarning(chatterinoBttv)
|
|
|
|
<< "Fetching BTTV emotes for channel" << channelId
|
|
|
|
<< "failed due to timeout";
|
2020-05-16 12:43:44 +02:00
|
|
|
shared->addMessage(makeSystemMessage(
|
|
|
|
"Failed to fetch BetterTTV channel emotes. (timed out)"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCWarning(chatterinoBttv)
|
|
|
|
<< "Error fetching BTTV emotes for channel" << channelId
|
|
|
|
<< ", error" << result.status();
|
2020-05-16 12:43:44 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-21 15:06:55 +01: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
|