2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/ffz/FfzEmotes.hpp"
|
2018-06-05 18:07:17 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
|
2018-07-15 14:11:46 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Outcome.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-06-05 18:07:17 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace {
|
2018-08-15 22:46:20 +02:00
|
|
|
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
|
|
|
|
{
|
|
|
|
auto emote = urls.value(emoteScale);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (emote.isUndefined())
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
return {""};
|
2018-06-07 12:25:52 +02:00
|
|
|
}
|
2018-08-11 17:15:17 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
assert(emote.isString());
|
2018-08-11 17:15:17 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
return {"https:" + emote.toString()};
|
|
|
|
}
|
|
|
|
void fillInEmoteData(const QJsonObject &urls, const EmoteName &name,
|
|
|
|
const QString &tooltip, Emote &emoteData)
|
|
|
|
{
|
|
|
|
auto url1x = getEmoteLink(urls, "1");
|
|
|
|
auto url2x = getEmoteLink(urls, "2");
|
|
|
|
auto url3x = getEmoteLink(urls, "4");
|
|
|
|
|
|
|
|
//, code, tooltip
|
|
|
|
emoteData.name = name;
|
|
|
|
emoteData.images =
|
2019-08-13 13:00:16 +02:00
|
|
|
ImageSet{Image::fromUrl(url1x, 1),
|
|
|
|
url2x.string.isEmpty() ? Image::getEmpty()
|
|
|
|
: Image::fromUrl(url2x, 0.5),
|
|
|
|
url3x.string.isEmpty() ? Image::getEmpty()
|
|
|
|
: Image::fromUrl(url3x, 0.25)};
|
2018-08-15 22:46:20 +02:00
|
|
|
emoteData.tooltip = {tooltip};
|
|
|
|
}
|
|
|
|
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> parseGlobalEmotes(
|
|
|
|
const QJsonObject &jsonRoot, const EmoteMap ¤tEmotes)
|
|
|
|
{
|
|
|
|
auto jsonSets = jsonRoot.value("sets").toObject();
|
|
|
|
auto emotes = EmoteMap();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto jsonSet : jsonSets)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto jsonEmoteValue : jsonEmotes)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
auto jsonEmote = jsonEmoteValue.toObject();
|
|
|
|
|
|
|
|
auto name = EmoteName{jsonEmote.value("name").toString()};
|
|
|
|
auto id = EmoteId{jsonEmote.value("id").toString()};
|
|
|
|
auto urls = jsonEmote.value("urls").toObject();
|
|
|
|
|
|
|
|
auto emote = Emote();
|
|
|
|
fillInEmoteData(urls, name,
|
|
|
|
name.string + "<br/>Global FFZ Emote", emote);
|
|
|
|
emote.homePage =
|
|
|
|
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
|
|
|
|
.arg(id.string)
|
|
|
|
.arg(name.string)};
|
|
|
|
|
|
|
|
emotes[name] =
|
|
|
|
cachedOrMakeEmotePtr(std::move(emote), currentEmotes);
|
|
|
|
}
|
2018-08-11 17:15:17 +02:00
|
|
|
}
|
2018-08-15 22:46:20 +02:00
|
|
|
|
|
|
|
return {Success, std::move(emotes)};
|
2018-08-11 17:15:17 +02:00
|
|
|
}
|
2019-09-08 13:57:24 +02:00
|
|
|
|
|
|
|
boost::optional<EmotePtr> parseModBadge(const QJsonObject &jsonRoot)
|
2018-08-15 22:46:20 +02:00
|
|
|
{
|
2019-09-07 13:48:30 +02:00
|
|
|
boost::optional<EmotePtr> modBadge;
|
|
|
|
|
|
|
|
auto room = jsonRoot.value("room").toObject();
|
|
|
|
auto modUrls = room.value("mod_urls").toObject();
|
|
|
|
if (!modUrls.isEmpty())
|
|
|
|
{
|
|
|
|
auto modBadge1x = getEmoteLink(modUrls, "1");
|
|
|
|
auto modBadge2x = getEmoteLink(modUrls, "2");
|
|
|
|
auto modBadge3x = getEmoteLink(modUrls, "4");
|
|
|
|
|
|
|
|
auto modBadgeImageSet = ImageSet{
|
2019-09-08 11:30:06 +02:00
|
|
|
Image::fromUrl(modBadge1x, 1),
|
|
|
|
modBadge2x.string.isEmpty() ? Image::getEmpty()
|
|
|
|
: Image::fromUrl(modBadge2x, 0.5),
|
|
|
|
modBadge3x.string.isEmpty() ? Image::getEmpty()
|
|
|
|
: Image::fromUrl(modBadge3x, 0.25),
|
2019-09-07 13:48:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
modBadge = std::make_shared<Emote>(Emote{
|
|
|
|
{""},
|
|
|
|
modBadgeImageSet,
|
|
|
|
Tooltip{"Twitch Channel Moderator"},
|
|
|
|
modBadge1x,
|
|
|
|
});
|
|
|
|
}
|
2019-09-08 13:57:24 +02:00
|
|
|
return modBadge;
|
|
|
|
}
|
2019-09-07 13:48:30 +02:00
|
|
|
|
2019-09-08 13:57:24 +02:00
|
|
|
EmoteMap parseChannelEmotes(const QJsonObject &jsonRoot)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
auto jsonSets = jsonRoot.value("sets").toObject();
|
|
|
|
auto emotes = EmoteMap();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto jsonSet : jsonSets)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
auto jsonEmotes = jsonSet.toObject().value("emoticons").toArray();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (auto _jsonEmote : jsonEmotes)
|
|
|
|
{
|
2018-08-15 22:46:20 +02:00
|
|
|
auto jsonEmote = _jsonEmote.toObject();
|
|
|
|
|
|
|
|
// margins
|
|
|
|
auto id =
|
|
|
|
EmoteId{QString::number(jsonEmote.value("id").toInt())};
|
|
|
|
auto name = EmoteName{jsonEmote.value("name").toString()};
|
|
|
|
auto urls = jsonEmote.value("urls").toObject();
|
|
|
|
|
|
|
|
Emote emote;
|
|
|
|
fillInEmoteData(urls, name,
|
|
|
|
name.string + "<br/>Channel FFZ Emote", emote);
|
|
|
|
emote.homePage =
|
|
|
|
Url{QString("https://www.frankerfacez.com/emoticon/%1-%2")
|
|
|
|
.arg(id.string)
|
|
|
|
.arg(name.string)};
|
|
|
|
|
|
|
|
emotes[name] = cachedOrMake(std::move(emote), id);
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 17:15:17 +02:00
|
|
|
|
2019-09-08 13:57:24 +02:00
|
|
|
return emotes;
|
2018-08-15 22:46:20 +02:00
|
|
|
}
|
2018-08-11 14:20:53 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
FfzEmotes::FfzEmotes()
|
|
|
|
: global_(std::make_shared<EmoteMap>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-12 00:01:37 +02:00
|
|
|
std::shared_ptr<const EmoteMap> FfzEmotes::emotes() const
|
2018-08-11 14:20:53 +02:00
|
|
|
{
|
|
|
|
return this->global_.get();
|
|
|
|
}
|
|
|
|
|
2018-08-12 00:01:37 +02:00
|
|
|
boost::optional<EmotePtr> FfzEmotes::emote(const EmoteName &name) const
|
2018-08-11 14:20:53 +02:00
|
|
|
{
|
|
|
|
auto emotes = this->global_.get();
|
|
|
|
auto it = emotes->find(name);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (it != emotes->end())
|
|
|
|
return it->second;
|
2018-08-11 14:20:53 +02:00
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
2018-08-12 00:01:37 +02:00
|
|
|
void FfzEmotes::loadEmotes()
|
2018-08-11 14:20:53 +02:00
|
|
|
{
|
|
|
|
QString url("https://api.frankerfacez.com/v1/set/global");
|
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url)
|
2019-08-27 20:45:55 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.timeout(30000)
|
|
|
|
.onSuccess([this](auto result) -> Outcome {
|
|
|
|
auto emotes = this->emotes();
|
|
|
|
auto pair = parseGlobalEmotes(result.parseJson(), *emotes);
|
|
|
|
if (pair.first)
|
|
|
|
this->global_.set(
|
|
|
|
std::make_shared<EmoteMap>(std::move(pair.second)));
|
|
|
|
return pair.first;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-06-07 12:25:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 13:48:30 +02:00
|
|
|
void FfzEmotes::loadChannel(
|
|
|
|
const QString &channelId, std::function<void(EmoteMap &&)> emoteCallback,
|
|
|
|
std::function<void(boost::optional<EmotePtr>)> modBadgeCallback)
|
2018-06-05 18:07:17 +02:00
|
|
|
{
|
2019-08-27 20:45:55 +02:00
|
|
|
log("[FFZEmotes] Reload FFZ Channel Emotes for channel {}\n", channelId);
|
|
|
|
|
|
|
|
NetworkRequest("https://api.frankerfacez.com/v1/room/id/" + channelId)
|
2018-06-05 18:07:17 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.timeout(20000)
|
2019-09-07 13:48:30 +02:00
|
|
|
.onSuccess([emoteCallback = std::move(emoteCallback),
|
|
|
|
modBadgeCallback =
|
|
|
|
std::move(modBadgeCallback)](auto result) -> Outcome {
|
2019-09-08 13:57:24 +02:00
|
|
|
auto json = result.parseJson();
|
|
|
|
auto emoteMap = parseChannelEmotes(json);
|
|
|
|
auto modBadge = parseModBadge(json);
|
2019-09-07 13:48:30 +02:00
|
|
|
|
|
|
|
emoteCallback(std::move(emoteMap));
|
|
|
|
modBadgeCallback(std::move(modBadge));
|
|
|
|
|
2019-09-08 13:57:24 +02:00
|
|
|
return Success;
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([channelId](NetworkResult result) {
|
|
|
|
if (result.status() == 203)
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
|
|
|
// User does not have any FFZ emotes
|
|
|
|
}
|
2019-09-19 19:03:50 +02:00
|
|
|
else if (result.status() == NetworkResult::timedoutStatus)
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
|
|
|
// TODO: Auto retry in case of a timeout, with a delay
|
|
|
|
log("Fetching FFZ emotes for channel {} failed due to timeout",
|
2019-08-27 20:45:55 +02:00
|
|
|
channelId);
|
2019-08-20 21:50:36 +02:00
|
|
|
}
|
2019-09-19 19:03:50 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
log("Error fetching FFZ emotes for channel {}, error {}",
|
|
|
|
channelId, result.status());
|
|
|
|
}
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.execute();
|
2018-06-05 18:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|