From fe24d09009e039927860e94b0e3d3f10bf9d4474 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 5 Sep 2018 15:37:51 +0300 Subject: [PATCH 1/4] Replace url of link with new one from json. --- src/providers/LinkResolver.cpp | 3 ++- src/providers/twitch/TwitchMessageBuilder.cpp | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index d6917157d..1c2ac41ec 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -22,10 +22,11 @@ void LinkResolver::getLinkInfo(const QString url, QString response = QString(); if (statusCode == 200) { response = root.value("tooltip").toString(); + url = root.value("link").toString(); } else { response = root.value("message").toString(); } - successCallback(QUrl::fromPercentEncoding(response.toUtf8())); + successCallback(QUrl::fromPercentEncoding(response.toUtf8()), url); return Success; }); diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 949b253c5..fdb97e5e8 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -280,10 +280,13 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) ->setLink(link); LinkResolver::getLinkInfo( - linkString, [linkMELowercase, linkMEOriginal](QString tooltipText) { + linkString, [linkMELowercase, linkMEOriginal](QString tooltipText, + QString originalLink) { if (!tooltipText.isEmpty()) { - linkMELowercase->setTooltip(tooltipText); - linkMEOriginal->setTooltip(tooltipText); + linkMELowercase->setTooltip(tooltipText) + ->setLink(originalLink); + linkMEOriginal->setTooltip(tooltipText) + ->setLink(originalLink); } }); } From 2d1701cf86d966eb0743a4b0e5b00fc8f7e30bf5 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 6 Sep 2018 13:54:28 +0300 Subject: [PATCH 2/4] Fixed build. --- src/providers/LinkResolver.cpp | 14 ++++++++------ src/providers/LinkResolver.hpp | 4 +++- src/providers/twitch/TwitchMessageBuilder.cpp | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index 1c2ac41ec..c781b5db8 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -2,13 +2,14 @@ #include "common/Common.hpp" #include "common/NetworkRequest.hpp" +#include "messages/Link.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,23 +17,24 @@ 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(); - url = root.value("link").toString(); + linkString = root.value("link").toString(); } else { response = root.value("message").toString(); } - successCallback(QUrl::fromPercentEncoding(response.toUtf8()), url); + 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 fdb97e5e8..ac2194f4c 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -281,7 +281,7 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) LinkResolver::getLinkInfo( linkString, [linkMELowercase, linkMEOriginal](QString tooltipText, - QString originalLink) { + Link originalLink) { if (!tooltipText.isEmpty()) { linkMELowercase->setTooltip(tooltipText) ->setLink(originalLink); From 5f19a57f5a9bd16a05ec3d82a6d4d66336c1d12c Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 6 Sep 2018 17:11:25 +0300 Subject: [PATCH 3/4] Refresh links of new messages. As I understand links update when all message snapshots update, so we need to refresh url manually via signal. --- src/messages/MessageElement.cpp | 15 +++++++++++++++ src/messages/MessageElement.hpp | 3 +++ src/providers/twitch/TwitchMessageBuilder.cpp | 15 +++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) 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/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index ac2194f4c..62a005601 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -280,13 +280,16 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_) ->setLink(link); LinkResolver::getLinkInfo( - linkString, [linkMELowercase, linkMEOriginal](QString tooltipText, - Link originalLink) { + linkString, [linkMELowercase, linkMEOriginal, linkString] + (QString tooltipText, Link originalLink) { if (!tooltipText.isEmpty()) { - linkMELowercase->setTooltip(tooltipText) - ->setLink(originalLink); - linkMEOriginal->setTooltip(tooltipText) - ->setLink(originalLink); + linkMELowercase->setTooltip(tooltipText); + linkMEOriginal->setTooltip(tooltipText); + } + if (originalLink.value != linkString && + !originalLink.value.isEmpty()) { + linkMELowercase->setLink(originalLink)->updateLink(); + linkMEOriginal->setLink(originalLink)->updateLink(); } }); } From 0d8b5cad63376f560fb2904a80f172fed11f1f59 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 6 Sep 2018 17:59:49 +0300 Subject: [PATCH 4/4] Added setting to disable auto unshort links. --- src/providers/LinkResolver.cpp | 5 ++++- src/singletons/Settings.hpp | 1 + src/widgets/settingspages/FeelPage.cpp | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index c781b5db8..f48834ef3 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -3,6 +3,7 @@ #include "common/Common.hpp" #include "common/NetworkRequest.hpp" #include "messages/Link.hpp" +#include "singletons/Settings.hpp" #include @@ -24,7 +25,9 @@ void LinkResolver::getLinkInfo(const QString url, QString linkString = url; if (statusCode == 200) { response = root.value("tooltip").toString(); - linkString = root.value("link").toString(); + if (getSettings()->enableUnshortLinks) { + linkString = root.value("link").toString(); + } } else { response = root.value("message").toString(); } 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);