2017-06-11 09:31:45 +02:00
|
|
|
#include "emotemanager.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2017-07-23 14:16:13 +02:00
|
|
|
#include "common.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "singletons/windowmanager.hpp"
|
2017-06-13 21:13:58 +02:00
|
|
|
#include "util/urlfetch.hpp"
|
2017-01-04 15:12:31 +01:00
|
|
|
|
2017-01-26 10:18:02 +01:00
|
|
|
#include <QDebug>
|
2017-12-31 00:50:07 +01:00
|
|
|
#include <QFile>
|
2017-01-26 17:26:20 +01:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2017-01-26 17:26:20 +01:00
|
|
|
#include <memory>
|
2017-01-26 10:18:02 +01:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
#define TWITCH_EMOTE_TEMPLATE "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
using namespace chatterino::providers::twitch;
|
2017-04-14 17:52:22 +02:00
|
|
|
using namespace chatterino::messages;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-12-31 22:58:35 +01:00
|
|
|
namespace singletons {
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
namespace {
|
|
|
|
|
2018-03-31 13:44:15 +02:00
|
|
|
QString GetTwitchEmoteLink(long id, const QString &emoteScale)
|
2018-01-07 02:56:45 +01:00
|
|
|
{
|
|
|
|
QString value = TWITCH_EMOTE_TEMPLATE;
|
|
|
|
|
|
|
|
value.detach();
|
|
|
|
|
|
|
|
return value.replace("{id}", QString::number(id)).replace("{scale}", emoteScale);
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:44:15 +02:00
|
|
|
QString GetBTTVEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale)
|
2018-01-07 02:56:45 +01:00
|
|
|
{
|
|
|
|
urlTemplate.detach();
|
|
|
|
|
|
|
|
return urlTemplate.replace("{{id}}", id).replace("{{image}}", emoteScale);
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:44:15 +02:00
|
|
|
QString GetFFZEmoteLink(const QJsonObject &urls, const QString &emoteScale)
|
2018-01-07 02:56:45 +01:00
|
|
|
{
|
|
|
|
auto emote = urls.value(emoteScale);
|
|
|
|
if (emote.isUndefined()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(emote.isString());
|
|
|
|
|
2018-04-16 22:51:30 +02:00
|
|
|
return "https:" + emote.toString();
|
2018-01-07 02:56:45 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 21:59:51 +02:00
|
|
|
void FillInFFZEmoteData(const QJsonObject &urls, const QString &code, const QString &tooltip,
|
|
|
|
util::EmoteData &emoteData)
|
2018-01-07 02:56:45 +01:00
|
|
|
{
|
|
|
|
QString url1x = GetFFZEmoteLink(urls, "1");
|
|
|
|
QString url2x = GetFFZEmoteLink(urls, "2");
|
|
|
|
QString url3x = GetFFZEmoteLink(urls, "4");
|
|
|
|
|
|
|
|
assert(!url1x.isEmpty());
|
|
|
|
|
2018-04-14 15:10:25 +02:00
|
|
|
emoteData.image1x = new Image(url1x, 1, code, tooltip);
|
2018-01-07 02:56:45 +01:00
|
|
|
|
|
|
|
if (!url2x.isEmpty()) {
|
2018-04-14 15:10:25 +02:00
|
|
|
emoteData.image2x = new Image(url2x, 0.5, code, tooltip);
|
2018-01-07 02:56:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!url3x.isEmpty()) {
|
2018-04-14 15:10:25 +02:00
|
|
|
emoteData.image3x = new Image(url3x, 0.25, code, tooltip);
|
2018-01-07 02:56:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-01 16:43:30 +02:00
|
|
|
EmoteManager::EmoteManager()
|
|
|
|
: findShortCodesRegex(":([-+\\w]+):")
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
2018-04-26 18:10:26 +02:00
|
|
|
qDebug() << "init EmoteManager";
|
2018-04-27 22:11:19 +02:00
|
|
|
}
|
2017-07-23 14:16:13 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
void EmoteManager::initialize()
|
|
|
|
{
|
2018-05-26 20:26:25 +02:00
|
|
|
getApp()->accounts->twitch.currentUserChanged.connect([this] {
|
|
|
|
auto currentUser = getApp()->accounts->twitch.getCurrent();
|
2017-12-22 17:37:24 +01:00
|
|
|
assert(currentUser);
|
|
|
|
this->refreshTwitchEmotes(currentUser);
|
2017-07-23 14:16:13 +02:00
|
|
|
});
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->loadEmojis();
|
2017-06-13 21:13:58 +02:00
|
|
|
this->loadBTTVEmotes();
|
|
|
|
this->loadFFZEmotes();
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName,
|
|
|
|
std::weak_ptr<util::EmoteMap> _map)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-13 21:13:58 +02:00
|
|
|
printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName));
|
|
|
|
|
|
|
|
QString url("https://api.betterttv.net/2/channels/" + channelName);
|
2017-07-09 17:58:59 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
debug::Log("Request bttv channel emotes for {}", channelName);
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.setTimeout(3000);
|
|
|
|
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
|
|
|
|
auto map = _map.lock();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
if (_map.expired()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
map->clear();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
auto emotesNode = rootNode.value("emotes").toArray();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
std::vector<std::string> codes;
|
|
|
|
for (const QJsonValue &emoteNode : emotesNode) {
|
|
|
|
QJsonObject emoteObject = emoteNode.toObject();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
QString id = emoteObject.value("id").toString();
|
|
|
|
QString code = emoteObject.value("code").toString();
|
|
|
|
// emoteObject.value("imageType").toString();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2018-06-04 14:52:21 +02:00
|
|
|
auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [&] {
|
|
|
|
util::EmoteData emoteData;
|
2018-06-04 21:02:51 +02:00
|
|
|
QString link = linkTemplate;
|
|
|
|
link.detach();
|
2018-06-04 14:52:21 +02:00
|
|
|
emoteData.image1x = new Image(link.replace("{{id}}", id).replace("{{image}}", "1x"),
|
|
|
|
1, code, code + "<br />Channel BTTV Emote");
|
2018-06-04 21:02:51 +02:00
|
|
|
link = linkTemplate;
|
|
|
|
link.detach();
|
2018-06-04 14:52:21 +02:00
|
|
|
emoteData.image2x = new Image(link.replace("{{id}}", id).replace("{{image}}", "2x"),
|
|
|
|
0.5, code, code + "<br />Channel BTTV Emote");
|
2018-06-04 21:02:51 +02:00
|
|
|
link = linkTemplate;
|
|
|
|
link.detach();
|
2018-06-04 14:52:21 +02:00
|
|
|
emoteData.image3x = new Image(link.replace("{{id}}", id).replace("{{image}}", "3x"),
|
|
|
|
0.25, code, code + "<br />Channel BTTV Emote");
|
2018-05-16 03:55:56 +02:00
|
|
|
emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id;
|
|
|
|
|
|
|
|
return emoteData;
|
2017-10-27 22:04:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this->bttvChannelEmotes.insert(code, emote);
|
|
|
|
map->insert(code, emote);
|
|
|
|
codes.push_back(code.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bttvChannelEmoteCodes[channelName.toStdString()] = codes;
|
|
|
|
});
|
2017-06-13 21:13:58 +02:00
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void EmoteManager::reloadFFZChannelEmotes(const QString &channelName,
|
|
|
|
std::weak_ptr<util::EmoteMap> _map)
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
|
|
|
printf("[EmoteManager] Reload FFZ Channel Emotes for channel %s\n", qPrintable(channelName));
|
|
|
|
|
2018-04-14 15:07:20 +02:00
|
|
|
QString url("https://api.frankerfacez.com/v1/room/" + channelName);
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.setTimeout(3000);
|
|
|
|
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
|
|
|
|
auto map = _map.lock();
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
if (_map.expired()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-09 17:58:59 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
map->clear();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
auto setsNode = rootNode.value("sets").toObject();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
std::vector<std::string> codes;
|
|
|
|
for (const QJsonValue &setNode : setsNode) {
|
|
|
|
auto emotesNode = setNode.toObject().value("emoticons").toArray();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
for (const QJsonValue &emoteNode : emotesNode) {
|
|
|
|
QJsonObject emoteObject = emoteNode.toObject();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
// margins
|
|
|
|
int id = emoteObject.value("id").toInt();
|
|
|
|
QString code = emoteObject.value("name").toString();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
QJsonObject urls = emoteObject.value("urls").toObject();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2018-05-16 03:55:56 +02:00
|
|
|
auto emote = this->getFFZChannelEmoteFromCaches().getOrAdd(id, [id, &code, &urls] {
|
2018-04-14 21:59:51 +02:00
|
|
|
util::EmoteData emoteData;
|
|
|
|
FillInFFZEmoteData(urls, code, code + "<br/>Channel FFZ Emote", emoteData);
|
2018-05-16 03:55:56 +02:00
|
|
|
emoteData.pageLink =
|
|
|
|
QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
|
2018-01-07 02:56:45 +01:00
|
|
|
|
2018-04-14 21:59:51 +02:00
|
|
|
return emoteData;
|
|
|
|
});
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-10-27 22:04:05 +02:00
|
|
|
this->ffzChannelEmotes.insert(code, emote);
|
|
|
|
map->insert(code, emote);
|
|
|
|
codes.push_back(code.toStdString());
|
2017-10-27 20:09:02 +02:00
|
|
|
}
|
2017-10-27 22:04:05 +02:00
|
|
|
|
|
|
|
this->ffzChannelEmoteCodes[channelName.toStdString()] = codes;
|
|
|
|
}
|
|
|
|
});
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-04 15:12:31 +01:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
util::ConcurrentMap<QString, providers::twitch::EmoteValue *> &EmoteManager::getTwitchEmotes()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _twitchEmotes;
|
|
|
|
}
|
2017-01-05 20:49:33 +01:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
util::EmoteMap &EmoteManager::getFFZEmotes()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-23 09:53:50 +02:00
|
|
|
return ffzGlobalEmotes;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-15 16:38:30 +01:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
util::EmoteMap &EmoteManager::getChatterinoEmotes()
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
2017-04-12 17:46:44 +02:00
|
|
|
return _chatterinoEmotes;
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
2017-01-04 15:12:31 +01:00
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
util::EmoteMap &EmoteManager::getBTTVChannelEmoteFromCaches()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _bttvChannelEmoteFromCaches;
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:16:54 +02:00
|
|
|
util::EmojiMap &EmoteManager::getEmojis()
|
2017-12-19 03:41:31 +01:00
|
|
|
{
|
|
|
|
return this->emojis;
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
util::ConcurrentMap<int, util::EmoteData> &EmoteManager::getFFZChannelEmoteFromCaches()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _ffzChannelEmoteFromCaches;
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
util::ConcurrentMap<long, util::EmoteData> &EmoteManager::getTwitchEmoteFromCache()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
return _twitchEmoteFromCache;
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
void EmoteManager::loadEmojis()
|
|
|
|
{
|
|
|
|
QFile file(":/emojidata.txt");
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
QTextStream in(&file);
|
|
|
|
|
|
|
|
uint unicodeBytes[4];
|
|
|
|
|
|
|
|
while (!in.atEnd()) {
|
|
|
|
// Line example: sunglasses 1f60e
|
|
|
|
QString line = in.readLine();
|
|
|
|
|
|
|
|
if (line.at(0) == '#') {
|
|
|
|
// Ignore lines starting with # (comments)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList parts = line.split(' ');
|
|
|
|
if (parts.length() < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString shortCode = parts[0];
|
|
|
|
QString code = parts[1];
|
|
|
|
|
|
|
|
QStringList unicodeCharacters = code.split('-');
|
|
|
|
if (unicodeCharacters.length() < 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int numUnicodeBytes = 0;
|
|
|
|
|
|
|
|
for (const QString &unicodeCharacter : unicodeCharacters) {
|
|
|
|
unicodeBytes[numUnicodeBytes++] = QString(unicodeCharacter).toUInt(nullptr, 16);
|
|
|
|
}
|
|
|
|
|
2018-04-18 19:26:24 +02:00
|
|
|
QString unicodeString = QString::fromUcs4(unicodeBytes, numUnicodeBytes);
|
|
|
|
|
2018-04-01 16:16:54 +02:00
|
|
|
QString url = "https://cdnjs.cloudflare.com/ajax/libs/"
|
|
|
|
"emojione/2.2.6/assets/png/" +
|
|
|
|
code + ".png";
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
EmojiData emojiData{
|
2018-04-18 19:26:24 +02:00
|
|
|
unicodeString, //
|
|
|
|
code, //
|
|
|
|
shortCode, //
|
|
|
|
{new Image(url, 0.35, unicodeString, ":" + shortCode + ":<br/>Emoji")},
|
2017-06-26 16:41:20 +02:00
|
|
|
};
|
|
|
|
|
2017-07-02 17:37:17 +02:00
|
|
|
this->emojiShortCodeToEmoji.insert(shortCode, emojiData);
|
2017-08-01 00:10:02 +02:00
|
|
|
this->emojiShortCodes.push_back(shortCode.toStdString());
|
2017-06-26 16:41:20 +02:00
|
|
|
|
2017-07-02 17:37:17 +02:00
|
|
|
this->emojiFirstByte[emojiData.value.at(0)].append(emojiData);
|
2017-07-02 18:12:11 +02:00
|
|
|
|
2018-04-01 16:16:54 +02:00
|
|
|
this->emojis.insert(code, emojiData);
|
2018-03-31 13:14:43 +02:00
|
|
|
}
|
2017-09-16 16:20:10 +02:00
|
|
|
|
2018-03-31 13:14:43 +02:00
|
|
|
for (auto &p : this->emojiFirstByte) {
|
|
|
|
std::stable_sort(p.begin(), p.end(), [](const auto &lhs, const auto &rhs) {
|
|
|
|
return lhs.value.length() > rhs.value.length();
|
|
|
|
});
|
2017-06-26 16:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
void EmoteManager::parseEmojis(std::vector<std::tuple<util::EmoteData, QString>> &parsedWords,
|
2017-07-09 17:58:59 +02:00
|
|
|
const QString &text)
|
2017-06-26 16:41:20 +02:00
|
|
|
{
|
2017-07-02 17:37:17 +02:00
|
|
|
int lastParsedEmojiEndIndex = 0;
|
2017-06-26 16:41:20 +02:00
|
|
|
|
2018-03-31 13:14:43 +02:00
|
|
|
for (auto i = 0; i < text.length(); ++i) {
|
2017-07-02 17:37:17 +02:00
|
|
|
const QChar character = text.at(i);
|
|
|
|
|
|
|
|
if (character.isLowSurrogate()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = this->emojiFirstByte.find(character);
|
|
|
|
if (it == this->emojiFirstByte.end()) {
|
|
|
|
// No emoji starts with this character
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QVector<EmojiData> possibleEmojis = it.value();
|
|
|
|
|
2018-03-31 13:14:43 +02:00
|
|
|
int remainingCharacters = text.length() - i - 1;
|
2017-07-02 17:37:17 +02:00
|
|
|
|
|
|
|
EmojiData matchedEmoji;
|
|
|
|
|
|
|
|
int matchedEmojiLength = 0;
|
|
|
|
|
|
|
|
for (const EmojiData &emoji : possibleEmojis) {
|
2018-03-31 13:14:43 +02:00
|
|
|
int emojiExtraCharacters = emoji.value.length() - 1;
|
|
|
|
if (emojiExtraCharacters > remainingCharacters) {
|
2017-07-02 17:37:17 +02:00
|
|
|
// It cannot be this emoji, there's not enough space for it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool match = true;
|
|
|
|
|
|
|
|
for (int j = 1; j < emoji.value.length(); ++j) {
|
|
|
|
if (text.at(i + j) != emoji.value.at(j)) {
|
|
|
|
match = false;
|
|
|
|
|
|
|
|
break;
|
2017-06-26 16:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-02 17:37:17 +02:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
matchedEmoji = emoji;
|
|
|
|
matchedEmojiLength = emoji.value.length();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-06-26 16:41:20 +02:00
|
|
|
}
|
2017-07-02 17:37:17 +02:00
|
|
|
|
|
|
|
if (matchedEmojiLength == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int currentParsedEmojiFirstIndex = i;
|
|
|
|
int currentParsedEmojiEndIndex = i + (matchedEmojiLength);
|
|
|
|
|
|
|
|
int charactersFromLastParsedEmoji = currentParsedEmojiFirstIndex - lastParsedEmojiEndIndex;
|
|
|
|
|
|
|
|
if (charactersFromLastParsedEmoji > 0) {
|
|
|
|
// Add characters inbetween emojis
|
2018-03-31 13:44:15 +02:00
|
|
|
parsedWords.emplace_back(util::EmoteData(), text.mid(lastParsedEmojiEndIndex,
|
|
|
|
charactersFromLastParsedEmoji));
|
2017-07-02 17:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push the emoji as a word to parsedWords
|
2018-04-01 16:16:54 +02:00
|
|
|
parsedWords.push_back(
|
|
|
|
std::tuple<util::EmoteData, QString>(matchedEmoji.emoteData, QString()));
|
2017-07-02 17:37:17 +02:00
|
|
|
|
|
|
|
lastParsedEmojiEndIndex = currentParsedEmojiEndIndex;
|
|
|
|
|
|
|
|
i += matchedEmojiLength - 1;
|
2017-06-26 16:41:20 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 17:37:17 +02:00
|
|
|
if (lastParsedEmojiEndIndex < text.length()) {
|
|
|
|
// Add remaining characters
|
2018-03-31 13:44:15 +02:00
|
|
|
parsedWords.emplace_back(util::EmoteData(), text.mid(lastParsedEmojiEndIndex));
|
2017-06-26 16:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 22:15:02 +02:00
|
|
|
QString EmoteManager::replaceShortCodes(const QString &text)
|
|
|
|
{
|
|
|
|
QString ret(text);
|
|
|
|
auto it = this->findShortCodesRegex.globalMatch(text);
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
auto match = it.next();
|
|
|
|
|
|
|
|
auto capturedString = match.captured();
|
|
|
|
|
|
|
|
QString matchString = capturedString.toLower().mid(1, capturedString.size() - 2);
|
|
|
|
|
|
|
|
auto emojiIt = this->emojiShortCodeToEmoji.constFind(matchString);
|
|
|
|
|
|
|
|
if (emojiIt == this->emojiShortCodeToEmoji.constEnd()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto emojiData = emojiIt.value();
|
|
|
|
|
|
|
|
ret.replace(offset + match.capturedStart(), match.capturedLength(), emojiData.value);
|
|
|
|
|
|
|
|
offset += emojiData.value.size() - match.capturedLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
void EmoteManager::refreshTwitchEmotes(const std::shared_ptr<TwitchAccount> &user)
|
2017-07-23 14:16:13 +02:00
|
|
|
{
|
2018-02-05 15:11:50 +01:00
|
|
|
debug::Log("Loading Twitch emotes for user {}", user->getUserName());
|
2017-07-23 14:16:13 +02:00
|
|
|
|
2017-12-22 17:37:24 +01:00
|
|
|
const auto &roomID = user->getUserId();
|
|
|
|
const auto &clientID = user->getOAuthClient();
|
|
|
|
const auto &oauthToken = user->getOAuthToken();
|
2017-07-23 14:16:13 +02:00
|
|
|
|
2017-12-22 17:37:24 +01:00
|
|
|
if (clientID.isEmpty() || oauthToken.isEmpty()) {
|
|
|
|
debug::Log("Missing Client ID or OAuth token");
|
2017-07-23 14:16:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-22 17:37:24 +01:00
|
|
|
TwitchAccountEmoteData &emoteData = this->twitchAccountEmotes[roomID.toStdString()];
|
2017-07-23 14:16:13 +02:00
|
|
|
|
2017-12-22 17:37:24 +01:00
|
|
|
if (emoteData.filled) {
|
|
|
|
qDebug() << "Already loaded for room id " << roomID;
|
2017-07-23 14:16:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-22 17:37:24 +01:00
|
|
|
QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes");
|
|
|
|
|
|
|
|
util::twitch::getAuthorized(
|
|
|
|
url, clientID, oauthToken, QThread::currentThread(),
|
2018-01-19 22:45:33 +01:00
|
|
|
[=, &emoteData](const QJsonObject &root) {
|
2017-12-22 17:37:24 +01:00
|
|
|
emoteData.emoteSets.clear();
|
|
|
|
emoteData.emoteCodes.clear();
|
2018-05-31 16:20:46 +02:00
|
|
|
|
2017-12-22 17:37:24 +01:00
|
|
|
auto emoticonSets = root.value("emoticon_sets").toObject();
|
|
|
|
for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) {
|
|
|
|
std::string emoteSetString = it.key().toStdString();
|
|
|
|
QJsonArray emoteSetList = it.value().toArray();
|
|
|
|
|
|
|
|
for (QJsonValue emoteValue : emoteSetList) {
|
|
|
|
QJsonObject emoticon = emoteValue.toObject();
|
2018-06-04 12:48:23 +02:00
|
|
|
std::string id = QString::number(emoticon["id"].toInt()).toStdString();
|
2017-12-22 17:37:24 +01:00
|
|
|
std::string code = emoticon["code"].toString().toStdString();
|
|
|
|
emoteData.emoteSets[emoteSetString].push_back({id, code});
|
|
|
|
emoteData.emoteCodes.push_back(code);
|
2018-04-15 15:05:12 +02:00
|
|
|
|
2018-04-18 19:26:24 +02:00
|
|
|
util::EmoteData emote =
|
|
|
|
getTwitchEmoteById(emoticon["id"].toInt(), emoticon["code"].toString());
|
2018-04-15 15:05:12 +02:00
|
|
|
emoteData.emotes.insert(emoticon["code"].toString(), emote);
|
2017-12-22 17:37:24 +01:00
|
|
|
}
|
2017-07-23 14:16:13 +02:00
|
|
|
}
|
2017-12-22 17:37:24 +01:00
|
|
|
|
|
|
|
emoteData.filled = true;
|
2018-02-05 15:11:50 +01:00
|
|
|
});
|
2017-07-23 14:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
void EmoteManager::loadBTTVEmotes()
|
2017-01-26 17:26:20 +01:00
|
|
|
{
|
2017-12-26 12:38:54 +01:00
|
|
|
QString url("https://api.betterttv.net/2/emotes");
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.setTimeout(30000);
|
2018-01-19 22:45:33 +01:00
|
|
|
req.setUseQuickLoadCache(true);
|
2017-12-26 12:38:54 +01:00
|
|
|
req.getJSON([this](QJsonObject &root) {
|
|
|
|
auto emotes = root.value("emotes").toArray();
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
QString urlTemplate = "https:" + root.value("urlTemplate").toString();
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
std::vector<std::string> codes;
|
|
|
|
for (const QJsonValue &emote : emotes) {
|
|
|
|
QString id = emote.toObject().value("id").toString();
|
|
|
|
QString code = emote.toObject().value("code").toString();
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
util::EmoteData emoteData;
|
2018-01-19 22:45:33 +01:00
|
|
|
emoteData.image1x = new Image(GetBTTVEmoteLink(urlTemplate, id, "1x"), 1, code,
|
|
|
|
code + "<br />Global BTTV Emote");
|
|
|
|
emoteData.image2x = new Image(GetBTTVEmoteLink(urlTemplate, id, "2x"), 0.5, code,
|
|
|
|
code + "<br />Global BTTV Emote");
|
|
|
|
emoteData.image3x = new Image(GetBTTVEmoteLink(urlTemplate, id, "3x"), 0.25, code,
|
|
|
|
code + "<br />Global BTTV Emote");
|
2018-05-16 03:55:56 +02:00
|
|
|
emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id;
|
2017-07-23 14:16:13 +02:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
this->bttvGlobalEmotes.insert(code, emoteData);
|
2017-12-26 12:38:54 +01:00
|
|
|
codes.push_back(code.toStdString());
|
2017-01-26 17:26:20 +01:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
this->bttvGlobalEmoteCodes = codes;
|
2017-01-26 17:26:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
void EmoteManager::loadFFZEmotes()
|
2017-01-26 17:26:20 +01:00
|
|
|
{
|
2017-12-26 12:38:54 +01:00
|
|
|
QString url("https://api.frankerfacez.com/v1/set/global");
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.setTimeout(30000);
|
|
|
|
req.getJSON([this](QJsonObject &root) {
|
|
|
|
auto sets = root.value("sets").toObject();
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
std::vector<std::string> codes;
|
|
|
|
for (const QJsonValue &set : sets) {
|
|
|
|
auto emoticons = set.toObject().value("emoticons").toArray();
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
for (const QJsonValue &emote : emoticons) {
|
|
|
|
QJsonObject object = emote.toObject();
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
QString code = object.value("name").toString();
|
2018-05-16 03:55:56 +02:00
|
|
|
int id = object.value("id").toInt();
|
2017-12-26 12:38:54 +01:00
|
|
|
QJsonObject urls = object.value("urls").toObject();
|
2017-07-23 14:16:13 +02:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
util::EmoteData emoteData;
|
2018-04-14 15:10:25 +02:00
|
|
|
FillInFFZEmoteData(urls, code, code + "<br/>Global FFZ Emote", emoteData);
|
2018-05-16 03:55:56 +02:00
|
|
|
emoteData.pageLink =
|
|
|
|
QString("https://www.frankerfacez.com/emoticon/%1-%2").arg(id).arg(code);
|
2018-01-07 02:56:45 +01:00
|
|
|
|
|
|
|
this->ffzGlobalEmotes.insert(code, emoteData);
|
2017-12-26 12:38:54 +01:00
|
|
|
codes.push_back(code.toStdString());
|
2017-01-26 17:26:20 +01:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:38:54 +01:00
|
|
|
this->ffzGlobalEmoteCodes = codes;
|
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
});
|
2018-01-07 02:56:45 +01:00
|
|
|
}
|
2017-01-26 17:26:20 +01:00
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
// id is used for lookup
|
|
|
|
// emoteName is used for giving a name to the emote in case it doesn't exist
|
2017-12-31 22:58:35 +01:00
|
|
|
util::EmoteData EmoteManager::getTwitchEmoteById(long id, const QString &emoteName)
|
2017-01-06 23:28:48 +01:00
|
|
|
{
|
2018-01-07 23:47:08 +01:00
|
|
|
QString _emoteName = emoteName;
|
|
|
|
_emoteName.replace("<", "<");
|
2018-06-04 23:24:54 +02:00
|
|
|
_emoteName.replace(">", ">");
|
2018-01-07 23:47:08 +01:00
|
|
|
|
2018-05-23 13:47:22 +02:00
|
|
|
static QMap<QString, QString> emoteNameReplacements{
|
2018-06-05 00:09:41 +02:00
|
|
|
{"[oO](_|\\.)[oO]", "O_o"}, {"\\>\\;\\(", ">("}, {"\\<\\;3", "<3"},
|
2018-06-05 00:29:51 +02:00
|
|
|
{"\\:-?(o|O)", ":O"}, {"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
|
|
|
|
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("}, {"\\:-?\\)", ":)"},
|
|
|
|
{"\\:-?D", ":D"}, {"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
|
|
|
|
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
|
2018-05-23 13:47:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it = emoteNameReplacements.find(_emoteName);
|
|
|
|
if (it != emoteNameReplacements.end()) {
|
|
|
|
_emoteName = it.value();
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:44:15 +02:00
|
|
|
return _twitchEmoteFromCache.getOrAdd(id, [&emoteName, &_emoteName, &id] {
|
2018-01-07 02:56:45 +01:00
|
|
|
util::EmoteData newEmoteData;
|
2018-01-11 20:16:25 +01:00
|
|
|
newEmoteData.image1x = new Image(GetTwitchEmoteLink(id, "1.0"), 1, emoteName,
|
2018-01-19 22:45:33 +01:00
|
|
|
_emoteName + "<br/>Twitch Emote 1x");
|
2018-01-11 20:16:25 +01:00
|
|
|
newEmoteData.image2x = new Image(GetTwitchEmoteLink(id, "2.0"), .5, emoteName,
|
2018-01-19 22:45:33 +01:00
|
|
|
_emoteName + "<br/>Twitch Emote 2x");
|
2018-01-11 20:16:25 +01:00
|
|
|
newEmoteData.image3x = new Image(GetTwitchEmoteLink(id, "3.0"), .25, emoteName,
|
2018-01-19 22:45:33 +01:00
|
|
|
_emoteName + "<br/>Twitch Emote 3x");
|
2018-01-07 02:56:45 +01:00
|
|
|
|
|
|
|
return newEmoteData;
|
2017-01-13 18:59:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
util::EmoteData EmoteManager::getCheerImage(long long amount, bool animated)
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
2017-01-15 17:21:28 +01:00
|
|
|
// TODO: fix this xD
|
2017-12-31 22:58:35 +01:00
|
|
|
return util::EmoteData();
|
2017-06-13 21:13:58 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 02:55:32 +02:00
|
|
|
pajlada::Signals::NoArgSignal &EmoteManager::getGifUpdateSignal()
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
2017-12-17 13:31:08 +01:00
|
|
|
if (!this->gifUpdateTimerInitiated) {
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2017-12-17 13:31:08 +01:00
|
|
|
this->gifUpdateTimerInitiated = true;
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-12-17 13:31:08 +01:00
|
|
|
this->gifUpdateTimer.setInterval(30);
|
|
|
|
this->gifUpdateTimer.start();
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
app->settings->enableGifAnimations.connect([this](bool enabled, auto) {
|
2017-12-17 13:26:42 +01:00
|
|
|
if (enabled) {
|
2017-12-17 13:31:08 +01:00
|
|
|
this->gifUpdateTimer.start();
|
2017-12-17 13:26:42 +01:00
|
|
|
} else {
|
2017-12-17 13:31:08 +01:00
|
|
|
this->gifUpdateTimer.stop();
|
2017-12-17 03:46:12 +01:00
|
|
|
}
|
2017-06-13 21:13:58 +02:00
|
|
|
});
|
2017-12-17 13:26:42 +01:00
|
|
|
|
2017-12-17 13:31:08 +01:00
|
|
|
QObject::connect(&this->gifUpdateTimer, &QTimer::timeout, [this] {
|
2018-04-03 02:55:32 +02:00
|
|
|
this->gifUpdateTimerSignal.invoke();
|
2017-12-31 00:50:07 +01:00
|
|
|
// fourtf:
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
|
|
|
app->windows->repaintGifEmotes();
|
2017-12-17 13:26:42 +01:00
|
|
|
});
|
2017-01-05 20:49:33 +01:00
|
|
|
}
|
2017-06-13 21:13:58 +02:00
|
|
|
|
2017-12-17 13:31:08 +01:00
|
|
|
return this->gifUpdateTimerSignal;
|
2017-01-04 15:12:31 +01:00
|
|
|
}
|
2017-05-27 16:16:39 +02:00
|
|
|
|
2018-01-07 02:56:45 +01:00
|
|
|
} // namespace singletons
|
2017-05-27 16:16:39 +02:00
|
|
|
} // namespace chatterino
|
2018-03-31 13:44:15 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
void EmojiTest()
|
|
|
|
{
|
|
|
|
auto &emoteManager = singletons::EmoteManager::getInstance();
|
|
|
|
|
|
|
|
emoteManager.loadEmojis();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<std::tuple<util::EmoteData, QString>> dummy;
|
|
|
|
|
|
|
|
// couple_mm 1f468-2764-1f468
|
|
|
|
// "\154075\156150❤\154075\156150"
|
|
|
|
// [0] 55357 0xd83d QChar
|
|
|
|
// [1] 56424 0xdc68 QChar
|
|
|
|
// [2] '❤' 10084 0x2764 QChar
|
|
|
|
// [3] 55357 0xd83d QChar
|
|
|
|
// [4] 56424 0xdc68 QChar
|
|
|
|
QString text = "👨❤👨";
|
|
|
|
|
|
|
|
emoteManager.parseEmojis(dummy, text);
|
|
|
|
|
|
|
|
assert(dummy.size() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<std::tuple<util::EmoteData, QString>> dummy;
|
|
|
|
|
|
|
|
// "✍\154074\157777"
|
|
|
|
// [0] '✍' 9997 0x270d QChar
|
|
|
|
// [1] 55356 0xd83c QChar
|
|
|
|
// [2] 57343 0xdfff QChar
|
|
|
|
QString text = "✍🏿";
|
|
|
|
|
|
|
|
emoteManager.parseEmojis(dummy, text);
|
|
|
|
|
|
|
|
assert(dummy.size() == 1);
|
|
|
|
|
|
|
|
assert(std::get<0>(dummy[0]).isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::vector<std::tuple<util::EmoteData, QString>> dummy;
|
|
|
|
|
|
|
|
QString text = "✍";
|
|
|
|
|
|
|
|
emoteManager.parseEmojis(dummy, text);
|
|
|
|
|
|
|
|
assert(dummy.size() == 1);
|
|
|
|
|
|
|
|
assert(std::get<0>(dummy[0]).isValid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|
|
|
|
#endif
|