Move FFZ emotes to its own class
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
#include "providers/ffz/ffzemotes.hpp"
|
||||
|
||||
#include "debug/log.hpp"
|
||||
#include "messages/image.hpp"
|
||||
#include "util/urlfetch.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace providers {
|
||||
namespace ffz {
|
||||
|
||||
namespace {
|
||||
|
||||
QString getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
|
||||
{
|
||||
auto emote = urls.value(emoteScale);
|
||||
if (emote.isUndefined()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
assert(emote.isString());
|
||||
|
||||
return "https:" + emote.toString();
|
||||
}
|
||||
|
||||
void fillInEmoteData(const QJsonObject &urls, const QString &code, const QString &tooltip,
|
||||
util::EmoteData &emoteData)
|
||||
{
|
||||
QString url1x = getEmoteLink(urls, "1");
|
||||
QString url2x = getEmoteLink(urls, "2");
|
||||
QString url3x = getEmoteLink(urls, "4");
|
||||
|
||||
assert(!url1x.isEmpty());
|
||||
|
||||
emoteData.image1x = new messages::Image(url1x, 1, code, tooltip);
|
||||
|
||||
if (!url2x.isEmpty()) {
|
||||
emoteData.image2x = new messages::Image(url2x, 0.5, code, tooltip);
|
||||
}
|
||||
|
||||
if (!url3x.isEmpty()) {
|
||||
emoteData.image3x = new messages::Image(url3x, 0.25, code, tooltip);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void FFZEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<util::EmoteMap> _map)
|
||||
{
|
||||
printf("[FFZEmotes] Reload FFZ Channel Emotes for channel %s\n", qPrintable(channelName));
|
||||
|
||||
QString url("https://api.frankerfacez.com/v1/room/" + channelName);
|
||||
|
||||
util::NetworkRequest req(url);
|
||||
req.setCaller(QThread::currentThread());
|
||||
req.setTimeout(3000);
|
||||
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
|
||||
auto map = _map.lock();
|
||||
|
||||
if (_map.expired()) {
|
||||
return;
|
||||
}
|
||||
|
||||
map->clear();
|
||||
|
||||
auto setsNode = rootNode.value("sets").toObject();
|
||||
|
||||
std::vector<std::string> codes;
|
||||
for (const QJsonValue &setNode : setsNode) {
|
||||
auto emotesNode = setNode.toObject().value("emoticons").toArray();
|
||||
|
||||
for (const QJsonValue &emoteNode : emotesNode) {
|
||||
QJsonObject emoteObject = emoteNode.toObject();
|
||||
|
||||
// margins
|
||||
int id = emoteObject.value("id").toInt();
|
||||
QString code = emoteObject.value("name").toString();
|
||||
|
||||
QJsonObject urls = emoteObject.value("urls").toObject();
|
||||
|
||||
auto emote = this->getChannelEmoteFromCaches().getOrAdd(id, [id, &code, &urls] {
|
||||
util::EmoteData emoteData;
|
||||
fillInEmoteData(urls, code, code + "<br/>Channel FFZ Emote", emoteData);
|
||||
emoteData.pageLink =
|
||||
QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
|
||||
|
||||
return emoteData;
|
||||
});
|
||||
|
||||
this->channelEmotes.insert(code, emote);
|
||||
map->insert(code, emote);
|
||||
codes.push_back(code.toStdString());
|
||||
}
|
||||
|
||||
this->channelEmoteCodes[channelName.toStdString()] = codes;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
util::EmoteMap &FFZEmotes::getEmotes()
|
||||
{
|
||||
return this->globalEmotes;
|
||||
}
|
||||
|
||||
util::ConcurrentMap<int, util::EmoteData> &FFZEmotes::getChannelEmoteFromCaches()
|
||||
{
|
||||
return this->_channelEmoteFromCaches;
|
||||
}
|
||||
|
||||
void FFZEmotes::loadGlobalEmotes()
|
||||
{
|
||||
QString url("https://api.frankerfacez.com/v1/set/global");
|
||||
|
||||
util::NetworkRequest req(url);
|
||||
req.setCaller(QThread::currentThread());
|
||||
req.setTimeout(30000);
|
||||
req.getJSON([this](QJsonObject &root) {
|
||||
auto sets = root.value("sets").toObject();
|
||||
|
||||
std::vector<std::string> codes;
|
||||
for (const QJsonValue &set : sets) {
|
||||
auto emoticons = set.toObject().value("emoticons").toArray();
|
||||
|
||||
for (const QJsonValue &emote : emoticons) {
|
||||
QJsonObject object = emote.toObject();
|
||||
|
||||
QString code = object.value("name").toString();
|
||||
int id = object.value("id").toInt();
|
||||
QJsonObject urls = object.value("urls").toObject();
|
||||
|
||||
util::EmoteData emoteData;
|
||||
fillInEmoteData(urls, code, code + "<br/>Global FFZ Emote", emoteData);
|
||||
emoteData.pageLink =
|
||||
QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
|
||||
|
||||
this->globalEmotes.insert(code, emoteData);
|
||||
codes.push_back(code.toStdString());
|
||||
}
|
||||
|
||||
this->globalEmoteCodes = codes;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace ffz
|
||||
} // namespace providers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "signalvector.hpp"
|
||||
#include "util/concurrentmap.hpp"
|
||||
#include "util/emotemap.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace chatterino {
|
||||
namespace providers {
|
||||
namespace ffz {
|
||||
|
||||
class FFZEmotes
|
||||
{
|
||||
public:
|
||||
util::EmoteMap globalEmotes;
|
||||
SignalVector<std::string> globalEmoteCodes;
|
||||
|
||||
util::EmoteMap channelEmotes;
|
||||
std::map<std::string, SignalVector<std::string>> channelEmoteCodes;
|
||||
|
||||
void loadGlobalEmotes();
|
||||
|
||||
void loadChannelEmotes(const QString &channelName,
|
||||
std::weak_ptr<util::EmoteMap> channelEmoteMap);
|
||||
|
||||
util::EmoteMap &getEmotes();
|
||||
util::ConcurrentMap<int, util::EmoteData> &getChannelEmoteFromCaches();
|
||||
|
||||
util::ConcurrentMap<QString, util::EmoteMap> channels;
|
||||
|
||||
private:
|
||||
util::ConcurrentMap<int, util::EmoteData> _channelEmoteFromCaches;
|
||||
};
|
||||
|
||||
} // namespace ffz
|
||||
} // namespace providers
|
||||
} // namespace chatterino
|
||||
@@ -141,7 +141,7 @@ void TwitchChannel::reloadChannelEmotes()
|
||||
debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
|
||||
|
||||
app->emotes->bttv.loadChannelEmotes(this->name, this->bttvChannelEmotes);
|
||||
app->emotes->reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes);
|
||||
app->emotes->ffz.loadChannelEmotes(this->name, this->ffzChannelEmotes);
|
||||
}
|
||||
|
||||
void TwitchChannel::sendMessage(const QString &message)
|
||||
|
||||
@@ -538,7 +538,7 @@ bool TwitchMessageBuilder::tryAppendEmote(QString &emoteString)
|
||||
this->twitchChannel->bttvChannelEmotes->tryGet(emoteString, emoteData)) {
|
||||
// BTTV Channel Emote
|
||||
return appendEmote(MessageElement::BttvEmote);
|
||||
} else if (app->emotes->ffzGlobalEmotes.tryGet(emoteString, emoteData)) {
|
||||
} else if (app->emotes->ffz.globalEmotes.tryGet(emoteString, emoteData)) {
|
||||
// FFZ Global Emote
|
||||
return appendEmote(MessageElement::FfzEmote);
|
||||
} else if (this->twitchChannel != nullptr &&
|
||||
|
||||
Reference in New Issue
Block a user