Move thread parsing & building to its own private function (#4222)

This commit is contained in:
pajlada 2022-12-07 20:13:59 +01:00 committed by GitHub
parent a16d148dfd
commit 9f008f86d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 66 deletions

View file

@ -226,72 +226,7 @@ MessagePtr TwitchMessageBuilder::build()
} }
// reply threads // reply threads
if (this->thread_) this->parseThread();
{
// set references
this->message().replyThread = this->thread_;
this->thread_->addToThread(this->weakOf());
// enable reply flag
this->message().flags.set(MessageFlag::ReplyMessage);
const auto &threadRoot = this->thread_->root();
QString usernameText = SharedMessageBuilder::stylizeUsername(
threadRoot->loginName, *threadRoot.get());
this->emplace<ReplyCurveElement>();
// construct reply elements
this->emplace<TextElement>(
"Replying to", MessageElementFlag::RepliedMessage,
MessageColor::System, FontStyle::ChatMediumSmall)
->setLink({Link::ViewThread, this->thread_->rootId()});
this->emplace<TextElement>(
"@" + usernameText + ":", MessageElementFlag::RepliedMessage,
threadRoot->usernameColor, FontStyle::ChatMediumSmall)
->setLink({Link::UserInfo, threadRoot->displayName});
this->emplace<SingleLineTextElement>(
threadRoot->messageText,
MessageElementFlags({MessageElementFlag::RepliedMessage,
MessageElementFlag::Text}),
this->textColor_, FontStyle::ChatMediumSmall)
->setLink({Link::ViewThread, this->thread_->rootId()});
}
else if (this->tags.find("reply-parent-msg-id") != this->tags.end())
{
// Message is a reply but we couldn't find the original message.
// Render the message using the additional reply tags
auto replyDisplayName = this->tags.find("reply-parent-display-name");
auto replyBody = this->tags.find("reply-parent-msg-body");
if (replyDisplayName != this->tags.end() &&
replyBody != this->tags.end())
{
auto name = replyDisplayName->toString();
auto body = parseTagString(replyBody->toString());
this->emplace<ReplyCurveElement>();
this->emplace<TextElement>(
"Replying to", MessageElementFlag::RepliedMessage,
MessageColor::System, FontStyle::ChatMediumSmall);
this->emplace<TextElement>(
"@" + name + ":", MessageElementFlag::RepliedMessage,
this->textColor_, FontStyle::ChatMediumSmall)
->setLink({Link::UserInfo, name});
this->emplace<SingleLineTextElement>(
body,
MessageElementFlags({MessageElementFlag::RepliedMessage,
MessageElementFlag::Text}),
this->textColor_, FontStyle::ChatMediumSmall);
}
}
// timestamp // timestamp
this->message().serverReceivedTime = calculateMessageTime(this->ircMessage); this->message().serverReceivedTime = calculateMessageTime(this->ircMessage);
@ -634,6 +569,76 @@ void TwitchMessageBuilder::parseRoomID()
} }
} }
void TwitchMessageBuilder::parseThread()
{
if (this->thread_)
{
// set references
this->message().replyThread = this->thread_;
this->thread_->addToThread(this->weakOf());
// enable reply flag
this->message().flags.set(MessageFlag::ReplyMessage);
const auto &threadRoot = this->thread_->root();
QString usernameText = SharedMessageBuilder::stylizeUsername(
threadRoot->loginName, *threadRoot.get());
this->emplace<ReplyCurveElement>();
// construct reply elements
this->emplace<TextElement>(
"Replying to", MessageElementFlag::RepliedMessage,
MessageColor::System, FontStyle::ChatMediumSmall)
->setLink({Link::ViewThread, this->thread_->rootId()});
this->emplace<TextElement>(
"@" + usernameText + ":", MessageElementFlag::RepliedMessage,
threadRoot->usernameColor, FontStyle::ChatMediumSmall)
->setLink({Link::UserInfo, threadRoot->displayName});
this->emplace<SingleLineTextElement>(
threadRoot->messageText,
MessageElementFlags({MessageElementFlag::RepliedMessage,
MessageElementFlag::Text}),
this->textColor_, FontStyle::ChatMediumSmall)
->setLink({Link::ViewThread, this->thread_->rootId()});
}
else if (this->tags.find("reply-parent-msg-id") != this->tags.end())
{
// Message is a reply but we couldn't find the original message.
// Render the message using the additional reply tags
auto replyDisplayName = this->tags.find("reply-parent-display-name");
auto replyBody = this->tags.find("reply-parent-msg-body");
if (replyDisplayName != this->tags.end() &&
replyBody != this->tags.end())
{
auto name = replyDisplayName->toString();
auto body = parseTagString(replyBody->toString());
this->emplace<ReplyCurveElement>();
this->emplace<TextElement>(
"Replying to", MessageElementFlag::RepliedMessage,
MessageColor::System, FontStyle::ChatMediumSmall);
this->emplace<TextElement>(
"@" + name + ":", MessageElementFlag::RepliedMessage,
this->textColor_, FontStyle::ChatMediumSmall)
->setLink({Link::UserInfo, name});
this->emplace<SingleLineTextElement>(
body,
MessageElementFlags({MessageElementFlag::RepliedMessage,
MessageElementFlag::Text}),
this->textColor_, FontStyle::ChatMediumSmall);
}
}
}
void TwitchMessageBuilder::parseUsernameColor() void TwitchMessageBuilder::parseUsernameColor()
{ {
const auto *userData = getIApp()->getUserData(); const auto *userData = getIApp()->getUserData();

View file

@ -95,6 +95,9 @@ private:
void parseUsername() override; void parseUsername() override;
void parseMessageID(); void parseMessageID();
void parseRoomID(); void parseRoomID();
// Parse & build thread information into the message
// Will read information from thread_ or from IRC tags
void parseThread();
void appendUsername(); void appendUsername();
void runIgnoreReplaces(std::vector<TwitchEmoteOccurrence> &twitchEmotes); void runIgnoreReplaces(std::vector<TwitchEmoteOccurrence> &twitchEmotes);