Skip to content

Commit

Permalink
Fixed restricted users' usernames not being clickable (Chatterino#5405)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Jun 1, 2024
1 parent 65bfec9 commit 2a46ee7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Minor: Added the ability to duplicate tabs. (#5277)
- Bugfix: Fixed tab move animation occasionally failing to start after closing a tab. (#5426)
- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Bugfix: Fixed restricted users usernames not being clickable. (#5405)
- Bugfix: Fixed a crash that could occur when logging was enabled in IRC servers that were removed. (#5419)
- Dev: Update Windows build from Qt 6.5.0 to Qt 6.7.1. (#5420)
- Dev: Update vcpkg build Qt from 6.5.0 to 6.7.0, boost from 1.83.0 to 1.85.0, openssl from 3.1.3 to 3.3.0. (#5422)
Expand Down
2 changes: 1 addition & 1 deletion src/messages/MessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ void MessageBuilder::addTextOrEmoji(const QString &string_)
auto &&textColor = this->textColor_;
if (string.startsWith('@'))
{
this->emplace<MentionElement>(string, textColor, textColor);
this->emplace<MentionElement>(string, "", textColor, textColor);
}
else
{
Expand Down
27 changes: 25 additions & 2 deletions src/messages/MessageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,14 @@ Link LinkElement::getLink() const
return {Link::Url, this->linkInfo_.url()};
}

MentionElement::MentionElement(const QString &name, MessageColor fallbackColor_,
MentionElement::MentionElement(const QString &displayName, QString loginName_,
MessageColor fallbackColor_,
MessageColor userColor_)
: TextElement(name, {MessageElementFlag::Text, MessageElementFlag::Mention})
: TextElement(displayName,
{MessageElementFlag::Text, MessageElementFlag::Mention})
, fallbackColor(fallbackColor_)
, userColor(userColor_)
, userLoginName(std::move(loginName_))
{
}

Expand Down Expand Up @@ -735,6 +738,26 @@ void MentionElement::addToContainer(MessageLayoutContainer &container,
TextElement::addToContainer(container, flags);
}

MessageElement *MentionElement::setLink(const Link &link)
{
assert(false && "MentionElement::setLink should not be called. Pass "
"through a valid login name in the constructor and it will "
"automatically be a UserInfo link");

return TextElement::setLink(link);
}

Link MentionElement::getLink() const
{
if (this->userLoginName.isEmpty())
{
// Some rare mention elements don't have the knowledge of the login name
return {};
}

return {Link::UserInfo, this->userLoginName};
}

// TIMESTAMP
TimestampElement::TimestampElement(QTime time)
: MessageElement(MessageElementFlag::Timestamp)
Expand Down
11 changes: 8 additions & 3 deletions src/messages/MessageElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class MessageElement
MessageElement(MessageElement &&) = delete;
MessageElement &operator=(MessageElement &&) = delete;

MessageElement *setLink(const Link &link);
virtual MessageElement *setLink(const Link &link);
MessageElement *setTooltip(const QString &tooltip);

MessageElement *setTrailingSpace(bool value);
Expand Down Expand Up @@ -314,8 +314,8 @@ class LinkElement : public TextElement
class MentionElement : public TextElement
{
public:
MentionElement(const QString &name, MessageColor fallbackColor_,
MessageColor userColor_);
MentionElement(const QString &displayName, QString loginName_,
MessageColor fallbackColor_, MessageColor userColor_);
~MentionElement() override = default;
MentionElement(const MentionElement &) = delete;
MentionElement(MentionElement &&) = delete;
Expand All @@ -325,6 +325,9 @@ class MentionElement : public TextElement
void addToContainer(MessageLayoutContainer &container,
MessageElementFlags flags) override;

MessageElement *setLink(const Link &link) override;
Link getLink() const override;

private:
/**
* The color of the element in case the "Colorize @usernames" is disabled
Expand All @@ -335,6 +338,8 @@ class MentionElement : public TextElement
* The color of the element in case the "Colorize @usernames" is enabled
**/
MessageColor userColor;

QString userLoginName;
};

// contains emote data and will pick the emote based on :
Expand Down
41 changes: 20 additions & 21 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,8 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)

auto prefixedUsername = '@' + username;
auto remainder = string.remove(prefixedUsername);
this->emplace<MentionElement>(prefixedUsername, originalTextColor,
textColor)
->setLink({Link::UserInfo, username})
this->emplace<MentionElement>(prefixedUsername, username,
originalTextColor, textColor)
->setTrailingSpace(remainder.isEmpty());

if (!remainder.isEmpty())
Expand Down Expand Up @@ -802,9 +801,8 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)
}

auto remainder = string.remove(username);
this->emplace<MentionElement>(username, originalTextColor,
this->emplace<MentionElement>(username, username, originalTextColor,
textColor)
->setLink({Link::UserInfo, username})
->setTrailingSpace(remainder.isEmpty());

if (!remainder.isEmpty())
Expand Down Expand Up @@ -1821,8 +1819,10 @@ void TwitchMessageBuilder::listOfUsersSystemMessage(QString prefix,
}
}

builder->emplace<MentionElement>(username, MessageColor::System, color)
->setLink({Link::UserInfo, username})
// TODO: Ensure we make use of display name / username(login name) correctly here
builder
->emplace<MentionElement>(username, username, MessageColor::System,
color)
->setTrailingSpace(false);
}
}
Expand Down Expand Up @@ -1867,9 +1867,8 @@ void TwitchMessageBuilder::listOfUsersSystemMessage(
}

builder
->emplace<MentionElement>(user.userName, MessageColor::System,
color)
->setLink({Link::UserInfo, user.userLogin})
->emplace<MentionElement>(user.userName, user.userLogin,
MessageColor::System, color)
->setTrailingSpace(false);
}

Expand Down Expand Up @@ -1938,8 +1937,8 @@ MessagePtr TwitchMessageBuilder::makeAutomodInfoMessage(
builder.emplace<BadgeElement>(makeAutoModBadge(),
MessageElementFlag::BadgeChannelAuthority);
// AutoMod "username"
builder.emplace<MentionElement>("AutoMod:", AUTOMOD_USER_COLOR,
AUTOMOD_USER_COLOR);
builder.emplace<TextElement>("AutoMod:", MessageElementFlag::Text,
AUTOMOD_USER_COLOR, FontStyle::ChatMediumBold);
switch (action.type)
{
case AutomodInfoAction::OnHold: {
Expand Down Expand Up @@ -1993,8 +1992,9 @@ std::pair<MessagePtr, MessagePtr> TwitchMessageBuilder::makeAutomodMessage(
builder.emplace<BadgeElement>(makeAutoModBadge(),
MessageElementFlag::BadgeChannelAuthority);
// AutoMod "username"
builder2.emplace<MentionElement>("AutoMod:", AUTOMOD_USER_COLOR,
AUTOMOD_USER_COLOR);
builder2.emplace<TextElement>("AutoMod:", MessageElementFlag::Text,
AUTOMOD_USER_COLOR,
FontStyle::ChatMediumBold);
// AutoMod header message
builder.emplace<TextElement>(
("Held a message for reason: " + action.reason +
Expand Down Expand Up @@ -2041,10 +2041,9 @@ std::pair<MessagePtr, MessagePtr> TwitchMessageBuilder::makeAutomodMessage(
builder2.message().flags.set(MessageFlag::AutoModOffendingMessage);

// sender username
builder2
.emplace<MentionElement>(action.target.displayName + ":",
MessageColor::Text, action.target.color)
->setLink({Link::UserInfo, action.target.login});
builder2.emplace<MentionElement>(action.target.displayName + ":",
action.target.login, MessageColor::Text,
action.target.color);
// sender's message caught by AutoMod
builder2.emplace<TextElement>(action.message, MessageElementFlag::Text,
MessageColor::Text);
Expand Down Expand Up @@ -2239,9 +2238,9 @@ std::pair<MessagePtr, MessagePtr> TwitchMessageBuilder::makeLowTrustUserMessage(
appendBadges(&builder2, action.senderBadges, {}, twitchChannel);

// sender username
builder2.emplace<MentionElement>(action.suspiciousUserDisplayName + ":",
MessageColor::Text,
action.suspiciousUserColor);
builder2.emplace<MentionElement>(
action.suspiciousUserDisplayName + ":", action.suspiciousUserLogin,
MessageColor::Text, action.suspiciousUserColor);

// sender's message caught by AutoMod
for (const auto &fragment : action.fragments)
Expand Down

0 comments on commit 2a46ee7

Please sign in to comment.