refactoring
This commit is contained in:
+127
-130
@@ -1,73 +1,89 @@
|
||||
#include "ircmanager.h"
|
||||
#include "asyncexec.h"
|
||||
#include "channel.h"
|
||||
#include "channels.h"
|
||||
#include "channelmanager.h"
|
||||
#include "messages/messageparseargs.h"
|
||||
#include "twitch/twitchmessagebuilder.h"
|
||||
#include "twitch/twitchparsemessage.h"
|
||||
#include "twitch/twitchuser.h"
|
||||
#include "usermanager.h"
|
||||
|
||||
#include <irccommand.h>
|
||||
#include <ircconnection.h>
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
#include <future>
|
||||
|
||||
using namespace chatterino::messages;
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
Account *IrcManager::account = nullptr;
|
||||
std::shared_ptr<IrcConnection> IrcManager::connection;
|
||||
QMutex IrcManager::connectionMutex;
|
||||
long IrcManager::connectionGeneration = 0;
|
||||
const QString IrcManager::defaultClientId = "7ue61iz46fz11y3cugd0l3tawb4taal";
|
||||
QNetworkAccessManager IrcManager::accessManager;
|
||||
IrcManager IrcManager::instance;
|
||||
|
||||
QMap<QString, bool> IrcManager::twitchBlockedUsers;
|
||||
QMutex IrcManager::twitchBlockedUsersMutex;
|
||||
const QString IrcManager::defaultClientId("7ue61iz46fz11y3cugd0l3tawb4taal");
|
||||
|
||||
IrcManager::IrcManager()
|
||||
: _account(AccountManager::getInstance().getAnon())
|
||||
, _connection()
|
||||
, _connectionMutex()
|
||||
, _connectionGeneration(0)
|
||||
, _twitchBlockedUsers()
|
||||
, _twitchBlockedUsersMutex()
|
||||
, _accessManager()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::connect()
|
||||
const twitch::TwitchUser &IrcManager::getUser() const
|
||||
{
|
||||
return _account;
|
||||
}
|
||||
|
||||
void IrcManager::setUser(const twitch::TwitchUser &account)
|
||||
{
|
||||
_account = account;
|
||||
}
|
||||
|
||||
void IrcManager::connect()
|
||||
{
|
||||
disconnect();
|
||||
|
||||
async_exec([] { beginConnecting(); });
|
||||
async_exec([this] { beginConnecting(); });
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::beginConnecting()
|
||||
void IrcManager::beginConnecting()
|
||||
{
|
||||
IrcManager::account = const_cast<Account *>(Account::getAnon());
|
||||
int generation = ++IrcManager::_connectionGeneration;
|
||||
|
||||
int generation = ++IrcManager::connectionGeneration;
|
||||
Communi::IrcConnection *c = new Communi::IrcConnection;
|
||||
|
||||
auto c = new IrcConnection();
|
||||
QObject::connect(c, &Communi::IrcConnection::messageReceived, this,
|
||||
&IrcManager::messageReceived);
|
||||
QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this,
|
||||
&IrcManager::privateMessageReceived);
|
||||
|
||||
QObject::connect(c, &IrcConnection::messageReceived, &messageReceived);
|
||||
QObject::connect(c, &IrcConnection::privateMessageReceived,
|
||||
&privateMessageReceived);
|
||||
|
||||
if (account->isAnon()) {
|
||||
if (_account.isAnon()) {
|
||||
// fetch ignored users
|
||||
QString username = account->getUsername();
|
||||
QString oauthClient = account->getOauthClient();
|
||||
QString oauthToken = account->getOauthToken();
|
||||
QString username = _account.getUserName();
|
||||
QString oauthClient = _account.getOAuthClient();
|
||||
QString oauthToken = _account.getOAuthToken();
|
||||
|
||||
{
|
||||
QString nextLink = "https://api.twitch.tv/kraken/users/" +
|
||||
username + "/blocks?limit=" + 100 +
|
||||
"&client_id=" + oauthClient;
|
||||
QString nextLink = "https://api.twitch.tv/kraken/users/" + username +
|
||||
"/blocks?limit=" + 100 + "&client_id=" + oauthClient;
|
||||
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
||||
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
|
||||
QNetworkReply *reply = manager->get(req);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
IrcManager::twitchBlockedUsersMutex.lock();
|
||||
IrcManager::twitchBlockedUsers.clear();
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
_twitchBlockedUsersMutex.lock();
|
||||
_twitchBlockedUsers.clear();
|
||||
_twitchBlockedUsersMutex.unlock();
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||
@@ -78,15 +94,13 @@ IrcManager::beginConnecting()
|
||||
|
||||
auto blocks = root.value("blocks").toArray();
|
||||
|
||||
IrcManager::twitchBlockedUsersMutex.lock();
|
||||
_twitchBlockedUsersMutex.lock();
|
||||
for (QJsonValue block : blocks) {
|
||||
QJsonObject user =
|
||||
block.toObject().value("user").toObject();
|
||||
QJsonObject user = block.toObject().value("user").toObject();
|
||||
// display_name
|
||||
IrcManager::twitchBlockedUsers.insert(
|
||||
user.value("name").toString().toLower(), true);
|
||||
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
||||
}
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
_twitchBlockedUsersMutex.unlock();
|
||||
|
||||
manager->deleteLater();
|
||||
});
|
||||
@@ -94,10 +108,10 @@ IrcManager::beginConnecting()
|
||||
|
||||
// fetch available twitch emtoes
|
||||
{
|
||||
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" +
|
||||
username + "/emotes?oauth_token=" +
|
||||
oauthToken + "&client_id=" + oauthClient));
|
||||
QNetworkReply *reply = IrcManager::accessManager.get(req);
|
||||
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username +
|
||||
"/emotes?oauth_token=" + oauthToken +
|
||||
"&client_id=" + oauthClient));
|
||||
QNetworkReply *reply = _accessManager.get(req);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
QByteArray data = reply->readAll();
|
||||
@@ -109,15 +123,13 @@ IrcManager::beginConnecting()
|
||||
|
||||
auto blocks = root.value("blocks").toArray();
|
||||
|
||||
IrcManager::twitchBlockedUsersMutex.lock();
|
||||
_twitchBlockedUsersMutex.lock();
|
||||
for (QJsonValue block : blocks) {
|
||||
QJsonObject user =
|
||||
block.toObject().value("user").toObject();
|
||||
QJsonObject user = block.toObject().value("user").toObject();
|
||||
// display_name
|
||||
IrcManager::twitchBlockedUsers.insert(
|
||||
user.value("name").toString().toLower(), true);
|
||||
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
||||
}
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
_twitchBlockedUsersMutex.unlock();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -129,19 +141,17 @@ IrcManager::beginConnecting()
|
||||
c->setNickName("justinfan123");
|
||||
c->setRealName("justinfan123");
|
||||
|
||||
c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/commands"));
|
||||
c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/tags"));
|
||||
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
|
||||
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
|
||||
|
||||
QMutexLocker locker(&IrcManager::connectionMutex);
|
||||
QMutexLocker locker(&_connectionMutex);
|
||||
|
||||
if (generation == IrcManager::connectionGeneration) {
|
||||
if (generation == _connectionGeneration) {
|
||||
c->moveToThread(QCoreApplication::instance()->thread());
|
||||
IrcManager::connection = std::shared_ptr<IrcConnection>(c);
|
||||
_connection = std::shared_ptr<Communi::IrcConnection>(c);
|
||||
|
||||
auto channels = Channels::getItems();
|
||||
|
||||
for (auto &channel : channels) {
|
||||
c->sendRaw("JOIN #" + channel.get()->getName());
|
||||
for (auto &channel : ChannelManager::getInstance().getItems()) {
|
||||
c->sendRaw("JOIN #" + channel->getName());
|
||||
}
|
||||
|
||||
c->open();
|
||||
@@ -150,53 +160,50 @@ IrcManager::beginConnecting()
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::disconnect()
|
||||
void IrcManager::disconnect()
|
||||
{
|
||||
IrcManager::connectionMutex.lock();
|
||||
_connectionMutex.lock();
|
||||
|
||||
auto c = IrcManager::connection;
|
||||
if (IrcManager::connection.get() != NULL) {
|
||||
IrcManager::connection = std::shared_ptr<IrcConnection>();
|
||||
auto c = _connection;
|
||||
if (_connection.get() != NULL) {
|
||||
_connection = std::shared_ptr<Communi::IrcConnection>();
|
||||
}
|
||||
|
||||
IrcManager::connectionMutex.unlock();
|
||||
_connectionMutex.unlock();
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::send(QString raw)
|
||||
void IrcManager::send(QString raw)
|
||||
{
|
||||
IrcManager::connectionMutex.lock();
|
||||
IrcManager::connection->sendRaw(raw);
|
||||
IrcManager::connectionMutex.unlock();
|
||||
_connectionMutex.lock();
|
||||
|
||||
_connection->sendRaw(raw);
|
||||
|
||||
_connectionMutex.unlock();
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::sendJoin(const QString &channel)
|
||||
void IrcManager::sendJoin(const QString &channel)
|
||||
{
|
||||
IrcManager::connectionMutex.lock();
|
||||
_connectionMutex.lock();
|
||||
|
||||
if (IrcManager::connection.get() != NULL) {
|
||||
IrcManager::connection.get()->sendRaw("JOIN #" + channel);
|
||||
if (_connection.get() != NULL) {
|
||||
_connection.get()->sendRaw("JOIN #" + channel);
|
||||
}
|
||||
|
||||
IrcManager::connectionMutex.unlock();
|
||||
_connectionMutex.unlock();
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::partChannel(const QString &channel)
|
||||
void IrcManager::partChannel(const QString &channel)
|
||||
{
|
||||
IrcManager::connectionMutex.lock();
|
||||
_connectionMutex.lock();
|
||||
|
||||
if (IrcManager::connection.get() != NULL) {
|
||||
IrcManager::connection.get()->sendRaw("PART #" + channel);
|
||||
if (_connection.get() != NULL) {
|
||||
_connection.get()->sendRaw("PART #" + channel);
|
||||
}
|
||||
|
||||
IrcManager::connectionMutex.unlock();
|
||||
_connectionMutex.unlock();
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::messageReceived(IrcMessage *message)
|
||||
void IrcManager::messageReceived(Communi::IrcMessage *message)
|
||||
{
|
||||
// qInfo(message->command().toStdString().c_str());
|
||||
|
||||
@@ -211,63 +218,51 @@ IrcManager::messageReceived(IrcMessage *message)
|
||||
// }
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::privateMessageReceived(IrcPrivateMessage *message)
|
||||
void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message)
|
||||
{
|
||||
// qInfo(message->content().toStdString().c_str());
|
||||
|
||||
auto c = Channels::getChannel(message->target().mid(1));
|
||||
auto c = ChannelManager::getInstance().getChannel(message->target().mid(1));
|
||||
|
||||
if (c != NULL) {
|
||||
c->addMessage(std::shared_ptr<messages::Message>(
|
||||
new messages::Message(*message, *c)));
|
||||
messages::MessageParseArgs args;
|
||||
|
||||
c->addMessage(twitch::TwitchMessageBuilder::parse(message, c.get(), args));
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
IrcManager::isTwitchBlockedUser(QString const &username)
|
||||
bool IrcManager::isTwitchBlockedUser(QString const &username)
|
||||
{
|
||||
IrcManager::twitchBlockedUsersMutex.lock();
|
||||
QMutexLocker locker(&_twitchBlockedUsersMutex);
|
||||
|
||||
auto iterator = IrcManager::twitchBlockedUsers.find(username);
|
||||
auto iterator = _twitchBlockedUsers.find(username);
|
||||
|
||||
if (iterator == IrcManager::twitchBlockedUsers.end()) {
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
return true;
|
||||
return iterator != _twitchBlockedUsers.end();
|
||||
}
|
||||
|
||||
bool
|
||||
IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
|
||||
bool IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
|
||||
{
|
||||
QUrl url("https://api.twitch.tv/kraken/users/" + account->getUsername() +
|
||||
"/blocks/" + username +
|
||||
"?oauth_token=" + account->getOauthToken() +
|
||||
"&client_id=" + account->getOauthClient());
|
||||
QUrl url("https://api.twitch.tv/kraken/users/" + _account.getUserName() + "/blocks/" +
|
||||
username + "?oauth_token=" + _account.getOAuthToken() +
|
||||
"&client_id=" + _account.getOAuthClient());
|
||||
|
||||
QNetworkRequest request(url);
|
||||
auto reply = IrcManager::accessManager.put(request, QByteArray());
|
||||
auto reply = _accessManager.put(request, QByteArray());
|
||||
reply->waitForReadyRead(10000);
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
IrcManager::twitchBlockedUsersMutex.lock();
|
||||
IrcManager::twitchBlockedUsers.insert(username, true);
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
_twitchBlockedUsersMutex.lock();
|
||||
_twitchBlockedUsers.insert(username, true);
|
||||
_twitchBlockedUsersMutex.unlock();
|
||||
|
||||
delete reply;
|
||||
return true;
|
||||
}
|
||||
|
||||
errorMessage = "Error while ignoring user \"" + username +
|
||||
"\": " + reply->errorString();
|
||||
reply->deleteLater();
|
||||
|
||||
errorMessage = "Error while ignoring user \"" + username + "\": " + reply->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::addIgnoredUser(QString const &username)
|
||||
void IrcManager::addIgnoredUser(QString const &username)
|
||||
{
|
||||
QString errorMessage;
|
||||
if (!tryAddIgnoredUser(username, errorMessage)) {
|
||||
@@ -275,38 +270,40 @@ IrcManager::addIgnoredUser(QString const &username)
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
|
||||
bool IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
|
||||
{
|
||||
QUrl url("https://api.twitch.tv/kraken/users/" + account->getUsername() +
|
||||
"/blocks/" + username +
|
||||
"?oauth_token=" + account->getOauthToken() +
|
||||
"&client_id=" + account->getOauthClient());
|
||||
QUrl url("https://api.twitch.tv/kraken/users/" + _account.getUserName() + "/blocks/" +
|
||||
username + "?oauth_token=" + _account.getOAuthToken() +
|
||||
"&client_id=" + _account.getOAuthClient());
|
||||
|
||||
QNetworkRequest request(url);
|
||||
auto reply = IrcManager::accessManager.deleteResource(request);
|
||||
auto reply = _accessManager.deleteResource(request);
|
||||
reply->waitForReadyRead(10000);
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
IrcManager::twitchBlockedUsersMutex.lock();
|
||||
IrcManager::twitchBlockedUsers.remove(username);
|
||||
IrcManager::twitchBlockedUsersMutex.unlock();
|
||||
_twitchBlockedUsersMutex.lock();
|
||||
_twitchBlockedUsers.remove(username);
|
||||
_twitchBlockedUsersMutex.unlock();
|
||||
|
||||
delete reply;
|
||||
return true;
|
||||
}
|
||||
|
||||
errorMessage = "Error while unignoring user \"" + username +
|
||||
"\": " + reply->errorString();
|
||||
reply->deleteLater();
|
||||
|
||||
errorMessage = "Error while unignoring user \"" + username + "\": " + reply->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
IrcManager::removeIgnoredUser(QString const &username)
|
||||
void IrcManager::removeIgnoredUser(QString const &username)
|
||||
{
|
||||
QString errorMessage;
|
||||
if (!tryRemoveIgnoredUser(username, errorMessage)) {
|
||||
// TODO: Implement IrcManager::removeIgnoredUser
|
||||
}
|
||||
}
|
||||
|
||||
QNetworkAccessManager &IrcManager::getAccessManager()
|
||||
{
|
||||
return _accessManager;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user