added basic keyword ignore setting

This commit is contained in:
fourtf 2018-01-23 21:33:49 +01:00
parent 91d45214d9
commit dd05ea28fe
13 changed files with 195 additions and 7 deletions

View file

@ -163,7 +163,9 @@ SOURCES += \
src/widgets/basewindow.cpp \
src/singletons/helper/moderationaction.cpp \
src/widgets/streamview.cpp \
src/util/networkrequest.cpp
src/util/networkrequest.cpp \
src/widgets/settingspages/ignoreuserspage.cpp \
src/widgets/settingspages/ignoremessagespage.cpp
HEADERS += \
src/precompiled_header.hpp \
@ -266,7 +268,9 @@ HEADERS += \
src/widgets/streamview.hpp \
src/util/networkrequest.hpp \
src/util/networkworker.hpp \
src/util/networkrequester.hpp
src/util/networkrequester.hpp \
src/widgets/settingspages/ignoreuserspage.hpp \
src/widgets/settingspages/ignoremessagespage.hpp
RESOURCES += \
resources/resources.qrc

View file

@ -255,7 +255,9 @@ void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message)
twitch::TwitchMessageBuilder builder(c.get(), message, args);
c->addMessage(builder.parse());
if (!builder.isIgnored()) {
c->addMessage(builder.parse());
}
}
void IrcManager::messageReceived(Communi::IrcMessage *message)

View file

@ -17,6 +17,7 @@ void _registerSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting)
SettingManager::SettingManager()
: snapshot(nullptr)
, _ignoredKeywords(new std::vector<QString>)
{
this->wordFlagsListener.addSetting(this->showTimestamps);
this->wordFlagsListener.addSetting(this->showBadges);
@ -29,6 +30,7 @@ SettingManager::SettingManager()
};
this->moderationActions.connect([this](auto, auto) { this->updateModerationActions(); });
this->ignoredKeywords.connect([this](auto, auto) { this->updateIgnoredKeywords(); });
}
MessageElement::Flags SettingManager::getWordFlags()
@ -135,6 +137,11 @@ std::vector<ModerationAction> SettingManager::getModerationActions() const
return this->_moderationActions;
}
const std::shared_ptr<std::vector<QString>> SettingManager::getIgnoredKeywords() const
{
return this->_ignoredKeywords;
}
void SettingManager::updateModerationActions()
{
auto &resources = singletons::ResourceManager::getInstance();
@ -202,5 +209,18 @@ void SettingManager::updateModerationActions()
}
}
}
void SettingManager::updateIgnoredKeywords()
{
static QRegularExpression newLineRegex("(\r\n?|\n)+");
auto items = new std::vector<QString>();
for (QString line : this->ignoredKeywords.getValue().split(newLineRegex)) {
items->push_back(line);
}
this->_ignoredKeywords = std::shared_ptr<std::vector<QString>>(items);
}
} // namespace singletons
} // namespace chatterino

View file

@ -75,6 +75,10 @@ public:
/// Links
BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false};
/// Ingored Users
BoolSetting enableTwitchIgnoredUsers = {"/ignore/enableTwitchIgnoredUsers", true};
QStringSetting ignoredKeywords = {"/ignore/ignoredKeywords", ""};
/// Moderation
QStringSetting moderationActions = {"/moderation/actions", "/ban {user}\n/timeout {user} 300"};
@ -107,6 +111,7 @@ public:
void recallSnapshot();
std::vector<ModerationAction> getModerationActions() const;
const std::shared_ptr<std::vector<QString>> getIgnoredKeywords() const;
signals:
void wordFlagsChanged();
@ -114,10 +119,12 @@ signals:
private:
std::vector<ModerationAction> _moderationActions;
std::unique_ptr<rapidjson::Document> snapshot;
std::shared_ptr<std::vector<QString>> _ignoredKeywords;
SettingManager();
void updateModerationActions();
void updateIgnoredKeywords();
messages::MessageElement::Flags wordFlags = messages::MessageElement::Default;

View file

@ -230,7 +230,6 @@ void TwitchChannel::fetchRecentMessages()
auto msgArray = obj.value("messages").toArray();
if (msgArray.size() > 0) {
std::vector<messages::MessagePtr> messages;
messages.resize(msgArray.size());
for (int i = 0; i < msgArray.size(); i++) {
QByteArray content = msgArray[i].toString().toUtf8();
@ -239,7 +238,9 @@ void TwitchChannel::fetchRecentMessages()
messages::MessageParseArgs args;
twitch::TwitchMessageBuilder builder(channel, privMsg, args);
messages.at(i) = builder.parse();
if (!builder.isIgnored()) {
messages.push_back(builder.parse());
}
}
channel->addMessagesAtStart(messages);
}

View file

@ -27,6 +27,21 @@ TwitchMessageBuilder::TwitchMessageBuilder(Channel *_channel,
, tags(this->ircMessage->tags())
, usernameColor(singletons::ThemeManager::getInstance().messages.textColors.system)
{
this->originalMessage = this->ircMessage->content();
}
bool TwitchMessageBuilder::isIgnored() const
{
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
std::shared_ptr<std::vector<QString>> ignoredKeywords = settings.getIgnoredKeywords();
for (const QString &keyword : *ignoredKeywords) {
if (this->originalMessage.contains(keyword, Qt::CaseInsensitive)) {
return true;
}
}
return false;
}
MessagePtr TwitchMessageBuilder::parse()
@ -34,8 +49,6 @@ MessagePtr TwitchMessageBuilder::parse()
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
singletons::EmoteManager &emoteManager = singletons::EmoteManager::getInstance();
this->originalMessage = this->ircMessage->content();
// PARSING
this->parseUsername();

View file

@ -38,6 +38,7 @@ public:
QString messageID;
QString userName;
bool isIgnored() const;
messages::MessagePtr parse();
private:

View file

@ -47,6 +47,18 @@ public:
return LayoutCreator<T2>(t);
}
template <typename T2, typename Q = T,
typename std::enable_if<std::is_base_of<QWidget, Q>::value, int>::type = 0,
typename std::enable_if<std::is_base_of<QLayout, T2>::value, int>::type = 0>
LayoutCreator<T2> setLayoutType()
{
T2 *layout = new T2;
this->item->setLayout(layout);
return LayoutCreator<T2>(layout);
}
LayoutCreator<T> assign(T **ptr)
{
*ptr = this->item;

View file

@ -8,6 +8,8 @@
#include "widgets/settingspages/commandpage.hpp"
#include "widgets/settingspages/emotespage.hpp"
#include "widgets/settingspages/highlightingpage.hpp"
#include "widgets/settingspages/ignoremessagespage.hpp"
#include "widgets/settingspages/ignoreuserspage.hpp"
#include "widgets/settingspages/logspage.hpp"
#include "widgets/settingspages/moderationpage.hpp"
@ -77,6 +79,8 @@ void SettingsDialog::addTabs()
this->addTab(new settingspages::BehaviourPage);
this->addTab(new settingspages::CommandPage);
this->addTab(new settingspages::EmotesPage);
this->addTab(new settingspages::IgnoreUsersPage);
this->addTab(new settingspages::IgnoreMessagesPage);
this->addTab(new settingspages::HighlightingPage);
// this->addTab(new settingspages::LogsPage);
this->addTab(new settingspages::ModerationPage);

View file

@ -0,0 +1,39 @@
#include "ignoremessagespage.hpp"
#include "util/layoutcreator.hpp"
#include <QLabel>
#include <QTextEdit>
namespace chatterino {
namespace widgets {
namespace settingspages {
IgnoreMessagesPage::IgnoreMessagesPage()
: SettingsPage("Ignore Messages", ":/images/theme.svg")
{
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
util::LayoutCreator<IgnoreMessagesPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();
layout.emplace<QLabel>("Ignored keywords:");
QTextEdit *textEdit = layout.emplace<QTextEdit>().getElement();
textEdit->setPlainText(settings.ignoredKeywords);
QObject::connect(textEdit, &QTextEdit::textChanged,
[this] { this->keywordsUpdated.start(200); });
QObject::connect(&this->keywordsUpdated, &QTimer::timeout, [textEdit, &settings] {
QString text = textEdit->toPlainText();
settings.ignoredKeywords = text;
qDebug() << "xD";
});
// ---- misc
this->keywordsUpdated.setSingleShot(true);
}
} // namespace settingspages
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,19 @@
#pragma once
#include <QTimer>
#include "widgets/settingspages/settingspage.hpp"
namespace chatterino {
namespace widgets {
namespace settingspages {
class IgnoreMessagesPage : public SettingsPage
{
public:
IgnoreMessagesPage();
QTimer keywordsUpdated;
};
} // namespace settingspages
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,51 @@
#include "ignoreuserspage.hpp"
#include "singletons/settingsmanager.hpp"
#include "util/layoutcreator.hpp"
#include <QCheckBox>
#include <QLabel>
#include <QListView>
#include <QPushButton>
#include <QVBoxLayout>
// clang-format off
#define INFO "/ignore <user> in chat ignores a user\n/unignore <user> in chat unignores a user\n\nChatterino uses the twitch api for ignored users so they are shared with the webchat.\nIf you use your own oauth key make sure that it has the correct permissions."
// clang-format on
namespace chatterino {
namespace widgets {
namespace settingspages {
IgnoreUsersPage::IgnoreUsersPage()
: SettingsPage("Ignore Users", ":/images/theme.svg")
{
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
util::LayoutCreator<IgnoreUsersPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>().withoutMargin();
auto label = layout.emplace<QLabel>(INFO);
label->setWordWrap(true);
label->setStyleSheet("color: #BBB");
layout.append(
this->createCheckBox("Enable twitch ignored users", settings.enableTwitchIgnoredUsers));
auto anyways = layout.emplace<QHBoxLayout>().withoutMargin();
{
anyways.emplace<QLabel>("Show anyways if:");
anyways.emplace<QComboBox>();
anyways->addStretch(1);
}
auto addremove = layout.emplace<QHBoxLayout>().withoutMargin();
{
auto add = addremove.emplace<QPushButton>("Ignore user");
auto remove = addremove.emplace<QPushButton>("Unignore User");
addremove->addStretch(1);
}
auto userList = layout.emplace<QListView>();
}
} // namespace settingspages
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,15 @@
#pragma once
#include "widgets/settingspages/settingspage.hpp"
namespace chatterino {
namespace widgets {
namespace settingspages {
class IgnoreUsersPage : public SettingsPage
{
public:
IgnoreUsersPage();
};
} // namespace settingspages
} // namespace widgets
} // namespace chatterino