refactor: make a single MessageBuilder (#5548)

This commit is contained in:
pajlada
2024-08-24 12:18:27 +02:00
committed by GitHub
parent 5170085d7c
commit 175afa8b16
33 changed files with 2819 additions and 3014 deletions
-1
View File
@@ -4,7 +4,6 @@
#include "common/network/NetworkResult.hpp"
#include "common/QLogging.hpp"
#include "providers/recentmessages/Impl.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "util/PostToThread.hpp"
namespace {
+1 -1
View File
@@ -2,9 +2,9 @@
#include "common/Env.hpp"
#include "common/QLogging.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/IrcMessageHandler.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "util/FormatTime.hpp"
#include <QJsonArray>
+19 -23
View File
@@ -20,7 +20,6 @@
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchHelpers.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
@@ -126,7 +125,7 @@ int stripLeadingReplyMention(const QVariantMap &tags, QString &content)
void updateReplyParticipatedStatus(const QVariantMap &tags,
const QString &senderLogin,
TwitchMessageBuilder &builder,
MessageBuilder &builder,
std::shared_ptr<MessageThread> &thread,
bool isNew)
{
@@ -245,7 +244,7 @@ QMap<QString, QString> parseBadges(const QString &badgesString)
void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
const std::vector<MessagePtr> &otherLoaded,
TwitchMessageBuilder &builder)
MessageBuilder &builder)
{
const auto &tags = message->tags();
if (const auto it = tags.find("reply-thread-parent-msg-id");
@@ -481,8 +480,7 @@ std::vector<MessagePtr> parseUserNoticeMessage(Channel *channel,
MessageParseArgs args;
args.trimSubscriberUsername = true;
TwitchMessageBuilder builder(channel, message, args, content,
false);
MessageBuilder builder(channel, message, args, content, false);
builder->flags.set(MessageFlag::Subscription);
builder->flags.unset(MessageFlag::Highlighted);
builtMessages.emplace_back(builder.build());
@@ -566,8 +564,8 @@ std::vector<MessagePtr> parsePrivMessage(Channel *channel,
std::vector<MessagePtr> builtMessages;
MessageParseArgs args;
TwitchMessageBuilder builder(channel, message, args, message->content(),
message->isAction());
MessageBuilder builder(channel, message, args, message->content(),
message->isAction());
if (!builder.isIgnored())
{
builtMessages.emplace_back(builder.build());
@@ -576,7 +574,7 @@ std::vector<MessagePtr> parsePrivMessage(Channel *channel,
if (message->tags().contains(u"pinned-chat-paid-amount"_s))
{
auto ptr = TwitchMessageBuilder::buildHypeChatMessage(message);
auto ptr = MessageBuilder::buildHypeChatMessage(message);
if (ptr)
{
builtMessages.emplace_back(std::move(ptr));
@@ -618,8 +616,8 @@ std::vector<MessagePtr> IrcMessageHandler::parseMessageWithReply(
QString content = privMsg->content();
int messageOffset = stripLeadingReplyMention(privMsg->tags(), content);
MessageParseArgs args;
TwitchMessageBuilder builder(channel, message, args, content,
privMsg->isAction());
MessageBuilder builder(channel, message, args, content,
privMsg->isAction());
builder.setMessageOffset(messageOffset);
populateReply(tc, message, otherLoaded, builder);
@@ -716,7 +714,7 @@ void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message,
if (message->tags().contains(u"pinned-chat-paid-amount"_s))
{
auto ptr = TwitchMessageBuilder::buildHypeChatMessage(message);
auto ptr = MessageBuilder::buildHypeChatMessage(message);
if (ptr)
{
chan->addMessage(ptr, MessageContext::Original);
@@ -865,9 +863,8 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
msg->flags.set(MessageFlag::Disabled);
if (!getSettings()->hideDeletionActions)
{
MessageBuilder builder;
TwitchMessageBuilder::deletionMessage(msg, &builder);
chan->addMessage(builder.release(), MessageContext::Original);
chan->addMessage(MessageBuilder::makeDeletionMessageFromIRC(msg),
MessageContext::Original);
}
}
@@ -947,7 +944,7 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *ircMessage)
auto *c = getApp()->getTwitch()->getWhispersChannel().get();
TwitchMessageBuilder builder(
MessageBuilder builder(
c, ircMessage, args,
ircMessage->parameter(1).replace(COMBINED_FIXER, ZERO_WIDTH_JOINER),
false);
@@ -1163,10 +1160,9 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
hostedChannelName.chop(1);
}
MessageBuilder builder;
TwitchMessageBuilder::hostingSystemMessage(hostedChannelName,
&builder, hostOn);
channel->addMessage(builder.release(), MessageContext::Original);
channel->addMessage(MessageBuilder::makeHostingSystemMessage(
hostedChannelName, hostOn),
MessageContext::Original);
}
else if (tags == "room_mods" || tags == "vips_success")
{
@@ -1193,9 +1189,9 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
.mid(1) // there is a space before the first user
.split(", ");
users.sort(Qt::CaseInsensitive);
TwitchMessageBuilder::listOfUsersSystemMessage(msgParts.at(0),
users, tc, &builder);
channel->addMessage(builder.release(), MessageContext::Original);
channel->addMessage(MessageBuilder::makeListOfUsersMessage(
msgParts.at(0), users, tc),
MessageContext::Original);
}
else
{
@@ -1367,7 +1363,7 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
QString content = originalContent;
int messageOffset = stripLeadingReplyMention(tags, content);
TwitchMessageBuilder builder(channel, message, args, content, isAction);
MessageBuilder builder(channel, message, args, content, isAction);
builder.setMessageOffset(messageOffset);
if (const auto it = tags.find("reply-thread-parent-msg-id");
+33 -27
View File
@@ -12,6 +12,7 @@
#include "messages/Image.hpp"
#include "messages/Link.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "messages/MessageElement.hpp"
#include "messages/MessageThread.hpp"
#include "providers/bttv/BttvEmotes.hpp"
@@ -31,7 +32,6 @@
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
@@ -311,10 +311,9 @@ void TwitchChannel::addChannelPointReward(const ChannelPointReward &reward)
if (!reward.isUserInputRequired)
{
MessageBuilder builder;
TwitchMessageBuilder::appendChannelPointRewardMessage(
reward, &builder, this->isMod(), this->isBroadcaster());
this->addMessage(builder.release(), MessageContext::Original);
this->addMessage(MessageBuilder::makeChannelPointRewardMessage(
reward, this->isMod(), this->isBroadcaster()),
MessageContext::Original);
return;
}
@@ -434,11 +433,11 @@ void TwitchChannel::onLiveStatusChanged(bool isLive, bool isInitialUpdate)
});
// Channel live message
MessageBuilder builder;
TwitchMessageBuilder::liveSystemMessage(this->getDisplayName(),
&builder);
builder.message().id = this->roomId();
this->addMessage(builder.release(), MessageContext::Original);
this->addMessage(
MessageBuilder::makeLiveMessage(
this->getDisplayName(), this->roomId(),
{MessageFlag::System, MessageFlag::DoNotTriggerNotification}),
MessageContext::Original);
}
else
{
@@ -446,10 +445,9 @@ void TwitchChannel::onLiveStatusChanged(bool isLive, bool isInitialUpdate)
<< "[TwitchChannel " << this->getName() << "] Offline";
// Channel offline message
MessageBuilder builder;
TwitchMessageBuilder::offlineSystemMessage(this->getDisplayName(),
&builder);
this->addMessage(builder.release(), MessageContext::Original);
this->addMessage(MessageBuilder::makeOfflineSystemMessage(
this->getDisplayName(), this->roomId()),
MessageContext::Original);
getApp()->getNotifications()->notifyTwitchChannelOffline(
this->roomId());
@@ -1077,19 +1075,27 @@ bool TwitchChannel::tryReplaceLastLiveUpdateAddOrRemove(
// Update the message
this->lastLiveUpdateEmoteNames_.push_back(emoteName);
MessageBuilder replacement;
if (op == MessageFlag::LiveUpdatesAdd)
{
replacement =
MessageBuilder(liveUpdatesAddEmoteMessage, platform,
last->loginName, this->lastLiveUpdateEmoteNames_);
}
else // op == RemoveEmoteMessage
{
replacement =
MessageBuilder(liveUpdatesRemoveEmoteMessage, platform,
last->loginName, this->lastLiveUpdateEmoteNames_);
}
auto makeReplacement = [&](MessageFlag op) -> MessageBuilder {
if (op == MessageFlag::LiveUpdatesAdd)
{
return {
liveUpdatesAddEmoteMessage,
platform,
last->loginName,
this->lastLiveUpdateEmoteNames_,
};
}
// op == RemoveEmoteMessage
return {
liveUpdatesRemoveEmoteMessage,
platform,
last->loginName,
this->lastLiveUpdateEmoteNames_,
};
};
auto replacement = makeReplacement(op);
replacement->flags = last->flags;
+1 -1
View File
@@ -458,7 +458,7 @@ private:
std::vector<boost::signals2::scoped_connection> bSignals_;
friend class TwitchIrcServer;
friend class TwitchMessageBuilder;
friend class MessageBuilder;
friend class IrcMessageHandler;
friend class Commands_E2E_Test;
};
File diff suppressed because it is too large Load Diff
@@ -1,166 +0,0 @@
#pragma once
#include "common/Aliases.hpp"
#include "common/Outcome.hpp"
#include "messages/SharedMessageBuilder.hpp"
#include "pubsubmessages/LowTrustUsers.hpp"
#include <IrcMessage>
#include <QString>
#include <QVariant>
#include <optional>
#include <unordered_map>
namespace chatterino {
struct Emote;
using EmotePtr = std::shared_ptr<const Emote>;
class Channel;
class TwitchChannel;
class MessageThread;
class IgnorePhrase;
struct HelixVip;
using HelixModerator = HelixVip;
struct ChannelPointReward;
struct DeleteAction;
struct TwitchEmoteOccurrence {
int start;
int end;
EmotePtr ptr;
EmoteName name;
bool operator==(const TwitchEmoteOccurrence &other) const
{
return std::tie(this->start, this->end, this->ptr, this->name) ==
std::tie(other.start, other.end, other.ptr, other.name);
}
};
class TwitchMessageBuilder : public SharedMessageBuilder
{
public:
TwitchMessageBuilder() = delete;
explicit TwitchMessageBuilder(Channel *_channel,
const Communi::IrcPrivateMessage *_ircMessage,
const MessageParseArgs &_args);
explicit TwitchMessageBuilder(Channel *_channel,
const Communi::IrcMessage *_ircMessage,
const MessageParseArgs &_args,
QString content, bool isAction);
TwitchChannel *twitchChannel;
[[nodiscard]] bool isIgnored() const override;
bool isIgnoredReply() const;
void triggerHighlights() override;
MessagePtr build() override;
void setThread(std::shared_ptr<MessageThread> thread);
void setParent(MessagePtr parent);
void setMessageOffset(int offset);
static void appendChannelPointRewardMessage(
const ChannelPointReward &reward, MessageBuilder *builder, bool isMod,
bool isBroadcaster);
// Message in the /live chat for channel going live
static void liveMessage(const QString &channelName,
MessageBuilder *builder);
// Messages in normal chat for channel stuff
static void liveSystemMessage(const QString &channelName,
MessageBuilder *builder);
static void offlineSystemMessage(const QString &channelName,
MessageBuilder *builder);
static void hostingSystemMessage(const QString &channelName,
MessageBuilder *builder, bool hostOn);
static void deletionMessage(const MessagePtr originalMessage,
MessageBuilder *builder);
static void deletionMessage(const DeleteAction &action,
MessageBuilder *builder);
static void listOfUsersSystemMessage(QString prefix, QStringList users,
Channel *channel,
MessageBuilder *builder);
static void listOfUsersSystemMessage(
QString prefix, const std::vector<HelixModerator> &users,
Channel *channel, MessageBuilder *builder);
static MessagePtr buildHypeChatMessage(Communi::IrcPrivateMessage *message);
static std::pair<MessagePtr, MessagePtr> makeAutomodMessage(
const AutomodAction &action, const QString &channelName);
static MessagePtr makeAutomodInfoMessage(const AutomodInfoAction &action);
static std::pair<MessagePtr, MessagePtr> makeLowTrustUserMessage(
const PubSubLowTrustUsersMessage &action, const QString &channelName,
const TwitchChannel *twitchChannel);
static MessagePtr makeLowTrustUpdateMessage(
const PubSubLowTrustUsersMessage &action);
// Shares some common logic from SharedMessageBuilder::parseBadgeTag
static std::unordered_map<QString, QString> parseBadgeInfoTag(
const QVariantMap &tags);
static std::vector<TwitchEmoteOccurrence> parseTwitchEmotes(
const QVariantMap &tags, const QString &originalMessage,
int messageOffset);
static void processIgnorePhrases(
const std::vector<IgnorePhrase> &phrases, QString &originalMessage,
std::vector<TwitchEmoteOccurrence> &twitchEmotes);
private:
void parseUsernameColor() override;
void parseUsername() override;
void parseMessageID();
void parseRoomID();
// Parse & build thread information into the message
// Will read information from thread_ or from IRC tags
void parseThread();
void appendUsername();
Outcome tryAppendEmote(const EmoteName &name) override;
void addWords(const QStringList &words,
const std::vector<TwitchEmoteOccurrence> &twitchEmotes);
void addTextOrEmoji(EmotePtr emote) override;
void addTextOrEmoji(const QString &value) override;
void appendTwitchBadges();
void appendChatterinoBadges();
void appendFfzBadges();
void appendSeventvBadges();
Outcome tryParseCheermote(const QString &string);
bool shouldAddModerationElements() const;
QString roomID_;
bool hasBits_ = false;
QString bits;
int bitsLeft{};
bool bitsStacked = false;
bool historicalMessage_ = false;
std::shared_ptr<MessageThread> thread_;
MessagePtr parent_;
/**
* Starting offset to be used on index-based operations on `originalMessage_`.
*
* For example:
* originalMessage_ = "there"
* messageOffset_ = 4
* (the irc message is "hey there")
*
* then the index 6 would resolve to 6 - 4 = 2 => 'e'
*/
int messageOffset_ = 0;
QString userId_;
bool senderIsBroadcaster{};
};
} // namespace chatterino