diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 8d98a999a..5767c66b0 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -189,8 +189,16 @@ QString CommandController::execCommand(const QString &textNoEmoji, void operator()(const QString &string, MessageBuilder &b) const { - b.emplace( - string, MessageElementFlag::Text); + auto linkString = b.matchLink(string); + if (linkString.isEmpty()) + { + b.emplace( + string, MessageElementFlag::Text); + } + else + { + b.addLink(string, linkString); + } } } visitor; boost::apply_visitor( diff --git a/src/messages/MessageBuilder.cpp b/src/messages/MessageBuilder.cpp index 2bc8e70dd..a029aea35 100644 --- a/src/messages/MessageBuilder.cpp +++ b/src/messages/MessageBuilder.cpp @@ -5,6 +5,7 @@ #include "messages/Image.hpp" #include "messages/Message.hpp" #include "messages/MessageElement.hpp" +#include "providers/LinkResolver.hpp" #include "providers/twitch/PubsubActions.hpp" #include "singletons/Emotes.hpp" #include "singletons/Resources.hpp" @@ -352,4 +353,57 @@ QString MessageBuilder::matchLink(const QString &string) return captured; } +void MessageBuilder::addLink(const QString &origLink, + const QString &matchedLink) +{ + static QRegularExpression domainRegex( + R"(^(?:(?:ftp|http)s?:\/\/)?([^\/]+)(?:\/.*)?$)", + QRegularExpression::CaseInsensitiveOption); + + QString lowercaseLinkString; + auto match = domainRegex.match(origLink); + if (match.isValid()) + { + lowercaseLinkString = origLink.mid(0, match.capturedStart(1)) + + match.captured(1).toLower() + + origLink.mid(match.capturedEnd(1)); + } + else + { + lowercaseLinkString = origLink; + } + auto linkElement = Link(Link::Url, matchedLink); + + auto textColor = MessageColor(MessageColor::Link); + auto linkMELowercase = + this->emplace(lowercaseLinkString, + MessageElementFlag::LowercaseLink, textColor) + ->setLink(linkElement); + auto linkMEOriginal = + this->emplace(origLink, MessageElementFlag::OriginalLink, + textColor) + ->setLink(linkElement); + + LinkResolver::getLinkInfo(matchedLink, [weakMessage = this->weakOf(), + linkMELowercase, linkMEOriginal, + matchedLink](QString tooltipText, + Link originalLink) { + auto shared = weakMessage.lock(); + if (!shared) + { + return; + } + if (!tooltipText.isEmpty()) + { + linkMELowercase->setTooltip(tooltipText); + linkMEOriginal->setTooltip(tooltipText); + } + if (originalLink.value != matchedLink && !originalLink.value.isEmpty()) + { + linkMELowercase->setLink(originalLink)->updateLink(); + linkMEOriginal->setLink(originalLink)->updateLink(); + } + }); +} + } // namespace chatterino diff --git a/src/messages/MessageBuilder.hpp b/src/messages/MessageBuilder.hpp index 8fe7915e2..dbc58cbb7 100644 --- a/src/messages/MessageBuilder.hpp +++ b/src/messages/MessageBuilder.hpp @@ -54,6 +54,7 @@ public: void append(std::unique_ptr element); QString matchLink(const QString &string); + void addLink(const QString &origLink, const QString &matchedLink); template T *emplace(Args &&... args) diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index b8c34cc65..46d604042 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -6,7 +6,6 @@ #include "controllers/ignores/IgnoreController.hpp" #include "debug/Log.hpp" #include "messages/Message.hpp" -#include "providers/LinkResolver.hpp" #include "providers/chatterino/ChatterinoBadges.hpp" #include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchChannel.hpp" @@ -512,56 +511,7 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) } else { - static QRegularExpression domainRegex( - R"(^(?:(?:ftp|http)s?:\/\/)?([^\/]+)(?:\/.*)?$)", - QRegularExpression::CaseInsensitiveOption); - - QString lowercaseLinkString; - auto match = domainRegex.match(string); - if (match.isValid()) - { - lowercaseLinkString = string.mid(0, match.capturedStart(1)) + - match.captured(1).toLower() + - string.mid(match.capturedEnd(1)); - } - else - { - lowercaseLinkString = string; - } - link = Link(Link::Url, linkString); - - textColor = MessageColor(MessageColor::Link); - auto linkMELowercase = - this->emplace(lowercaseLinkString, - MessageElementFlag::LowercaseLink, - textColor) - ->setLink(link); - auto linkMEOriginal = - this->emplace(string, MessageElementFlag::OriginalLink, - textColor) - ->setLink(link); - - LinkResolver::getLinkInfo( - linkString, - [weakMessage = this->weakOf(), linkMELowercase, linkMEOriginal, - linkString](QString tooltipText, Link originalLink) { - auto shared = weakMessage.lock(); - if (!shared) - { - return; - } - if (!tooltipText.isEmpty()) - { - linkMELowercase->setTooltip(tooltipText); - linkMEOriginal->setTooltip(tooltipText); - } - if (originalLink.value != linkString && - !originalLink.value.isEmpty()) - { - linkMELowercase->setLink(originalLink)->updateLink(); - linkMEOriginal->setLink(originalLink)->updateLink(); - } - }); + this->addLink(string, linkString); } // if (!linkString.isEmpty()) {