From 8ba8bbeef6d08a105f70322b2cfb543450c93a32 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 26 May 2019 21:44:13 +0200 Subject: [PATCH] Move environment variable parsing and storage to its own class Make the Link resolver and Twitch emotes set resolver urls modifiable with environment variables --- chatterino.pro | 1 + docs/ENV.md | 18 +++++++++++++++--- src/providers/LinkResolver.cpp | 6 +++--- src/providers/twitch/TwitchAccount.cpp | 5 ++--- src/providers/twitch/TwitchChannel.cpp | 16 +++------------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/chatterino.pro b/chatterino.pro index 42df60d06..41c875862 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -55,6 +55,7 @@ SOURCES += \ src/common/Channel.cpp \ src/common/CompletionModel.cpp \ src/common/DownloadManager.cpp \ + src/common/Env.cpp \ src/common/LinkParser.cpp \ src/common/NetworkData.cpp \ src/common/NetworkManager.cpp \ diff --git a/docs/ENV.md b/docs/ENV.md index 8b84946c3..586aac354 100644 --- a/docs/ENV.md +++ b/docs/ENV.md @@ -1,8 +1,20 @@ # Environment variables Below I have tried to list all environment variables that can be used to modify the behaviour of Chatterino. Used for things that I don't feel like fit in the settings system. -## CHATTERINO2_RECENT_MESSAGES_URL +### CHATTERINO2_RECENT_MESSAGES_URL Used to change the URL that Chatterino2 uses when trying to load historic Twitch chat messages (if the setting is enabled). -Default value: `"https://recent-messages.robotty.de/api/v2/recent-messages/%1?clearchatToNotice=true"` +Default value: `https://recent-messages.robotty.de/api/v2/recent-messages/%1?clearchatToNotice=true` Arguments: -1) `%1` = Name of the Twitch channel + - `%1` = Name of the Twitch channel + +### CHATTERINO2_LINK_RESOLVER_URL +Used to change the URL that Chatterino2 uses when trying to get link information to display in the tooltip on hover. +Default value: `https://braize.pajlada.com/chatterino/link_resolver/%1` +Arguments: + - `%1` = Escaped URL the link resolver should resolve + +### CHATTERINO2_TWITCH_EMOTE_SET_RESOLVER_URL +Used to change the URL that Chatterino2 uses when trying to get emote set information +Default value: `https://braize.pajlada.com/chatterino/twitchemotes/set/%1/` +Arguments: + - `%1` = Emote set ID diff --git a/src/providers/LinkResolver.cpp b/src/providers/LinkResolver.cpp index 7446af98d..0137a130c 100644 --- a/src/providers/LinkResolver.cpp +++ b/src/providers/LinkResolver.cpp @@ -1,6 +1,7 @@ #include "providers/LinkResolver.hpp" #include "common/Common.hpp" +#include "common/Env.hpp" #include "common/NetworkRequest.hpp" #include "messages/Link.hpp" #include "singletons/Settings.hpp" @@ -17,11 +18,10 @@ void LinkResolver::getLinkInfo( successCallback("No link info loaded", Link(Link::Url, url)); return; } - QString requestUrl("https://braize.pajlada.com/chatterino/link_resolver/" + - QUrl::toPercentEncoding(url, "", "/:")); // Uncomment to test crashes // QTimer::singleShot(3000, [=]() { - NetworkRequest request(requestUrl); + NetworkRequest request(Env::get().linkResolverUrl.arg( + QString::fromUtf8(QUrl::toPercentEncoding(url, "", "/:")))); request.setCaller(QThread::currentThread()); request.setTimeout(30000); request.onSuccess([successCallback, url](auto result) mutable -> Outcome { diff --git a/src/providers/twitch/TwitchAccount.cpp b/src/providers/twitch/TwitchAccount.cpp index 1d798f8ce..c397a2248 100644 --- a/src/providers/twitch/TwitchAccount.cpp +++ b/src/providers/twitch/TwitchAccount.cpp @@ -3,6 +3,7 @@ #include #include "Application.hpp" +#include "common/Env.hpp" #include "common/NetworkRequest.hpp" #include "common/Outcome.hpp" #include "debug/Log.hpp" @@ -534,9 +535,7 @@ void TwitchAccount::loadEmoteSetData(std::shared_ptr emoteSet) return; } - NetworkRequest req( - "https://braize.pajlada.com/chatterino/twitchemotes/set/" + - emoteSet->key + "/"); + NetworkRequest req(Env::get().twitchEmoteSetResolverUrl.arg(emoteSet->key)); req.setUseQuickLoadCache(true); req.onError([](int errorCode) -> bool { diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index 4eb494ae6..b8ce6509b 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -2,6 +2,7 @@ #include "Application.hpp" #include "common/Common.hpp" +#include "common/Env.hpp" #include "common/NetworkRequest.hpp" #include "controllers/accounts/AccountController.hpp" #include "controllers/notifications/NotificationController.hpp" @@ -607,19 +608,8 @@ void TwitchChannel::loadRecentMessages() return; } - static QString genericURL = [] { - QString url("https://recent-messages.robotty.de/api/v2/recent-messages/" - "%1?clearchatToNotice=true"); - auto envString = std::getenv("CHATTERINO2_RECENT_MESSAGES_URL"); - if (envString != nullptr) - { - url = envString; - } - - return url; - }(); - - NetworkRequest request(genericURL.arg(this->getName())); + NetworkRequest request( + Env::get().recentMessagesApiUrl.arg(this->getName())); request.setCaller(QThread::currentThread()); // can't be concurrent right now due to SignalVector // request.setExecuteConcurrently(true);