mirror-chatterino2/src/twitch/twitchmessagebuilder.cpp

707 lines
24 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#include "twitchmessagebuilder.hpp"
#include "debug/log.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/emotemanager.hpp"
#include "singletons/ircmanager.hpp"
#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
#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-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
{
}
MessagePtr 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();
this->originalMessage = this->ircMessage->content();
// PARSING
this->parseUsername();
// this->message->setCollapsedDefault(true);
2017-12-31 00:50:07 +01:00
// this->appendWord(Word(Resources::getInstance().badgeCollapsed, Word::Collapsed, QString(),
// QString()));
// The timestamp is always appended to the builder
// Whether or not will be rendered is decided/checked later
// 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) {
// This may be architecture dependent(datatype)
qint64 ts = this->tags.value("tmi-sent-ts").toLongLong();
QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(ts);
this->emplace<TimestampElement>(dateTime.time());
} else {
this->emplace<TimestampElement>();
}
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
// TIMESTAMP
this->emplace<TwitchModerationElement>();
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
this->addChatterinoBadges();
2017-08-12 13:20:52 +02:00
2017-07-02 18:12:11 +02:00
if (this->args.includeChannelName) {
this->parseChannelName();
2017-04-12 17:46:44 +02:00
}
this->appendUsername();
2017-04-12 17:46:44 +02:00
// highlights
if (settings.enableHighlights && !isPastMsg) {
this->parseHighlights();
}
2017-04-12 17:46:44 +02:00
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()) {
QStringList emoteString = iterator.value().toString().split('/');
2017-04-12 17:46:44 +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
QStringList splits = this->originalMessage.split(' ');
2017-04-12 17:46:44 +02:00
long int i = 0;
for (QString split : splits) {
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) {
auto emoteImage = currentTwitchEmote->second;
this->emplace<EmoteElement>(emoteImage, MessageElement::TwitchEmote);
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
emoteManager.parseEmojis(parsed, split);
2017-04-12 17:46:44 +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
if (!emoteData.isValid()) { // is text
2017-04-12 17:46:44 +02:00
QString string = std::get<1>(tuple);
if (!bits.isEmpty() && this->tryParseCheermote(string)) {
// This string was parsed as a cheermote
2017-04-12 17:46:44 +02:00
continue;
}
// 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);
textColor = MessageColor(MessageColor::Link);
2017-08-12 12:09:26 +02:00
}
2017-08-05 18:44:14 +02:00
this->emplace<TextElement>(string, EmoteElement::Text) //
->setLink(link);
2017-04-12 17:46:44 +02:00
} else { // is emoji
this->emplace<EmoteElement>(emoteData, EmoteElement::EmojiAll);
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
}
2018-01-17 14:25:03 +01:00
this->message->setSearchText(this->userName + ": " + this->originalMessage);
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-07-02 18:12:11 +02:00
if (iterator != std::end(this->tags)) {
this->roomID = iterator.value().toString();
2017-07-02 18:12:11 +02:00
if (this->twitchChannel->roomID.isEmpty()) {
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);
Link link(Link::Url, this->channel->name + "\n" + this->messageID);
this->emplace<TextElement>(channelName, MessageElement::ChannelName, MessageColor::System) //
->setLink(link);
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
this->message->loginName = this->userName;
}
2017-07-02 18:12:11 +02:00
void TwitchMessageBuilder::appendUsername()
{
QString username = this->userName;
2018-01-17 02:46:50 +01:00
this->message->loginName = username;
QString localizedName;
2017-07-02 18:12:11 +02:00
auto iterator = this->tags.find("display-name");
if (iterator != this->tags.end()) {
QString displayName = iterator.value().toString();
if (QString::compare(displayName, this->userName, Qt::CaseInsensitive) == 0) {
username = displayName;
this->message->displayName = displayName;
} else {
localizedName = displayName;
this->message->displayName = username;
this->message->localizedName = displayName;
}
2017-07-02 18:12:11 +02:00
}
bool hasLocalizedName = !localizedName.isEmpty();
// The full string that will be rendered in the chat widget
QString usernameText;
pajlada::Settings::Setting<int> usernameDisplayMode(
"/appearance/messages/usernameDisplayMode", UsernameDisplayMode::UsernameAndLocalizedName);
switch (usernameDisplayMode.getValue()) {
case UsernameDisplayMode::Username: {
usernameText = username;
} break;
case UsernameDisplayMode::LocalizedName: {
if (hasLocalizedName) {
usernameText = localizedName;
} else {
usernameText = username;
}
} break;
default:
case UsernameDisplayMode::UsernameAndLocalizedName: {
if (hasLocalizedName) {
usernameText = username + "(" + localizedName + ")";
} else {
usernameText = 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()) {
usernameText += ":";
2017-07-02 18:12:11 +02:00
}
this->emplace<TextElement>(usernameText, MessageElement::Text, this->usernameColor,
FontStyle::MediumBold)
->setLink({Link::UserInfo, this->userName});
2017-07-02 18:12:11 +02:00
}
void TwitchMessageBuilder::parseHighlights()
{
static auto player = new QMediaPlayer;
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");
QString currentUsername = QString::fromStdString(currentUser.getValue());
if (this->ircMessage->nick() == currentUsername) {
// Do nothing. Highlights cannot be triggered by yourself
return;
}
// update the media player url if necessary
QUrl highlightSoundUrl;
if (settings.customHighlightSound) {
highlightSoundUrl = QUrl(settings.pathHighlightSound.getValue());
} else {
highlightSoundUrl = QUrl("qrc:/sounds/ping2.wav");
}
if (currentPlayerUrl != highlightSoundUrl) {
player->setMedia(highlightSoundUrl);
currentPlayerUrl = highlightSoundUrl;
}
2017-09-24 18:43:24 +02:00
QStringList blackList =
settings.highlightUserBlacklist.getValue().split("\n", QString::SkipEmptyParts);
// TODO: This vector should only be rebuilt upon highlights being changed
auto activeHighlights = settings.highlightProperties.getValue();
if (settings.enableHighlightsSelf && currentUsername.size() > 0) {
messages::HighlightPhrase selfHighlight;
selfHighlight.key = currentUsername;
selfHighlight.sound = settings.enableHighlightSound;
selfHighlight.alert = settings.enableHighlightTaskbar;
activeHighlights.emplace_back(std::move(selfHighlight));
}
bool doHighlight = false;
bool playSound = false;
bool doAlert = false;
bool hasFocus = (QApplication::focusWidget() != nullptr);
2017-09-24 18:43:24 +02:00
if (!blackList.contains(this->ircMessage->nick(), Qt::CaseInsensitive)) {
for (const messages::HighlightPhrase &highlight : activeHighlights) {
if (this->originalMessage.contains(highlight.key, Qt::CaseInsensitive)) {
debug::Log("Highlight because {} contains {}", this->originalMessage,
highlight.key);
doHighlight = true;
if (highlight.sound) {
playSound = true;
}
if (highlight.alert) {
doAlert = true;
}
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;
}
}
}
this->setHighlight(doHighlight);
if (playSound && (!hasFocus || settings.highlightAlwaysPlaySound)) {
player->play();
}
if (doAlert) {
QApplication::alert(singletons::WindowManager::getInstance().getMainWindow().window(),
2500);
}
2018-01-06 03:48:56 +01:00
if (doHighlight) {
this->message->addFlags(Message::Highlighted);
}
}
}
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-12-31 22:58:35 +01:00
singletons::EmoteManager &emoteManager = singletons::EmoteManager::getInstance();
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);
vec.push_back(
2017-12-31 22:58:35 +01:00
std::pair<long int, util::EmoteData>(start, emoteManager.getTwitchEmoteById(id, name)));
}
}
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;
}
bool TwitchMessageBuilder::appendEmote(const util::EmoteData &emoteData)
2017-07-23 09:53:50 +02:00
{
this->emplace<EmoteElement>(emoteData, MessageElement::BttvEmote);
2017-07-23 09:53:50 +02:00
// Perhaps check for ignored emotes here?
return true;
}
// fourtf: this is ugly
// maybe put the individual badges into a map instead of this mess
2017-07-02 18:12:11 +02:00
void TwitchMessageBuilder::parseTwitchBadges()
{
singletons::ResourceManager &resourceManager = singletons::ResourceManager::getInstance();
const auto &channelResources = resourceManager.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(',');
for (QString badge : badges) {
if (badge.isEmpty()) {
continue;
}
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);
std::string versionKey = cheerAmountQS.toStdString();
2017-06-15 23:13:01 +02:00
2018-01-12 22:38:00 +01:00
// Try to fetch channel-specific bit badge
2017-06-16 08:03:13 +02:00
try {
2018-01-12 22:38:00 +01:00
const auto &badge = channelResources.badgeSets.at("bits").versions.at(versionKey);
this->emplace<ImageElement>(badge.badgeImage1x, MessageElement::BadgeVanity);
2018-01-12 22:38:00 +01:00
continue;
} catch (const std::out_of_range &) {
// Channel does not contain a special bit badge for this version
}
2017-06-16 08:03:13 +02:00
2018-01-12 22:38:00 +01:00
// Use default bit badge
try {
const auto &badge = resourceManager.badgeSets.at("bits").versions.at(versionKey);
this->emplace<ImageElement>(badge.badgeImage1x, MessageElement::BadgeVanity);
2018-01-12 22:38:00 +01:00
} catch (const std::out_of_range &) {
debug::Log("No default bit badge for version {} found", versionKey);
continue;
2017-06-16 08:03:13 +02:00
}
} else if (badge == "staff/1") {
this->emplace<ImageElement>(resourceManager.badgeStaff,
MessageElement::BadgeGlobalAuthority)
->setTooltip("Twitch Staff");
} else if (badge == "admin/1") {
this->emplace<ImageElement>(resourceManager.badgeAdmin,
MessageElement::BadgeGlobalAuthority)
->setTooltip("Twitch Admin");
} else if (badge == "global_mod/1") {
this->emplace<ImageElement>(resourceManager.badgeGlobalModerator,
MessageElement::BadgeGlobalAuthority)
->setTooltip("Twitch Global Moderator");
} else if (badge == "moderator/1") {
// TODO: Implement custom FFZ moderator badge
this->emplace<ImageElement>(resourceManager.badgeModerator,
MessageElement::BadgeChannelAuthority)
->setTooltip("Twitch Channel Moderator");
} else if (badge == "turbo/1") {
this->emplace<ImageElement>(resourceManager.badgeTurbo,
MessageElement::BadgeGlobalAuthority)
->setTooltip("Twitch Turbo Subscriber");
} else if (badge == "broadcaster/1") {
this->emplace<ImageElement>(resourceManager.badgeBroadcaster,
MessageElement::BadgeChannelAuthority)
->setTooltip("Twitch Broadcaster");
} else if (badge == "premium/1") {
this->emplace<ImageElement>(resourceManager.badgePremium, MessageElement::BadgeVanity)
->setTooltip("Twitch Prime Subscriber");
} else if (badge.startsWith("partner/")) {
int index = badge.midRef(8).toInt();
switch (index) {
case 1: {
this->emplace<ImageElement>(resourceManager.badgeVerified,
MessageElement::BadgeVanity)
->setTooltip("Twitch Verified");
} 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;
}
auto badgeSetIt = channelResources.badgeSets.find("subscriber");
if (badgeSetIt == channelResources.badgeSets.end()) {
// Fall back to default badge
this->emplace<ImageElement>(resourceManager.badgeSubscriber,
MessageElement::BadgeSubscription)
->setTooltip("Twitch Subscriber");
continue;
}
2017-07-02 15:11:33 +02:00
const auto &badgeSet = badgeSetIt->second;
2017-07-02 15:11:33 +02:00
std::string versionKey = badge.mid(11).toStdString();
2017-07-02 15:11:33 +02:00
auto badgeVersionIt = badgeSet.versions.find(versionKey);
if (badgeVersionIt == badgeSet.versions.end()) {
// Fall back to default badge
this->emplace<ImageElement>(resourceManager.badgeSubscriber,
MessageElement::BadgeSubscription)
->setTooltip("Twitch Subscriber");
continue;
}
2017-07-02 15:11:33 +02:00
auto &badgeVersion = badgeVersionIt->second;
this->emplace<ImageElement>(badgeVersion.badgeImage1x,
MessageElement::BadgeSubscription)
->setTooltip("Twitch " + QString::fromStdString(badgeVersion.title));
} else {
if (!resourceManager.dynamicBadgesLoaded) {
// Do nothing
continue;
}
QStringList parts = badge.split('/');
if (parts.length() != 2) {
qDebug() << "Bad number of parts: " << parts.length() << " in " << parts;
continue;
}
MessageElement::Flags badgeType = MessageElement::Flags::BadgeVanity;
std::string badgeSetKey = parts[0].toStdString();
std::string versionKey = parts[1].toStdString();
try {
auto &badgeSet = resourceManager.badgeSets.at(badgeSetKey);
try {
auto &badgeVersion = badgeSet.versions.at(versionKey);
this->emplace<ImageElement>(badgeVersion.badgeImage1x, badgeType)
->setTooltip("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();
}
}
}
}
void TwitchMessageBuilder::addChatterinoBadges()
2017-08-12 13:20:52 +02:00
{
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->emplace<ImageElement>(badge->image, MessageElement::BadgeChatterino)
->setTooltip(QString::fromStdString(badge->tooltip));
2017-08-12 13:20:52 +02:00
}
bool TwitchMessageBuilder::tryParseCheermote(const QString &string)
{
// Try to parse custom cheermotes
const auto &channelResources =
singletons::ResourceManager::getInstance().channels[this->roomID];
if (channelResources.loaded) {
for (const auto &cheermoteSet : channelResources.cheermoteSets) {
auto match = cheermoteSet.regex.match(string);
if (!match.hasMatch()) {
continue;
}
QString amount = match.captured(1);
bool ok = false;
int numBits = amount.toInt(&ok);
if (!ok) {
debug::Log("Error parsing bit amount in tryParseCheermote");
return false;
}
auto savedIt = cheermoteSet.cheermotes.end();
// Fetch cheermote that matches our numBits
for (auto it = cheermoteSet.cheermotes.begin(); it != cheermoteSet.cheermotes.end();
++it) {
if (numBits >= it->minBits) {
savedIt = it;
} else {
break;
}
}
if (savedIt == cheermoteSet.cheermotes.end()) {
debug::Log("Error getting a cheermote from a cheermote set for the bit amount {}",
numBits);
return false;
}
const auto &cheermote = *savedIt;
this->emplace<EmoteElement>(cheermote.emoteDataAnimated, EmoteElement::BitsAnimated);
this->emplace<TextElement>(amount, EmoteElement::Text, cheermote.color);
return true;
}
}
return false;
}
} // namespace twitch
} // namespace chatterino