mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Refactor IrcManager a bit
Create a new readConnection so we can read our own messages
This commit is contained in:
parent
ccf8e3bd83
commit
1a428a54d5
2 changed files with 125 additions and 108 deletions
|
@ -29,12 +29,6 @@ const QString IrcManager::defaultClientId("7ue61iz46fz11y3cugd0l3tawb4taal");
|
||||||
|
|
||||||
IrcManager::IrcManager()
|
IrcManager::IrcManager()
|
||||||
: _account(AccountManager::getInstance().getTwitchUser())
|
: _account(AccountManager::getInstance().getTwitchUser())
|
||||||
, _connection()
|
|
||||||
, _connectionMutex()
|
|
||||||
, _connectionGeneration(0)
|
|
||||||
, _twitchBlockedUsers()
|
|
||||||
, _twitchBlockedUsersMutex()
|
|
||||||
, _accessManager()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,110 +49,132 @@ void IrcManager::connect()
|
||||||
async_exec([this] { beginConnecting(); });
|
async_exec([this] { beginConnecting(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void IrcManager::beginConnecting()
|
Communi::IrcConnection *IrcManager::createConnection(bool doRead)
|
||||||
{
|
{
|
||||||
int generation = ++IrcManager::_connectionGeneration;
|
Communi::IrcConnection *connection = new Communi::IrcConnection;
|
||||||
|
|
||||||
Communi::IrcConnection *c = new Communi::IrcConnection;
|
if (doRead) {
|
||||||
|
QObject::connect(connection, &Communi::IrcConnection::messageReceived, this,
|
||||||
QObject::connect(c, &Communi::IrcConnection::messageReceived, this,
|
&IrcManager::messageReceived);
|
||||||
&IrcManager::messageReceived);
|
QObject::connect(connection, &Communi::IrcConnection::privateMessageReceived, this,
|
||||||
QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this,
|
&IrcManager::privateMessageReceived);
|
||||||
&IrcManager::privateMessageReceived);
|
}
|
||||||
|
|
||||||
QString username = _account.getUserName();
|
QString username = _account.getUserName();
|
||||||
QString oauthClient = _account.getOAuthClient();
|
QString oauthClient = _account.getOAuthClient();
|
||||||
QString oauthToken = _account.getOAuthToken();
|
QString oauthToken = _account.getOAuthToken();
|
||||||
|
|
||||||
c->setUserName(username);
|
connection->setUserName(username);
|
||||||
c->setNickName(username);
|
connection->setNickName(username);
|
||||||
c->setRealName(username);
|
connection->setRealName(username);
|
||||||
|
|
||||||
if (!_account.isAnon()) {
|
if (!_account.isAnon()) {
|
||||||
c->setPassword(oauthToken);
|
connection->setPassword(oauthToken);
|
||||||
|
|
||||||
// fetch ignored users
|
this->refreshIgnoredUsers(username, oauthClient, oauthToken);
|
||||||
{
|
}
|
||||||
QString nextLink = "https://api.twitch.tv/kraken/users/" + username +
|
|
||||||
"/blocks?limit=" + 100 + "&client_id=" + oauthClient;
|
|
||||||
|
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
this->refreshTwitchEmotes(username, oauthClient, oauthToken);
|
||||||
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
|
|
||||||
QNetworkReply *reply = manager->get(req);
|
|
||||||
|
|
||||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/membership"));
|
||||||
_twitchBlockedUsersMutex.lock();
|
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
|
||||||
_twitchBlockedUsers.clear();
|
connection->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
|
||||||
_twitchBlockedUsersMutex.unlock();
|
|
||||||
|
|
||||||
QByteArray data = reply->readAll();
|
connection->setHost("irc.chat.twitch.tv");
|
||||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
connection->setPort(6667);
|
||||||
QJsonObject root = jsonDoc.object();
|
|
||||||
|
|
||||||
// nextLink =
|
return connection;
|
||||||
// root.value("_links").toObject().value("next").toString();
|
}
|
||||||
|
|
||||||
auto blocks = root.value("blocks").toArray();
|
void IrcManager::refreshIgnoredUsers(const QString &username, const QString &oauthClient,
|
||||||
|
const QString &oauthToken)
|
||||||
|
{
|
||||||
|
QString nextLink = "https://api.twitch.tv/kraken/users/" + username + "/blocks?limit=" + 100 +
|
||||||
|
"&client_id=" + oauthClient;
|
||||||
|
|
||||||
_twitchBlockedUsersMutex.lock();
|
QNetworkAccessManager *manager = new QNetworkAccessManager();
|
||||||
for (QJsonValue block : blocks) {
|
QNetworkRequest req(QUrl(nextLink + "&oauth_token=" + oauthToken));
|
||||||
QJsonObject user = block.toObject().value("user").toObject();
|
QNetworkReply *reply = manager->get(req);
|
||||||
// display_name
|
|
||||||
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
|
||||||
}
|
|
||||||
_twitchBlockedUsersMutex.unlock();
|
|
||||||
|
|
||||||
manager->deleteLater();
|
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||||
});
|
_twitchBlockedUsersMutex.lock();
|
||||||
|
_twitchBlockedUsers.clear();
|
||||||
|
_twitchBlockedUsersMutex.unlock();
|
||||||
|
|
||||||
|
QByteArray data = reply->readAll();
|
||||||
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||||
|
QJsonObject root = jsonDoc.object();
|
||||||
|
|
||||||
|
// nextLink =
|
||||||
|
// root.value("_links").toObject().value("next").toString();
|
||||||
|
|
||||||
|
auto blocks = root.value("blocks").toArray();
|
||||||
|
|
||||||
|
_twitchBlockedUsersMutex.lock();
|
||||||
|
for (QJsonValue block : blocks) {
|
||||||
|
QJsonObject user = block.toObject().value("user").toObject();
|
||||||
|
// display_name
|
||||||
|
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
||||||
}
|
}
|
||||||
}
|
_twitchBlockedUsersMutex.unlock();
|
||||||
|
|
||||||
// fetch available twitch emtoes
|
manager->deleteLater();
|
||||||
{
|
});
|
||||||
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, [=] {
|
void IrcManager::refreshTwitchEmotes(const QString &username, const QString &oauthClient,
|
||||||
QByteArray data = reply->readAll();
|
const QString &oauthToken)
|
||||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
{
|
||||||
QJsonObject root = jsonDoc.object();
|
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username +
|
||||||
|
"/emotes?oauth_token=" + oauthToken + "&client_id=" + oauthClient));
|
||||||
|
QNetworkReply *reply = _accessManager.get(req);
|
||||||
|
|
||||||
// nextLink =
|
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||||
// root.value("_links").toObject().value("next").toString();
|
QByteArray data = reply->readAll();
|
||||||
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||||
|
QJsonObject root = jsonDoc.object();
|
||||||
|
|
||||||
auto blocks = root.value("blocks").toArray();
|
// nextLink =
|
||||||
|
// root.value("_links").toObject().value("next").toString();
|
||||||
|
|
||||||
_twitchBlockedUsersMutex.lock();
|
auto blocks = root.value("blocks").toArray();
|
||||||
for (QJsonValue block : blocks) {
|
|
||||||
QJsonObject user = block.toObject().value("user").toObject();
|
|
||||||
// display_name
|
|
||||||
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
|
||||||
}
|
|
||||||
_twitchBlockedUsersMutex.unlock();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
c->setHost("irc.chat.twitch.tv");
|
_twitchBlockedUsersMutex.lock();
|
||||||
c->setPort(6667);
|
for (QJsonValue block : blocks) {
|
||||||
|
QJsonObject user = block.toObject().value("user").toObject();
|
||||||
|
// display_name
|
||||||
|
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
|
||||||
|
}
|
||||||
|
_twitchBlockedUsersMutex.unlock();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
|
void IrcManager::beginConnecting()
|
||||||
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
|
{
|
||||||
|
uint32_t generation = ++this->connectionGeneration;
|
||||||
|
|
||||||
|
Communi::IrcConnection *_writeConnection = this->createConnection(false);
|
||||||
|
Communi::IrcConnection *_readConnection = this->createConnection(true);
|
||||||
|
|
||||||
QMutexLocker locker(&_connectionMutex);
|
QMutexLocker locker(&_connectionMutex);
|
||||||
|
|
||||||
if (generation == _connectionGeneration) {
|
if (generation == this->connectionGeneration) {
|
||||||
c->moveToThread(QCoreApplication::instance()->thread());
|
this->writeConnection = std::shared_ptr<Communi::IrcConnection>(_writeConnection);
|
||||||
_connection = std::shared_ptr<Communi::IrcConnection>(c);
|
this->readConnection = std::shared_ptr<Communi::IrcConnection>(_readConnection);
|
||||||
|
|
||||||
|
this->writeConnection->moveToThread(QCoreApplication::instance()->thread());
|
||||||
|
this->readConnection->moveToThread(QCoreApplication::instance()->thread());
|
||||||
|
|
||||||
for (auto &channel : ChannelManager::getInstance().getItems()) {
|
for (auto &channel : ChannelManager::getInstance().getItems()) {
|
||||||
c->sendRaw("JOIN #" + channel->getName());
|
this->writeConnection->sendRaw("JOIN #" + channel->getName());
|
||||||
|
this->readConnection->sendRaw("JOIN #" + channel->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
c->open();
|
this->writeConnection->open();
|
||||||
|
this->readConnection->open();
|
||||||
} else {
|
} else {
|
||||||
delete c;
|
delete _writeConnection;
|
||||||
|
delete _readConnection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,19 +182,11 @@ void IrcManager::disconnect()
|
||||||
{
|
{
|
||||||
_connectionMutex.lock();
|
_connectionMutex.lock();
|
||||||
|
|
||||||
auto c = _connection;
|
auto _readConnection = this->readConnection;
|
||||||
if (_connection.get() != NULL) {
|
auto _writeConnection = this->writeConnection;
|
||||||
_connection = std::shared_ptr<Communi::IrcConnection>();
|
|
||||||
}
|
|
||||||
|
|
||||||
_connectionMutex.unlock();
|
this->readConnection.reset();
|
||||||
}
|
this->writeConnection.reset();
|
||||||
|
|
||||||
void IrcManager::send(QString raw)
|
|
||||||
{
|
|
||||||
_connectionMutex.lock();
|
|
||||||
|
|
||||||
_connection->sendRaw(raw);
|
|
||||||
|
|
||||||
_connectionMutex.unlock();
|
_connectionMutex.unlock();
|
||||||
}
|
}
|
||||||
|
@ -187,8 +195,9 @@ void IrcManager::sendJoin(const QString &channel)
|
||||||
{
|
{
|
||||||
_connectionMutex.lock();
|
_connectionMutex.lock();
|
||||||
|
|
||||||
if (_connection.get() != NULL) {
|
if (this->readConnection && this->writeConnection) {
|
||||||
_connection->sendRaw("JOIN #" + channel);
|
this->readConnection->sendRaw("JOIN #" + channel);
|
||||||
|
this->writeConnection->sendRaw("JOIN #" + channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
_connectionMutex.unlock();
|
_connectionMutex.unlock();
|
||||||
|
@ -198,11 +207,8 @@ void IrcManager::sendMessage(const QString &channelName, const QString &message)
|
||||||
{
|
{
|
||||||
_connectionMutex.lock();
|
_connectionMutex.lock();
|
||||||
|
|
||||||
if (_connection.get() != nullptr) {
|
if (this->writeConnection) {
|
||||||
qDebug() << "IRC Manager send message " << message << " to channel " << channelName;
|
this->writeConnection->sendRaw("PRIVMSG #" + channelName + " :" + message);
|
||||||
QString xd = "PRIVMSG #" + channelName + " :" + message;
|
|
||||||
qDebug() << xd;
|
|
||||||
_connection->sendRaw(xd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_connectionMutex.unlock();
|
_connectionMutex.unlock();
|
||||||
|
@ -212,19 +218,25 @@ void IrcManager::partChannel(const QString &channel)
|
||||||
{
|
{
|
||||||
_connectionMutex.lock();
|
_connectionMutex.lock();
|
||||||
|
|
||||||
if (_connection.get() != NULL) {
|
if (this->readConnection && this->writeConnection) {
|
||||||
_connection.get()->sendRaw("PART #" + channel);
|
this->readConnection->sendRaw("PART #" + channel);
|
||||||
|
this->writeConnection->sendRaw("PART #" + channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
_connectionMutex.unlock();
|
_connectionMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IrcManager::messageReceived(Communi::IrcMessage * /*message*/)
|
void IrcManager::messageReceived(Communi::IrcMessage *message)
|
||||||
{
|
{
|
||||||
|
qDebug() << "Message received: " << message->command();
|
||||||
// qInfo(message->command().toStdString().c_str());
|
// qInfo(message->command().toStdString().c_str());
|
||||||
|
//
|
||||||
|
for (const auto ¶m : message->parameters()) {
|
||||||
|
qDebug() << "Param: " << param;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
const QString &command = message->command();
|
const QString &command = message->command();
|
||||||
|
/*
|
||||||
|
|
||||||
if (command == "CLEARCHAT") {
|
if (command == "CLEARCHAT") {
|
||||||
} else if (command == "ROOMSTATE") {
|
} else if (command == "ROOMSTATE") {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#ifndef IRCMANAGER_H
|
#pragma once
|
||||||
#define IRCMANAGER_H
|
|
||||||
|
|
||||||
#define TWITCH_MAX_MESSAGELENGTH 500
|
#define TWITCH_MAX_MESSAGELENGTH 500
|
||||||
|
|
||||||
|
@ -31,8 +30,6 @@ public:
|
||||||
void connect();
|
void connect();
|
||||||
void disconnect();
|
void disconnect();
|
||||||
|
|
||||||
void send(QString raw);
|
|
||||||
|
|
||||||
bool isTwitchBlockedUser(QString const &username);
|
bool isTwitchBlockedUser(QString const &username);
|
||||||
bool tryAddIgnoredUser(QString const &username, QString &errorMessage);
|
bool tryAddIgnoredUser(QString const &username, QString &errorMessage);
|
||||||
void addIgnoredUser(QString const &username);
|
void addIgnoredUser(QString const &username);
|
||||||
|
@ -56,9 +53,11 @@ private:
|
||||||
// variables
|
// variables
|
||||||
twitch::TwitchUser _account;
|
twitch::TwitchUser _account;
|
||||||
|
|
||||||
std::shared_ptr<Communi::IrcConnection> _connection;
|
std::shared_ptr<Communi::IrcConnection> writeConnection = nullptr;
|
||||||
|
std::shared_ptr<Communi::IrcConnection> readConnection = nullptr;
|
||||||
|
|
||||||
QMutex _connectionMutex;
|
QMutex _connectionMutex;
|
||||||
long _connectionGeneration;
|
uint32_t connectionGeneration = 0;
|
||||||
|
|
||||||
QMap<QString, bool> _twitchBlockedUsers;
|
QMap<QString, bool> _twitchBlockedUsers;
|
||||||
QMutex _twitchBlockedUsersMutex;
|
QMutex _twitchBlockedUsersMutex;
|
||||||
|
@ -66,11 +65,17 @@ private:
|
||||||
QNetworkAccessManager _accessManager;
|
QNetworkAccessManager _accessManager;
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
|
Communi::IrcConnection *createConnection(bool doRead);
|
||||||
|
|
||||||
|
void refreshIgnoredUsers(const QString &username, const QString &oauthClient,
|
||||||
|
const QString &oauthToken);
|
||||||
|
void refreshTwitchEmotes(const QString &username, const QString &oauthClient,
|
||||||
|
const QString &oauthToken);
|
||||||
|
|
||||||
void beginConnecting();
|
void beginConnecting();
|
||||||
|
|
||||||
void messageReceived(Communi::IrcMessage *message);
|
void messageReceived(Communi::IrcMessage *message);
|
||||||
void privateMessageReceived(Communi::IrcPrivateMessage *message);
|
void privateMessageReceived(Communi::IrcPrivateMessage *message);
|
||||||
};
|
};
|
||||||
} // namespace chatterino
|
|
||||||
|
|
||||||
#endif // IRCMANAGER_H
|
} // namespace chatterino
|
||||||
|
|
Loading…
Reference in a new issue