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);
}
} // namespace chatterino

View file

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

View file

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

View file

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

View file

@ -66,12 +66,18 @@ void IrcManager::beginConnecting()
QObject::connect(c, &Communi::IrcConnection::privateMessageReceived, this,
&IrcManager::privateMessageReceived);
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();
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 +
"/blocks?limit=" + 100 + "&client_id=" + oauthClient;
@ -105,42 +111,38 @@ void IrcManager::beginConnecting()
manager->deleteLater();
});
}
}
// fetch available twitch emtoes
{
QNetworkRequest req(QUrl("https://api.twitch.tv/kraken/users/" + username +
"/emotes?oauth_token=" + oauthToken +
"&client_id=" + oauthClient));
QNetworkReply *reply = _accessManager.get(req);
// fetch available twitch emtoes
{
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();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
QObject::connect(reply, &QNetworkReply::finished, [=] {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject root = jsonDoc.object();
// nextLink =
// root.value("_links").toObject().value("next").toString();
// nextLink =
// root.value("_links").toObject().value("next").toString();
auto blocks = root.value("blocks").toArray();
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();
});
}
_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();
});
}
c->setHost("irc.chat.twitch.tv");
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/tags"));
@ -186,7 +188,21 @@ void IrcManager::sendJoin(const QString &channel)
_connectionMutex.lock();
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();

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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