Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up TwitchAccount emote stuff #4243

Merged
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ set(SOURCE_FILES
controllers/ignores/IgnoreController.hpp
controllers/ignores/IgnoreModel.cpp
controllers/ignores/IgnoreModel.hpp
controllers/ignores/IgnorePhrase.cpp
controllers/ignores/IgnorePhrase.hpp

controllers/moderationactions/ModerationAction.cpp
controllers/moderationactions/ModerationAction.hpp
Expand Down
2 changes: 2 additions & 0 deletions src/common/CompletionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "common/ChatterSet.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandController.hpp"
#include "messages/Emote.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/controllers/commands/CommandController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "providers/irc/IrcChannel2.hpp"
#include "providers/irc/IrcServer.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/controllers/commands/builtin/twitch/ChatSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "controllers/accounts/AccountController.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "util/FormatTime.hpp"
#include "util/Helpers.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/controllers/highlights/HighlightController.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "controllers/highlights/HighlightController.hpp"

#include "common/QLogging.hpp"
#include "providers/twitch/TwitchAccount.hpp"

namespace {

Expand Down
1 change: 1 addition & 0 deletions src/controllers/ignores/IgnoreController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/QLogging.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "singletons/Settings.hpp"

namespace chatterino {
Expand Down
115 changes: 115 additions & 0 deletions src/controllers/ignores/IgnorePhrase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "controllers/ignores/IgnorePhrase.hpp"

#include "Application.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/TwitchAccount.hpp"

namespace chatterino {

IgnorePhrase::IgnorePhrase(const QString &pattern, bool isRegex, bool isBlock,
pajlada marked this conversation as resolved.
Show resolved Hide resolved
const QString &replace, bool isCaseSensitive)
pajlada marked this conversation as resolved.
Show resolved Hide resolved
: pattern_(pattern)
, isRegex_(isRegex)
, regex_(pattern)
, isBlock_(isBlock)
, replace_(replace)
, isCaseSensitive_(isCaseSensitive)
{
if (this->isCaseSensitive_)
pajlada marked this conversation as resolved.
Show resolved Hide resolved
{
regex_.setPatternOptions(
QRegularExpression::UseUnicodePropertiesOption);
}
else
{
regex_.setPatternOptions(
QRegularExpression::CaseInsensitiveOption |
QRegularExpression::UseUnicodePropertiesOption);
}
}

bool IgnorePhrase::operator==(const IgnorePhrase &other) const
{
return std::tie(this->pattern_, this->isRegex_, this->isBlock_,
this->replace_, this->isCaseSensitive_) ==
std::tie(other.pattern_, other.isRegex_, other.isBlock_,
other.replace_, other.isCaseSensitive_);
}

const QString &IgnorePhrase::getPattern() const
{
return this->pattern_;
}

bool IgnorePhrase::isRegex() const
{
return this->isRegex_;
}

bool IgnorePhrase::isRegexValid() const
{
return this->regex_.isValid();
}

bool IgnorePhrase::isMatch(const QString &subject) const
{
return !this->pattern_.isEmpty() &&
(this->isRegex()
? (this->regex_.isValid() &&
this->regex_.match(subject).hasMatch())
: subject.contains(this->pattern_, this->caseSensitivity()));
}

const QRegularExpression &IgnorePhrase::getRegex() const
{
return this->regex_;
}

bool IgnorePhrase::isBlock() const
{
return this->isBlock_;
}

const QString &IgnorePhrase::getReplace() const
{
return this->replace_;
}

bool IgnorePhrase::isCaseSensitive() const
{
return this->isCaseSensitive_;
}

Qt::CaseSensitivity IgnorePhrase::caseSensitivity() const
{
return this->isCaseSensitive_ ? Qt::CaseSensitive : Qt::CaseInsensitive;
}

const std::unordered_map<EmoteName, EmotePtr> &IgnorePhrase::getEmotes() const
{
return this->emotes_;
}

bool IgnorePhrase::containsEmote() const
{
if (!this->emotesChecked_)
{
const auto &accvec = getApp()->accounts->twitch.accounts;
for (const auto &acc : accvec)
{
const auto &accemotes = *acc->accessEmotes();
for (const auto &emote : accemotes.emotes)
{
if (this->replace_.contains(emote.first.string,
Qt::CaseSensitive))
{
this->emotes_.emplace(emote.first, emote.second);
}
}
}
this->emotesChecked_ = true;
}
return !this->emotes_.empty();
}

} // namespace chatterino
112 changes: 16 additions & 96 deletions src/controllers/ignores/IgnorePhrase.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "Application.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "messages/Emote.hpp"
#include "singletons/Settings.hpp"
#include "util/RapidjsonHelpers.hpp"
#include "util/RapidJsonSerializeQString.hpp"
Expand All @@ -10,118 +9,39 @@
#include <QRegularExpression>
#include <QString>

#include <memory>
#include <unordered_map>

namespace chatterino {

class IgnorePhrase
{
public:
bool operator==(const IgnorePhrase &other) const
{
return std::tie(this->pattern_, this->isRegex_, this->isBlock_,
this->replace_, this->isCaseSensitive_) ==
std::tie(other.pattern_, other.isRegex_, other.isBlock_,
other.replace_, other.isCaseSensitive_);
}

IgnorePhrase(const QString &pattern, bool isRegex, bool isBlock,
const QString &replace, bool isCaseSensitive)
: pattern_(pattern)
, isRegex_(isRegex)
, regex_(pattern)
, isBlock_(isBlock)
, replace_(replace)
, isCaseSensitive_(isCaseSensitive)
{
if (this->isCaseSensitive_)
{
regex_.setPatternOptions(
QRegularExpression::UseUnicodePropertiesOption);
}
else
{
regex_.setPatternOptions(
QRegularExpression::CaseInsensitiveOption |
QRegularExpression::UseUnicodePropertiesOption);
}
}
const QString &replace, bool isCaseSensitive);

const QString &getPattern() const
{
return this->pattern_;
}
bool operator==(const IgnorePhrase &other) const;

bool isRegex() const
{
return this->isRegex_;
}
const QString &getPattern() const;

bool isRegexValid() const
{
return this->regex_.isValid();
}
bool isRegex() const;

bool isMatch(const QString &subject) const
{
return !this->pattern_.isEmpty() &&
(this->isRegex() ? (this->regex_.isValid() &&
this->regex_.match(subject).hasMatch())
: subject.contains(this->pattern_,
this->caseSensitivity()));
}
bool isRegexValid() const;

const QRegularExpression &getRegex() const
{
return this->regex_;
}
bool isMatch(const QString &subject) const;

bool isBlock() const
{
return this->isBlock_;
}
const QRegularExpression &getRegex() const;

const QString &getReplace() const
{
return this->replace_;
}
bool isBlock() const;

bool isCaseSensitive() const
{
return this->isCaseSensitive_;
}
const QString &getReplace() const;

Qt::CaseSensitivity caseSensitivity() const
{
return this->isCaseSensitive_ ? Qt::CaseSensitive : Qt::CaseInsensitive;
}
bool isCaseSensitive() const;

const std::unordered_map<EmoteName, EmotePtr> &getEmotes() const
{
return this->emotes_;
}
Qt::CaseSensitivity caseSensitivity() const;

bool containsEmote() const
{
if (!this->emotesChecked_)
{
const auto &accvec = getApp()->accounts->twitch.accounts;
for (const auto &acc : accvec)
{
const auto &accemotes = *acc->accessEmotes();
for (const auto &emote : accemotes.emotes)
{
if (this->replace_.contains(emote.first.string,
Qt::CaseSensitive))
{
this->emotes_.emplace(emote.first, emote.second);
}
}
}
this->emotesChecked_ = true;
}
return !this->emotes_.empty();
}
const std::unordered_map<EmoteName, EmotePtr> &getEmotes() const;

bool containsEmote() const;

private:
QString pattern_;
Expand Down
1 change: 1 addition & 0 deletions src/controllers/nicknames/Nickname.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "util/RapidJsonSerializeQString.hpp"

#include <pajlada/serialize.hpp>
#include <QRegularExpression>
#include <QString>

#include <memory>
Expand Down
1 change: 1 addition & 0 deletions src/messages/MessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "messages/MessageElement.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/twitch/PubSubActions.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Theme.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "controllers/accounts/AccountController.hpp"
#include "messages/LimitedQueue.hpp"
#include "messages/Message.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchAccountManager.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchHelpers.hpp"
Expand Down
13 changes: 13 additions & 0 deletions src/providers/twitch/PubSubManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "providers/twitch/PubSubActions.hpp"
#include "providers/twitch/PubSubHelpers.hpp"
#include "providers/twitch/PubSubMessages.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "util/DebugCount.hpp"
#include "util/Helpers.hpp"
#include "util/RapidjsonHelpers.hpp"
Expand Down Expand Up @@ -476,6 +477,18 @@ PubSub::PubSub(const QString &host, std::chrono::seconds pingInterval)
bind(&PubSub::onConnectionFail, this, ::_1));
}

void PubSub::setAccount(std::shared_ptr<TwitchAccount> account)
{
this->token_ = account->getOAuthToken();
this->userID_ = account->getUserId();
}

void PubSub::setAccountData(QString token, QString userID)
{
this->token_ = token;
pajlada marked this conversation as resolved.
Show resolved Hide resolved
this->userID_ = userID;
pajlada marked this conversation as resolved.
Show resolved Hide resolved
}

void PubSub::addClient()
{
if (this->addingClient)
Expand Down
16 changes: 5 additions & 11 deletions src/providers/twitch/PubSubManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "providers/twitch/PubSubClientOptions.hpp"
#include "providers/twitch/PubSubMessages.hpp"
#include "providers/twitch/PubSubWebsocket.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "util/ExponentialBackoff.hpp"
#include "util/QStringHash.hpp"

#include <pajlada/signals/signal.hpp>
#include <QJsonObject>
Expand All @@ -24,6 +24,8 @@

namespace chatterino {

class TwitchAccount;

class PubSub
{
using WebsocketMessagePtr =
Expand Down Expand Up @@ -57,17 +59,9 @@ class PubSub
PubSub(const QString &host,
std::chrono::seconds pingInterval = std::chrono::seconds(15));

void setAccount(std::shared_ptr<TwitchAccount> account)
{
this->token_ = account->getOAuthToken();
this->userID_ = account->getUserId();
}
void setAccount(std::shared_ptr<TwitchAccount> account);

void setAccountData(QString token, QString userID)
{
this->token_ = token;
this->userID_ = userID;
}
void setAccountData(QString token, QString userID);

~PubSub() = delete;

Expand Down
Loading