refactor(message-builder): move static helper methods to functions (#5652)

This commit is contained in:
nerix
2024-10-18 13:03:36 +02:00
committed by GitHub
parent 6d139af553
commit 800f6df2cf
12 changed files with 1090 additions and 928 deletions
+6 -453
View File
@@ -30,6 +30,7 @@
#include "providers/twitch/TwitchBadge.hpp"
#include "providers/twitch/TwitchBadges.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchIrc.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp"
@@ -237,77 +238,6 @@ QString stylizeUsername(const QString &username, const Message &message)
return usernameText;
}
void appendTwitchEmoteOccurrences(const QString &emote,
std::vector<TwitchEmoteOccurrence> &vec,
const std::vector<int> &correctPositions,
const QString &originalMessage,
int messageOffset)
{
auto *app = getApp();
if (!emote.contains(':'))
{
return;
}
auto parameters = emote.split(':');
if (parameters.length() < 2)
{
return;
}
auto id = EmoteId{parameters.at(0)};
auto occurrences = parameters.at(1).split(',');
for (const QString &occurrence : occurrences)
{
auto coords = occurrence.split('-');
if (coords.length() < 2)
{
return;
}
auto from = coords.at(0).toUInt() - messageOffset;
auto to = coords.at(1).toUInt() - messageOffset;
auto maxPositions = correctPositions.size();
if (from > to || to >= maxPositions)
{
// Emote coords are out of range
qCDebug(chatterinoTwitch)
<< "Emote coords" << from << "-" << to << "are out of range ("
<< maxPositions << ")";
return;
}
auto start = correctPositions[from];
auto end = correctPositions[to];
if (start > end || start < 0 || end > originalMessage.length())
{
// Emote coords are out of range from the modified character positions
qCDebug(chatterinoTwitch) << "Emote coords" << from << "-" << to
<< "are out of range after offsets ("
<< originalMessage.length() << ")";
return;
}
auto name = EmoteName{originalMessage.mid(start, end - start + 1)};
TwitchEmoteOccurrence emoteOccurrence{
start,
end,
app->getEmotes()->getTwitchEmotes()->getOrCreateEmote(id, name),
name,
};
if (emoteOccurrence.ptr == nullptr)
{
qCDebug(chatterinoTwitch)
<< "nullptr" << emoteOccurrence.name.string;
}
vec.push_back(std::move(emoteOccurrence));
}
}
std::optional<EmotePtr> getTwitchBadge(const Badge &badge,
const TwitchChannel *twitchChannel)
{
@@ -420,120 +350,6 @@ void appendBadges(MessageBuilder *builder, const std::vector<Badge> &badges,
builder->message().badgeInfos = badgeInfos;
}
/**
* Computes (only) the replacement of @a match in @a source.
* The parts before and after the match in @a source are ignored.
*
* Occurrences of \b{\\1}, \b{\\2}, ..., in @a replacement are replaced
* with the string captured by the corresponding capturing group.
* This function should only be used if the regex contains capturing groups.
*
* Since Qt doesn't provide a way of replacing a single match with some replacement
* while supporting both capturing groups and lookahead/-behind in the regex,
* this is included here. It's essentially the implementation of
* QString::replace(const QRegularExpression &, const QString &).
* @see https://github.com/qt/qtbase/blob/97bb0ecfe628b5bb78e798563212adf02129c6f6/src/corelib/text/qstring.cpp#L4594-L4703
*/
QString makeRegexReplacement(QStringView source,
const QRegularExpression &regex,
const QRegularExpressionMatch &match,
const QString &replacement)
{
using SizeType = QString::size_type;
struct QStringCapture {
SizeType pos;
SizeType len;
int captureNumber;
};
qsizetype numCaptures = regex.captureCount();
// 1. build the backreferences list, holding where the backreferences
// are in the replacement string
QVarLengthArray<QStringCapture> backReferences;
SizeType replacementLength = replacement.size();
for (SizeType i = 0; i < replacementLength - 1; i++)
{
if (replacement[i] != u'\\')
{
continue;
}
int no = replacement[i + 1].digitValue();
if (no <= 0 || no > numCaptures)
{
continue;
}
QStringCapture backReference{.pos = i, .len = 2};
if (i < replacementLength - 2)
{
int secondDigit = replacement[i + 2].digitValue();
if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures)
{
no = (no * 10) + secondDigit;
++backReference.len;
}
}
backReference.captureNumber = no;
backReferences.append(backReference);
}
// 2. iterate on the matches.
// For every match, copy the replacement string in chunks
// with the proper replacements for the backreferences
// length of the new string, with all the replacements
SizeType newLength = 0;
QVarLengthArray<QStringView> chunks;
QStringView replacementView{replacement};
// Initially: empty, as we only care about the replacement
SizeType len = 0;
SizeType lastEnd = 0;
for (const QStringCapture &backReference : std::as_const(backReferences))
{
// part of "replacement" before the backreference
len = backReference.pos - lastEnd;
if (len > 0)
{
chunks << replacementView.mid(lastEnd, len);
newLength += len;
}
// backreference itself
len = match.capturedLength(backReference.captureNumber);
if (len > 0)
{
chunks << source.mid(
match.capturedStart(backReference.captureNumber), len);
newLength += len;
}
lastEnd = backReference.pos + backReference.len;
}
// add the last part of the replacement string
len = replacementView.size() - lastEnd;
if (len > 0)
{
chunks << replacementView.mid(lastEnd, len);
newLength += len;
}
// 3. assemble the chunks together
QString dst;
dst.reserve(newLength);
for (const QStringView &chunk : std::as_const(chunks))
{
dst += chunk;
}
return dst;
}
bool doesWordContainATwitchEmote(
int cursor, const QString &word,
const std::vector<TwitchEmoteOccurrence> &twitchEmotes,
@@ -1358,13 +1174,12 @@ MessagePtr MessageBuilder::build()
}
// Twitch emotes
auto twitchEmotes = MessageBuilder::parseTwitchEmotes(
this->tags, this->originalMessage_, this->messageOffset_);
auto twitchEmotes = parseTwitchEmotes(this->tags, this->originalMessage_,
this->messageOffset_);
// This runs through all ignored phrases and runs its replacements on this->originalMessage_
MessageBuilder::processIgnorePhrases(
*getSettings()->ignoredMessages.readOnly(), this->originalMessage_,
twitchEmotes);
processIgnorePhrases(*getSettings()->ignoredMessages.readOnly(),
this->originalMessage_, twitchEmotes);
std::sort(twitchEmotes.begin(), twitchEmotes.end(),
[](const auto &a, const auto &b) {
@@ -2178,268 +1993,6 @@ MessagePtr MessageBuilder::makeLowTrustUpdateMessage(
return builder.release();
}
std::unordered_map<QString, QString> MessageBuilder::parseBadgeInfoTag(
const QVariantMap &tags)
{
std::unordered_map<QString, QString> infoMap;
auto infoIt = tags.constFind("badge-info");
if (infoIt == tags.end())
{
return infoMap;
}
auto info = infoIt.value().toString().split(',', Qt::SkipEmptyParts);
for (const QString &badge : info)
{
infoMap.emplace(slashKeyValue(badge));
}
return infoMap;
}
std::vector<Badge> MessageBuilder::parseBadgeTag(const QVariantMap &tags)
{
std::vector<Badge> b;
auto badgesIt = tags.constFind("badges");
if (badgesIt == tags.end())
{
return b;
}
auto badges = badgesIt.value().toString().split(',', Qt::SkipEmptyParts);
for (const QString &badge : badges)
{
if (!badge.contains('/'))
{
continue;
}
auto pair = slashKeyValue(badge);
b.emplace_back(Badge{pair.first, pair.second});
}
return b;
}
std::vector<TwitchEmoteOccurrence> MessageBuilder::parseTwitchEmotes(
const QVariantMap &tags, const QString &originalMessage, int messageOffset)
{
// Twitch emotes
std::vector<TwitchEmoteOccurrence> twitchEmotes;
auto emotesTag = tags.find("emotes");
if (emotesTag == tags.end())
{
return twitchEmotes;
}
QStringList emoteString = emotesTag.value().toString().split('/');
std::vector<int> correctPositions;
for (int i = 0; i < originalMessage.size(); ++i)
{
if (!originalMessage.at(i).isLowSurrogate())
{
correctPositions.push_back(i);
}
}
for (const QString &emote : emoteString)
{
appendTwitchEmoteOccurrences(emote, twitchEmotes, correctPositions,
originalMessage, messageOffset);
}
return twitchEmotes;
}
void MessageBuilder::processIgnorePhrases(
const std::vector<IgnorePhrase> &phrases, QString &originalMessage,
std::vector<TwitchEmoteOccurrence> &twitchEmotes)
{
using SizeType = QString::size_type;
auto removeEmotesInRange = [&twitchEmotes](SizeType pos, SizeType len) {
// all emotes outside the range come before `it`
// all emotes in the range start at `it`
auto it = std::partition(
twitchEmotes.begin(), twitchEmotes.end(),
[pos, len](const auto &item) {
// returns true for emotes outside the range
return !((item.start >= pos) && item.start < (pos + len));
});
std::vector<TwitchEmoteOccurrence> emotesInRange(it,
twitchEmotes.end());
twitchEmotes.erase(it, twitchEmotes.end());
return emotesInRange;
};
auto shiftIndicesAfter = [&twitchEmotes](int pos, int by) {
for (auto &item : twitchEmotes)
{
auto &index = item.start;
if (index >= pos)
{
index += by;
item.end += by;
}
}
};
auto addReplEmotes = [&twitchEmotes](const IgnorePhrase &phrase,
const auto &midrepl,
SizeType startIndex) {
if (!phrase.containsEmote())
{
return;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
auto words = midrepl.tokenize(u' ');
#else
auto words = midrepl.split(' ');
#endif
SizeType pos = 0;
for (const auto &word : words)
{
for (const auto &emote : phrase.getEmotes())
{
if (word == emote.first.string)
{
if (emote.second == nullptr)
{
qCDebug(chatterinoTwitch)
<< "emote null" << emote.first.string;
}
twitchEmotes.push_back(TwitchEmoteOccurrence{
static_cast<int>(startIndex + pos),
static_cast<int>(startIndex + pos +
emote.first.string.length()),
emote.second,
emote.first,
});
}
}
pos += word.length() + 1;
}
};
auto replaceMessageAt = [&](const IgnorePhrase &phrase, SizeType from,
SizeType length, const QString &replacement) {
auto removedEmotes = removeEmotesInRange(from, length);
originalMessage.replace(from, length, replacement);
auto wordStart = from;
while (wordStart > 0)
{
if (originalMessage[wordStart - 1] == ' ')
{
break;
}
--wordStart;
}
auto wordEnd = from + replacement.length();
while (wordEnd < originalMessage.length())
{
if (originalMessage[wordEnd] == ' ')
{
break;
}
++wordEnd;
}
shiftIndicesAfter(static_cast<int>(from + length),
static_cast<int>(replacement.length() - length));
auto midExtendedRef =
QStringView{originalMessage}.mid(wordStart, wordEnd - wordStart);
for (auto &emote : removedEmotes)
{
if (emote.ptr == nullptr)
{
qCDebug(chatterinoTwitch)
<< "Invalid emote occurrence" << emote.name.string;
continue;
}
QRegularExpression emoteregex(
"\\b" + emote.name.string + "\\b",
QRegularExpression::UseUnicodePropertiesOption);
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
auto match = emoteregex.matchView(midExtendedRef);
#else
auto match = emoteregex.match(midExtendedRef);
#endif
if (match.hasMatch())
{
emote.start = static_cast<int>(from + match.capturedStart());
emote.end = static_cast<int>(from + match.capturedEnd());
twitchEmotes.push_back(std::move(emote));
}
}
addReplEmotes(phrase, midExtendedRef, wordStart);
};
for (const auto &phrase : phrases)
{
if (phrase.isBlock())
{
continue;
}
const auto &pattern = phrase.getPattern();
if (pattern.isEmpty())
{
continue;
}
if (phrase.isRegex())
{
const auto &regex = phrase.getRegex();
if (!regex.isValid())
{
continue;
}
QRegularExpressionMatch match;
size_t iterations = 0;
SizeType from = 0;
while ((from = originalMessage.indexOf(regex, from, &match)) != -1)
{
auto replacement = phrase.getReplace();
if (regex.captureCount() > 0)
{
replacement = makeRegexReplacement(originalMessage, regex,
match, replacement);
}
replaceMessageAt(phrase, from, match.capturedLength(),
replacement);
from += phrase.getReplace().length();
iterations++;
if (iterations >= 128)
{
originalMessage =
u"Too many replacements - check your ignores!"_s;
return;
}
}
continue;
}
SizeType from = 0;
while ((from = originalMessage.indexOf(pattern, from,
phrase.caseSensitivity())) != -1)
{
replaceMessageAt(phrase, from, pattern.length(),
phrase.getReplace());
from += phrase.getReplace().length();
}
}
}
void MessageBuilder::addTextOrEmoji(EmotePtr emote)
{
this->emplace<EmoteElement>(emote, MessageElementFlag::EmojiAll);
@@ -3159,7 +2712,7 @@ void MessageBuilder::appendTwitchBadges()
return;
}
auto badgeInfos = MessageBuilder::parseBadgeInfoTag(this->tags);
auto badgeInfos = parseBadgeInfoTag(this->tags);
auto badges = parseBadgeTag(this->tags);
appendBadges(this, badges, badgeInfos, this->twitchChannel);
}