mirror-chatterino2/src/messages/message.cpp
2018-01-05 13:42:23 +01:00

189 lines
4.2 KiB
C++

#include "messages/message.hpp"
#include "channel.hpp"
#include "emojis.hpp"
#include "messages/link.hpp"
#include "singletons/emotemanager.hpp"
#include "singletons/fontmanager.hpp"
#include "singletons/ircmanager.hpp"
#include "singletons/resourcemanager.hpp"
#include "singletons/thememanager.hpp"
#include "util/irchelpers.hpp"
#include <ctime>
#include <list>
#include <tuple>
namespace chatterino {
namespace messages {
bool Message::containsHighlightedPhrase() const
{
return this->highlightTab;
}
void Message::setHighlight(bool value)
{
this->highlightTab = value;
}
const QString &Message::getTimeoutUser() const
{
return this->timeoutUser;
}
int Message::getTimeoutCount() const
{
return this->timeoutCount;
}
const QString &Message::getContent() const
{
if (this->content.isNull()) {
this->updateContent();
}
return this->content;
}
const std::chrono::time_point<std::chrono::system_clock> &Message::getParseTime() const
{
return this->parseTime;
}
std::vector<Word> &Message::getWords()
{
return this->words;
}
bool Message::isDisabled() const
{
return this->disabled;
}
void Message::setDisabled(bool value)
{
this->disabled = value;
}
const QString &Message::getId() const
{
return this->id;
}
bool Message::getCollapsedDefault() const
{
return this->collapsedDefault;
}
void Message::setCollapsedDefault(bool value)
{
this->collapsedDefault = value;
}
bool Message::getDisableCompactEmotes() const
{
return this->disableCompactEmotes;
}
void Message::setDisableCompactEmotes(bool value)
{
this->disableCompactEmotes = value;
}
void Message::updateContent() const
{
QString _content("");
bool first;
for (const Word &word : this->words) {
if (!first) {
_content += "";
}
_content += word.getCopyText();
first = false;
}
this->content = _content;
}
namespace {
void AddCurrentTimestamp(Message *message)
{
std::time_t t;
time(&t);
char timeStampBuffer[69];
// Add word for timestamp with no seconds
strftime(timeStampBuffer, 69, "%H:%M", localtime(&t));
QString timestampNoSeconds(timeStampBuffer);
message->getWords().push_back(Word(timestampNoSeconds, Word::TimestampNoSeconds,
MessageColor(MessageColor::System),
singletons::FontManager::Medium, QString(), QString()));
// Add word for timestamp with seconds
strftime(timeStampBuffer, 69, "%H:%M:%S", localtime(&t));
QString timestampWithSeconds(timeStampBuffer);
message->getWords().push_back(Word(timestampWithSeconds, Word::TimestampWithSeconds,
MessageColor(MessageColor::System),
singletons::FontManager::Medium, QString(), QString()));
}
} // namespace
/// Static
Message *Message::createSystemMessage(const QString &text)
{
Message *message = new Message;
AddCurrentTimestamp(message);
QStringList words = text.split(' ');
for (QString word : words) {
message->getWords().push_back(Word(word, Word::Flags::Default,
MessageColor(MessageColor::Type::System),
singletons::FontManager::Medium, word, QString()));
}
return message;
}
Message *Message::createTimeoutMessage(const QString &username, const QString &durationInSeconds,
const QString &reason)
{
QString text;
text.append(username);
if (!durationInSeconds.isEmpty()) {
text.append(" has been timed out");
// TODO: Implement who timed the user out
text.append(" for ");
text.append(durationInSeconds);
bool ok = true;
int timeoutDuration = durationInSeconds.toInt(&ok);
text.append(" second");
if (ok && timeoutDuration > 1) {
text.append("s");
}
} else {
text.append(" has been permanently banned");
}
if (reason.length() > 0) {
text.append(": \"");
text.append(ParseTagString(reason));
text.append("\"");
}
text.append(".");
return Message::createSystemMessage(text);
}
} // namespace messages
} // namespace chatterino