Skip to content

Commit

Permalink
Check for ignored phrases/users in channel point redemptions (#3102)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Aug 1, 2021
1 parent 77f6835 commit 784fdd2
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Bugfix: Copy buttons in usercard now show properly in light mode (#3057)
- Bugfix: Fixed comma appended to username completion when not at the beginning of the message. (#3060)
- Bugfix: Fixed bug misplacing chat when zooming on Chrome with Chatterino Native Host extension (#1936)
- Bugfix: Channel point redemptions from ignored users are now properly blocked. (#3102)
- Dev: Ubuntu packages are now available (#2936)
- Dev: Disabled update checker on Flatpak. (#3051)
- Dev: Add logging for HTTP requests (#2991)
Expand Down
1 change: 1 addition & 0 deletions chatterino.pro
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ SOURCES += \
src/controllers/highlights/HighlightModel.cpp \
src/controllers/highlights/HighlightPhrase.cpp \
src/controllers/highlights/UserHighlightModel.cpp \
src/controllers/ignores/IgnoreController.cpp \
src/controllers/ignores/IgnoreModel.cpp \
src/controllers/moderationactions/ModerationAction.cpp \
src/controllers/moderationactions/ModerationActionModel.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ set(SOURCE_FILES
controllers/highlights/UserHighlightModel.cpp
controllers/highlights/UserHighlightModel.hpp

controllers/ignores/IgnoreController.cpp
controllers/ignores/IgnoreController.hpp
controllers/ignores/IgnoreModel.cpp
controllers/ignores/IgnoreModel.hpp

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

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

namespace chatterino {

bool isIgnoredMessage(IgnoredMessageParameters &&params)
{
if (!params.message.isEmpty())
{
// TODO(pajlada): Do we need to check if the phrase is valid first?
auto phrases = getCSettings().ignoredMessages.readOnly();
for (const auto &phrase : *phrases)
{
if (phrase.isBlock() && phrase.isMatch(params.message))
{
qCDebug(chatterinoMessage)
<< "Blocking message because it contains ignored phrase"
<< phrase.getPattern();
return true;
}
}
}

if (!params.twitchUserID.isEmpty() &&
getSettings()->enableTwitchBlockedUsers)
{
auto sourceUserID = params.twitchUserID;

auto blocks =
getApp()->accounts->twitch.getCurrent()->accessBlockedUserIds();

if (auto it = blocks->find(sourceUserID); it != blocks->end())
{
switch (static_cast<ShowIgnoredUsersMessages>(
getSettings()->showBlockedUsersMessages.getValue()))
{
case ShowIgnoredUsersMessages::IfModerator:
if (params.isMod || params.isBroadcaster)
{
return false;
}
break;
case ShowIgnoredUsersMessages::IfBroadcaster:
if (params.isBroadcaster)
{
return false;
}
break;
case ShowIgnoredUsersMessages::Never:
break;
}

return true;
}
}

return false;
}

} // namespace chatterino
12 changes: 12 additions & 0 deletions src/controllers/ignores/IgnoreController.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#pragma once

#include <QString>

namespace chatterino {

enum class ShowIgnoredUsersMessages { Never, IfModerator, IfBroadcaster };

struct IgnoredMessageParameters {
QString message;

QString twitchUserID;
bool isMod;
bool isBroadcaster;
};

bool isIgnoredMessage(IgnoredMessageParameters &&params);

} // namespace chatterino
18 changes: 4 additions & 14 deletions src/messages/SharedMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Application.hpp"
#include "common/QLogging.hpp"
#include "controllers/ignores/IgnoreController.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
Expand Down Expand Up @@ -104,20 +105,9 @@ void SharedMessageBuilder::parse()

bool SharedMessageBuilder::isIgnored() const
{
// TODO(pajlada): Do we need to check if the phrase is valid first?
auto phrases = getCSettings().ignoredMessages.readOnly();
for (const auto &phrase : *phrases)
{
if (phrase.isBlock() && phrase.isMatch(this->originalMessage_))
{
qCDebug(chatterinoMessage)
<< "Blocking message because it contains ignored phrase"
<< phrase.getPattern();
return true;
}
}

return false;
return isIgnoredMessage({
/*.message = */ this->originalMessage_,
});
}

void SharedMessageBuilder::parseUsernameColor()
Expand Down
3 changes: 2 additions & 1 deletion src/providers/twitch/TwitchChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ void TwitchChannel::addChannelPointReward(const ChannelPointReward &reward)
if (!reward.isUserInputRequired)
{
MessageBuilder builder;
TwitchMessageBuilder::appendChannelPointRewardMessage(reward, &builder);
TwitchMessageBuilder::appendChannelPointRewardMessage(
reward, &builder, this->isMod(), this->isBroadcaster());
this->addMessage(builder.release());
return;
}
Expand Down
61 changes: 21 additions & 40 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,44 +114,12 @@ TwitchMessageBuilder::TwitchMessageBuilder(

bool TwitchMessageBuilder::isIgnored() const
{
if (SharedMessageBuilder::isIgnored())
{
return true;
}

auto app = getApp();

if (getSettings()->enableTwitchBlockedUsers &&
this->tags.contains("user-id"))
{
auto sourceUserID = this->tags.value("user-id").toString();

auto blocks =
app->accounts->twitch.getCurrent()->accessBlockedUserIds();

if (auto it = blocks->find(sourceUserID); it != blocks->end())
{
switch (static_cast<ShowIgnoredUsersMessages>(
getSettings()->showBlockedUsersMessages.getValue()))
{
case ShowIgnoredUsersMessages::IfModerator:
if (this->channel->isMod() ||
this->channel->isBroadcaster())
return false;
break;
case ShowIgnoredUsersMessages::IfBroadcaster:
if (this->channel->isBroadcaster())
return false;
break;
case ShowIgnoredUsersMessages::Never:
break;
}

return true;
}
}

return false;
return isIgnoredMessage({
/*.message = */ this->originalMessage_,
/*.twitchUserID = */ this->tags.value("user-id").toString(),
/*.isMod = */ this->channel->isMod(),
/*.isBroadcaster = */ this->channel->isBroadcaster(),
});
}

void TwitchMessageBuilder::triggerHighlights()
Expand Down Expand Up @@ -190,7 +158,9 @@ MessagePtr TwitchMessageBuilder::build()
this->args.channelPointRewardId);
if (reward)
{
this->appendChannelPointRewardMessage(reward.get(), this);
this->appendChannelPointRewardMessage(
reward.get(), this, this->channel->isMod(),
this->channel->isBroadcaster());
}
}

Expand Down Expand Up @@ -1261,8 +1231,19 @@ Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string)
}

void TwitchMessageBuilder::appendChannelPointRewardMessage(
const ChannelPointReward &reward, MessageBuilder *builder)
const ChannelPointReward &reward, MessageBuilder *builder, bool isMod,
bool isBroadcaster)
{
if (isIgnoredMessage({
/*.message = */ "",
/*.twitchUserID = */ reward.user.id,
/*.isMod = */ isMod,
/*.isBroadcaster = */ isBroadcaster,
}))
{
return;
}

builder->emplace<TimestampElement>();
QString redeemed = "Redeemed";
QStringList textList;
Expand Down
3 changes: 2 additions & 1 deletion src/providers/twitch/TwitchMessageBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class TwitchMessageBuilder : public SharedMessageBuilder
MessagePtr build() override;

static void appendChannelPointRewardMessage(
const ChannelPointReward &reward, MessageBuilder *builder);
const ChannelPointReward &reward, MessageBuilder *builder, bool isMod,
bool isBroadcaster);

// Message in the /live chat for channel going live
static void liveMessage(const QString &channelName,
Expand Down

0 comments on commit 784fdd2

Please sign in to comment.