From c26e18c2bf8adc0aa0c636aee25db0f652c545ce Mon Sep 17 00:00:00 2001 From: apa420 Date: Thu, 12 Jul 2018 03:47:37 +0200 Subject: [PATCH 1/4] Added functionality of /follow and /unfollow --- chatterino.pro | 6 ++- .../commands/CommandController.cpp | 51 +++++++++++++++++++ src/providers/twitch/twitchapi.cpp | 37 ++++++++++++++ src/providers/twitch/twitchapi.hpp | 17 +++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/providers/twitch/twitchapi.cpp create mode 100644 src/providers/twitch/twitchapi.hpp diff --git a/chatterino.pro b/chatterino.pro index 22554da5f..a7450cc82 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -231,7 +231,8 @@ SOURCES += \ src/util/InitUpdateButton.cpp \ src/widgets/dialogs/UpdateDialog.cpp \ src/widgets/settingspages/IgnoresPage.cpp \ - src/providers/twitch/PubsubClient.cpp + src/providers/twitch/PubsubClient.cpp \ + src/providers/twitch/TwitchApi.cpp HEADERS += \ src/Application.hpp \ @@ -412,7 +413,8 @@ HEADERS += \ src/util/InitUpdateButton.hpp \ src/widgets/dialogs/UpdateDialog.hpp \ src/widgets/settingspages/IgnoresPage.hpp \ - src/providers/twitch/PubsubClient.hpp + src/providers/twitch/PubsubClient.hpp \ + src/providers/twitch/TwitchApi.hpp RESOURCES += \ resources/resources.qrc \ diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 8d33391f9..9e570fd39 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -2,12 +2,15 @@ #include "Application.hpp" #include "common/SignalVector.hpp" +#include "common/UrlFetch.hpp" #include "controllers/accounts/AccountController.hpp" #include "controllers/commands/Command.hpp" #include "controllers/commands/CommandModel.hpp" #include "messages/MessageBuilder.hpp" +#include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchServer.hpp" +#include "providers/twitch/twitchapi.hpp" #include "singletons/Paths.hpp" #include "singletons/Settings.hpp" #include "widgets/dialogs/LogsPopup.hpp" @@ -203,6 +206,54 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel, channel->addMessage(Message::createSystemMessage(message)); }); + return ""; + } else if (commandName == "/follow" && words.size() >= 2) { + auto app = getApp(); + + auto user = app->accounts->twitch.getCurrent(); + auto target = words.at(1); + if (user->isAnon()) { + channel->addMessage( + Message::createSystemMessage("You must be logged in to follow someone")); + + return ""; + } + TwitchApi::FindUserId(target, [user, channel, target](QString userId) { + if (userId.isEmpty()) { + channel->addMessage( + Message::createSystemMessage("User " + target + " was not found!")); + return; + } + user->followUser(userId, [channel, target]() { + channel->addMessage( + Message::createSystemMessage("You successfully followed " + target)); + }); + }); + + return ""; + } else if (commandName == "/unfollow" && words.size() >= 2) { + auto app = getApp(); + + auto user = app->accounts->twitch.getCurrent(); + auto target = words.at(1); + if (user->isAnon()) { + channel->addMessage( + Message::createSystemMessage("You must be logged in to follow someone")); + return ""; + } + + TwitchApi::FindUserId(target, [user, channel, target](QString userId) { + if (userId.isEmpty()) { + channel->addMessage( + Message::createSystemMessage("User " + target + " was not found!")); + return; + } + user->unfollowUser(userId, [channel, target]() { + channel->addMessage( + Message::createSystemMessage("You successfully unfollowed " + target)); + }); + }); + return ""; } else if (commandName == "/logs") { if (words.size() < 2) { diff --git a/src/providers/twitch/twitchapi.cpp b/src/providers/twitch/twitchapi.cpp new file mode 100644 index 000000000..153778369 --- /dev/null +++ b/src/providers/twitch/twitchapi.cpp @@ -0,0 +1,37 @@ +#include "providers/twitch/twitchapi.hpp" + +#include "Application.hpp" +#include "common/UrlFetch.hpp" +#include "controllers/accounts/AccountController.hpp" +#include "providers/twitch/TwitchCommon.hpp" + +#include +#include + +namespace chatterino { + +void TwitchApi::FindUserId(const QString user, std::function callback) +{ + QString requestUrl("https://api.twitch.tv/kraken/users?login=" + user + + "&api_version=5&client_id=" + getDefaultClientID()); + NetworkRequest request(requestUrl); + request.setCaller(QThread::currentThread()); + request.setTimeout(30000); + request.onSuccess([callback](auto result) mutable { + QString userId; + + auto root = result.parseJson(); + if (root.value("users").toArray().isEmpty()) { + callback(""); + return true; + } + userId = root.value("users").toArray()[0].toObject().value("_id").toString(); + callback(userId); + return true; + }); + + request.execute(); + return; +} + +} // namespace chatterino diff --git a/src/providers/twitch/twitchapi.hpp b/src/providers/twitch/twitchapi.hpp new file mode 100644 index 000000000..9e38bd2cb --- /dev/null +++ b/src/providers/twitch/twitchapi.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "common/Singleton.hpp" + +#include + +namespace chatterino { + +class TwitchApi +{ +public: + static void FindUserId(const QString user, std::function callback); + +private: +}; + +} // namespace chatterino From 801779b87298560078193e0216cb73cca3de1262 Mon Sep 17 00:00:00 2001 From: apa420 Date: Thu, 12 Jul 2018 10:52:18 +0200 Subject: [PATCH 2/4] fixed stuff for PR --- .../commands/CommandController.cpp | 16 +++--- src/providers/twitch/TwitchApi.cpp | 52 +++++++++++++++++++ .../twitch/{twitchapi.hpp => TwitchApi.hpp} | 4 +- src/providers/twitch/twitchapi.cpp | 37 ------------- 4 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 src/providers/twitch/TwitchApi.cpp rename src/providers/twitch/{twitchapi.hpp => TwitchApi.hpp} (60%) delete mode 100644 src/providers/twitch/twitchapi.cpp diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 9e570fd39..d56e8ce6d 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -212,16 +212,17 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel, auto user = app->accounts->twitch.getCurrent(); auto target = words.at(1); + if (user->isAnon()) { channel->addMessage( Message::createSystemMessage("You must be logged in to follow someone")); - return ""; } - TwitchApi::FindUserId(target, [user, channel, target](QString userId) { + + TwitchApi::findUserId(target, [user, channel, target](QString userId) { if (userId.isEmpty()) { - channel->addMessage( - Message::createSystemMessage("User " + target + " was not found!")); + channel->addMessage(Message::createSystemMessage( + "User " + target + " could not be followed!")); return; } user->followUser(userId, [channel, target]() { @@ -236,16 +237,17 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel, auto user = app->accounts->twitch.getCurrent(); auto target = words.at(1); + if (user->isAnon()) { channel->addMessage( Message::createSystemMessage("You must be logged in to follow someone")); return ""; } - TwitchApi::FindUserId(target, [user, channel, target](QString userId) { + TwitchApi::findUserId(target, [user, channel, target](QString userId) { if (userId.isEmpty()) { - channel->addMessage( - Message::createSystemMessage("User " + target + " was not found!")); + channel->addMessage(Message::createSystemMessage( + "User " + target + " could not be followed!")); return; } user->unfollowUser(userId, [channel, target]() { diff --git a/src/providers/twitch/TwitchApi.cpp b/src/providers/twitch/TwitchApi.cpp new file mode 100644 index 000000000..06c3380fa --- /dev/null +++ b/src/providers/twitch/TwitchApi.cpp @@ -0,0 +1,52 @@ +#include "providers/twitch/twitchapi.hpp" + +#include "common/NetworkRequest.hpp" +#include "debug/Log.hpp" +#include "providers/twitch/TwitchCommon.hpp" + +#include + +namespace chatterino { + +void TwitchApi::findUserId(const QString user, std::function successCallback) +{ + QString requestUrl("https://api.twitch.tv/kraken/users?login=" + user); + + NetworkRequest request(requestUrl); + request.setCaller(QThread::currentThread()); + request.makeAuthorizedV5(getDefaultClientID()); + request.setTimeout(30000); + request.onSuccess([successCallback](auto result) mutable { + auto root = result.parseJson(); + if (!root.value("users").isArray()) { + Log("API Error while getting user id, users is not an array"); + successCallback(""); + return true; + } + auto users = root.value("users").toArray(); + if (users.size() != 1) { + Log("API Error while getting user id, users array size is not 1"); + successCallback(""); + return true; + } + if (!users[0].isObject()) { + Log("API Error while getting user id, first user is not an object"); + successCallback(""); + return true; + } + auto firstUser = users[0].toObject(); + auto id = firstUser.value("_id"); + if (!id.isString()) { + Log("API Error: while getting user id, first user object `_id` key is not a " + "string"); + successCallback(""); + return true; + } + successCallback(id.toString()); + return true; + }); + + request.execute(); +} + +} // namespace chatterino diff --git a/src/providers/twitch/twitchapi.hpp b/src/providers/twitch/TwitchApi.hpp similarity index 60% rename from src/providers/twitch/twitchapi.hpp rename to src/providers/twitch/TwitchApi.hpp index 9e38bd2cb..00b8ff4d7 100644 --- a/src/providers/twitch/twitchapi.hpp +++ b/src/providers/twitch/TwitchApi.hpp @@ -1,7 +1,5 @@ #pragma once -#include "common/Singleton.hpp" - #include namespace chatterino { @@ -9,7 +7,7 @@ namespace chatterino { class TwitchApi { public: - static void FindUserId(const QString user, std::function callback); + static void findUserId(const QString user, std::function callback); private: }; diff --git a/src/providers/twitch/twitchapi.cpp b/src/providers/twitch/twitchapi.cpp deleted file mode 100644 index 153778369..000000000 --- a/src/providers/twitch/twitchapi.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "providers/twitch/twitchapi.hpp" - -#include "Application.hpp" -#include "common/UrlFetch.hpp" -#include "controllers/accounts/AccountController.hpp" -#include "providers/twitch/TwitchCommon.hpp" - -#include -#include - -namespace chatterino { - -void TwitchApi::FindUserId(const QString user, std::function callback) -{ - QString requestUrl("https://api.twitch.tv/kraken/users?login=" + user + - "&api_version=5&client_id=" + getDefaultClientID()); - NetworkRequest request(requestUrl); - request.setCaller(QThread::currentThread()); - request.setTimeout(30000); - request.onSuccess([callback](auto result) mutable { - QString userId; - - auto root = result.parseJson(); - if (root.value("users").toArray().isEmpty()) { - callback(""); - return true; - } - userId = root.value("users").toArray()[0].toObject().value("_id").toString(); - callback(userId); - return true; - }); - - request.execute(); - return; -} - -} // namespace chatterino From 7288c5a0b63863cabcaaed9493b3fc38c247fe4e Mon Sep 17 00:00:00 2001 From: apa420 Date: Thu, 12 Jul 2018 10:58:29 +0200 Subject: [PATCH 3/4] removed unecessary #include --- src/controllers/commands/CommandController.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index d56e8ce6d..c85d6e143 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -2,15 +2,14 @@ #include "Application.hpp" #include "common/SignalVector.hpp" -#include "common/UrlFetch.hpp" #include "controllers/accounts/AccountController.hpp" #include "controllers/commands/Command.hpp" #include "controllers/commands/CommandModel.hpp" #include "messages/MessageBuilder.hpp" #include "providers/twitch/TwitchAccount.hpp" +#include "providers/twitch/TwitchApi.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchServer.hpp" -#include "providers/twitch/twitchapi.hpp" #include "singletons/Paths.hpp" #include "singletons/Settings.hpp" #include "widgets/dialogs/LogsPopup.hpp" From 1d2e56f19df6992db35b8115a5be3f3333d7378a Mon Sep 17 00:00:00 2001 From: apa420 Date: Thu, 12 Jul 2018 11:11:21 +0200 Subject: [PATCH 4/4] changed return-value of errors to false --- src/providers/twitch/TwitchApi.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/providers/twitch/TwitchApi.cpp b/src/providers/twitch/TwitchApi.cpp index 06c3380fa..a1363a813 100644 --- a/src/providers/twitch/TwitchApi.cpp +++ b/src/providers/twitch/TwitchApi.cpp @@ -21,18 +21,18 @@ void TwitchApi::findUserId(const QString user, std::function succ if (!root.value("users").isArray()) { Log("API Error while getting user id, users is not an array"); successCallback(""); - return true; + return false; } auto users = root.value("users").toArray(); if (users.size() != 1) { Log("API Error while getting user id, users array size is not 1"); successCallback(""); - return true; + return false; } if (!users[0].isObject()) { Log("API Error while getting user id, first user is not an object"); successCallback(""); - return true; + return false; } auto firstUser = users[0].toObject(); auto id = firstUser.value("_id"); @@ -40,7 +40,7 @@ void TwitchApi::findUserId(const QString user, std::function succ Log("API Error: while getting user id, first user object `_id` key is not a " "string"); successCallback(""); - return true; + return false; } successCallback(id.toString()); return true;