fix: Display Sent IRC Messages Like Received Ones (#4027)

This commit is contained in:
nerix
2022-10-01 17:36:22 +02:00
committed by GitHub
parent a275a1793a
commit ba586f01d0
8 changed files with 230 additions and 186 deletions
+36 -12
View File
@@ -4,7 +4,9 @@
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/irc/IrcCommands.hpp"
#include "providers/irc/IrcMessageBuilder.hpp"
#include "providers/irc/IrcServer.hpp"
#include "util/Helpers.hpp"
namespace chatterino {
@@ -33,20 +35,42 @@ void IrcChannel::sendMessage(const QString &message)
}
else
{
if (this->server())
if (this->server() != nullptr)
{
this->server()->sendMessage(this->getName(), message);
MessageBuilder builder;
builder.emplace<TimestampElement>();
const auto &nick = this->server()->nick();
builder.emplace<TextElement>(nick + ":", MessageElementFlag::Username)
->setLink({Link::UserInfo, nick});
builder.emplace<TextElement>(message, MessageElementFlag::Text);
builder.message().messageText = message;
builder.message().searchText = nick + ": " + message;
builder.message().loginName = nick;
builder.message().displayName = nick;
this->addMessage(builder.release());
MessageBuilder builder;
builder
.emplace<TextElement>("#" + this->getName(),
MessageElementFlag::ChannelName,
MessageColor::System)
->setLink({Link::JumpToChannel, this->getName()});
auto now = QDateTime::currentDateTime();
builder.emplace<TimestampElement>(now.time());
builder.message().serverReceivedTime = now;
auto username = this->server()->nick();
builder
.emplace<TextElement>(
username + ":", MessageElementFlag::Username,
getRandomColor(username), FontStyle::ChatMediumBold)
->setLink({Link::UserInfo, username});
builder.message().loginName = username;
builder.message().displayName = username;
// message
builder.addIrcMessageText(message);
builder.message().messageText = message;
builder.message().searchText = username + ": " + message;
this->addMessage(builder.release());
}
else
{
this->addMessage(makeSystemMessage("You are not connected."));
}
}
}
+2 -130
View File
@@ -16,14 +16,6 @@
#include "util/IrcHelpers.hpp"
#include "widgets/Window.hpp"
namespace {
QRegularExpression IRC_COLOR_PARSE_REGEX(
"(\u0003(\\d{1,2})?(,(\\d{1,2}))?|\u000f)",
QRegularExpression::UseUnicodePropertiesOption);
} // namespace
namespace chatterino {
IrcMessageBuilder::IrcMessageBuilder(
@@ -63,10 +55,9 @@ MessagePtr IrcMessageBuilder::build()
this->appendUsername();
// words
this->addWords(this->originalMessage_.split(' '));
// message
this->addIrcMessageText(this->originalMessage_);
this->message().messageText = this->originalMessage_;
this->message().searchText = this->message().localizedName + " " +
this->userName + ": " + this->originalMessage_;
@@ -82,125 +73,6 @@ MessagePtr IrcMessageBuilder::build()
return this->release();
}
void IrcMessageBuilder::addWords(const QStringList &words)
{
MessageColor defaultColorType = this->textColor_;
auto defaultColor = defaultColorType.getColor(*getApp()->themes);
QColor textColor = defaultColor;
int fg = -1;
int bg = -1;
for (auto word : words)
{
if (word.isEmpty())
{
continue;
}
auto string = QString(word);
// Actually just text
auto linkString = this->matchLink(string);
auto link = Link();
if (!linkString.isEmpty())
{
this->addLink(string, linkString);
continue;
}
// Does the word contain a color changer? If so, split on it.
// Add color indicators, then combine into the same word with the color being changed
auto i = IRC_COLOR_PARSE_REGEX.globalMatch(string);
if (!i.hasNext())
{
this->addText(string, textColor);
continue;
}
int lastPos = 0;
while (i.hasNext())
{
auto match = i.next();
if (lastPos != match.capturedStart() && match.capturedStart() != 0)
{
if (fg >= 0 && fg <= 98)
{
textColor = IRC_COLORS[fg];
getApp()->themes->normalizeColor(textColor);
}
else
{
textColor = defaultColor;
}
this->addText(
string.mid(lastPos, match.capturedStart() - lastPos),
textColor, false);
lastPos = match.capturedStart() + match.capturedLength();
}
if (!match.captured(1).isEmpty())
{
fg = -1;
bg = -1;
}
if (!match.captured(2).isEmpty())
{
fg = match.captured(2).toInt(nullptr);
}
else
{
fg = -1;
}
if (!match.captured(4).isEmpty())
{
bg = match.captured(4).toInt(nullptr);
}
else if (fg == -1)
{
bg = -1;
}
lastPos = match.capturedStart() + match.capturedLength();
}
if (fg >= 0 && fg <= 98)
{
textColor = IRC_COLORS[fg];
getApp()->themes->normalizeColor(textColor);
}
else
{
textColor = defaultColor;
}
this->addText(string.mid(lastPos), textColor);
}
this->message().elements.back()->setTrailingSpace(false);
}
void IrcMessageBuilder::addText(const QString &text, const QColor &color,
bool addSpace)
{
this->textColor_ = color;
for (auto &variant : getApp()->emotes->emojis.parse(text))
{
boost::apply_visitor(
[&](auto &&arg) {
this->addTextOrEmoji(arg);
},
variant);
if (!addSpace)
{
this->message().elements.back()->setTrailingSpace(false);
}
}
}
void IrcMessageBuilder::appendUsername()
{
QString username = this->userName;
-4
View File
@@ -40,10 +40,6 @@ public:
private:
void appendUsername();
void addWords(const QStringList &words);
void addText(const QString &text, const QColor &color,
bool addSpace = true);
};
} // namespace chatterino