refactored MessageBuilder

This commit is contained in:
fourtf
2018-08-07 01:35:24 +02:00
parent f71ff08e68
commit c26422aec1
29 changed files with 964 additions and 970 deletions
+10 -7
View File
@@ -3,6 +3,7 @@
#include "common/Common.hpp"
#include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include <QCoreApplication>
@@ -189,8 +190,8 @@ void AbstractIrcServer::onConnected()
{
std::lock_guard<std::mutex> lock(this->channelMutex);
MessagePtr connMsg = Message::createSystemMessage("connected to chat");
MessagePtr reconnMsg = Message::createSystemMessage("reconnected to chat");
auto connected = makeSystemMessage("connected to chat");
auto reconnected = makeSystemMessage("reconnected to chat");
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
@@ -205,11 +206,12 @@ void AbstractIrcServer::onConnected()
Message::DisconnectedMessage;
if (replaceMessage) {
chan->replaceMessage(snapshot[snapshot.getLength() - 1], reconnMsg);
chan->replaceMessage(snapshot[snapshot.getLength() - 1],
reconnected);
continue;
}
chan->addMessage(connMsg);
chan->addMessage(connected);
}
}
@@ -217,8 +219,9 @@ void AbstractIrcServer::onDisconnected()
{
std::lock_guard<std::mutex> lock(this->channelMutex);
MessagePtr msg = Message::createSystemMessage("disconnected from chat");
msg->flags |= Message::DisconnectedMessage;
MessageBuilder b(systemMessage, "disconnected from chat");
b->flags |= Message::DisconnectedMessage;
auto disconnected = b.release();
for (std::weak_ptr<Channel> &weak : this->channels.values()) {
std::shared_ptr<Channel> chan = weak.lock();
@@ -226,7 +229,7 @@ void AbstractIrcServer::onDisconnected()
continue;
}
chan->addMessage(msg);
chan->addMessage(disconnected);
}
}
+22 -17
View File
@@ -60,13 +60,16 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
TwitchMessageBuilder builder(chan.get(), _message, args, content, isAction);
if (isSub || !builder.isIgnored()) {
MessagePtr msg = builder.build();
if (isSub) {
msg->flags |= Message::Subscription;
msg->flags &= ~Message::Highlighted;
} else {
if (msg->flags & Message::Highlighted) {
builder->flags |= Message::Subscription;
builder->flags &= ~Message::Highlighted;
}
auto highlighted = bool(builder->flags & Message::Highlighted);
auto msg = builder.build();
if (!isSub) {
if (highlighted) {
server.mentionsChannel->addMessage(msg);
getApp()->highlights->addHighlight(msg);
}
@@ -151,8 +154,8 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
// check if the chat has been cleared by a moderator
if (message->parameters().length() == 1) {
chan->disableAllMessages();
chan->addMessage(Message::createSystemMessage(
"Chat has been cleared by a moderator."));
chan->addMessage(
makeSystemMessage("Chat has been cleared by a moderator."));
return;
}
@@ -170,8 +173,9 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
reason = v.toString();
}
auto timeoutMsg = Message::createTimeoutMessage(username, durationInSeconds,
reason, false);
auto timeoutMsg = MessageBuilder(timeoutMessage, username,
durationInSeconds, reason, false)
.release();
chan->addOrReplaceTimeout(timeoutMsg);
// refresh all
@@ -216,17 +220,17 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
false);
if (!builder.isIgnored()) {
app->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
MessagePtr _message = builder.build();
if (_message->flags & Message::Highlighted) {
app->twitch.server->mentionsChannel->addMessage(_message);
}
app->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
c->addMessage(_message);
_message->flags |= Message::DoNotTriggerNotification;
// _message->flags |= Message::DoNotTriggerNotification;
if (app->settings->inlineWhispers) {
app->twitch.server->forEachChannel([_message](ChannelPtr channel) {
@@ -262,10 +266,11 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
auto it = tags.find("system-msg");
if (it != tags.end()) {
auto newMessage =
Message::createSystemMessage(parseTagString(it.value().toString()));
auto b = MessageBuilder(systemMessage,
parseTagString(it.value().toString()));
newMessage->flags |= Message::Subscription;
b->flags |= Message::Subscription;
auto newMessage = b.release();
QString channelName;
@@ -306,7 +311,7 @@ void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message)
void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
auto app = getApp();
MessagePtr msg = Message::createSystemMessage(message->content());
MessagePtr msg = makeSystemMessage(message->content());
QString channelName;
if (!trimChannelName(message->target(), channelName)) {
+13 -13
View File
@@ -68,7 +68,7 @@ TwitchChannel::TwitchChannel(const QString &name)
// debugging
#if 0
for (int i = 0; i < 1000; i++) {
this->addMessage(Message::createSystemMessage("asdf"));
this->addMessage(makeSystemMessage("asdf"));
}
#endif
}
@@ -104,9 +104,9 @@ void TwitchChannel::sendMessage(const QString &message)
if (!app->accounts->twitch.isLoggedIn()) {
// XXX: It would be nice if we could add a link here somehow that opened
// the "account manager" dialog
this->addMessage(Message::createSystemMessage(
"You need to log in to send messages. You can "
"link your Twitch account in the settings."));
this->addMessage(
makeSystemMessage("You need to log in to send messages. You can "
"link your Twitch account in the settings."));
return;
}
@@ -159,7 +159,7 @@ bool TwitchChannel::isBroadcaster() const
return this->getName() == app->accounts->twitch.getCurrent()->getUserName();
}
void TwitchChannel::addRecentChatter(const std::shared_ptr<Message> &message)
void TwitchChannel::addRecentChatter(const MessagePtr &message)
{
assert(!message->loginName.isEmpty());
@@ -183,11 +183,11 @@ void TwitchChannel::addJoinedUser(const QString &user)
QTimer::singleShot(500, &this->lifetimeGuard_, [this] {
auto joinedUsers = this->joinedUsers_.access();
auto message = Message::createSystemMessage(
"Users joined: " + joinedUsers->join(", "));
message->flags |= Message::Collapsed;
MessageBuilder builder(systemMessage,
"Users joined: " + joinedUsers->join(", "));
builder->flags |= Message::Collapsed;
joinedUsers->clear();
this->addMessage(message);
this->addMessage(builder.release());
this->joinedUsersMergeQueued_ = false;
});
}
@@ -211,10 +211,10 @@ void TwitchChannel::addPartedUser(const QString &user)
QTimer::singleShot(500, &this->lifetimeGuard_, [this] {
auto partedUsers = this->partedUsers_.access();
auto message = Message::createSystemMessage(
"Users parted: " + partedUsers->join(", "));
message->flags |= Message::Collapsed;
this->addMessage(message);
MessageBuilder builder(systemMessage,
"Users parted: " + partedUsers->join(", "));
builder->flags |= Message::Collapsed;
this->addMessage(builder.release());
partedUsers->clear();
this->partedUsersMergeQueued_ = false;
+1 -1
View File
@@ -54,7 +54,7 @@ public:
virtual void sendMessage(const QString &message) override;
// Auto completion
void addRecentChatter(const std::shared_ptr<Message> &message) final;
void addRecentChatter(const MessagePtr &message) final;
void addJoinedUser(const QString &user);
void addPartedUser(const QString &user);
+10 -14
View File
@@ -98,7 +98,7 @@ MessagePtr TwitchMessageBuilder::build()
// MessageElement::Collapsed);
// }
//#endif
this->message_->flags |= Message::Collapsed;
this->message().flags |= Message::Collapsed;
// PARSING
this->parseMessageID();
@@ -175,9 +175,9 @@ MessagePtr TwitchMessageBuilder::build()
this->addWords(splits, twitchEmotes);
this->message_->searchText = this->userName + ": " + this->originalMessage_;
this->message().searchText = this->userName + ": " + this->originalMessage_;
return this->getMessage();
return this->release();
}
void TwitchMessageBuilder::addWords(
@@ -376,7 +376,7 @@ void TwitchMessageBuilder::parseUsername()
// this->userName + ")";
// }
this->message_->loginName = this->userName;
this->message().loginName = this->userName;
}
void TwitchMessageBuilder::appendUsername()
@@ -384,7 +384,7 @@ void TwitchMessageBuilder::appendUsername()
auto app = getApp();
QString username = this->userName;
this->message_->loginName = username;
this->message().loginName = username;
QString localizedName;
auto iterator = this->tags.find("display-name");
@@ -396,12 +396,12 @@ void TwitchMessageBuilder::appendUsername()
Qt::CaseInsensitive) == 0) {
username = displayName;
this->message_->displayName = displayName;
this->message().displayName = displayName;
} else {
localizedName = displayName;
this->message_->displayName = username;
this->message_->localizedName = displayName;
this->message().displayName = username;
this->message().localizedName = displayName;
}
}
@@ -573,7 +573,7 @@ void TwitchMessageBuilder::parseHighlights(bool isPastMsg)
}
}
this->setHighlight(doHighlight);
this->message().flags.set(Message::Highlighted, doHighlight);
if (!isPastMsg) {
if (playSound &&
@@ -586,10 +586,6 @@ void TwitchMessageBuilder::parseHighlights(bool isPastMsg)
2500);
}
}
if (doHighlight) {
this->message_->flags |= Message::Highlighted;
}
}
}
@@ -662,7 +658,7 @@ Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name)
// fourtf: this is ugly
// maybe put the individual badges into a map instead of this
//mess
// mess
void TwitchMessageBuilder::appendTwitchBadges()
{
auto app = getApp();
+2 -2
View File
@@ -217,7 +217,7 @@ void TwitchServer::onMessageSendRequested(TwitchChannel *channel,
lastMessage.back() + minMessageOffset > now) {
if (this->lastErrorTimeSpeed_ + 30s < now) {
auto errorMessage =
Message::createSystemMessage("sending messages too fast");
makeSystemMessage("sending messages too fast");
channel->addMessage(errorMessage);
@@ -235,7 +235,7 @@ void TwitchServer::onMessageSendRequested(TwitchChannel *channel,
if (lastMessage.size() >= maxMessageCount) {
if (this->lastErrorTimeAmount_ + 30s < now) {
auto errorMessage =
Message::createSystemMessage("sending too many messages");
makeSystemMessage("sending too many messages");
channel->addMessage(errorMessage);