2017-12-31 00:50:07 +01:00
|
|
|
#include "twitchmessagebuilder.hpp"
|
2017-09-24 19:23:07 +02:00
|
|
|
#include "debug/log.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/emotemanager.hpp"
|
|
|
|
#include "singletons/ircmanager.hpp"
|
2018-01-01 22:29:21 +01:00
|
|
|
#include "singletons/resourcemanager.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "singletons/thememanager.hpp"
|
|
|
|
#include "singletons/windowmanager.hpp"
|
|
|
|
#include "twitch/twitchchannel.hpp"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-31 00:37:22 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QMediaPlayer>
|
|
|
|
|
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 {
|
|
|
|
namespace twitch {
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
TwitchMessageBuilder::TwitchMessageBuilder(Channel *_channel,
|
2017-07-02 18:12:11 +02:00
|
|
|
const Communi::IrcPrivateMessage *_ircMessage,
|
|
|
|
const messages::MessageParseArgs &_args)
|
|
|
|
: channel(_channel)
|
2017-12-31 00:50:07 +01:00
|
|
|
, twitchChannel(dynamic_cast<TwitchChannel *>(_channel))
|
2017-07-02 18:12:11 +02:00
|
|
|
, ircMessage(_ircMessage)
|
|
|
|
, args(_args)
|
|
|
|
, tags(this->ircMessage->tags())
|
2018-01-02 02:15:11 +01:00
|
|
|
, usernameColor(singletons::ThemeManager::getInstance().messages.textColors.system)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
SharedMessage TwitchMessageBuilder::parse()
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
|
|
|
|
singletons::EmoteManager &emoteManager = singletons::EmoteManager::getInstance();
|
2017-07-31 01:19:25 +02:00
|
|
|
|
|
|
|
this->originalMessage = this->ircMessage->content();
|
|
|
|
|
2017-08-12 13:18:48 +02:00
|
|
|
this->parseUsername();
|
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
// this->message->setCollapsedDefault(true);
|
2017-12-31 00:50:07 +01:00
|
|
|
// this->appendWord(Word(Resources::getInstance().badgeCollapsed, Word::Collapsed, QString(),
|
2017-12-19 00:09:38 +01:00
|
|
|
// QString()));
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
// The timestamp is always appended to the builder
|
|
|
|
// Whether or not will be rendered is decided/checked later
|
2017-12-27 01:22:12 +01:00
|
|
|
|
|
|
|
// Appends the correct timestamp if the message is a past message
|
|
|
|
bool isPastMsg = this->tags.contains("historical");
|
2017-12-28 17:56:00 +01:00
|
|
|
if (isPastMsg) {
|
2017-12-27 01:22:12 +01:00
|
|
|
// This may be architecture dependent(datatype)
|
|
|
|
qint64 ts = this->tags.value("tmi-sent-ts").toLongLong();
|
|
|
|
QDateTime time = QDateTime::fromMSecsSinceEpoch(ts);
|
|
|
|
this->appendTimestamp(time);
|
|
|
|
} else {
|
|
|
|
this->appendTimestamp();
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
this->parseMessageID();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
this->parseRoomID();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
this->appendModerationButtons();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
this->parseTwitchBadges();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-08-12 13:20:52 +02:00
|
|
|
this->parseChatterinoBadges();
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
if (this->args.includeChannelName) {
|
|
|
|
this->parseChannelName();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 13:18:48 +02:00
|
|
|
this->appendUsername();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
// highlights
|
2017-12-27 01:22:12 +01:00
|
|
|
if (settings.enableHighlights && !isPastMsg) {
|
2017-07-31 01:19:25 +02:00
|
|
|
this->parseHighlights();
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
// bits
|
|
|
|
QString bits = "";
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
auto iterator = this->tags.find("bits");
|
|
|
|
if (iterator != this->tags.end()) {
|
2017-04-12 17:46:44 +02:00
|
|
|
bits = iterator.value().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// twitch emotes
|
2017-12-31 22:58:35 +01:00
|
|
|
std::vector<std::pair<long, util::EmoteData>> twitchEmotes;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
iterator = this->tags.find("emotes");
|
|
|
|
if (iterator != this->tags.end()) {
|
2017-06-13 21:13:58 +02:00
|
|
|
QStringList emoteString = iterator.value().toString().split('/');
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
for (QString emote : emoteString) {
|
2017-12-17 02:18:13 +01:00
|
|
|
this->appendTwitchEmote(ircMessage, emote, twitchEmotes);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
2017-12-31 22:58:35 +01:00
|
|
|
bool operator()(const std::pair<long, util::EmoteData> &lhs,
|
|
|
|
const std::pair<long, util::EmoteData> &rhs)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-07-02 18:12:11 +02:00
|
|
|
return lhs.first < rhs.first;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
} customLess;
|
|
|
|
|
|
|
|
std::sort(twitchEmotes.begin(), twitchEmotes.end(), customLess);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto currentTwitchEmote = twitchEmotes.begin();
|
|
|
|
|
|
|
|
// words
|
|
|
|
|
2017-07-31 01:19:25 +02:00
|
|
|
QStringList splits = this->originalMessage.split(' ');
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
long int i = 0;
|
|
|
|
|
|
|
|
for (QString split : splits) {
|
2017-09-21 00:54:10 +02:00
|
|
|
MessageColor textColor = ircMessage->isAction() ? MessageColor(this->usernameColor)
|
|
|
|
: MessageColor(MessageColor::Text);
|
2017-08-12 12:07:53 +02:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// twitch emote
|
|
|
|
if (currentTwitchEmote != twitchEmotes.end() && currentTwitchEmote->first == i) {
|
2017-07-02 18:12:11 +02:00
|
|
|
this->appendWord(
|
2017-07-09 17:58:59 +02:00
|
|
|
Word(currentTwitchEmote->second.image, Word::TwitchEmoteImage,
|
|
|
|
currentTwitchEmote->second.image->getName(),
|
|
|
|
currentTwitchEmote->second.image->getName() + QString("\nTwitch Emote")));
|
2017-07-02 18:12:11 +02:00
|
|
|
this->appendWord(
|
2017-07-09 17:58:59 +02:00
|
|
|
Word(currentTwitchEmote->second.image->getName(), Word::TwitchEmoteText, textColor,
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::FontManager::Medium, currentTwitchEmote->second.image->getName(),
|
2017-07-09 17:58:59 +02:00
|
|
|
currentTwitchEmote->second.image->getName() + QString("\nTwitch Emote")));
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
i += split.length() + 1;
|
|
|
|
currentTwitchEmote = std::next(currentTwitchEmote);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// split words
|
2017-12-31 22:58:35 +01:00
|
|
|
std::vector<std::tuple<util::EmoteData, QString>> parsed;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 17:37:17 +02:00
|
|
|
// Parse emojis and take all non-emojis and put them in parsed as full text-words
|
2017-06-26 16:41:20 +02:00
|
|
|
emoteManager.parseEmojis(parsed, split);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
for (const auto &tuple : parsed) {
|
2017-12-31 22:58:35 +01:00
|
|
|
const util::EmoteData &emoteData = std::get<0>(tuple);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
if (emoteData.image == nullptr) { // is text
|
2017-04-12 17:46:44 +02:00
|
|
|
QString string = std::get<1>(tuple);
|
|
|
|
|
|
|
|
static QRegularExpression cheerRegex("cheer[1-9][0-9]*");
|
|
|
|
|
|
|
|
// cheers
|
|
|
|
if (!bits.isEmpty() && string.length() >= 6 && cheerRegex.match(string).isValid()) {
|
|
|
|
auto cheer = string.mid(5).toInt();
|
|
|
|
|
|
|
|
QString color;
|
|
|
|
|
|
|
|
QColor bitsColor;
|
|
|
|
|
|
|
|
if (cheer >= 10000) {
|
|
|
|
color = "red";
|
|
|
|
bitsColor = QColor::fromHslF(0, 1, 0.5);
|
|
|
|
} else if (cheer >= 5000) {
|
|
|
|
color = "blue";
|
|
|
|
bitsColor = QColor::fromHslF(0.61, 1, 0.4);
|
|
|
|
} else if (cheer >= 1000) {
|
|
|
|
color = "green";
|
|
|
|
bitsColor = QColor::fromHslF(0.5, 1, 0.5);
|
|
|
|
} else if (cheer >= 100) {
|
|
|
|
color = "purple";
|
|
|
|
bitsColor = QColor::fromHslF(0.8, 1, 0.5);
|
|
|
|
} else {
|
|
|
|
color = "gray";
|
|
|
|
bitsColor = QColor::fromHslF(0.5f, 0.5f, 0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString bitsLinkAnimated =
|
|
|
|
QString("http://static-cdn.jtvnw.net/bits/dark/animated/" + color + "/1");
|
|
|
|
QString bitsLink =
|
|
|
|
QString("http://static-cdn.jtvnw.net/bits/dark/static/" + color + "/1");
|
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
LazyLoadedImage *imageAnimated = emoteManager.miscImageCache.getOrAdd(
|
2017-07-02 18:12:11 +02:00
|
|
|
bitsLinkAnimated, [this, &bitsLinkAnimated] {
|
2017-12-17 02:18:13 +01:00
|
|
|
return new LazyLoadedImage(bitsLinkAnimated);
|
2017-06-13 21:13:58 +02:00
|
|
|
});
|
2017-12-17 02:18:13 +01:00
|
|
|
LazyLoadedImage *image = emoteManager.miscImageCache.getOrAdd(
|
|
|
|
bitsLink, [this, &bitsLink] { return new LazyLoadedImage(bitsLink); });
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
this->appendWord(Word(imageAnimated, Word::BitsAnimated, QString("cheer"),
|
|
|
|
QString("Twitch Cheer"),
|
|
|
|
Link(Link::Url, QString("https://blog.twitch.tv/"
|
|
|
|
"introducing-cheering-celebrate-"
|
|
|
|
"together-da62af41fac6"))));
|
2017-09-21 00:54:10 +02:00
|
|
|
this->appendWord(Word(
|
|
|
|
image, Word::BitsStatic, QString("cheer"), QString("Twitch Cheer"),
|
|
|
|
Link(Link::Url,
|
|
|
|
QString("https://blog.twitch.tv/"
|
|
|
|
"introducing-cheering-celebrate-together-da62af41fac6"))));
|
|
|
|
|
|
|
|
this->appendWord(Word(
|
|
|
|
QString("x" + string.mid(5)), Word::BitsAmount, MessageColor(bitsColor),
|
2018-01-01 22:29:21 +01:00
|
|
|
singletons::FontManager::Medium, QString(string.mid(5)),
|
|
|
|
QString("Twitch Cheer"),
|
2017-09-21 00:54:10 +02:00
|
|
|
Link(Link::Url,
|
|
|
|
QString("https://blog.twitch.tv/"
|
|
|
|
"introducing-cheering-celebrate-together-da62af41fac6"))));
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
// TODO: Implement ignored emotes
|
|
|
|
// Format of ignored emotes:
|
|
|
|
// Emote name: "forsenPuke" - if string in ignoredEmotes
|
|
|
|
// Will match emote regardless of source (i.e. bttv, ffz)
|
|
|
|
// Emote source + name: "bttv:nyanPls"
|
2017-07-23 09:53:50 +02:00
|
|
|
if (this->tryAppendEmote(string)) {
|
|
|
|
// Successfully appended an emote
|
2017-04-12 17:46:44 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-23 09:53:50 +02:00
|
|
|
// Actually just text
|
2017-08-05 18:44:14 +02:00
|
|
|
QString linkString = this->matchLink(string);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-08-12 12:09:26 +02:00
|
|
|
Link link;
|
|
|
|
|
|
|
|
if (linkString.isEmpty()) {
|
|
|
|
link = Link();
|
|
|
|
} else {
|
|
|
|
link = Link(Link::Url, linkString);
|
2017-09-21 00:54:10 +02:00
|
|
|
textColor = MessageColor(MessageColor::Link);
|
2017-08-12 12:09:26 +02:00
|
|
|
}
|
2017-08-05 18:44:14 +02:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
this->appendWord(Word(string, Word::Text, textColor,
|
|
|
|
singletons::FontManager::Medium, string, QString(), link));
|
2017-04-12 17:46:44 +02:00
|
|
|
} else { // is emoji
|
2017-07-09 17:58:59 +02:00
|
|
|
this->appendWord(Word(emoteData.image, Word::EmojiImage, emoteData.image->getName(),
|
2017-12-19 03:36:05 +01:00
|
|
|
emoteData.image->getTooltip()));
|
2018-01-01 22:29:21 +01:00
|
|
|
Word(emoteData.image->getName(), Word::EmojiText, textColor,
|
|
|
|
singletons::FontManager::Medium, emoteData.image->getName(),
|
|
|
|
emoteData.image->getTooltip());
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:46:16 +02:00
|
|
|
for (int j = 0; j < split.size(); j++) {
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (split.at(j).isHighSurrogate()) {
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
return this->getMessage();
|
2017-07-02 18:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchMessageBuilder::parseMessageID()
|
|
|
|
{
|
|
|
|
auto iterator = this->tags.find("id");
|
|
|
|
|
|
|
|
if (iterator != this->tags.end()) {
|
|
|
|
this->messageID = iterator.value().toString();
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
void TwitchMessageBuilder::parseRoomID()
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
if (this->twitchChannel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
auto iterator = this->tags.find("room-id");
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
if (iterator != std::end(this->tags)) {
|
2017-11-04 14:57:29 +01:00
|
|
|
this->roomID = iterator.value().toString();
|
2017-07-02 18:12:11 +02:00
|
|
|
|
2017-11-04 14:57:29 +01:00
|
|
|
if (this->twitchChannel->roomID.isEmpty()) {
|
2017-09-16 00:05:06 +02:00
|
|
|
this->twitchChannel->roomID = this->roomID;
|
2017-07-02 18:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchMessageBuilder::parseChannelName()
|
|
|
|
{
|
2017-07-23 09:53:50 +02:00
|
|
|
QString channelName("#" + this->channel->name);
|
2017-09-21 00:54:10 +02:00
|
|
|
this->appendWord(Word(channelName, Word::Misc, MessageColor(MessageColor::System),
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::FontManager::Medium, QString(channelName), QString(),
|
2017-07-23 09:53:50 +02:00
|
|
|
Link(Link::Url, this->channel->name + "\n" + this->messageID)));
|
2017-07-02 18:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchMessageBuilder::parseUsername()
|
|
|
|
{
|
|
|
|
auto iterator = this->tags.find("color");
|
|
|
|
if (iterator != this->tags.end()) {
|
|
|
|
this->usernameColor = QColor(iterator.value().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// username
|
|
|
|
this->userName = ircMessage->nick();
|
|
|
|
|
|
|
|
if (this->userName.isEmpty()) {
|
|
|
|
this->userName = this->tags.value(QLatin1String("login")).toString();
|
|
|
|
}
|
2017-12-17 17:48:46 +01:00
|
|
|
|
2017-12-17 21:05:25 +01:00
|
|
|
this->message->loginName = this->userName;
|
2018-01-01 23:29:54 +01:00
|
|
|
this->message->timeoutUser = this->userName;
|
2017-08-12 13:18:48 +02:00
|
|
|
}
|
2017-07-02 18:12:11 +02:00
|
|
|
|
2017-08-12 13:18:48 +02:00
|
|
|
void TwitchMessageBuilder::appendUsername()
|
|
|
|
{
|
2017-07-23 11:59:05 +02:00
|
|
|
QString username = this->userName;
|
|
|
|
QString localizedName;
|
2017-07-02 18:12:11 +02:00
|
|
|
|
2017-08-12 13:18:48 +02:00
|
|
|
auto iterator = this->tags.find("display-name");
|
2017-07-23 11:59:05 +02:00
|
|
|
if (iterator != this->tags.end()) {
|
|
|
|
QString displayName = iterator.value().toString();
|
|
|
|
|
|
|
|
if (QString::compare(displayName, this->userName, Qt::CaseInsensitive) == 0) {
|
|
|
|
username = displayName;
|
2017-12-17 21:05:25 +01:00
|
|
|
|
|
|
|
this->message->displayName = displayName;
|
2017-07-23 11:59:05 +02:00
|
|
|
} else {
|
|
|
|
localizedName = displayName;
|
2017-12-17 21:05:25 +01:00
|
|
|
|
|
|
|
this->message->displayName = username;
|
|
|
|
this->message->localizedName = displayName;
|
2017-07-23 11:59:05 +02:00
|
|
|
}
|
2017-07-02 18:12:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-23 11:59:05 +02:00
|
|
|
bool hasLocalizedName = !localizedName.isEmpty();
|
|
|
|
|
|
|
|
// The full string that will be rendered in the chat widget
|
|
|
|
QString usernameString;
|
|
|
|
|
|
|
|
pajlada::Settings::Setting<int> usernameDisplayMode(
|
|
|
|
"/appearance/messages/usernameDisplayMode", UsernameDisplayMode::UsernameAndLocalizedName);
|
|
|
|
|
|
|
|
switch (usernameDisplayMode.getValue()) {
|
|
|
|
case UsernameDisplayMode::Username: {
|
|
|
|
usernameString = username;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UsernameDisplayMode::LocalizedName: {
|
|
|
|
if (hasLocalizedName) {
|
|
|
|
usernameString = localizedName;
|
|
|
|
} else {
|
|
|
|
usernameString = username;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case UsernameDisplayMode::UsernameAndLocalizedName: {
|
|
|
|
if (hasLocalizedName) {
|
|
|
|
usernameString = username + "(" + localizedName + ")";
|
|
|
|
} else {
|
|
|
|
usernameString = username;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
2017-07-02 18:12:11 +02:00
|
|
|
|
|
|
|
if (this->args.isSentWhisper) {
|
|
|
|
// TODO(pajlada): Re-implement
|
|
|
|
// userDisplayString += IrcManager::getInstance().getUser().getUserName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->args.isReceivedWhisper) {
|
|
|
|
// TODO(pajlada): Re-implement
|
|
|
|
// userDisplayString += " -> " + IrcManager::getInstance().getUser().getUserName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ircMessage->isAction()) {
|
2017-12-28 17:56:00 +01:00
|
|
|
usernameString += ":";
|
2017-07-02 18:12:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 00:54:10 +02:00
|
|
|
this->appendWord(Word(usernameString, Word::Username, MessageColor(this->usernameColor),
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::FontManager::MediumBold, usernameString, QString(),
|
2017-12-23 21:18:13 +01:00
|
|
|
Link(Link::UserInfo, this->userName)));
|
2017-07-02 18:12:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:19:25 +02:00
|
|
|
void TwitchMessageBuilder::parseHighlights()
|
|
|
|
{
|
|
|
|
static auto player = new QMediaPlayer;
|
2017-12-18 17:57:56 +01:00
|
|
|
static QUrl currentPlayerUrl;
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
|
2017-09-24 18:14:22 +02:00
|
|
|
static pajlada::Settings::Setting<std::string> currentUser("/accounts/current");
|
2017-07-31 01:19:25 +02:00
|
|
|
|
2017-08-19 15:37:56 +02:00
|
|
|
QString currentUsername = QString::fromStdString(currentUser.getValue());
|
|
|
|
|
|
|
|
if (this->ircMessage->nick() == currentUsername) {
|
2017-07-31 01:19:25 +02:00
|
|
|
// Do nothing. Highlights cannot be triggered by yourself
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:57:56 +01:00
|
|
|
// update the media player url if necessary
|
|
|
|
QUrl highlightSoundUrl;
|
2017-12-17 01:19:56 +01:00
|
|
|
if (settings.customHighlightSound) {
|
2017-12-18 17:57:56 +01:00
|
|
|
highlightSoundUrl = QUrl(settings.pathHighlightSound.get());
|
2017-07-31 01:19:25 +02:00
|
|
|
} else {
|
2017-12-18 17:57:56 +01:00
|
|
|
highlightSoundUrl = QUrl("qrc:/sounds/ping2.wav");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentPlayerUrl != highlightSoundUrl) {
|
|
|
|
player->setMedia(highlightSoundUrl);
|
|
|
|
|
|
|
|
currentPlayerUrl = highlightSoundUrl;
|
2017-07-31 01:19:25 +02:00
|
|
|
}
|
|
|
|
|
2017-09-24 18:43:24 +02:00
|
|
|
QStringList blackList =
|
|
|
|
settings.highlightUserBlacklist.get().split("\n", QString::SkipEmptyParts);
|
2017-09-23 19:23:10 +02:00
|
|
|
|
2017-07-31 01:19:25 +02:00
|
|
|
// TODO: This vector should only be rebuilt upon highlights being changed
|
2018-01-04 01:52:37 +01:00
|
|
|
auto activeHighlights = settings.highlightProperties.getValue();
|
2017-07-31 01:19:25 +02:00
|
|
|
|
2017-12-17 01:19:56 +01:00
|
|
|
if (settings.enableHighlightsSelf && currentUsername.size() > 0) {
|
2018-01-04 01:52:37 +01:00
|
|
|
messages::HighlightPhrase selfHighlight;
|
|
|
|
selfHighlight.key = currentUsername;
|
|
|
|
selfHighlight.sound = settings.enableHighlightSound;
|
|
|
|
selfHighlight.alert = settings.enableHighlightTaskbar;
|
|
|
|
activeHighlights.emplace_back(std::move(selfHighlight));
|
2017-07-31 01:19:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 11:08:32 +02:00
|
|
|
bool doHighlight = false;
|
2017-07-31 01:19:25 +02:00
|
|
|
bool playSound = false;
|
|
|
|
bool doAlert = false;
|
2017-10-08 15:58:28 +02:00
|
|
|
|
|
|
|
bool hasFocus = (QApplication::focusWidget() != nullptr);
|
|
|
|
|
2017-09-24 18:43:24 +02:00
|
|
|
if (!blackList.contains(this->ircMessage->nick(), Qt::CaseInsensitive)) {
|
2018-01-04 01:52:37 +01:00
|
|
|
for (const messages::HighlightPhrase &highlight : activeHighlights) {
|
|
|
|
if (this->originalMessage.contains(highlight.key, Qt::CaseInsensitive)) {
|
2017-09-24 19:23:07 +02:00
|
|
|
debug::Log("Highlight because {} contains {}", this->originalMessage,
|
2018-01-04 01:52:37 +01:00
|
|
|
highlight.key);
|
2017-09-23 19:23:10 +02:00
|
|
|
doHighlight = true;
|
|
|
|
|
|
|
|
if (highlight.sound) {
|
|
|
|
playSound = true;
|
|
|
|
}
|
2017-07-31 01:19:25 +02:00
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
if (highlight.alert) {
|
|
|
|
doAlert = true;
|
|
|
|
}
|
2017-07-31 01:19:25 +02:00
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
if (playSound && doAlert) {
|
|
|
|
// Break if no further action can be taken from other highlights
|
|
|
|
// This might change if highlights can have custom colors/sounds/actions
|
|
|
|
break;
|
|
|
|
}
|
2017-07-31 01:19:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
this->setHighlight(doHighlight);
|
2017-07-31 01:19:25 +02:00
|
|
|
|
2017-10-08 15:58:28 +02:00
|
|
|
if (playSound && (!hasFocus || settings.highlightAlwaysPlaySound)) {
|
2017-09-23 19:23:10 +02:00
|
|
|
player->play();
|
|
|
|
}
|
2017-07-31 01:19:25 +02:00
|
|
|
|
2017-09-23 19:23:10 +02:00
|
|
|
if (doAlert) {
|
2018-01-01 22:29:21 +01:00
|
|
|
QApplication::alert(singletons::WindowManager::getInstance().getMainWindow().window(),
|
|
|
|
2500);
|
2017-09-23 19:23:10 +02:00
|
|
|
}
|
2017-07-31 01:19:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
void TwitchMessageBuilder::appendModerationButtons()
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
|
|
|
// mod buttons
|
|
|
|
static QString buttonBanTooltip("Ban user");
|
|
|
|
static QString buttonTimeoutTooltip("Timeout user");
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
this->appendWord(Word(singletons::ResourceManager::getInstance().buttonBan, Word::ButtonBan,
|
|
|
|
QString(), buttonBanTooltip, Link(Link::UserBan, ircMessage->account())));
|
|
|
|
this->appendWord(Word(singletons::ResourceManager::getInstance().buttonTimeout,
|
|
|
|
Word::ButtonTimeout, QString(), buttonTimeoutTooltip,
|
|
|
|
Link(Link::UserTimeout, ircMessage->account())));
|
2017-06-13 21:13:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcPrivateMessage *ircMessage,
|
|
|
|
const QString &emote,
|
2017-12-31 22:58:35 +01:00
|
|
|
std::vector<std::pair<long int, util::EmoteData>> &vec)
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::EmoteManager &emoteManager = singletons::EmoteManager::getInstance();
|
2017-06-13 21:13:58 +02:00
|
|
|
if (!emote.contains(':')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList parameters = emote.split(':');
|
|
|
|
|
|
|
|
if (parameters.length() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long int id = std::stol(parameters.at(0).toStdString(), nullptr, 10);
|
|
|
|
|
|
|
|
QStringList occurences = parameters.at(1).split(',');
|
|
|
|
|
|
|
|
for (QString occurence : occurences) {
|
|
|
|
QStringList coords = occurence.split('-');
|
|
|
|
|
|
|
|
if (coords.length() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long int start = std::stol(coords.at(0).toStdString(), nullptr, 10);
|
|
|
|
long int end = std::stol(coords.at(1).toStdString(), nullptr, 10);
|
|
|
|
|
|
|
|
if (start >= end || start < 0 || end > ircMessage->content().length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString name = ircMessage->content().mid(start, end - start + 1);
|
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
vec.push_back(
|
2017-12-31 22:58:35 +01:00
|
|
|
std::pair<long int, util::EmoteData>(start, emoteManager.getTwitchEmoteById(id, name)));
|
2017-06-13 21:13:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 09:53:50 +02:00
|
|
|
bool TwitchMessageBuilder::tryAppendEmote(QString &emoteString)
|
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
singletons::EmoteManager &emoteManager = singletons::EmoteManager::getInstance();
|
|
|
|
util::EmoteData emoteData;
|
2017-07-23 09:53:50 +02:00
|
|
|
|
|
|
|
if (emoteManager.bttvGlobalEmotes.tryGet(emoteString, emoteData)) {
|
|
|
|
// BTTV Global Emote
|
|
|
|
return this->appendEmote(emoteData);
|
2017-12-31 00:50:07 +01:00
|
|
|
} else if (this->twitchChannel != nullptr &&
|
|
|
|
this->twitchChannel->bttvChannelEmotes->tryGet(emoteString, emoteData)) {
|
2017-07-23 09:53:50 +02:00
|
|
|
// BTTV Channel Emote
|
|
|
|
return this->appendEmote(emoteData);
|
|
|
|
} else if (emoteManager.ffzGlobalEmotes.tryGet(emoteString, emoteData)) {
|
|
|
|
// FFZ Global Emote
|
|
|
|
return this->appendEmote(emoteData);
|
2017-12-31 00:50:07 +01:00
|
|
|
} else if (this->twitchChannel != nullptr &&
|
|
|
|
this->twitchChannel->ffzChannelEmotes->tryGet(emoteString, emoteData)) {
|
2017-07-23 09:53:50 +02:00
|
|
|
// FFZ Channel Emote
|
|
|
|
return this->appendEmote(emoteData);
|
|
|
|
} else if (emoteManager.getChatterinoEmotes().tryGet(emoteString, emoteData)) {
|
|
|
|
// Chatterino Emote
|
|
|
|
return this->appendEmote(emoteData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
bool TwitchMessageBuilder::appendEmote(util::EmoteData &emoteData)
|
2017-07-23 09:53:50 +02:00
|
|
|
{
|
|
|
|
this->appendWord(Word(emoteData.image, Word::BttvEmoteImage, emoteData.image->getName(),
|
|
|
|
emoteData.image->getTooltip(),
|
|
|
|
Link(Link::Url, emoteData.image->getUrl())));
|
|
|
|
|
|
|
|
// Perhaps check for ignored emotes here?
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:12:11 +02:00
|
|
|
void TwitchMessageBuilder::parseTwitchBadges()
|
2017-06-13 21:13:58 +02:00
|
|
|
{
|
2018-01-01 22:29:21 +01:00
|
|
|
const auto &channelResources =
|
|
|
|
singletons::ResourceManager::getInstance().channels[this->roomID];
|
2017-07-02 18:12:11 +02:00
|
|
|
|
|
|
|
auto iterator = this->tags.find("badges");
|
|
|
|
|
|
|
|
if (iterator == this->tags.end()) {
|
|
|
|
// No badges in this message
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList badges = iterator.value().toString().split(',');
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
for (QString badge : badges) {
|
2017-06-13 22:03:29 +02:00
|
|
|
if (badge.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:13:58 +02:00
|
|
|
if (badge.startsWith("bits/")) {
|
2017-12-31 22:58:35 +01:00
|
|
|
if (!singletons::ResourceManager::getInstance().dynamicBadgesLoaded) {
|
2017-06-15 23:13:01 +02:00
|
|
|
// Do nothing
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString cheerAmountQS = badge.mid(5);
|
2017-06-16 10:01:21 +02:00
|
|
|
std::string versionKey = cheerAmountQS.toStdString();
|
2017-06-15 23:13:01 +02:00
|
|
|
|
2017-06-16 08:03:13 +02:00
|
|
|
try {
|
2017-12-31 22:58:35 +01:00
|
|
|
auto &badgeSet = singletons::ResourceManager::getInstance().badgeSets.at("bits");
|
2017-06-16 08:03:13 +02:00
|
|
|
|
|
|
|
try {
|
2017-06-16 10:01:21 +02:00
|
|
|
auto &badgeVersion = badgeSet.versions.at(versionKey);
|
2017-06-16 08:03:13 +02:00
|
|
|
|
|
|
|
appendWord(
|
2017-06-16 10:11:44 +02:00
|
|
|
Word(badgeVersion.badgeImage1x, Word::BadgeVanity, QString(),
|
2017-06-16 08:03:13 +02:00
|
|
|
QString("Twitch " + QString::fromStdString(badgeVersion.title))));
|
|
|
|
} catch (const std::exception &e) {
|
2018-01-01 22:29:21 +01:00
|
|
|
debug::Log("Exception caught: {} when trying to fetch badge version {} ",
|
2017-09-24 19:23:07 +02:00
|
|
|
e.what(), versionKey);
|
2017-06-16 08:03:13 +02:00
|
|
|
}
|
|
|
|
} catch (const std::exception &e) {
|
2017-09-24 19:23:07 +02:00
|
|
|
debug::Log("No badge set with key bits. Exception: {}", e.what());
|
2017-06-16 08:03:13 +02:00
|
|
|
}
|
2017-06-13 21:13:58 +02:00
|
|
|
} else if (badge == "staff/1") {
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeStaff,
|
|
|
|
Word::BadgeGlobalAuthority, QString(), QString("Twitch Staff")));
|
2017-06-13 21:13:58 +02:00
|
|
|
} else if (badge == "admin/1") {
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeAdmin,
|
|
|
|
Word::BadgeGlobalAuthority, QString(), QString("Twitch Admin")));
|
2017-06-13 21:13:58 +02:00
|
|
|
} else if (badge == "global_mod/1") {
|
2017-12-31 22:58:35 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeGlobalModerator,
|
2017-12-31 00:50:07 +01:00
|
|
|
Word::BadgeGlobalAuthority, QString(), QString("Global Moderator")));
|
2017-06-13 21:13:58 +02:00
|
|
|
} else if (badge == "moderator/1") {
|
2017-06-13 22:03:29 +02:00
|
|
|
// TODO: Implement custom FFZ moderator badge
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeModerator,
|
|
|
|
Word::BadgeChannelAuthority, QString(),
|
2017-06-13 21:13:58 +02:00
|
|
|
QString("Channel Moderator"))); // custom badge
|
|
|
|
} else if (badge == "turbo/1") {
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeTurbo,
|
|
|
|
Word::BadgeVanity, QString(), QString("Turbo Subscriber")));
|
2017-06-13 21:13:58 +02:00
|
|
|
} else if (badge == "broadcaster/1") {
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeBroadcaster,
|
|
|
|
Word::BadgeChannelAuthority, QString(),
|
|
|
|
QString("Channel Broadcaster")));
|
2017-06-13 21:13:58 +02:00
|
|
|
} else if (badge == "premium/1") {
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgePremium,
|
|
|
|
Word::BadgeVanity, QString(), QString("Twitch Prime")));
|
2017-06-13 22:03:29 +02:00
|
|
|
|
|
|
|
} else if (badge.startsWith("partner/")) {
|
|
|
|
int index = badge.midRef(8).toInt();
|
|
|
|
switch (index) {
|
|
|
|
case 1: {
|
2018-01-01 22:29:21 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeVerified,
|
|
|
|
Word::BadgeVanity, QString(), "Twitch Verified"));
|
2017-06-13 22:03:29 +02:00
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
printf("[TwitchMessageBuilder] Unhandled partner badge index: %d\n", index);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} else if (badge.startsWith("subscriber/")) {
|
2017-07-02 15:11:33 +02:00
|
|
|
if (channelResources.loaded == false) {
|
|
|
|
qDebug() << "Channel resources are not loaded, can't add the subscriber badge";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-04 17:22:53 +01:00
|
|
|
auto badgeSetIt = channelResources.badgeSets.find("subscriber");
|
|
|
|
if (badgeSetIt == channelResources.badgeSets.end()) {
|
|
|
|
// Fall back to default badge
|
2017-12-31 22:58:35 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeSubscriber,
|
2017-12-31 00:50:07 +01:00
|
|
|
Word::Flags::BadgeSubscription, QString(),
|
|
|
|
QString("Twitch Subscriber")));
|
2017-11-04 17:22:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-02 15:11:33 +02:00
|
|
|
|
2017-11-04 17:22:53 +01:00
|
|
|
const auto &badgeSet = badgeSetIt->second;
|
2017-07-02 15:11:33 +02:00
|
|
|
|
2017-11-04 17:22:53 +01:00
|
|
|
std::string versionKey = badge.mid(11).toStdString();
|
2017-07-02 15:11:33 +02:00
|
|
|
|
2017-11-04 17:22:53 +01:00
|
|
|
auto badgeVersionIt = badgeSet.versions.find(versionKey);
|
|
|
|
|
|
|
|
if (badgeVersionIt == badgeSet.versions.end()) {
|
|
|
|
// Fall back to default badge
|
2017-12-31 22:58:35 +01:00
|
|
|
appendWord(Word(singletons::ResourceManager::getInstance().badgeSubscriber,
|
2017-12-31 00:50:07 +01:00
|
|
|
Word::Flags::BadgeSubscription, QString(),
|
|
|
|
QString("Twitch Subscriber")));
|
2017-11-04 17:22:53 +01:00
|
|
|
continue;
|
2017-06-13 22:03:29 +02:00
|
|
|
}
|
2017-07-02 15:11:33 +02:00
|
|
|
|
2017-11-04 17:22:53 +01:00
|
|
|
auto &badgeVersion = badgeVersionIt->second;
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
appendWord(Word(badgeVersion.badgeImage1x, Word::Flags::BadgeSubscription, QString(),
|
2017-11-04 17:22:53 +01:00
|
|
|
QString("Twitch " + QString::fromStdString(badgeVersion.title))));
|
2017-06-13 22:03:29 +02:00
|
|
|
} else {
|
2017-12-31 22:58:35 +01:00
|
|
|
if (!singletons::ResourceManager::getInstance().dynamicBadgesLoaded) {
|
2017-06-16 10:01:21 +02:00
|
|
|
// Do nothing
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList parts = badge.split('/');
|
|
|
|
|
|
|
|
if (parts.length() != 2) {
|
|
|
|
qDebug() << "Bad number of parts: " << parts.length() << " in " << parts;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
Word::Flags badgeType = Word::Flags::BadgeVanity;
|
2017-06-16 10:01:21 +02:00
|
|
|
|
|
|
|
std::string badgeSetKey = parts[0].toStdString();
|
|
|
|
std::string versionKey = parts[1].toStdString();
|
|
|
|
|
|
|
|
try {
|
2018-01-01 22:29:21 +01:00
|
|
|
auto &badgeSet =
|
|
|
|
singletons::ResourceManager::getInstance().badgeSets.at(badgeSetKey);
|
2017-06-16 10:01:21 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
auto &badgeVersion = badgeSet.versions.at(versionKey);
|
|
|
|
|
|
|
|
appendWord(
|
|
|
|
Word(badgeVersion.badgeImage1x, badgeType, QString(),
|
|
|
|
QString("Twitch " + QString::fromStdString(badgeVersion.title))));
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
qDebug() << "Exception caught:" << e.what()
|
|
|
|
<< "when trying to fetch badge version " << versionKey.c_str();
|
|
|
|
}
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
qDebug() << "No badge set with key" << badgeSetKey.c_str()
|
|
|
|
<< ". Exception: " << e.what();
|
|
|
|
}
|
2017-06-13 21:13:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 13:20:52 +02:00
|
|
|
void TwitchMessageBuilder::parseChatterinoBadges()
|
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
auto &badges = singletons::ResourceManager::getInstance().chatterinoBadges;
|
2017-08-12 13:20:52 +02:00
|
|
|
auto it = badges.find(this->userName.toStdString());
|
|
|
|
|
|
|
|
if (it == badges.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto badge = it->second;
|
|
|
|
|
|
|
|
this->appendWord(Word(badge->image, Word::BadgeChatterino, QString(), badge->tooltip.c_str()));
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// bool
|
|
|
|
// sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
|
|
|
|
// const std::pair<long int, LazyLoadedImage *> &b)
|
|
|
|
//{
|
|
|
|
// return a.first < b.first;
|
|
|
|
//}
|
2017-06-07 10:09:24 +02:00
|
|
|
|
|
|
|
} // namespace twitch
|
|
|
|
} // namespace chatterino
|