From b79d5fa6f06c8d8e7cb4a9dcfffc34c1ece29c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= <44851575+zneix@users.noreply.github.com> Date: Sun, 22 Nov 2020 14:02:55 +0100 Subject: [PATCH] Fixed "raw mentions" not being clickable with special characters appended to them (#2212) --- CHANGELOG.md | 1 + src/providers/twitch/TwitchMessageBuilder.cpp | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1d19d39..3fd6af72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unversioned +- Minor: Made `Try to find usernames without @ prefix` option still resolve usernames when special characters (commas, dots, (semi)colons, exclamation mark, question mark) are appended to them. (#2212) - Minor: Made usercard update user's display name (#2160) - Minor: Added placeholder text for message text input box. (#2143, #2149) - Minor: Added support for FrankerFaceZ badges. (#2101, part of #1658) diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index ec93e8bc..c7c7db98 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -29,8 +29,13 @@ namespace { +const QString regexHelpString("(\\w+)[.,!?;:]*?$"); + // matches a mention with punctuation at the end, like "@username," or "@username!!!" where capture group would return "username" -const QRegularExpression mentionRegex("^@(\\w+)[.,!?;]*?$"); +const QRegularExpression mentionRegex("^@" + regexHelpString); + +// if findAllUsernames setting is enabled, matches strings like in the examples above, but without @ symbol at the beginning +const QRegularExpression allUsernamesMentionRegex("^" + regexHelpString); const QSet zeroWidthEmotes{ "SoSnowy", "IceCold", "SantaHat", "TopHat", @@ -474,6 +479,7 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) if (match.hasMatch()) { QString username = match.captured(1); + this->emplace(string, MessageElementFlag::BoldUsername, textColor, FontStyle::ChatMediumBold) ->setLink({Link::UserInfo, username}); @@ -488,15 +494,18 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) if (this->twitchChannel != nullptr && getSettings()->findAllUsernames) { auto chatters = this->twitchChannel->accessChatters(); - if (chatters->contains(string)) + auto match = allUsernamesMentionRegex.match(string); + QString username = match.captured(1); + + if (match.hasMatch() && chatters->contains(username)) { this->emplace(string, MessageElementFlag::BoldUsername, textColor, FontStyle::ChatMediumBold) - ->setLink({Link::UserInfo, string}); + ->setLink({Link::UserInfo, username}); this->emplace( string, MessageElementFlag::NonBoldUsername, textColor) - ->setLink({Link::UserInfo, string}); + ->setLink({Link::UserInfo, username}); return; } }