added message-sending through channel->sendMessage

doesn't do anything with the user input box on enter yet though
This commit is contained in:
Rasmus Karlsson 2017-05-27 17:45:40 +02:00
parent c4de5c806a
commit d113115822
12 changed files with 91 additions and 50 deletions

View file

@ -39,4 +39,5 @@ void AccountManager::addTwitchUser(const twitch::TwitchUser &user)
_twitchUsers.push_back(user); _twitchUsers.push_back(user);
} }
} // namespace chatterino } // namespace chatterino

View file

@ -24,13 +24,13 @@ public:
void addTwitchUser(const twitch::TwitchUser &user); void addTwitchUser(const twitch::TwitchUser &user);
private: private:
AccountManager(); AccountManager();
twitch::TwitchUser _twitchAnon; twitch::TwitchUser _twitchAnon;
std::vector<twitch::TwitchUser> _twitchUsers; std::vector<twitch::TwitchUser> _twitchUsers;
std::mutex _twitchUsersMutex; std::mutex _twitchUsersMutex;
}; };
} // namespace chatterino } // namespace chatterino
#endif // ACCOUNTMANAGER_H #endif // ACCOUNTMANAGER_H

View file

@ -1,10 +1,12 @@
#include "channel.h" #include "channel.h"
#include "emotemanager.h" #include "emotemanager.h"
#include "ircmanager.h"
#include "logging/loggingmanager.h" #include "logging/loggingmanager.h"
#include "messages/message.h" #include "messages/message.h"
#include "util/urlfetch.h" #include "util/urlfetch.h"
#include "windowmanager.h" #include "windowmanager.h"
#include <QDebug>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
@ -29,6 +31,8 @@ Channel::Channel(const QString &channel)
, _popoutPlayerLink("https://player.twitch.tv/?channel=" + _name) , _popoutPlayerLink("https://player.twitch.tv/?channel=" + _name)
// , _loggingChannel(logging::get(_name)) // , _loggingChannel(logging::get(_name))
{ {
qDebug() << "Open channel:" << channel << ". Name: " << _name;
printf("Channel pointer: %p\n", this);
reloadChannelEmotes(); reloadChannelEmotes();
} }
@ -127,6 +131,13 @@ void Channel::reloadChannelEmotes()
reloadFfzEmotes(); reloadFfzEmotes();
} }
void Channel::sendMessage(const QString &message)
{
qDebug() << "Channel send message: " << message;
IrcManager &instance = IrcManager::getInstance();
instance.sendMessage(_name, message);
}
void Channel::reloadBttvEmotes() void Channel::reloadBttvEmotes()
{ {
util::urlJsonFetch( util::urlJsonFetch(

View file

@ -50,6 +50,8 @@ public:
void addMessage(messages::SharedMessage message); void addMessage(messages::SharedMessage message);
void reloadChannelEmotes(); void reloadChannelEmotes();
void sendMessage(const QString &message);
private: private:
// variabeles // variabeles
messages::LimitedQueue<messages::SharedMessage> _messages; messages::LimitedQueue<messages::SharedMessage> _messages;

View file

@ -66,12 +66,18 @@ void IrcManager::beginConnecting()
QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this, QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this,
&IrcManager::privateMessageReceived); &IrcManager::privateMessageReceived);
if (_account.isAnon()) { QString username = _account.getUserName();
// fetch ignored users QString oauthClient = _account.getOAuthClient();
QString username = _account.getUserName(); QString oauthToken = _account.getOAuthToken();
QString oauthClient = _account.getOAuthClient();
QString oauthToken = _account.getOAuthToken();
c->setUserName(username);
c->setNickName(username);
c->setRealName(username);
if (!_account.isAnon()) {
c->setPassword(oauthToken);
// fetch ignored users
{ {
QString nextLink = "https://api.twitch.tv/kraken/users/" + username + QString nextLink = "https://api.twitch.tv/kraken/users/" + username +
"/blocks?limit=" + 100 + "&client_id=" + oauthClient; "/blocks?limit=" + 100 + "&client_id=" + oauthClient;
@ -105,42 +111,38 @@ void IrcManager::beginConnecting()
manager->deleteLater(); manager->deleteLater();
}); });
} }
}
// fetch available twitch emtoes // fetch available twitch emtoes
{ {
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username + QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username +
"/emotes?oauth_token=" + oauthToken + "/emotes?oauth_token=" + oauthToken +
"&client_id=" + oauthClient)); "&client_id=" + oauthClient));
QNetworkReply *reply = _accessManager.get(req); QNetworkReply *reply = _accessManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [=] { QObject::connect(reply, &QNetworkReply::finished, [=] {
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object(); QJsonObject root = jsonDoc.object();
// nextLink = // nextLink =
// root.value("_links").toObject().value("next").toString(); // root.value("_links").toObject().value("next").toString();
auto blocks = root.value("blocks").toArray(); auto blocks = root.value("blocks").toArray();
_twitchBlockedUsersMutex.lock(); _twitchBlockedUsersMutex.lock();
for (QJsonValue block : blocks) { for (QJsonValue block : blocks) {
QJsonObject user = block.toObject().value("user").toObject(); QJsonObject user = block.toObject().value("user").toObject();
// display_name // display_name
_twitchBlockedUsers.insert(user.value("name").toString().toLower(), true); _twitchBlockedUsers.insert(user.value("name").toString().toLower(), true);
} }
_twitchBlockedUsersMutex.unlock(); _twitchBlockedUsersMutex.unlock();
}); });
}
} }
c->setHost("irc.chat.twitch.tv"); c->setHost("irc.chat.twitch.tv");
c->setPort(6667); c->setPort(6667);
c->setUserName("justinfan123");
c->setNickName("justinfan123");
c->setRealName("justinfan123");
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands")); c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/commands"));
c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags")); c->sendCommand(Communi::IrcCommand::createCapability("REQ", "twitch.tv/tags"));
@ -186,7 +188,21 @@ void IrcManager::sendJoin(const QString &channel)
_connectionMutex.lock(); _connectionMutex.lock();
if (_connection.get() != NULL) { if (_connection.get() != NULL) {
_connection.get()->sendRaw("JOIN #" + channel); _connection->sendRaw("JOIN #" + channel);
}
_connectionMutex.unlock();
}
void IrcManager::sendMessage(const QString &channelName, const QString &message)
{
_connectionMutex.lock();
if (_connection.get() != nullptr) {
qDebug() << "IRC Manager send message " << message << " to channel " << channelName;
QString xd = "PRIVMSG #" + channelName + " :" + message;
qDebug() << xd;
_connection->sendRaw(xd);
} }
_connectionMutex.unlock(); _connectionMutex.unlock();

View file

@ -42,6 +42,7 @@ public:
QNetworkAccessManager &getAccessManager(); QNetworkAccessManager &getAccessManager();
void sendJoin(const QString &channel); void sendJoin(const QString &channel);
void sendMessage(const QString &channelName, const QString &message);
void partChannel(const QString &channel); void partChannel(const QString &channel);

View file

@ -22,7 +22,8 @@ private:
QString _oauthClient; QString _oauthClient;
QString _oauthToken; QString _oauthToken;
}; };
}
} } // namespace twitch
} // namespace chatterino
#endif // ACCOUNT_H #endif // ACCOUNT_H

View file

@ -7,10 +7,10 @@
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
AccountPopupWidget::AccountPopupWidget(std::shared_ptr<Channel> &&channel) AccountPopupWidget::AccountPopupWidget(SharedChannel &channel)
: QWidget(nullptr) : QWidget(nullptr)
, _ui(new Ui::AccountPopup) , _ui(new Ui::AccountPopup)
, _channel(std::move(channel)) , _channel(channel)
{ {
_ui->setupUi(this); _ui->setupUi(this);
@ -25,10 +25,10 @@ AccountPopupWidget::AccountPopupWidget(std::shared_ptr<Channel> &&channel)
connect(_ui->btnPurge, &QPushButton::clicked, [=]() { connect(_ui->btnPurge, &QPushButton::clicked, [=]() {
qDebug() << "xD: " << _channel->getName(); qDebug() << "xD: " << _channel->getName();
/* printf("Channel pointer in dialog: %p\n", _channel.get());
_channel->sendMessage(
QString(".timeout %1 0").arg(_ui->lblUsername->text())); //_channel->sendMessage(QString(".timeout %1 0").arg(_ui->lblUsername->text()));
*/ _channel->sendMessage("xD");
}); });
} }

View file

@ -19,14 +19,14 @@ class AccountPopupWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
AccountPopupWidget(std::shared_ptr<Channel> &&_channel); AccountPopupWidget(std::shared_ptr<Channel> &channel);
void setName(const QString &name); void setName(const QString &name);
private: private:
Ui::AccountPopup *_ui; Ui::AccountPopup *_ui;
std::shared_ptr<Channel> _channel; std::shared_ptr<Channel> &_channel;
}; };
} // namespace widgets } // namespace widgets

View file

@ -43,6 +43,11 @@ std::shared_ptr<Channel> ChatWidget::getChannel() const
return _channel; return _channel;
} }
std::shared_ptr<Channel> &ChatWidget::getChannelRef()
{
return _channel;
}
const QString &ChatWidget::getChannelName() const const QString &ChatWidget::getChannelName() const
{ {
return _channelName; return _channelName;
@ -50,11 +55,11 @@ const QString &ChatWidget::getChannelName() const
void ChatWidget::setChannelName(const QString &name) void ChatWidget::setChannelName(const QString &name)
{ {
QString channel = name.trimmed(); QString channelName = name.trimmed();
// return if channel name is the same // return if channel name is the same
if (QString::compare(channel, _channelName, Qt::CaseInsensitive) == 0) { if (QString::compare(channelName, _channelName, Qt::CaseInsensitive) == 0) {
_channelName = channel; _channelName = channelName;
_header.updateChannelText(); _header.updateChannelText();
return; return;
@ -68,15 +73,18 @@ void ChatWidget::setChannelName(const QString &name)
} }
// update members // update members
_channelName = channel; _channelName = channelName;
// update messages // update messages
_messages.clear(); _messages.clear();
if (channel.isEmpty()) { printf("Set channel name xD %s\n", qPrintable(name));
if (channelName.isEmpty()) {
_channel = NULL; _channel = NULL;
} else { } else {
_channel = ChannelManager::getInstance().addChannel(channel); _channel = ChannelManager::getInstance().addChannel(channelName);
printf("Created channel FeelsGoodMan %p\n", _channel.get());
attachChannel(_channel); attachChannel(_channel);
} }

View file

@ -28,6 +28,7 @@ public:
~ChatWidget(); ~ChatWidget();
SharedChannel getChannel() const; SharedChannel getChannel() const;
SharedChannel &getChannelRef();
const QString &getChannelName() const; const QString &getChannelName() const;
void setChannelName(const QString &name); void setChannelName(const QString &name);

View file

@ -22,7 +22,7 @@ ChatWidgetView::ChatWidgetView(ChatWidget *parent)
: QWidget() : QWidget()
, _chatWidget(parent) , _chatWidget(parent)
, _scrollbar(this) , _scrollbar(this)
, _userPopupWidget(_chatWidget->getChannel()) , _userPopupWidget(_chatWidget->getChannelRef())
, _onlyUpdateEmotes(false) , _onlyUpdateEmotes(false)
, _mouseDown(false) , _mouseDown(false)
, _lastPressPosition() , _lastPressPosition()