2018-07-05 22:47:51 +02:00
|
|
|
#include "LogsPopup.hpp"
|
|
|
|
|
|
|
|
#include "IrcMessage"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Channel.hpp"
|
2018-07-05 22:47:51 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-11-03 15:37:56 +01:00
|
|
|
#include "debug/Log.hpp"
|
2018-10-27 10:53:12 +02:00
|
|
|
#include "providers/twitch/PartialTwitchUser.hpp"
|
2018-07-05 22:47:51 +02:00
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
|
|
|
#include "providers/twitch/TwitchMessageBuilder.hpp"
|
2019-07-31 23:03:55 +02:00
|
|
|
#include "util/PostToThread.hpp"
|
2018-07-05 22:47:51 +02:00
|
|
|
#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()
|
2018-08-11 22:23:06 +02:00
|
|
|
: channel_(Channel::getEmpty())
|
2018-07-05 22:47:51 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-03 15:37:56 +01:00
|
|
|
void LogsPopup::setChannelName(QString channelName)
|
|
|
|
{
|
|
|
|
this->channelName_ = channelName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::setChannel(std::shared_ptr<Channel> channel)
|
2018-07-05 22:47:51 +02:00
|
|
|
{
|
|
|
|
this->channel_ = channel;
|
2018-11-03 15:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::setTargetUserName(QString userName)
|
|
|
|
{
|
2018-07-05 22:47:51 +02:00
|
|
|
this->userName_ = userName;
|
2018-11-03 15:37:56 +01:00
|
|
|
}
|
2018-10-27 10:53:12 +02:00
|
|
|
|
2018-11-03 15:37:56 +01:00
|
|
|
void LogsPopup::getLogs()
|
|
|
|
{
|
|
|
|
if (this->channel_ && !this->channel_->isEmpty())
|
2018-10-27 10:53:12 +02:00
|
|
|
{
|
2018-11-03 15:37:56 +01:00
|
|
|
if (auto twitchChannel =
|
|
|
|
dynamic_cast<TwitchChannel *>(this->channel_.get()))
|
|
|
|
{
|
|
|
|
this->channelName_ = twitchChannel->getName();
|
|
|
|
this->getLogviewerLogs(twitchChannel->roomId());
|
|
|
|
|
|
|
|
this->setWindowTitle(this->userName_ + "'s logs in #" +
|
|
|
|
this->channelName_);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-27 10:53:12 +02:00
|
|
|
}
|
2018-11-03 15:37:56 +01:00
|
|
|
|
|
|
|
if (!this->channelName_.isEmpty())
|
2018-10-27 10:53:12 +02:00
|
|
|
{
|
2018-11-03 15:37:56 +01:00
|
|
|
PartialTwitchUser::byName(this->channelName_)
|
|
|
|
.getId(
|
|
|
|
[=](const QString &roomID) { this->getLogviewerLogs(roomID); },
|
|
|
|
this);
|
2018-10-27 10:53:12 +02:00
|
|
|
return;
|
2018-11-03 15:37:56 +01:00
|
|
|
}
|
2018-10-27 10:53:12 +02:00
|
|
|
|
2018-11-03 15:37:56 +01:00
|
|
|
log("Unable to get logs, no channel name or something specified");
|
2018-07-05 22:47:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-27 10:53:12 +02:00
|
|
|
void LogsPopup::getLogviewerLogs(const QString &roomID)
|
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")
|
2018-10-27 10:53:12 +02:00
|
|
|
.arg(this->channelName_, this->userName_);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url)
|
|
|
|
.caller(this)
|
2019-08-20 23:29:11 +02:00
|
|
|
.onError([this](int /*errorCode*/) {
|
2019-08-20 21:50:36 +02:00
|
|
|
this->getOverrustleLogs();
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.onSuccess([this, roomID](auto result) -> Outcome {
|
|
|
|
auto data = result.parseJson();
|
|
|
|
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;");
|
|
|
|
message.insert(1, QString("tmi-sent-ts=%10000;")
|
|
|
|
.arg(messageObject["time"].toInt()));
|
|
|
|
message.insert(1, QString("room-id=%1;").arg(roomID));
|
|
|
|
|
|
|
|
MessageParseArgs args;
|
|
|
|
auto ircMessage =
|
|
|
|
Communi::IrcMessage::fromData(message.toUtf8(), nullptr);
|
|
|
|
auto privMsg =
|
|
|
|
static_cast<Communi::IrcPrivateMessage *>(ircMessage);
|
|
|
|
TwitchMessageBuilder builder(this->channel_.get(), privMsg,
|
|
|
|
args);
|
|
|
|
messages.push_back(builder.build());
|
|
|
|
}
|
2019-08-21 02:00:42 +02:00
|
|
|
|
|
|
|
messages.push_back(
|
|
|
|
MessageBuilder(systemMessage,
|
|
|
|
"Logs provided by https://cbenni.com")
|
|
|
|
.release());
|
2019-08-20 21:50:36 +02:00
|
|
|
this->setMessages(messages);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-07-05 22:47:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogsPopup::getOverrustleLogs()
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
QString url =
|
|
|
|
QString("https://overrustlelogs.net/api/v1/stalk/%1/%2.json?limit=500")
|
2018-10-27 10:53:12 +02:00
|
|
|
.arg(this->channelName_, this->userName_);
|
2018-07-05 22:47:51 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url)
|
|
|
|
.caller(this)
|
2019-08-20 23:29:11 +02:00
|
|
|
.onError([this](int /*errorCode*/) {
|
2019-08-20 21:50:36 +02:00
|
|
|
auto box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Error getting logs",
|
|
|
|
"No logs could be found for channel " + this->channelName_);
|
2019-08-21 02:00:42 +02:00
|
|
|
box->setWindowFlag(Qt::WindowStaysOnTopHint);
|
2019-08-20 21:50:36 +02:00
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->show();
|
|
|
|
box->raise();
|
2019-08-21 02:16:33 +02:00
|
|
|
this->close();
|
|
|
|
box->exec();
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.onSuccess([this](auto result) -> Outcome {
|
|
|
|
auto data = result.parseJson();
|
|
|
|
std::vector<MessagePtr> messages;
|
|
|
|
if (data.contains("lines"))
|
|
|
|
{
|
|
|
|
QJsonArray dataMessages = data.value("lines").toArray();
|
|
|
|
for (auto i : dataMessages)
|
|
|
|
{
|
|
|
|
QJsonObject singleMessage = i.toObject();
|
|
|
|
QTime timeStamp =
|
|
|
|
QDateTime::fromSecsSinceEpoch(
|
|
|
|
singleMessage.value("timestamp").toInt())
|
|
|
|
.time();
|
|
|
|
|
|
|
|
MessageBuilder builder;
|
|
|
|
builder.emplace<TimestampElement>(timeStamp);
|
|
|
|
builder.emplace<TextElement>(this->userName_,
|
|
|
|
MessageElementFlag::Username,
|
|
|
|
MessageColor::System);
|
|
|
|
builder.emplace<TextElement>(
|
|
|
|
singleMessage.value("text").toString(),
|
|
|
|
MessageElementFlag::Text, MessageColor::Text);
|
|
|
|
messages.push_back(builder.release());
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 02:00:42 +02:00
|
|
|
messages.push_back(
|
|
|
|
MessageBuilder(systemMessage,
|
|
|
|
"Logs provided by https://overrustlelogs.net")
|
|
|
|
.release());
|
2019-08-20 21:50:36 +02:00
|
|
|
this->setMessages(messages);
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-07-05 22:47:51 +02:00
|
|
|
}
|
|
|
|
} // namespace chatterino
|