2018-06-26 14:09:39 +02:00
|
|
|
#include "messages/Message.hpp"
|
|
|
|
#include "MessageElement.hpp"
|
|
|
|
#include "providers/twitch/PubsubActions.hpp"
|
|
|
|
#include "util/IrcHelpers.hpp"
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
using SBHighlight = chatterino::ScrollbarHighlight;
|
2018-01-06 03:48:56 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2018-06-19 22:02:51 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
void Message::addElement(MessageElement *element)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
this->elements_.push_back(std::unique_ptr<MessageElement>(element));
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
const std::vector<std::unique_ptr<MessageElement>> &Message::getElements() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->elements_;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-01-07 20:43:55 +01:00
|
|
|
|
2018-01-06 03:48:56 +01:00
|
|
|
SBHighlight Message::getScrollBarHighlight() const
|
|
|
|
{
|
2018-01-28 03:29:42 +01:00
|
|
|
if (this->flags & Message::Highlighted) {
|
2018-01-06 03:48:56 +01:00
|
|
|
return SBHighlight(SBHighlight::Highlight);
|
2018-06-04 12:23:23 +02:00
|
|
|
} else if (this->flags & Message::Subscription) {
|
|
|
|
return SBHighlight(SBHighlight::Subscription);
|
2018-01-06 03:48:56 +01:00
|
|
|
}
|
|
|
|
return SBHighlight();
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
// Static
|
|
|
|
MessagePtr Message::createSystemMessage(const QString &text)
|
2017-12-16 19:03:22 +01:00
|
|
|
{
|
2018-01-11 20:16:25 +01:00
|
|
|
MessagePtr message(new Message);
|
2017-12-16 19:03:22 +01:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
message->addElement(new TimestampElement(QTime::currentTime()));
|
2018-08-06 21:17:03 +02:00
|
|
|
message->addElement(
|
|
|
|
new TextElement(text, MessageElement::Text, MessageColor::System));
|
2018-05-28 08:34:54 +02:00
|
|
|
message->flags |= MessageFlags::System;
|
|
|
|
message->flags |= MessageFlags::DoNotTriggerNotification;
|
2018-01-28 03:29:42 +01:00
|
|
|
message->searchText = text;
|
2017-12-16 19:03:22 +01:00
|
|
|
|
2018-01-28 14:24:37 +01:00
|
|
|
return message;
|
2017-12-16 19:03:22 +01:00
|
|
|
}
|
2017-12-16 18:11:36 +01:00
|
|
|
|
2018-06-04 12:23:23 +02:00
|
|
|
MessagePtr Message::createMessage(const QString &text)
|
|
|
|
{
|
|
|
|
MessagePtr message(new Message);
|
|
|
|
|
|
|
|
message->addElement(new TimestampElement(QTime::currentTime()));
|
2018-08-06 21:17:03 +02:00
|
|
|
message->addElement(
|
|
|
|
new TextElement(text, MessageElement::Text, MessageColor::Text));
|
2018-06-04 12:23:23 +02:00
|
|
|
message->searchText = text;
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2018-06-19 22:02:51 +02:00
|
|
|
namespace {
|
|
|
|
|
2018-07-11 16:58:57 +02:00
|
|
|
void appendDuration(int count, QChar &&order, QString &outString)
|
2018-06-19 22:02:51 +02:00
|
|
|
{
|
2018-07-11 16:58:57 +02:00
|
|
|
outString.append(QString::number(count));
|
|
|
|
outString.append(order);
|
2018-06-19 22:02:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 16:09:28 +02:00
|
|
|
QString makeDuration(int timeoutSeconds)
|
2018-06-19 22:02:51 +02:00
|
|
|
{
|
2018-07-11 16:53:19 +02:00
|
|
|
QString res;
|
2018-07-11 16:09:28 +02:00
|
|
|
|
|
|
|
int seconds = timeoutSeconds % 60;
|
|
|
|
int timeoutMinutes = timeoutSeconds / 60;
|
|
|
|
int minutes = timeoutMinutes % 60;
|
|
|
|
int timeoutHours = timeoutMinutes / 60;
|
|
|
|
int hours = timeoutHours % 24;
|
|
|
|
int days = timeoutHours / 24;
|
|
|
|
if (days > 0) {
|
2018-07-11 16:58:57 +02:00
|
|
|
appendDuration(days, 'd', res);
|
2018-06-19 22:02:51 +02:00
|
|
|
}
|
2018-07-11 16:09:28 +02:00
|
|
|
if (hours > 0) {
|
2018-07-11 16:58:57 +02:00
|
|
|
if (!res.isEmpty()) {
|
2018-07-11 16:09:28 +02:00
|
|
|
res.append(" ");
|
2018-07-11 16:58:57 +02:00
|
|
|
}
|
|
|
|
appendDuration(hours, 'h', res);
|
2018-07-11 16:09:28 +02:00
|
|
|
}
|
|
|
|
if (minutes > 0) {
|
2018-07-11 16:58:57 +02:00
|
|
|
if (!res.isEmpty()) {
|
2018-07-11 16:09:28 +02:00
|
|
|
res.append(" ");
|
2018-07-11 16:58:57 +02:00
|
|
|
}
|
|
|
|
appendDuration(minutes, 'm', res);
|
2018-07-11 16:09:28 +02:00
|
|
|
}
|
|
|
|
if (seconds > 0) {
|
2018-07-11 16:58:57 +02:00
|
|
|
if (!res.isEmpty()) {
|
2018-07-11 16:09:28 +02:00
|
|
|
res.append(" ");
|
2018-07-11 16:58:57 +02:00
|
|
|
}
|
|
|
|
appendDuration(seconds, 's', res);
|
2018-07-11 16:09:28 +02:00
|
|
|
}
|
|
|
|
return res;
|
2018-06-19 22:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
MessagePtr Message::createTimeoutMessage(const QString &username,
|
|
|
|
const QString &durationInSeconds,
|
|
|
|
const QString &reason,
|
|
|
|
bool multipleTimes)
|
2017-12-16 19:08:32 +01:00
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
|
|
|
|
text.append(username);
|
2017-12-16 19:45:23 +01:00
|
|
|
if (!durationInSeconds.isEmpty()) {
|
|
|
|
text.append(" has been timed out");
|
|
|
|
|
|
|
|
// TODO: Implement who timed the user out
|
|
|
|
|
|
|
|
text.append(" for ");
|
|
|
|
bool ok = true;
|
2018-06-19 22:02:51 +02:00
|
|
|
int timeoutSeconds = durationInSeconds.toInt(&ok);
|
|
|
|
if (ok) {
|
|
|
|
text.append(makeDuration(timeoutSeconds));
|
2017-12-16 19:45:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text.append(" has been permanently banned");
|
2017-12-16 19:08:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reason.length() > 0) {
|
|
|
|
text.append(": \"");
|
2018-06-26 17:06:17 +02:00
|
|
|
text.append(parseTagString(reason));
|
2017-12-16 19:08:32 +01:00
|
|
|
text.append("\"");
|
|
|
|
}
|
|
|
|
text.append(".");
|
|
|
|
|
2018-01-05 23:14:55 +01:00
|
|
|
if (multipleTimes) {
|
|
|
|
text.append(" (multiple times)");
|
|
|
|
}
|
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
MessagePtr message = Message::createSystemMessage(text);
|
2018-03-31 13:59:17 +02:00
|
|
|
message->flags.EnableFlag(MessageFlags::System);
|
|
|
|
message->flags.EnableFlag(MessageFlags::Timeout);
|
2018-01-05 23:14:55 +01:00
|
|
|
message->timeoutUser = username;
|
|
|
|
return message;
|
2017-12-16 18:11:36 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
MessagePtr Message::createTimeoutMessage(const BanAction &action,
|
|
|
|
uint32_t count)
|
2018-04-27 18:35:31 +02:00
|
|
|
{
|
|
|
|
MessagePtr msg(new Message);
|
|
|
|
|
|
|
|
msg->addElement(new TimestampElement(QTime::currentTime()));
|
|
|
|
msg->flags.EnableFlag(MessageFlags::System);
|
|
|
|
msg->flags.EnableFlag(MessageFlags::Timeout);
|
|
|
|
|
|
|
|
msg->timeoutUser = action.target.name;
|
|
|
|
msg->count = count;
|
|
|
|
|
|
|
|
QString text;
|
|
|
|
|
|
|
|
if (action.isBan()) {
|
|
|
|
if (action.reason.isEmpty()) {
|
|
|
|
text = QString("%1 banned %2.") //
|
|
|
|
.arg(action.source.name)
|
|
|
|
.arg(action.target.name);
|
|
|
|
} else {
|
|
|
|
text = QString("%1 banned %2: \"%3\".") //
|
|
|
|
.arg(action.source.name)
|
|
|
|
.arg(action.target.name)
|
|
|
|
.arg(action.reason);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (action.reason.isEmpty()) {
|
2018-06-19 22:02:51 +02:00
|
|
|
text = QString("%1 timed out %2 for %3.") //
|
2018-04-27 18:35:31 +02:00
|
|
|
.arg(action.source.name)
|
|
|
|
.arg(action.target.name)
|
2018-06-19 22:02:51 +02:00
|
|
|
.arg(makeDuration(action.duration));
|
2018-04-27 18:35:31 +02:00
|
|
|
} else {
|
2018-06-19 22:02:51 +02:00
|
|
|
text = QString("%1 timed out %2 for %3: \"%4\".") //
|
2018-04-27 18:35:31 +02:00
|
|
|
.arg(action.source.name)
|
|
|
|
.arg(action.target.name)
|
2018-06-19 22:02:51 +02:00
|
|
|
.arg(makeDuration(action.duration))
|
2018-04-27 18:35:31 +02:00
|
|
|
.arg(action.reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 1) {
|
|
|
|
text.append(QString(" (%1 times)").arg(count));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
msg->addElement(
|
|
|
|
new TextElement(text, MessageElement::Text, MessageColor::System));
|
2018-04-27 18:35:31 +02:00
|
|
|
msg->searchText = text;
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
MessagePtr Message::createUntimeoutMessage(const UnbanAction &action)
|
2018-04-27 18:35:31 +02:00
|
|
|
{
|
|
|
|
MessagePtr msg(new Message);
|
|
|
|
|
|
|
|
msg->addElement(new TimestampElement(QTime::currentTime()));
|
|
|
|
msg->flags.EnableFlag(MessageFlags::System);
|
|
|
|
msg->flags.EnableFlag(MessageFlags::Untimeout);
|
|
|
|
|
|
|
|
msg->timeoutUser = action.target.name;
|
|
|
|
|
|
|
|
QString text;
|
|
|
|
|
|
|
|
if (action.wasBan()) {
|
|
|
|
text = QString("%1 unbanned %2.") //
|
|
|
|
.arg(action.source.name)
|
|
|
|
.arg(action.target.name);
|
|
|
|
} else {
|
|
|
|
text = QString("%1 untimedout %2.") //
|
|
|
|
.arg(action.source.name)
|
|
|
|
.arg(action.target.name);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
msg->addElement(
|
|
|
|
new TextElement(text, MessageElement::Text, MessageColor::System));
|
2018-04-27 18:35:31 +02:00
|
|
|
msg->searchText = text;
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|