feat: show mod/VIP badges from shared chats (#6653)
Reviewed-by: Nerixyz <nerixdev@outlook.de> Reviewed-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -348,6 +348,38 @@ void appendBadges(MessageBuilder *builder, const std::vector<Badge> &badges,
|
||||
builder->message().badgeInfos = badgeInfos;
|
||||
}
|
||||
|
||||
std::vector<Badge> appendSharedChatBadges(
|
||||
MessageBuilder *builder, const std::vector<Badge> &sharedBadges,
|
||||
const QString &sharedChannelName, const TwitchChannel *twitchChannel)
|
||||
{
|
||||
auto appendedBadges = std::vector<Badge>{};
|
||||
for (const auto &badge : sharedBadges)
|
||||
{
|
||||
if (badge.key_ != "moderator" && badge.key_ != "vip")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto badgeEmote = getTwitchBadge(badge, twitchChannel);
|
||||
if (!badgeEmote)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto tooltip = (*badgeEmote)->tooltip.string;
|
||||
if (sharedChannelName != "")
|
||||
{
|
||||
tooltip = QString("%1 (%2)").arg(tooltip, sharedChannelName);
|
||||
}
|
||||
|
||||
builder->emplace<BadgeElement>(*badgeEmote, badge.flag_)
|
||||
->setTooltip(tooltip);
|
||||
appendedBadges.push_back(badge);
|
||||
}
|
||||
|
||||
return appendedBadges;
|
||||
}
|
||||
|
||||
bool doesWordContainATwitchEmote(
|
||||
int cursor, const QString &word,
|
||||
const std::vector<TwitchEmoteOccurrence> &twitchEmotes,
|
||||
@@ -2379,6 +2411,8 @@ void MessageBuilder::appendTwitchBadges(const QVariantMap &tags,
|
||||
return;
|
||||
}
|
||||
|
||||
auto badges = parseBadgeTag(tags);
|
||||
|
||||
if (this->message().flags.has(MessageFlag::SharedMessage))
|
||||
{
|
||||
const QString sourceId = tags["source-room-id"].toString();
|
||||
@@ -2410,10 +2444,24 @@ void MessageBuilder::appendTwitchBadges(const QVariantMap &tags,
|
||||
this->emplace<BadgeElement>(
|
||||
makeSharedChatBadge(sourceName, sourceProfilePicture, sourceLogin),
|
||||
MessageElementFlag::BadgeSharedChannel);
|
||||
|
||||
const auto sourceBadges = parseBadgeTag(tags, "source-badges");
|
||||
const auto appendedBadges = appendSharedChatBadges(
|
||||
this, sourceBadges, sourceName, twitchChannel);
|
||||
|
||||
// Dedup mod/vip badges if user is mod/vip in both chats,
|
||||
// preferring source channel's badges for the tooltips
|
||||
for (const auto &appendedBadge : appendedBadges)
|
||||
{
|
||||
if (auto b = std::ranges::find(badges, appendedBadge);
|
||||
b != badges.end())
|
||||
{
|
||||
badges.erase(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto badgeInfos = parseBadgeInfoTag(tags);
|
||||
auto badges = parseBadgeTag(tags);
|
||||
appendBadges(this, badges, badgeInfos, twitchChannel);
|
||||
}
|
||||
|
||||
|
||||
@@ -106,11 +106,12 @@ std::unordered_map<QString, QString> parseBadgeInfoTag(const QVariantMap &tags)
|
||||
return infoMap;
|
||||
}
|
||||
|
||||
std::vector<Badge> parseBadgeTag(const QVariantMap &tags)
|
||||
std::vector<Badge> parseBadgeTag(const QVariantMap &tags,
|
||||
const QString &tagName)
|
||||
{
|
||||
std::vector<Badge> b;
|
||||
|
||||
auto badgesIt = tags.constFind("badges");
|
||||
auto badgesIt = tags.constFind(tagName);
|
||||
if (badgesIt == tags.end())
|
||||
{
|
||||
return b;
|
||||
|
||||
@@ -35,18 +35,20 @@ struct TwitchEmoteOccurrence {
|
||||
/// @returns A map of badge-names to their values
|
||||
std::unordered_map<QString, QString> parseBadgeInfoTag(const QVariantMap &tags);
|
||||
|
||||
/// @brief Parses the `badges` tag of an IRC message
|
||||
/// @brief Parses the badges from the specified tag of an IRC message
|
||||
///
|
||||
/// The `badges` tag contains a comma separated list of key-value elements which
|
||||
/// make up the name and version of each badge.
|
||||
/// All `badges`-type tags contain a comma separated list of key-value elements
|
||||
/// which make up the name and version of each badge.
|
||||
///
|
||||
/// **Example**:
|
||||
/// `badges=broadcaster/1,subscriber/18` would be parsed as
|
||||
/// `[(broadcaster, 1), (subscriber, 18)]`
|
||||
///
|
||||
/// @param tags The tags of the IRC message
|
||||
/// @param tagName The name of the tag to read badges from
|
||||
/// @returns A list of badges (name and version)
|
||||
std::vector<Badge> parseBadgeTag(const QVariantMap &tags);
|
||||
std::vector<Badge> parseBadgeTag(const QVariantMap &tags,
|
||||
const QString &tagName = "badges");
|
||||
|
||||
/// @brief Parses Twitch emotes in an IRC message
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user