Added functionality of /follow and /unfollow

This commit is contained in:
apa420 2018-07-12 03:47:37 +02:00
parent 352da60d07
commit c26e18c2bf
4 changed files with 109 additions and 2 deletions

View file

@ -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 \

View file

@ -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) {

View file

@ -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 <QString>
#include <QThread>
namespace chatterino {
void TwitchApi::FindUserId(const QString user, std::function<void(QString)> 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

View file

@ -0,0 +1,17 @@
#pragma once
#include "common/Singleton.hpp"
#include <QString>
namespace chatterino {
class TwitchApi
{
public:
static void FindUserId(const QString user, std::function<void(QString)> callback);
private:
};
} // namespace chatterino