diff --git a/src/messages/MessageElement.cpp b/src/messages/MessageElement.cpp index b9271d2ed..49b2cadbc 100644 --- a/src/messages/MessageElement.cpp +++ b/src/messages/MessageElement.cpp @@ -61,6 +61,12 @@ MessageElementFlags MessageElement::getFlags() const return this->flags_; } +MessageElement *MessageElement::updateLink() +{ + this->linkChanged.invoke(); + return this; +} + // IMAGE ImageElement::ImageElement(ImagePtr image, MessageElementFlags flags) : MessageElement(flags) @@ -152,6 +158,15 @@ void TextElement::addToContainer(MessageLayoutContainer &container, color, this->style_, container.getScale())) ->setLink(this->getLink()); e->setTrailingSpace(trailingSpace); + + // If URL link was changed, + // Should update it in MessageLayoutElement too! + if (this->getLink().type == Link::Url) { + this->linkChanged.connect( + [this, e]() { + e->setLink(this->getLink()); + }); + } return e; }; diff --git a/src/messages/MessageElement.hpp b/src/messages/MessageElement.hpp index 8d96d1135..e82bee5e0 100644 --- a/src/messages/MessageElement.hpp +++ b/src/messages/MessageElement.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace chatterino { class Channel; @@ -128,6 +129,7 @@ public: const Link &getLink() const; bool hasTrailingSpace() const; MessageElementFlags getFlags() const; + MessageElement *updateLink(); virtual void addToContainer(MessageLayoutContainer &container, MessageElementFlags flags) = 0; @@ -135,6 +137,7 @@ public: protected: MessageElement(MessageElementFlags flags); bool trailingSpace = true; + pajlada::Signals::NoArgSignal linkChanged; private: Link link_; diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index d6917157d..f48834ef3 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -2,13 +2,15 @@ #include "common/Common.hpp" #include "common/NetworkRequest.hpp" +#include "messages/Link.hpp" +#include "singletons/Settings.hpp" #include namespace chatterino { void LinkResolver::getLinkInfo(const QString url, - std::function successCallback) + std::function successCallback) { QString requestUrl("https://braize.pajlada.com/chatterino/link_resolver/" + QUrl::toPercentEncoding(url, "", "/:")); @@ -16,22 +18,26 @@ void LinkResolver::getLinkInfo(const QString url, NetworkRequest request(requestUrl); request.setCaller(QThread::currentThread()); request.setTimeout(30000); - request.onSuccess([successCallback](auto result) mutable -> Outcome { + request.onSuccess([successCallback, url](auto result) mutable -> Outcome { auto root = result.parseJson(); auto statusCode = root.value("status").toInt(); QString response = QString(); + QString linkString = url; if (statusCode == 200) { response = root.value("tooltip").toString(); + if (getSettings()->enableUnshortLinks) { + linkString = root.value("link").toString(); + } } else { response = root.value("message").toString(); } - successCallback(QUrl::fromPercentEncoding(response.toUtf8())); + successCallback(QUrl::fromPercentEncoding(response.toUtf8()), Link(Link::Url, linkString)); return Success; }); - request.onError([successCallback](auto result) { - successCallback("No link info found"); + request.onError([successCallback, url](auto result) { + successCallback("No link info found", Link(Link::Url, url)); return true; }); diff --git a/src/providers/LinkResolver.hpp b/src/providers/LinkResolver.hpp index 919109aaf..919f0c3bf 100644 --- a/src/providers/LinkResolver.hpp +++ b/src/providers/LinkResolver.hpp @@ -3,13 +3,15 @@ #include #include +#include "messages/Link.hpp" + namespace chatterino { class LinkResolver { public: static void getLinkInfo(const QString url, - std::function callback); + std::function callback); private: }; diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 949b253c5..62a005601 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -280,11 +280,17 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) ->setLink(link); LinkResolver::getLinkInfo( - linkString, [linkMELowercase, linkMEOriginal](QString tooltipText) { + linkString, [linkMELowercase, linkMEOriginal, linkString] + (QString tooltipText, Link originalLink) { if (!tooltipText.isEmpty()) { linkMELowercase->setTooltip(tooltipText); linkMEOriginal->setTooltip(tooltipText); } + if (originalLink.value != linkString && + !originalLink.value.isEmpty()) { + linkMELowercase->setLink(originalLink)->updateLink(); + linkMEOriginal->setLink(originalLink)->updateLink(); + } }); } diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 2ec96101c..72bd0ce0b 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -104,6 +104,7 @@ public: /// Links BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false}; BoolSetting enableLinkInfoTooltip = {"/links/linkInfoTooltip", false}; + BoolSetting enableUnshortLinks = {"/links/unshortLinks", false}; BoolSetting enableLowercaseLink = {"/links/linkLowercase", true}; /// Ingored Users diff --git a/src/widgets/settingspages/FeelPage.cpp b/src/widgets/settingspages/FeelPage.cpp index b87a2d098..cab08ebc3 100644 --- a/src/widgets/settingspages/FeelPage.cpp +++ b/src/widgets/settingspages/FeelPage.cpp @@ -53,6 +53,9 @@ FeelPage::FeelPage() form->addRow("", this->createCheckBox("Show link info in tooltips", getSettings()->enableLinkInfoTooltip)); + form->addRow("", + this->createCheckBox("Auto unshort links (requires restart)", + getSettings()->enableUnshortLinks)); } layout->addSpacing(16);