2018-07-05 22:47:51 +02:00
|
|
|
#include "LogsPopup.hpp"
|
|
|
|
|
|
|
|
#include "IrcMessage"
|
|
|
|
#include "common/NetworkRequest.hpp"
|
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
|
|
|
#include "providers/twitch/TwitchMessageBuilder.hpp"
|
|
|
|
#include "widgets/helper/ChannelView.hpp"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
2018-07-07 13:08:57 +02:00
|
|
|
#include <QJsonArray>
|
2018-07-05 22:47:51 +02:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
LogsPopup::LogsPopup()
|
|
|
|
{
|
|
|
|
this->initLayout();
|
|
|
|
this->resize(400, 600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::initLayout()
|
|
|
|
{
|
2018-07-05 22:55:06 +02:00
|
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
|
|
layout->setMargin(0);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2018-07-05 22:55:06 +02:00
|
|
|
this->channelView_ = new ChannelView(this);
|
|
|
|
layout->addWidget(this->channelView_);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2018-07-05 22:55:06 +02:00
|
|
|
this->setLayout(layout);
|
2018-07-05 22:47:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::setInfo(ChannelPtr channel, QString userName)
|
|
|
|
{
|
|
|
|
this->channel_ = channel;
|
|
|
|
this->userName_ = userName;
|
2018-07-23 14:50:09 +02:00
|
|
|
this->getRoomID();
|
2018-08-06 21:17:03 +02:00
|
|
|
this->setWindowTitle(this->userName_ + "'s logs in #" +
|
|
|
|
this->channel_->getName());
|
2018-07-05 22:47:51 +02:00
|
|
|
this->getLogviewerLogs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::setMessages(std::vector<MessagePtr> &messages)
|
|
|
|
{
|
2018-07-06 17:30:12 +02:00
|
|
|
ChannelPtr logsChannel(new Channel("logs", Channel::Type::Misc));
|
2018-07-05 22:47:51 +02:00
|
|
|
|
|
|
|
logsChannel->addMessagesAtStart(messages);
|
|
|
|
this->channelView_->setChannel(logsChannel);
|
|
|
|
}
|
|
|
|
|
2018-07-23 14:50:09 +02:00
|
|
|
void LogsPopup::getRoomID()
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
TwitchChannel *twitchChannel =
|
|
|
|
dynamic_cast<TwitchChannel *>(this->channel_.get());
|
2018-07-23 14:50:09 +02:00
|
|
|
if (twitchChannel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:41:30 +02:00
|
|
|
QString channelName = twitchChannel->getName();
|
2018-07-23 14:50:09 +02:00
|
|
|
|
|
|
|
QString url = QString("https://cbenni.com/api/channel/%1").arg(channelName);
|
|
|
|
|
|
|
|
NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
|
|
|
|
req.onError([this](int errorCode) {
|
|
|
|
this->getOverrustleLogs();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-08-06 18:41:30 +02:00
|
|
|
req.onSuccess([this, channelName](auto result) -> Outcome {
|
2018-07-23 14:50:09 +02:00
|
|
|
auto data = result.parseJson();
|
2018-08-06 18:41:30 +02:00
|
|
|
this->roomID_ = data.value("channel").toObject()["id"].toInt();
|
2018-07-23 14:50:09 +02:00
|
|
|
this->getLogviewerLogs();
|
2018-08-06 18:41:30 +02:00
|
|
|
return Success;
|
2018-07-23 14:50:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
2018-07-05 22:47:51 +02:00
|
|
|
void LogsPopup::getLogviewerLogs()
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
TwitchChannel *twitchChannel =
|
|
|
|
dynamic_cast<TwitchChannel *>(this->channel_.get());
|
2018-07-05 22:47:51 +02:00
|
|
|
if (twitchChannel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
QString channelName = twitchChannel->getName();
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2018-07-23 14:50:09 +02:00
|
|
|
auto url = QString("https://cbenni.com/api/logs/%1/?nick=%2&before=500")
|
|
|
|
.arg(channelName, this->userName_);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
|
|
|
NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
|
|
|
|
req.onError([this](int errorCode) {
|
|
|
|
this->getOverrustleLogs();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
req.onSuccess([this, channelName](auto result) -> Outcome {
|
2018-07-07 13:08:57 +02:00
|
|
|
auto data = result.parseJson();
|
2018-07-05 22:47:51 +02:00
|
|
|
std::vector<MessagePtr> messages;
|
|
|
|
|
|
|
|
QJsonValue before = data.value("before");
|
|
|
|
|
|
|
|
for (auto i : before.toArray()) {
|
|
|
|
auto messageObject = i.toObject();
|
|
|
|
QString message = messageObject.value("text").toString();
|
|
|
|
|
|
|
|
// Hacky way to fix the timestamp
|
|
|
|
message.insert(1, "historical=1;");
|
2018-08-06 21:17:03 +02:00
|
|
|
message.insert(1, QString("tmi-sent-ts=%10000;")
|
|
|
|
.arg(messageObject["time"].toInt()));
|
2018-07-23 14:50:09 +02:00
|
|
|
message.insert(1, QString("room-id=%1;").arg(this->roomID_));
|
2018-07-05 22:47:51 +02:00
|
|
|
|
|
|
|
MessageParseArgs args;
|
2018-08-06 21:17:03 +02:00
|
|
|
auto ircMessage =
|
|
|
|
Communi::IrcMessage::fromData(message.toUtf8(), nullptr);
|
|
|
|
auto privMsg =
|
|
|
|
static_cast<Communi::IrcPrivateMessage *>(ircMessage);
|
2018-07-23 14:50:09 +02:00
|
|
|
TwitchMessageBuilder builder(this->channel_.get(), privMsg, args);
|
2018-07-05 22:47:51 +02:00
|
|
|
messages.push_back(builder.build());
|
|
|
|
};
|
|
|
|
this->setMessages(messages);
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return Success;
|
2018-07-05 22:47:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::getOverrustleLogs()
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
TwitchChannel *twitchChannel =
|
|
|
|
dynamic_cast<TwitchChannel *>(this->channel_.get());
|
2018-07-05 22:47:51 +02:00
|
|
|
if (twitchChannel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
QString channelName = twitchChannel->getName();
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
QString url =
|
|
|
|
QString("https://overrustlelogs.net/api/v1/stalk/%1/%2.json?limit=500")
|
|
|
|
.arg(channelName, this->userName_);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
|
|
|
NetworkRequest req(url);
|
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.onError([this, channelName](int errorCode) {
|
|
|
|
this->close();
|
2018-08-06 21:17:03 +02:00
|
|
|
QMessageBox *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Error getting logs",
|
|
|
|
"No logs could be found for channel " + channelName);
|
2018-07-05 22:47:51 +02:00
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->show();
|
|
|
|
box->raise();
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-07-05 22:47:51 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
req.onSuccess([this, channelName](auto result) -> Outcome {
|
2018-07-07 13:08:57 +02:00
|
|
|
auto data = result.parseJson();
|
2018-07-05 22:47:51 +02:00
|
|
|
std::vector<MessagePtr> messages;
|
|
|
|
if (data.contains("lines")) {
|
|
|
|
QJsonArray dataMessages = data.value("lines").toArray();
|
|
|
|
for (auto i : dataMessages) {
|
|
|
|
QJsonObject singleMessage = i.toObject();
|
2018-08-06 21:17:03 +02:00
|
|
|
QTime timeStamp = QDateTime::fromSecsSinceEpoch(
|
|
|
|
singleMessage.value("timestamp").toInt())
|
|
|
|
.time();
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
MessageBuilder builder;
|
|
|
|
builder.emplace<TimestampElement>(timeStamp);
|
|
|
|
builder.emplace<TextElement>(this->userName_,
|
|
|
|
MessageElement::Username,
|
|
|
|
MessageColor::System);
|
|
|
|
builder.emplace<TextElement>(
|
|
|
|
singleMessage.value("text").toString(),
|
|
|
|
MessageElement::Text, MessageColor::Text);
|
|
|
|
messages.push_back(builder.release());
|
2018-07-05 22:47:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this->setMessages(messages);
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return Success;
|
2018-07-05 22:47:51 +02:00
|
|
|
});
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-07-05 22:47:51 +02:00
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
} // namespace chatterino
|