ignore phrases

This commit is contained in:
hemirt 2018-07-08 16:21:33 +02:00
parent d2450c298b
commit d3b6e294ed
6 changed files with 52 additions and 12 deletions

View file

@ -8,7 +8,7 @@ namespace chatterino {
// commandmodel // commandmodel
IgnoreModel::IgnoreModel(QObject *parent) IgnoreModel::IgnoreModel(QObject *parent)
: SignalVectorModel<IgnorePhrase>(2, parent) : SignalVectorModel<IgnorePhrase>(4, parent)
{ {
} }
@ -18,8 +18,9 @@ IgnorePhrase IgnoreModel::getItemFromRow(std::vector<QStandardItem *> &row,
{ {
// key, regex // key, regex
return IgnorePhrase{row[0]->data(Qt::DisplayRole).toString(), return IgnorePhrase{
row[1]->data(Qt::CheckStateRole).toBool()}; row[0]->data(Qt::DisplayRole).toString(), row[1]->data(Qt::CheckStateRole).toBool(),
row[2]->data(Qt::CheckStateRole).toBool(), row[3]->data(Qt::DisplayRole).toString()};
} }
// turns a row in the model into a vector item // turns a row in the model into a vector item
@ -27,6 +28,8 @@ void IgnoreModel::getRowFromItem(const IgnorePhrase &item, std::vector<QStandard
{ {
setStringItem(row[0], item.getPattern()); setStringItem(row[0], item.getPattern());
setBoolItem(row[1], item.isRegex()); setBoolItem(row[1], item.isRegex());
setBoolItem(row[2], item.isReplace());
setStringItem(row[3], item.getReplace());
} }
} // namespace chatterino } // namespace chatterino

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "common/SerializeCustom.hpp" #include "common/SerializeCustom.hpp"
#include "singletons/Settings.hpp"
#include "util/RapidjsonHelpers.hpp" #include "util/RapidjsonHelpers.hpp"
#include <QRegularExpression> #include <QRegularExpression>
@ -16,15 +17,18 @@ class IgnorePhrase
public: public:
bool operator==(const IgnorePhrase &other) const bool operator==(const IgnorePhrase &other) const
{ {
return std::tie(this->pattern_, this->isRegex_) == std::tie(other.pattern_, other.isRegex_); return std::tie(this->pattern_, this->isRegex_, this->isReplace_, this->replace_) ==
std::tie(other.pattern_, other.isRegex_, other.isReplace_, other.replace_);
} }
IgnorePhrase(const QString &pattern, bool isRegex) IgnorePhrase(const QString &pattern, bool isRegex, bool isReplace, const QString &replace)
: pattern_(pattern) : pattern_(pattern)
, isRegex_(isRegex) , isRegex_(isRegex)
, regex_(isRegex_ ? pattern : "\\b" + QRegularExpression::escape(pattern) + "\\b", , regex_(isRegex_ ? pattern : "\\b" + QRegularExpression::escape(pattern) + "\\b",
QRegularExpression::CaseInsensitiveOption | QRegularExpression::CaseInsensitiveOption |
QRegularExpression::UseUnicodePropertiesOption) QRegularExpression::UseUnicodePropertiesOption)
, isReplace_(isReplace)
, replace_(replace)
{ {
} }
@ -47,10 +51,22 @@ public:
return this->isValid() && this->regex_.match(subject).hasMatch(); return this->isValid() && this->regex_.match(subject).hasMatch();
} }
bool isReplace() const
{
return this->isReplace_;
}
const QString &getReplace() const
{
return this->replace_;
}
private: private:
QString pattern_; QString pattern_;
bool isRegex_; bool isRegex_;
QRegularExpression regex_; QRegularExpression regex_;
bool isReplace_;
QString replace_;
}; };
} // namespace chatterino } // namespace chatterino
@ -66,6 +82,8 @@ struct Serialize<chatterino::IgnorePhrase> {
AddMember(ret, "pattern", value.getPattern(), a); AddMember(ret, "pattern", value.getPattern(), a);
AddMember(ret, "regex", value.isRegex(), a); AddMember(ret, "regex", value.isRegex(), a);
AddMember(ret, "onlyWord", value.isReplace(), a);
AddMember(ret, "replace", value.getReplace(), a);
return ret; return ret;
} }
@ -76,16 +94,22 @@ struct Deserialize<chatterino::IgnorePhrase> {
static chatterino::IgnorePhrase get(const rapidjson::Value &value) static chatterino::IgnorePhrase get(const rapidjson::Value &value)
{ {
if (!value.IsObject()) { if (!value.IsObject()) {
return chatterino::IgnorePhrase(QString(), false); return chatterino::IgnorePhrase(
QString(), false, false,
::chatterino::getSettings()->ignoredPhraseReplace.getValue());
} }
QString _pattern; QString _pattern;
bool _isRegex = false; bool _isRegex = false;
bool _isReplace = false;
QString _replace;
chatterino::rj::getSafe(value, "pattern", _pattern); chatterino::rj::getSafe(value, "pattern", _pattern);
chatterino::rj::getSafe(value, "regex", _isRegex); chatterino::rj::getSafe(value, "regex", _isRegex);
chatterino::rj::getSafe(value, "onlyWord", _isReplace);
chatterino::rj::getSafe(value, "replace", _replace);
return chatterino::IgnorePhrase(_pattern, _isRegex); return chatterino::IgnorePhrase(_pattern, _isRegex, _isReplace, _replace);
} }
}; };

View file

@ -56,7 +56,7 @@ bool TwitchMessageBuilder::isIgnored() const
// TODO(pajlada): Do we need to check if the phrase is valid first? // TODO(pajlada): Do we need to check if the phrase is valid first?
for (const auto &phrase : app->ignores->phrases.getVector()) { for (const auto &phrase : app->ignores->phrases.getVector()) {
if (phrase.isMatch(this->originalMessage_)) { if (!phrase.isReplace() && phrase.isMatch(this->originalMessage_)) {
Log("Blocking message because it contains ignored phrase {}", phrase.getPattern()); Log("Blocking message because it contains ignored phrase {}", phrase.getPattern());
return true; return true;
} }
@ -168,13 +168,22 @@ MessagePtr TwitchMessageBuilder::build()
auto currentTwitchEmote = twitchEmotes.begin(); auto currentTwitchEmote = twitchEmotes.begin();
/*for (const auto &phrase : app->ignores->phrases.getVector()) {
if (phrase.isReplace() && phrase.isMatch(this->originalMessage_)) {
Log("Replacing message because it contains ignored phrase {}", phrase.getPattern());
this->originalMessage_.replace(phrase.getPattern(), phrase.getReplace());
}
}*/
// words // words
QStringList splits = this->originalMessage_.split(' '); QStringList splits = this->originalMessage_.split(' ');
long int i = 0; long int i = 0;
for (QString split : splits) { for (int current = 0; current < splits.size(); ++current) {
const QString &split = splits[current];
Log("Splits {} {} {}", current, split, i);
MessageColor textColor = MessageColor textColor =
this->action_ ? MessageColor(this->usernameColor_) : MessageColor(MessageColor::Text); this->action_ ? MessageColor(this->usernameColor_) : MessageColor(MessageColor::Text);

View file

@ -61,7 +61,7 @@ private:
QString roomID_; QString roomID_;
QColor usernameColor_; QColor usernameColor_;
const QString originalMessage_; QString originalMessage_;
bool senderIsBroadcaster{}; bool senderIsBroadcaster{};
const bool action_ = false; const bool action_ = false;

View file

@ -83,6 +83,9 @@ public:
BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false}; BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false};
BoolSetting lowercaseLink = {"/links/linkLowercase", true}; BoolSetting lowercaseLink = {"/links/linkLowercase", true};
/// Ignored phrases
QStringSetting ignoredPhraseReplace = {"/ignore/ignoredPhraseReplace", "***"};
/// Ingored Users /// Ingored Users
BoolSetting enableTwitchIgnoredUsers = {"/ignore/enableTwitchIgnoredUsers", true}; BoolSetting enableTwitchIgnoredUsers = {"/ignore/enableTwitchIgnoredUsers", true};

View file

@ -45,7 +45,7 @@ void addPhrasesTab(LayoutCreator<QVBoxLayout> layout)
{ {
EditableModelView *view = EditableModelView *view =
layout.emplace<EditableModelView>(getApp()->ignores->createModel(nullptr)).getElement(); layout.emplace<EditableModelView>(getApp()->ignores->createModel(nullptr)).getElement();
view->setTitles({"Pattern", "Regex"}); view->setTitles({"Pattern", "Regex", "Replace", "Pattern"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); view->getTableView()->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
view->getTableView()->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); view->getTableView()->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
@ -55,7 +55,8 @@ void addPhrasesTab(LayoutCreator<QVBoxLayout> layout)
}); });
view->addButtonPressed.connect([] { view->addButtonPressed.connect([] {
getApp()->ignores->phrases.appendItem(IgnorePhrase{"my phrase", false}); getApp()->ignores->phrases.appendItem(IgnorePhrase{
"my phrase", false, false, getSettings()->ignoredPhraseReplace.getValue()});
}); });
} }