From bf4148a3701d1b29662938eb6179556d93fcd3b1 Mon Sep 17 00:00:00 2001 From: chrrs <9766338+chrrs@users.noreply.github.com> Date: Wed, 31 May 2023 21:38:17 +0200 Subject: [PATCH] Consider nicknames when searching for messages (#4663) Co-authored-by: pajlada --- CHANGELOG.md | 1 + src/controllers/nicknames/Nickname.hpp | 16 ++++++++-------- src/messages/SharedMessageBuilder.cpp | 9 ++------- src/providers/irc/IrcMessageBuilder.cpp | 6 +++++- src/providers/twitch/TwitchMessageBuilder.cpp | 6 +++++- src/singletons/Settings.cpp | 16 ++++++++++++++++ src/singletons/Settings.hpp | 1 + src/widgets/settingspages/NicknamesPage.cpp | 2 +- 8 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68eadc707..4e8332da0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unversioned +- Minor: Nicknames are now taken into consideration when searching for messages. (#4663) - Minor: Add an icon showing when streamer mode is enabled (#4410) - Minor: Added `/shoutout ` commands to shoutout specified user. (#4638) - Minor: Improved editing hotkeys. (#4628) diff --git a/src/controllers/nicknames/Nickname.hpp b/src/controllers/nicknames/Nickname.hpp index 529f5fa0d..fa8bdf6d0 100644 --- a/src/controllers/nicknames/Nickname.hpp +++ b/src/controllers/nicknames/Nickname.hpp @@ -3,6 +3,7 @@ #include "util/RapidjsonHelpers.hpp" #include "util/RapidJsonSerializeQString.hpp" +#include #include #include #include @@ -58,25 +59,25 @@ public: return this->isCaseSensitive_; } - [[nodiscard]] bool match(QString &usernameText) const + [[nodiscard]] boost::optional match( + const QString &usernameText) const { if (this->isRegex()) { if (!this->regex_.isValid()) { - return false; + return boost::none; } if (this->name().isEmpty()) { - return false; + return boost::none; } auto workingCopy = usernameText; workingCopy.replace(this->regex_, this->replace()); if (workingCopy != usernameText) { - usernameText = workingCopy; - return true; + return workingCopy; } } else @@ -85,12 +86,11 @@ public: this->name().compare(usernameText, this->caseSensitivity()); if (res == 0) { - usernameText = this->replace(); - return true; + return this->replace(); } } - return false; + return boost::none; } private: diff --git a/src/messages/SharedMessageBuilder.cpp b/src/messages/SharedMessageBuilder.cpp index 9d0fda90c..97c795c3a 100644 --- a/src/messages/SharedMessageBuilder.cpp +++ b/src/messages/SharedMessageBuilder.cpp @@ -270,14 +270,9 @@ QString SharedMessageBuilder::stylizeUsername(const QString &username, break; } - auto nicknames = getCSettings().nicknames.readOnly(); - - for (const auto &nickname : *nicknames) + if (auto nicknameText = getCSettings().matchNickname(usernameText)) { - if (nickname.match(usernameText)) - { - break; - } + usernameText = *nicknameText; } return usernameText; diff --git a/src/providers/irc/IrcMessageBuilder.cpp b/src/providers/irc/IrcMessageBuilder.cpp index 0474bdb76..3b7fb30da 100644 --- a/src/providers/irc/IrcMessageBuilder.cpp +++ b/src/providers/irc/IrcMessageBuilder.cpp @@ -64,7 +64,11 @@ MessagePtr IrcMessageBuilder::build() // message this->addIrcMessageText(this->originalMessage_); - this->message().searchText = this->message().localizedName + " " + + QString stylizedUsername = + this->stylizeUsername(this->userName, this->message()); + + this->message().searchText = stylizedUsername + " " + + this->message().localizedName + " " + this->userName + ": " + this->originalMessage_; // highlights diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 98888fa48..3cd93303c 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -294,8 +294,12 @@ MessagePtr TwitchMessageBuilder::build() this->addWords(splits, twitchEmotes); + QString stylizedUsername = + this->stylizeUsername(this->userName, this->message()); + this->message().messageText = this->originalMessage_; - this->message().searchText = this->message().localizedName + " " + + this->message().searchText = stylizedUsername + " " + + this->message().localizedName + " " + this->userName + ": " + this->originalMessage_; // highlights diff --git a/src/singletons/Settings.cpp b/src/singletons/Settings.cpp index 07e10142c..e01f316a1 100644 --- a/src/singletons/Settings.cpp +++ b/src/singletons/Settings.cpp @@ -81,6 +81,22 @@ bool ConcurrentSettings::isMutedChannel(const QString &channelName) return false; } +boost::optional ConcurrentSettings::matchNickname( + const QString &usernameText) +{ + auto nicknames = getCSettings().nicknames.readOnly(); + + for (const auto &nickname : *nicknames) + { + if (auto nicknameText = nickname.match(usernameText)) + { + return nicknameText; + } + } + + return boost::none; +} + void ConcurrentSettings::mute(const QString &channelName) { mutedChannels.append(channelName); diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 4ff10a071..3d393a6ab 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -47,6 +47,7 @@ public: bool isBlacklistedUser(const QString &username); bool isMutedChannel(const QString &channelName); bool toggleMutedChannel(const QString &channelName); + boost::optional matchNickname(const QString &username); private: void mute(const QString &channelName); diff --git a/src/widgets/settingspages/NicknamesPage.cpp b/src/widgets/settingspages/NicknamesPage.cpp index e8fcf56fe..a61042bf9 100644 --- a/src/widgets/settingspages/NicknamesPage.cpp +++ b/src/widgets/settingspages/NicknamesPage.cpp @@ -18,7 +18,7 @@ NicknamesPage::NicknamesPage() auto layout = layoutCreator.setLayoutType(); layout.emplace( - "Nicknames do not work with features such as search or user highlights." + "Nicknames do not work with features such as user highlights." "\nWith those features you will still need to use the user's original " "name."); EditableModelView *view =