From 01de4d4b5d0c055e0b89e3548dd821746b99d463 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Sun, 20 Nov 2022 17:28:29 +0100 Subject: [PATCH] Add support for Nicknames on IRC (#4170) Co-authored-by: pajlada --- CHANGELOG.md | 1 + src/messages/SharedMessageBuilder.cpp | 56 +++++++++++++++++ src/messages/SharedMessageBuilder.hpp | 3 + src/providers/irc/IrcMessageBuilder.cpp | 3 +- src/providers/twitch/TwitchMessageBuilder.cpp | 63 ++----------------- 5 files changed, 66 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 199bcb269..b51cd4cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ - Minor: Add support for `echo-message` capabilities for IRC. (#4157) - Minor: Add proper support for IRC private messages. (#4158) - Minor: Improved look of tabs when using a layout other than top. (#3925, #4152) +- Minor: Added support for Nicknames on IRC. (#4170) - Bugfix: Fixed channels with two leading `#`s not being usable on IRC (#4154) - Bugfix: Fixed `Add new account` dialog causing main chatterino window to be non movable. (#4121) - Bugfix: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716) diff --git a/src/messages/SharedMessageBuilder.cpp b/src/messages/SharedMessageBuilder.cpp index 5fc76c870..270c9c896 100644 --- a/src/messages/SharedMessageBuilder.cpp +++ b/src/messages/SharedMessageBuilder.cpp @@ -246,4 +246,60 @@ void SharedMessageBuilder::triggerHighlights() } } +QString SharedMessageBuilder::stylizeUsername(const QString &username, + const Message &message) +{ + auto app = getApp(); + + const QString &localizedName = message.localizedName; + bool hasLocalizedName = !localizedName.isEmpty(); + + // The full string that will be rendered in the chat widget + QString usernameText; + + switch (getSettings()->usernameDisplayMode.getValue()) + { + case UsernameDisplayMode::Username: { + usernameText = username; + } + break; + + case UsernameDisplayMode::LocalizedName: { + if (hasLocalizedName) + { + usernameText = localizedName; + } + else + { + usernameText = username; + } + } + break; + + default: + case UsernameDisplayMode::UsernameAndLocalizedName: { + if (hasLocalizedName) + { + usernameText = username + "(" + localizedName + ")"; + } + else + { + usernameText = username; + } + } + break; + } + + auto nicknames = getCSettings().nicknames.readOnly(); + + for (const auto &nickname : *nicknames) + { + if (nickname.match(usernameText)) + { + break; + } + } + + return usernameText; +} } // namespace chatterino diff --git a/src/messages/SharedMessageBuilder.hpp b/src/messages/SharedMessageBuilder.hpp index 421339418..3de4531c1 100644 --- a/src/messages/SharedMessageBuilder.hpp +++ b/src/messages/SharedMessageBuilder.hpp @@ -38,6 +38,9 @@ public: // Parses "badges" tag which contains a comma separated list of key-value elements static std::vector parseBadgeTag(const QVariantMap &tags); + static QString stylizeUsername(const QString &username, + const Message &message); + protected: virtual void parse(); diff --git a/src/providers/irc/IrcMessageBuilder.cpp b/src/providers/irc/IrcMessageBuilder.cpp index 68469d967..7fa9652cb 100644 --- a/src/providers/irc/IrcMessageBuilder.cpp +++ b/src/providers/irc/IrcMessageBuilder.cpp @@ -90,7 +90,8 @@ void IrcMessageBuilder::appendUsername() this->message().displayName = username; // The full string that will be rendered in the chat widget - QString usernameText = username; + QString usernameText = + SharedMessageBuilder::stylizeUsername(username, this->message()); if (this->args.isReceivedWhisper) { diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 9c4b922a3..71849baca 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -52,62 +52,6 @@ namespace chatterino { namespace { - QString stylizeUsername(const QString &username, const Message &message) - { - auto app = getApp(); - - const QString &localizedName = message.localizedName; - bool hasLocalizedName = !localizedName.isEmpty(); - - // The full string that will be rendered in the chat widget - QString usernameText; - - switch (getSettings()->usernameDisplayMode.getValue()) - { - case UsernameDisplayMode::Username: { - usernameText = username; - } - break; - - case UsernameDisplayMode::LocalizedName: { - if (hasLocalizedName) - { - usernameText = localizedName; - } - else - { - usernameText = username; - } - } - break; - - default: - case UsernameDisplayMode::UsernameAndLocalizedName: { - if (hasLocalizedName) - { - usernameText = username + "(" + localizedName + ")"; - } - else - { - usernameText = username; - } - } - break; - } - - auto nicknames = getCSettings().nicknames.readOnly(); - - for (const auto &nickname : *nicknames) - { - if (nickname.match(usernameText)) - { - break; - } - } - - return usernameText; - } - void appendTwitchEmoteOccurrences(const QString &emote, std::vector &vec, const std::vector &correctPositions, @@ -293,8 +237,8 @@ MessagePtr TwitchMessageBuilder::build() const auto &threadRoot = this->thread_->root(); - QString usernameText = - stylizeUsername(threadRoot->loginName, *threadRoot.get()); + QString usernameText = SharedMessageBuilder::stylizeUsername( + threadRoot->loginName, *threadRoot.get()); this->emplace(); @@ -783,7 +727,8 @@ void TwitchMessageBuilder::appendUsername() } } - QString usernameText = stylizeUsername(username, this->message()); + QString usernameText = + SharedMessageBuilder::stylizeUsername(username, this->message()); if (this->args.isSentWhisper) {