Made links clickable in sent whispers.

This commit is contained in:
Cranken 2019-03-13 15:26:55 +01:00
parent 3f41dfeff9
commit 497c958485
4 changed files with 66 additions and 53 deletions

View file

@ -188,10 +188,18 @@ QString CommandController::execCommand(const QString &textNoEmoji,
} }
void operator()(const QString &string, void operator()(const QString &string,
MessageBuilder &b) const MessageBuilder &b) const
{
auto linkString = b.matchLink(string);
if (linkString.isEmpty())
{ {
b.emplace<TextElement>( b.emplace<TextElement>(
string, MessageElementFlag::Text); string, MessageElementFlag::Text);
} }
else
{
b.addLink(string, linkString);
}
}
} visitor; } visitor;
boost::apply_visitor( boost::apply_visitor(
[&b](auto &&arg) { visitor(arg, b); }, variant); [&b](auto &&arg) { visitor(arg, b); }, variant);

View file

@ -5,6 +5,7 @@
#include "messages/Image.hpp" #include "messages/Image.hpp"
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "messages/MessageElement.hpp" #include "messages/MessageElement.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/twitch/PubsubActions.hpp" #include "providers/twitch/PubsubActions.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
@ -352,4 +353,57 @@ QString MessageBuilder::matchLink(const QString &string)
return captured; 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<TextElement>(lowercaseLinkString,
MessageElementFlag::LowercaseLink, textColor)
->setLink(linkElement);
auto linkMEOriginal =
this->emplace<TextElement>(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 } // namespace chatterino

View file

@ -54,6 +54,7 @@ public:
void append(std::unique_ptr<MessageElement> element); void append(std::unique_ptr<MessageElement> element);
QString matchLink(const QString &string); QString matchLink(const QString &string);
void addLink(const QString &origLink, const QString &matchedLink);
template <typename T, typename... Args> template <typename T, typename... Args>
T *emplace(Args &&... args) T *emplace(Args &&... args)

View file

@ -6,7 +6,6 @@
#include "controllers/ignores/IgnoreController.hpp" #include "controllers/ignores/IgnoreController.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/chatterino/ChatterinoBadges.hpp" #include "providers/chatterino/ChatterinoBadges.hpp"
#include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchBadges.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
@ -512,56 +511,7 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)
} }
else else
{ {
static QRegularExpression domainRegex( this->addLink(string, linkString);
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<TextElement>(lowercaseLinkString,
MessageElementFlag::LowercaseLink,
textColor)
->setLink(link);
auto linkMEOriginal =
this->emplace<TextElement>(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();
}
});
} }
// if (!linkString.isEmpty()) { // if (!linkString.isEmpty()) {