Skip to content

Commit

Permalink
refactor: decouple reply parsing from MessageBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Oct 19, 2024
1 parent dab97b3 commit 8c515c2
Showing 1 changed file with 57 additions and 34 deletions.
91 changes: 57 additions & 34 deletions src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,21 @@ int stripLeadingReplyMention(const QVariantMap &tags, QString &content)
return 0;
}

void updateReplyParticipatedStatus(const QVariantMap &tags,
const QString &senderLogin,
MessageBuilder &builder,
std::shared_ptr<MessageThread> &thread,
bool isNew)
[[nodiscard]] bool shouldHighlightReplyThread(
const QVariantMap &tags, const QString &senderLogin,
std::shared_ptr<MessageThread> &thread, bool isNew)
{
const auto &currentLogin =
getApp()->getAccounts()->twitch.getCurrent()->getUserName();

if (thread->subscribed())
{
builder.message().flags.set(MessageFlag::SubscribedThread);
return;
return true;
}

if (thread->unsubscribed())
{
return;
return false;
}

if (getSettings()->autoSubToParticipatedThreads)
Expand All @@ -154,8 +151,7 @@ void updateReplyParticipatedStatus(const QVariantMap &tags,
if (name == currentLogin)
{
thread->markSubscribed();
builder.message().flags.set(MessageFlag::SubscribedThread);
return; // already marked as participated
return true; // already marked as participated
}
}
}
Expand All @@ -166,6 +162,8 @@ void updateReplyParticipatedStatus(const QVariantMap &tags,
// don't set the highlight here
}
}

return false;
}

ChannelPtr channelOrEmptyByTarget(const QString &target,
Expand Down Expand Up @@ -242,10 +240,18 @@ QMap<QString, QString> parseBadges(const QString &badgesString)
return badges;
}

void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
const std::vector<MessagePtr> &otherLoaded,
MessageBuilder &builder)
struct ReplyContext {
std::shared_ptr<MessageThread> thread;
MessagePtr parent;
bool highlight = false;
};

[[nodiscard]] ReplyContext getReplyContext(
TwitchChannel *channel, Communi::IrcMessage *message,
const std::vector<MessagePtr> &otherLoaded)
{
ReplyContext ctx;

const auto &tags = message->tags();
if (const auto it = tags.find("reply-thread-parent-msg-id");
it != tags.end())
Expand All @@ -259,9 +265,9 @@ void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
if (owned)
{
// Thread already exists (has a reply)
updateReplyParticipatedStatus(tags, message->nick(), builder,
owned, false);
builder.setThread(owned);
ctx.highlight = shouldHighlightReplyThread(
tags, message->nick(), owned, false);
ctx.thread = owned;
rootThread = owned;
}
}
Expand Down Expand Up @@ -295,10 +301,10 @@ void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
{
std::shared_ptr<MessageThread> newThread =
std::make_shared<MessageThread>(foundMessage);
updateReplyParticipatedStatus(tags, message->nick(), builder,
newThread, true);
ctx.highlight = shouldHighlightReplyThread(
tags, message->nick(), newThread, true);

builder.setThread(newThread);
ctx.thread = newThread;
rootThread = newThread;
// Store weak reference to thread in channel
channel->addReplyThread(newThread);
Expand All @@ -313,7 +319,7 @@ void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
{
if (rootThread)
{
builder.setParent(rootThread->root());
ctx.parent = rootThread->root();
}
}
else
Expand All @@ -324,20 +330,22 @@ void populateReply(TwitchChannel *channel, Communi::IrcMessage *message,
auto thread = parentThreadIt->second.lock();
if (thread)
{
builder.setParent(thread->root());
ctx.parent = thread->root();
}
}
else
{
auto parent = channel->findMessage(parentID);
if (parent)
{
builder.setParent(parent);
ctx.parent = parent;
}
}
}
}
}

return ctx;
}

std::optional<ClearChatMessage> parseClearChatMessage(
Expand Down Expand Up @@ -676,7 +684,13 @@ std::vector<MessagePtr> IrcMessageHandler::parseMessageWithReply(
privMsg->isAction());
builder.setMessageOffset(messageOffset);

populateReply(tc, message, otherLoaded, builder);
auto replyCtx = getReplyContext(tc, message, otherLoaded);
builder.setThread(std::move(replyCtx.thread));
builder.setParent(std::move(replyCtx.parent));
if (replyCtx.highlight)
{
builder.message().flags.set(MessageFlag::SubscribedThread);
}

if (!builder.isIgnored())
{
Expand Down Expand Up @@ -1446,8 +1460,7 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
QString content = originalContent;
int messageOffset = stripLeadingReplyMention(tags, content);

MessageBuilder builder(channel, message, args, content, isAction);
builder.setMessageOffset(messageOffset);
ReplyContext replyCtx;

if (const auto it = tags.find("reply-thread-parent-msg-id");
it != tags.end())
Expand All @@ -1459,9 +1472,9 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
{
// Thread already exists (has a reply)
auto thread = threadIt->second.lock();
updateReplyParticipatedStatus(tags, message->nick(), builder,
thread, false);
builder.setThread(thread);
replyCtx.highlight = shouldHighlightReplyThread(
tags, message->nick(), thread, false);
replyCtx.thread = thread;
rootThread = thread;
}
else
Expand All @@ -1472,10 +1485,10 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
{
// Found root reply message
auto newThread = std::make_shared<MessageThread>(root);
updateReplyParticipatedStatus(tags, message->nick(), builder,
newThread, true);
replyCtx.highlight = shouldHighlightReplyThread(
tags, message->nick(), newThread, true);

builder.setThread(newThread);
replyCtx.thread = newThread;
rootThread = newThread;
// Store weak reference to thread in channel
channel->addReplyThread(newThread);
Expand All @@ -1490,7 +1503,7 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
{
if (rootThread)
{
builder.setParent(rootThread->root());
replyCtx.parent = rootThread->root();
}
}
else
Expand All @@ -1501,21 +1514,31 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
auto thread = parentThreadIt->second.lock();
if (thread)
{
builder.setParent(thread->root());
replyCtx.parent = thread->root();
}
}
else
{
auto parent = channel->findMessage(parentID);
if (parent)
{
builder.setParent(parent);
replyCtx.parent = parent;
}
}
}
}
}

MessageBuilder builder(channel, message, args, content, isAction);
builder.setMessageOffset(messageOffset);

builder.setThread(std::move(replyCtx.thread));
builder.setParent(std::move(replyCtx.parent));
if (replyCtx.highlight)
{
builder.message().flags.set(MessageFlag::SubscribedThread);
}

if (isSub || !builder.isIgnored())
{
if (isSub)
Expand Down

0 comments on commit 8c515c2

Please sign in to comment.