Skip to content

Commit

Permalink
refactor: Remove Outcome from network requests (Chatterino#4959)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz authored Nov 12, 2023
1 parent 95620e6 commit 6faf63c
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 228 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926)
- Dev: Refactor `IrcMessageHandler`, removing a bunch of clang-tidy warnings & changing its public API. (#4927)
- Dev: `Details` file properties tab is now populated on Windows. (#4912)
- Dev: Removed `Outcome` from network requests. (#4959)

## 2.4.6

Expand Down
3 changes: 1 addition & 2 deletions src/common/NetworkCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class QNetworkReply;

namespace chatterino {

class Outcome;
class NetworkResult;

using NetworkSuccessCallback = std::function<Outcome(NetworkResult)>;
using NetworkSuccessCallback = std::function<void(NetworkResult)>;
using NetworkErrorCallback = std::function<void(NetworkResult)>;
using NetworkReplyCreatedCallback = std::function<void(QNetworkReply *)>;
using NetworkFinallyCallback = std::function<void()>;
Expand Down
1 change: 0 additions & 1 deletion src/common/NetworkPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "common/NetworkManager.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "singletons/Paths.hpp"
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/notifications/NotificationController.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "controllers/notifications/NotificationController.hpp"

#include "Application.hpp"
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "controllers/notifications/NotificationModel.hpp"
#include "controllers/sound/SoundController.hpp"
Expand Down
15 changes: 6 additions & 9 deletions src/messages/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "debug/Benchmark.hpp"
Expand Down Expand Up @@ -502,11 +501,11 @@ void Image::actuallyLoad()
NetworkRequest(this->url().string)
.concurrent()
.cache()
.onSuccess([weak](auto result) -> Outcome {
.onSuccess([weak](auto result) {
auto shared = weak.lock();
if (!shared)
{
return Failure;
return;
}

auto data = result.getData();
Expand All @@ -521,14 +520,14 @@ void Image::actuallyLoad()
qCDebug(chatterinoImage)
<< "Error: image cant be read " << shared->url().string;
shared->empty_ = true;
return Failure;
return;
}

const auto size = reader.size();
if (size.isEmpty())
{
shared->empty_ = true;
return Failure;
return;
}

// returns 1 for non-animated formats
Expand All @@ -538,7 +537,7 @@ void Image::actuallyLoad()
<< "Error: image has less than 1 frame "
<< shared->url().string << ": " << reader.errorString();
shared->empty_ = true;
return Failure;
return;
}

// use "double" to prevent int overflows
Expand All @@ -549,7 +548,7 @@ void Image::actuallyLoad()
qCDebug(chatterinoImage) << "image too large in RAM";

shared->empty_ = true;
return Failure;
return;
}

auto parsed = detail::readFrames(reader, shared->url());
Expand All @@ -562,8 +561,6 @@ void Image::actuallyLoad()
std::forward<decltype(frames)>(frames));
}
}));

return Success;
})
.onError([weak](auto /*result*/) {
auto shared = weak.lock();
Expand Down
9 changes: 2 additions & 7 deletions src/providers/IvrApi.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "IvrApi.hpp"

#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"

#include <QUrlQuery>
Expand All @@ -18,12 +17,10 @@ void IvrApi::getSubage(QString userName, QString channelName,

this->makeRequest(
QString("twitch/subage/%1/%2").arg(userName).arg(channelName), {})
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
.onSuccess([successCallback, failureCallback](auto result) {
auto root = result.parseJson();

successCallback(root);

return Success;
})
.onError([failureCallback](auto result) {
qCWarning(chatterinoIvr)
Expand All @@ -42,12 +39,10 @@ void IvrApi::getBulkEmoteSets(QString emoteSetList,
urlQuery.addQueryItem("set_id", emoteSetList);

this->makeRequest("twitch/emotes/sets", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
.onSuccess([successCallback, failureCallback](auto result) {
auto root = result.parseJsonArray();

successCallback(root);

return Success;
})
.onError([failureCallback](auto result) {
qCWarning(chatterinoIvr)
Expand Down
6 changes: 1 addition & 5 deletions src/providers/LinkResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "common/Env.hpp"
#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "messages/Image.hpp"
#include "messages/Link.hpp"
#include "singletons/Settings.hpp"
Expand All @@ -27,8 +26,7 @@ void LinkResolver::getLinkInfo(
QUrl::toPercentEncoding(url, "", "/:"))))
.caller(caller)
.timeout(30000)
.onSuccess([successCallback,
url](NetworkResult result) mutable -> Outcome {
.onSuccess([successCallback, url](NetworkResult result) mutable {
auto root = result.parseJson();
auto statusCode = root.value("status").toInt();
QString response;
Expand All @@ -54,8 +52,6 @@ void LinkResolver::getLinkInfo(
}
successCallback(QUrl::fromPercentEncoding(response.toUtf8()),
Link(Link::Url, linkString), thumbnail);

return Success;
})
.onError([successCallback, url](auto /*result*/) {
successCallback("No link info found", Link(Link::Url, url),
Expand Down
7 changes: 3 additions & 4 deletions src/providers/bttv/BttvEmotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
Expand Down Expand Up @@ -202,15 +203,14 @@ void BttvEmotes::loadEmotes()

NetworkRequest(QString(globalEmoteApiUrl))
.timeout(30000)
.onSuccess([this](auto result) -> Outcome {
.onSuccess([this](auto result) {
auto emotes = this->global_.get();
auto pair = parseGlobalEmotes(result.parseJsonArray(), *emotes);
if (pair.first)
{
this->setEmotes(
std::make_shared<EmoteMap>(std::move(pair.second)));
}
return pair.first;
})
.execute();
}
Expand All @@ -229,7 +229,7 @@ void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
NetworkRequest(QString(bttvChannelEmoteApiUrl) + channelId)
.timeout(20000)
.onSuccess([callback = std::move(callback), channel, channelDisplayName,
manualRefresh](auto result) -> Outcome {
manualRefresh](auto result) {
auto pair =
parseChannelEmotes(result.parseJson(), channelDisplayName);
bool hasEmotes = false;
Expand All @@ -251,7 +251,6 @@ void BttvEmotes::loadChannel(std::weak_ptr<Channel> channel,
makeSystemMessage(CHANNEL_HAS_NO_EMOTES));
}
}
return pair.first;
})
.onError([channelId, channel, manualRefresh](auto result) {
auto shared = channel.lock();
Expand Down
5 changes: 1 addition & 4 deletions src/providers/chatterino/ChatterinoBadges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "messages/Emote.hpp"

#include <QJsonArray>
Expand Down Expand Up @@ -39,7 +38,7 @@ void ChatterinoBadges::loadChatterinoBadges()

NetworkRequest(url)
.concurrent()
.onSuccess([this](auto result) -> Outcome {
.onSuccess([this](auto result) {
auto jsonRoot = result.parseJson();

std::unique_lock lock(this->mutex_);
Expand All @@ -64,8 +63,6 @@ void ChatterinoBadges::loadChatterinoBadges()
}
++index;
}

return Success;
})
.execute();
}
Expand Down
5 changes: 1 addition & 4 deletions src/providers/ffz/FfzBadges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "messages/Emote.hpp"
#include "providers/ffz/FfzUtil.hpp"

Expand Down Expand Up @@ -59,7 +58,7 @@ void FfzBadges::load()
static QUrl url("https://api.frankerfacez.com/v1/badges/ids");

NetworkRequest(url)
.onSuccess([this](auto result) -> Outcome {
.onSuccess([this](auto result) {
std::unique_lock lock(this->mutex_);

auto jsonRoot = result.parseJson();
Expand Down Expand Up @@ -103,8 +102,6 @@ void FfzBadges::load()
}
}
}

return Success;
})
.execute();
}
Expand Down
9 changes: 2 additions & 7 deletions src/providers/ffz/FfzEmotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
Expand Down Expand Up @@ -197,11 +196,9 @@ void FfzEmotes::loadEmotes()
NetworkRequest(url)

.timeout(30000)
.onSuccess([this](auto result) -> Outcome {
.onSuccess([this](auto result) {
auto parsedSet = parseGlobalEmotes(result.parseJson());
this->setEmotes(std::make_shared<EmoteMap>(std::move(parsedSet)));

return Success;
})
.execute();
}
Expand All @@ -227,7 +224,7 @@ void FfzEmotes::loadChannel(
.onSuccess([emoteCallback = std::move(emoteCallback),
modBadgeCallback = std::move(modBadgeCallback),
vipBadgeCallback = std::move(vipBadgeCallback), channel,
manualRefresh](const auto &result) -> Outcome {
manualRefresh](const auto &result) {
const auto json = result.parseJson();

auto emoteMap = parseChannelEmotes(json);
Expand All @@ -254,8 +251,6 @@ void FfzEmotes::loadChannel(
makeSystemMessage(CHANNEL_HAS_NO_EMOTES));
}
}

return Success;
})
.onError([channelID, channel, manualRefresh](const auto &result) {
auto shared = channel.lock();
Expand Down
6 changes: 2 additions & 4 deletions src/providers/recentmessages/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ void load(const QString &channelName, std::weak_ptr<Channel> channelPtr,
const auto url = constructRecentMessagesUrl(channelName);

NetworkRequest(url)
.onSuccess([channelPtr, onLoaded](const auto &result) -> Outcome {
.onSuccess([channelPtr, onLoaded](const auto &result) {
auto shared = channelPtr.lock();
if (!shared)
{
return Failure;
return;
}

qCDebug(LOG) << "Successfully loaded recent messages for"
Expand Down Expand Up @@ -65,8 +65,6 @@ void load(const QString &channelName, std::weak_ptr<Channel> channelPtr,

onLoaded(messages);
});

return Success;
})
.onError([channelPtr, onError](const NetworkResult &result) {
auto shared = channelPtr.lock();
Expand Down
26 changes: 11 additions & 15 deletions src/providers/seventv/SeventvAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "common/Literals.hpp"
#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"

namespace {

Expand All @@ -24,12 +23,11 @@ void SeventvAPI::getUserByTwitchID(
{
NetworkRequest(API_URL_USER.arg(twitchID), NetworkRequestType::Get)
.timeout(20000)
.onSuccess([callback = std::move(onSuccess)](
const NetworkResult &result) -> Outcome {
auto json = result.parseJson();
callback(json);
return Success;
})
.onSuccess(
[callback = std::move(onSuccess)](const NetworkResult &result) {
auto json = result.parseJson();
callback(json);
})
.onError([callback = std::move(onError)](const NetworkResult &result) {
callback(result);
})
Expand All @@ -42,12 +40,11 @@ void SeventvAPI::getEmoteSet(const QString &emoteSet,
{
NetworkRequest(API_URL_EMOTE_SET.arg(emoteSet), NetworkRequestType::Get)
.timeout(25000)
.onSuccess([callback = std::move(onSuccess)](
const NetworkResult &result) -> Outcome {
auto json = result.parseJson();
callback(json);
return Success;
})
.onSuccess(
[callback = std::move(onSuccess)](const NetworkResult &result) {
auto json = result.parseJson();
callback(json);
})
.onError([callback = std::move(onError)](const NetworkResult &result) {
callback(result);
})
Expand All @@ -72,9 +69,8 @@ void SeventvAPI::updatePresence(const QString &twitchChannelID,
NetworkRequestType::Post)
.json(payload)
.timeout(10000)
.onSuccess([callback = std::move(onSuccess)](const auto &) -> Outcome {
.onSuccess([callback = std::move(onSuccess)](const auto &) {
callback();
return Success;
})
.onError([callback = std::move(onError)](const NetworkResult &result) {
callback(result);
Expand Down
2 changes: 0 additions & 2 deletions src/providers/twitch/TwitchAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "common/Channel.hpp"
#include "common/Env.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "debug/AssertInGuiThread.hpp"
Expand Down Expand Up @@ -500,7 +499,6 @@ void TwitchAccount::loadSeventvUserID()
{
this->seventvUserID_ = id;
}
return Success;
},
[](const auto &result) {
qCDebug(chatterinoSeventv)
Expand Down
Loading

0 comments on commit 6faf63c

Please sign in to comment.