mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
fixed stuff for PR
This commit is contained in:
parent
c26e18c2bf
commit
801779b872
4 changed files with 62 additions and 47 deletions
|
@ -212,16 +212,17 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel,
|
||||||
|
|
||||||
auto user = app->accounts->twitch.getCurrent();
|
auto user = app->accounts->twitch.getCurrent();
|
||||||
auto target = words.at(1);
|
auto target = words.at(1);
|
||||||
|
|
||||||
if (user->isAnon()) {
|
if (user->isAnon()) {
|
||||||
channel->addMessage(
|
channel->addMessage(
|
||||||
Message::createSystemMessage("You must be logged in to follow someone"));
|
Message::createSystemMessage("You must be logged in to follow someone"));
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
TwitchApi::FindUserId(target, [user, channel, target](QString userId) {
|
|
||||||
|
TwitchApi::findUserId(target, [user, channel, target](QString userId) {
|
||||||
if (userId.isEmpty()) {
|
if (userId.isEmpty()) {
|
||||||
channel->addMessage(
|
channel->addMessage(Message::createSystemMessage(
|
||||||
Message::createSystemMessage("User " + target + " was not found!"));
|
"User " + target + " could not be followed!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user->followUser(userId, [channel, target]() {
|
user->followUser(userId, [channel, target]() {
|
||||||
|
@ -236,16 +237,17 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel,
|
||||||
|
|
||||||
auto user = app->accounts->twitch.getCurrent();
|
auto user = app->accounts->twitch.getCurrent();
|
||||||
auto target = words.at(1);
|
auto target = words.at(1);
|
||||||
|
|
||||||
if (user->isAnon()) {
|
if (user->isAnon()) {
|
||||||
channel->addMessage(
|
channel->addMessage(
|
||||||
Message::createSystemMessage("You must be logged in to follow someone"));
|
Message::createSystemMessage("You must be logged in to follow someone"));
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
TwitchApi::FindUserId(target, [user, channel, target](QString userId) {
|
TwitchApi::findUserId(target, [user, channel, target](QString userId) {
|
||||||
if (userId.isEmpty()) {
|
if (userId.isEmpty()) {
|
||||||
channel->addMessage(
|
channel->addMessage(Message::createSystemMessage(
|
||||||
Message::createSystemMessage("User " + target + " was not found!"));
|
"User " + target + " could not be followed!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user->unfollowUser(userId, [channel, target]() {
|
user->unfollowUser(userId, [channel, target]() {
|
||||||
|
|
52
src/providers/twitch/TwitchApi.cpp
Normal file
52
src/providers/twitch/TwitchApi.cpp
Normal 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 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
|
|
@ -1,7 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "common/Singleton.hpp"
|
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
|
@ -9,7 +7,7 @@ namespace chatterino {
|
||||||
class TwitchApi
|
class TwitchApi
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void FindUserId(const QString user, std::function<void(QString)> callback);
|
static void findUserId(const QString user, std::function<void(QString)> callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
|
@ -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 <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
|
|
Loading…
Reference in a new issue