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..c85d6e143 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -6,6 +6,8 @@ #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 "singletons/Paths.hpp" @@ -203,6 +205,56 @@ 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 + " could not be followed!")); + 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 + " could not be followed!")); + 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..a1363a813 --- /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 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 false; + } + if (!users[0].isObject()) { + Log("API Error while getting user id, first user is not an object"); + successCallback(""); + return false; + } + 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 false; + } + successCallback(id.toString()); + return true; + }); + + request.execute(); +} + +} // namespace chatterino diff --git a/src/providers/twitch/TwitchApi.hpp b/src/providers/twitch/TwitchApi.hpp new file mode 100644 index 000000000..00b8ff4d7 --- /dev/null +++ b/src/providers/twitch/TwitchApi.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace chatterino { + +class TwitchApi +{ +public: + static void findUserId(const QString user, std::function callback); + +private: +}; + +} // namespace chatterino