refactor: Remove Leading Mention in Replies and Highlight Participated Threads (#4047)
This commit is contained in:
@@ -147,6 +147,20 @@ void ColorProvider::initTypeColorMap()
|
||||
std::make_shared<QColor>(
|
||||
HighlightPhrase::FALLBACK_ELEVATED_MESSAGE_HIGHLIGHT_COLOR)});
|
||||
}
|
||||
|
||||
customColor = getSettings()->threadHighlightColor;
|
||||
if (QColor(customColor).isValid())
|
||||
{
|
||||
this->typeColorMap_.insert({ColorType::ThreadMessageHighlight,
|
||||
std::make_shared<QColor>(customColor)});
|
||||
}
|
||||
else
|
||||
{
|
||||
this->typeColorMap_.insert(
|
||||
{ColorType::ThreadMessageHighlight,
|
||||
std::make_shared<QColor>(
|
||||
HighlightPhrase::FALLBACK_THREAD_HIGHLIGHT_COLOR)});
|
||||
}
|
||||
}
|
||||
|
||||
void ColorProvider::initDefaultColors()
|
||||
|
||||
@@ -14,6 +14,7 @@ enum class ColorType {
|
||||
RedeemedHighlight,
|
||||
FirstMessageHighlight,
|
||||
ElevatedMessageHighlight,
|
||||
ThreadMessageHighlight,
|
||||
};
|
||||
|
||||
class ColorProvider
|
||||
|
||||
@@ -67,6 +67,65 @@ MessagePtr generateBannedMessage(bool confirmedBan)
|
||||
return builder.release();
|
||||
}
|
||||
|
||||
int stripLeadingReplyMention(const QVariantMap &tags, QString &content)
|
||||
{
|
||||
if (!getSettings()->stripReplyMention)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (const auto it = tags.find("reply-parent-display-name");
|
||||
it != tags.end())
|
||||
{
|
||||
auto displayName = it.value().toString();
|
||||
if (content.startsWith('@') &&
|
||||
content.at(1 + displayName.length()) == ' ' &&
|
||||
content.indexOf(displayName, 1) == 1)
|
||||
{
|
||||
int messageOffset = 1 + displayName.length() + 1;
|
||||
content.remove(0, messageOffset);
|
||||
return messageOffset;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void updateReplyParticipatedStatus(const QVariantMap &tags,
|
||||
const QString &senderLogin,
|
||||
TwitchMessageBuilder &builder,
|
||||
std::shared_ptr<MessageThread> &thread,
|
||||
bool isNew)
|
||||
{
|
||||
const auto ¤tLogin =
|
||||
getApp()->accounts->twitch.getCurrent()->getUserName();
|
||||
if (thread->participated())
|
||||
{
|
||||
builder.message().flags.set(MessageFlag::ParticipatedThread);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
if (const auto it = tags.find("reply-parent-user-login");
|
||||
it != tags.end())
|
||||
{
|
||||
auto name = it.value().toString();
|
||||
if (name == currentLogin)
|
||||
{
|
||||
thread->markParticipated();
|
||||
builder.message().flags.set(MessageFlag::ParticipatedThread);
|
||||
return; // already marked as participated
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (senderLogin == currentLogin)
|
||||
{
|
||||
thread->markParticipated();
|
||||
// don't set the highlight here
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
namespace chatterino {
|
||||
|
||||
@@ -259,9 +318,12 @@ std::vector<MessagePtr> IrcMessageHandler::parseMessageWithReply(
|
||||
return this->parsePrivMessage(channel, privMsg);
|
||||
}
|
||||
|
||||
QString content = privMsg->content();
|
||||
int messageOffset = stripLeadingReplyMention(privMsg->tags(), content);
|
||||
MessageParseArgs args;
|
||||
TwitchMessageBuilder builder(channel, message, args, privMsg->content(),
|
||||
TwitchMessageBuilder builder(channel, message, args, content,
|
||||
privMsg->isAction());
|
||||
builder.setMessageOffset(messageOffset);
|
||||
|
||||
this->populateReply(tc, message, otherLoaded, builder);
|
||||
|
||||
@@ -295,10 +357,12 @@ void IrcMessageHandler::populateReply(
|
||||
auto threadIt = channel->threads_.find(replyID);
|
||||
if (threadIt != channel->threads_.end())
|
||||
{
|
||||
const auto owned = threadIt->second.lock();
|
||||
auto owned = threadIt->second.lock();
|
||||
if (owned)
|
||||
{
|
||||
// Thread already exists (has a reply)
|
||||
updateReplyParticipatedStatus(tags, message->nick(), builder,
|
||||
owned, false);
|
||||
builder.setThread(owned);
|
||||
return;
|
||||
}
|
||||
@@ -331,6 +395,8 @@ void IrcMessageHandler::populateReply(
|
||||
{
|
||||
std::shared_ptr<MessageThread> newThread =
|
||||
std::make_shared<MessageThread>(foundMessage);
|
||||
updateReplyParticipatedStatus(tags, message->nick(), builder,
|
||||
newThread, true);
|
||||
|
||||
builder.setThread(newThread);
|
||||
// Store weak reference to thread in channel
|
||||
@@ -341,7 +407,7 @@ void IrcMessageHandler::populateReply(
|
||||
|
||||
void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
|
||||
const QString &target,
|
||||
const QString &content,
|
||||
const QString &content_,
|
||||
TwitchIrcServer &server, bool isSub,
|
||||
bool isAction)
|
||||
{
|
||||
@@ -384,7 +450,7 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
|
||||
[=, &server](ChannelPointReward reward) {
|
||||
if (reward.id == rewardId)
|
||||
{
|
||||
this->addMessage(clone, target, content, server, isSub,
|
||||
this->addMessage(clone, target, content_, server, isSub,
|
||||
isAction);
|
||||
clone->deleteLater();
|
||||
return true;
|
||||
@@ -396,7 +462,11 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
|
||||
args.channelPointRewardId = rewardId;
|
||||
}
|
||||
|
||||
QString content = content_;
|
||||
int messageOffset = stripLeadingReplyMention(tags, content);
|
||||
|
||||
TwitchMessageBuilder builder(chan.get(), _message, args, content, isAction);
|
||||
builder.setMessageOffset(messageOffset);
|
||||
|
||||
if (const auto it = tags.find("reply-parent-msg-id"); it != tags.end())
|
||||
{
|
||||
@@ -405,7 +475,10 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
|
||||
if (threadIt != channel->threads_.end() && !threadIt->second.expired())
|
||||
{
|
||||
// Thread already exists (has a reply)
|
||||
builder.setThread(threadIt->second.lock());
|
||||
auto thread = threadIt->second.lock();
|
||||
updateReplyParticipatedStatus(tags, _message->nick(), builder,
|
||||
thread, false);
|
||||
builder.setThread(thread);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -414,7 +487,9 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
|
||||
if (root)
|
||||
{
|
||||
// Found root reply message
|
||||
const auto newThread = std::make_shared<MessageThread>(root);
|
||||
auto newThread = std::make_shared<MessageThread>(root);
|
||||
updateReplyParticipatedStatus(tags, _message->nick(), builder,
|
||||
newThread, true);
|
||||
|
||||
builder.setThread(newThread);
|
||||
// Store weak reference to thread in channel
|
||||
|
||||
@@ -999,8 +999,10 @@ void TwitchMessageBuilder::appendTwitchEmote(
|
||||
return;
|
||||
}
|
||||
|
||||
auto start = correctPositions[coords.at(0).toUInt()];
|
||||
auto end = correctPositions[coords.at(1).toUInt()];
|
||||
auto start =
|
||||
correctPositions[coords.at(0).toUInt() - this->messageOffset_];
|
||||
auto end =
|
||||
correctPositions[coords.at(1).toUInt() - this->messageOffset_];
|
||||
|
||||
if (start >= end || start < 0 || end > this->originalMessage_.length())
|
||||
{
|
||||
@@ -1589,4 +1591,9 @@ void TwitchMessageBuilder::setThread(std::shared_ptr<MessageThread> thread)
|
||||
this->thread_ = std::move(thread);
|
||||
}
|
||||
|
||||
void TwitchMessageBuilder::setMessageOffset(int offset)
|
||||
{
|
||||
this->messageOffset_ = offset;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -47,6 +47,7 @@ public:
|
||||
MessagePtr build() override;
|
||||
|
||||
void setThread(std::shared_ptr<MessageThread> thread);
|
||||
void setMessageOffset(int offset);
|
||||
|
||||
static void appendChannelPointRewardMessage(
|
||||
const ChannelPointReward &reward, MessageBuilder *builder, bool isMod,
|
||||
@@ -110,6 +111,18 @@ private:
|
||||
bool historicalMessage_ = false;
|
||||
std::shared_ptr<MessageThread> thread_;
|
||||
|
||||
/**
|
||||
* 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{};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user