Merge pull request #604 from apa420/dank-branch

Added functionality of /follow and /unfollow
This commit is contained in:
pajlada 2018-07-12 11:20:32 +02:00 committed by GitHub
commit 5757a6a761
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 123 additions and 2 deletions

View file

@ -231,7 +231,8 @@ SOURCES += \
src/util/InitUpdateButton.cpp \ src/util/InitUpdateButton.cpp \
src/widgets/dialogs/UpdateDialog.cpp \ src/widgets/dialogs/UpdateDialog.cpp \
src/widgets/settingspages/IgnoresPage.cpp \ src/widgets/settingspages/IgnoresPage.cpp \
src/providers/twitch/PubsubClient.cpp src/providers/twitch/PubsubClient.cpp \
src/providers/twitch/TwitchApi.cpp
HEADERS += \ HEADERS += \
src/Application.hpp \ src/Application.hpp \
@ -412,7 +413,8 @@ HEADERS += \
src/util/InitUpdateButton.hpp \ src/util/InitUpdateButton.hpp \
src/widgets/dialogs/UpdateDialog.hpp \ src/widgets/dialogs/UpdateDialog.hpp \
src/widgets/settingspages/IgnoresPage.hpp \ src/widgets/settingspages/IgnoresPage.hpp \
src/providers/twitch/PubsubClient.hpp src/providers/twitch/PubsubClient.hpp \
src/providers/twitch/TwitchApi.hpp
RESOURCES += \ RESOURCES += \
resources/resources.qrc \ resources/resources.qrc \

View file

@ -6,6 +6,8 @@
#include "controllers/commands/Command.hpp" #include "controllers/commands/Command.hpp"
#include "controllers/commands/CommandModel.hpp" #include "controllers/commands/CommandModel.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchApi.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.hpp" #include "providers/twitch/TwitchServer.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
@ -203,6 +205,56 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel,
channel->addMessage(Message::createSystemMessage(message)); 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 ""; return "";
} else if (commandName == "/logs") { } else if (commandName == "/logs") {
if (words.size() < 2) { if (words.size() < 2) {

View file

@ -0,0 +1,52 @@
#include "providers/twitch/twitchapi.hpp"
#include "common/NetworkRequest.hpp"
#include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QString>
namespace chatterino {
void TwitchApi::findUserId(const QString user, std::function<void(QString)> 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

View file

@ -0,0 +1,15 @@
#pragma once
#include <QString>
namespace chatterino {
class TwitchApi
{
public:
static void findUserId(const QString user, std::function<void(QString)> callback);
private:
};
} // namespace chatterino