Move environment variable parsing and storage to its own class

Make the Link resolver and Twitch emotes set resolver urls modifiable with
environment variables
This commit is contained in:
Rasmus Karlsson 2019-05-26 21:44:13 +02:00
parent acf69139b9
commit 8ba8bbeef6
5 changed files with 24 additions and 22 deletions

View file

@ -55,6 +55,7 @@ SOURCES += \
src/common/Channel.cpp \ src/common/Channel.cpp \
src/common/CompletionModel.cpp \ src/common/CompletionModel.cpp \
src/common/DownloadManager.cpp \ src/common/DownloadManager.cpp \
src/common/Env.cpp \
src/common/LinkParser.cpp \ src/common/LinkParser.cpp \
src/common/NetworkData.cpp \ src/common/NetworkData.cpp \
src/common/NetworkManager.cpp \ src/common/NetworkManager.cpp \

View file

@ -1,8 +1,20 @@
# Environment variables # 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. 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). 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: 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

View file

@ -1,6 +1,7 @@
#include "providers/LinkResolver.hpp" #include "providers/LinkResolver.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include "common/Env.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "messages/Link.hpp" #include "messages/Link.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
@ -17,11 +18,10 @@ void LinkResolver::getLinkInfo(
successCallback("No link info loaded", Link(Link::Url, url)); successCallback("No link info loaded", Link(Link::Url, url));
return; return;
} }
QString requestUrl("https://braize.pajlada.com/chatterino/link_resolver/" +
QUrl::toPercentEncoding(url, "", "/:"));
// Uncomment to test crashes // Uncomment to test crashes
// QTimer::singleShot(3000, [=]() { // QTimer::singleShot(3000, [=]() {
NetworkRequest request(requestUrl); NetworkRequest request(Env::get().linkResolverUrl.arg(
QString::fromUtf8(QUrl::toPercentEncoding(url, "", "/:"))));
request.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
request.setTimeout(30000); request.setTimeout(30000);
request.onSuccess([successCallback, url](auto result) mutable -> Outcome { request.onSuccess([successCallback, url](auto result) mutable -> Outcome {

View file

@ -3,6 +3,7 @@
#include <QThread> #include <QThread>
#include "Application.hpp" #include "Application.hpp"
#include "common/Env.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp" #include "common/Outcome.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
@ -534,9 +535,7 @@ void TwitchAccount::loadEmoteSetData(std::shared_ptr<EmoteSet> emoteSet)
return; return;
} }
NetworkRequest req( NetworkRequest req(Env::get().twitchEmoteSetResolverUrl.arg(emoteSet->key));
"https://braize.pajlada.com/chatterino/twitchemotes/set/" +
emoteSet->key + "/");
req.setUseQuickLoadCache(true); req.setUseQuickLoadCache(true);
req.onError([](int errorCode) -> bool { req.onError([](int errorCode) -> bool {

View file

@ -2,6 +2,7 @@
#include "Application.hpp" #include "Application.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include "common/Env.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
#include "controllers/notifications/NotificationController.hpp" #include "controllers/notifications/NotificationController.hpp"
@ -607,19 +608,8 @@ void TwitchChannel::loadRecentMessages()
return; return;
} }
static QString genericURL = [] { NetworkRequest request(
QString url("https://recent-messages.robotty.de/api/v2/recent-messages/" Env::get().recentMessagesApiUrl.arg(this->getName()));
"%1?clearchatToNotice=true");
auto envString = std::getenv("CHATTERINO2_RECENT_MESSAGES_URL");
if (envString != nullptr)
{
url = envString;
}
return url;
}();
NetworkRequest request(genericURL.arg(this->getName()));
request.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
// can't be concurrent right now due to SignalVector // can't be concurrent right now due to SignalVector
// request.setExecuteConcurrently(true); // request.setExecuteConcurrently(true);