Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notate power-up automatic reward redemptions #5471

Merged
merged 6 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Minor: Moderators can now see when users are warned. (#5441)
- Minor: Added support for Brave & google-chrome-stable browsers. (#5452)
- Minor: Added drop indicator line while dragging in tables. (#5256)
- Minor: Add channel points indication for new bits power-up redemptions. (#5471)
- 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)
Expand Down
48 changes: 48 additions & 0 deletions src/providers/twitch/ChannelPointReward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@ ChannelPointReward::ChannelPointReward(const QJsonObject &redemption)
this->title = reward.value("title").toString();
this->cost = reward.value("cost").toInt();
this->isUserInputRequired = reward.value("is_user_input_required").toBool();
this->isBits = reward.value("pricing_type").toString() == "BITS";

// accommodate idiosyncrasies of automatic reward redemptions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample pubsub messages
{"type":"MESSAGE","data":{"topic":"community-points-channel-v1.11148817","message":"{\"type\":\"automatic-reward-redeemed\",\"data\":{\"timestamp\":\"2024-06-18T08:48:30.369543084Z\",\"redemption\":{\"id\":\"06f3ae08-d8b9-4e65-a3b7-28061c1e9bc5\",\"user\":{\"id\":\"11148817\",\"login\":\"pajlada\",\"display_name\":\"pajlada\"},\"channel_id\":\"11148817\",\"redeemed_at\":\"2024-06-18T08:48:30Z\",\"reward\":{\"channel_id\":\"11148817\",\"reward_type\":\"SEND_ANIMATED_MESSAGE\",\"cost\":0,\"default_cost\":0,\"min_cost\":0,\"image\":null,\"default_image\":{\"url_1x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/comments-1.png\",\"url_2x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/comments-2.png\",\"url_4x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/comments-4.png\"},\"background_color\":null,\"default_background_color\":\"#F177C1\",\"is_enabled\":true,\"is_hidden_for_subs\":false,\"updated_for_indicator_at\":null,\"globally_updated_for_indicator_at\":\"2024-04-02T21:00:00Z\",\"is_in_stock\":true,\"max_per_stream\":{\"is_enabled\":false,\"max_per_stream\":0},\"max_per_user_per_stream\":{\"is_enabled\":false,\"max_per_user_per_stream\":0},\"global_cooldown\":{\"is_enabled\":false,\"global_cooldown_seconds\":0},\"cooldown_expires_at\":null,\"redemptions_redeemed_current_stream\":null,\"default_bits_cost\":60,\"bits_cost\":0,\"pricing_type\":\"BITS\"},\"redemption_metadata\":{\"send_animated_message_metadata\":{\"animation_id\":\"simmer\"}}}}}"}}
{"type":"MESSAGE","data":{"topic":"community-points-channel-v1.11148817","message":"{\"type\":\"automatic-reward-redeemed\",\"data\":{\"timestamp\":\"2024-06-18T08:48:34.700580425Z\",\"redemption\":{\"id\":\"59697f15-b669-4cb2-b3e9-358fe558d2c9\",\"user\":{\"id\":\"11148817\",\"login\":\"pajlada\",\"display_name\":\"pajlada\"},\"channel_id\":\"11148817\",\"redeemed_at\":\"2024-06-18T08:48:34Z\",\"reward\":{\"channel_id\":\"11148817\",\"reward_type\":\"SEND_ANIMATED_MESSAGE\",\"cost\":0,\"default_cost\":0,\"min_cost\":0,\"image\":null,\"default_image\":{\"url_1x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/comments-1.png\",\"url_2x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/comments-2.png\",\"url_4x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/comments-4.png\"},\"background_color\":null,\"default_background_color\":\"#F177C1\",\"is_enabled\":true,\"is_hidden_for_subs\":false,\"updated_for_indicator_at\":null,\"globally_updated_for_indicator_at\":\"2024-04-02T21:00:00Z\",\"is_in_stock\":true,\"max_per_stream\":{\"is_enabled\":false,\"max_per_stream\":0},\"max_per_user_per_stream\":{\"is_enabled\":false,\"max_per_user_per_stream\":0},\"global_cooldown\":{\"is_enabled\":false,\"global_cooldown_seconds\":0},\"cooldown_expires_at\":null,\"redemptions_redeemed_current_stream\":null,\"default_bits_cost\":60,\"bits_cost\":0,\"pricing_type\":\"BITS\"},\"redemption_metadata\":{\"send_animated_message_metadata\":{\"animation_id\":\"rainbow-eclipse\"}}}}}"}}
{"type":"MESSAGE","data":{"topic":"community-points-channel-v1.11148817","message":"{\"type\":\"automatic-reward-redeemed\",\"data\":{\"timestamp\":\"2024-06-18T08:48:44.945205709Z\",\"redemption\":{\"id\":\"45c89a4a-6290-4e43-91dc-01ea1bfc5982\",\"user\":{\"id\":\"11148817\",\"login\":\"pajlada\",\"display_name\":\"pajlada\"},\"channel_id\":\"11148817\",\"redeemed_at\":\"2024-06-18T08:48:44Z\",\"reward\":{\"channel_id\":\"11148817\",\"reward_type\":\"SEND_GIGANTIFIED_EMOTE\",\"cost\":0,\"default_cost\":0,\"min_cost\":0,\"image\":null,\"default_image\":{\"url_1x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/emote_add-1.png\",\"url_2x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/emote_add-2.png\",\"url_4x\":\"https://static-cdn.jtvnw.net/automatic-reward-images/emote_add-4.png\"},\"background_color\":null,\"default_background_color\":\"#69B6FF\",\"is_enabled\":true,\"is_hidden_for_subs\":false,\"updated_for_indicator_at\":null,\"globally_updated_for_indicator_at\":\"2024-04-02T21:00:00Z\",\"is_in_stock\":true,\"max_per_stream\":{\"is_enabled\":false,\"max_per_stream\":0},\"max_per_user_per_stream\":{\"is_enabled\":false,\"max_per_user_per_stream\":0},\"global_cooldown\":{\"is_enabled\":false,\"global_cooldown_seconds\":0},\"cooldown_expires_at\":null,\"redemptions_redeemed_current_stream\":null,\"default_bits_cost\":100,\"bits_cost\":0,\"pricing_type\":\"BITS\"},\"redemption_metadata\":{\"send_gigantified_emote_metadata\":{\"emote\":{\"id\":\"116051\",\"token\":\"forsen1\"}}}}}}"}}

notably, reward id/title/is_user_input_required are all missing in the raw payload

const auto rewardType = reward.value("reward_type").toString();
if (rewardType == "SEND_ANIMATED_MESSAGE")
{
this->id = "animated-message";
this->isUserInputRequired = true;
this->title = "Message Effects";
}
else if (rewardType == "SEND_GIGANTIFIED_EMOTE")
{
this->id = "gigantified-emote-message";
this->isUserInputRequired = true;
this->title = "Gigantify an Emote";
}
else if (rewardType == "CELEBRATION")
{
this->id = rewardType;
this->title = "On-Screen Celebration";
const auto metadata =
redemption.value("redemption_metadata").toObject();
const auto emote = metadata.value("celebration_emote_metadata")
.toObject()
.value("emote")
.toObject();
this->emoteId = emote.value("id").toString();
this->emoteName = emote.value("token").toString();
}

// use bits cost when channel points were not used
if (cost == 0)
{
this->cost = reward.value("bits_cost").toInt();
}

// workaround twitch bug where bits_cost is always 0 in practice
if (cost == 0)
{
this->cost = reward.value("default_bits_cost").toInt();
}

// We don't need to store user information for rewards with user input
// because we will get the user info from a corresponding IRC message
Expand All @@ -27,6 +68,13 @@ ChannelPointReward::ChannelPointReward(const QJsonObject &redemption)
}

auto imageValue = reward.value("image");

// automatic reward redemptions have specialized default images
if (imageValue.isNull() && this->isBits)
{
imageValue = reward.value("default_image");
}

// From Twitch docs
// The size is only an estimation, the actual size might vary.
constexpr QSize baseSize(28, 28);
Expand Down
3 changes: 3 additions & 0 deletions src/providers/twitch/ChannelPointReward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct ChannelPointReward {
int cost;
ImageSet image;
bool isUserInputRequired = false;
bool isBits = false;
QString emoteId; // currently only for celebrations
QString emoteName; // currently only for celebrations

struct {
QString id;
Expand Down
29 changes: 19 additions & 10 deletions src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,21 +1338,30 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
auto *channel = dynamic_cast<TwitchChannel *>(chan.get());

const auto &tags = message->tags();
QString rewardId;
if (const auto it = tags.find("custom-reward-id"); it != tags.end())
{
const auto rewardId = it.value().toString();
if (!rewardId.isEmpty() &&
!channel->isChannelPointRewardKnown(rewardId))
rewardId = it.value().toString();
}
else if (const auto typeIt = tags.find("msg-id"); typeIt != tags.end())
{
// slight hack to treat bits power-ups as channel point redemptions
const auto msgId = typeIt.value().toString();
if (msgId == "animated-message" || msgId == "gigantified-emote-message")
{
// Need to wait for pubsub reward notification
qCDebug(chatterinoTwitch) << "TwitchChannel reward added ADD "
"callback since reward is not known:"
<< rewardId;
channel->addQueuedRedemption(rewardId, originalContent, message);
return;
rewardId = msgId;
}
args.channelPointRewardId = rewardId;
}
if (!rewardId.isEmpty() && !channel->isChannelPointRewardKnown(rewardId))
{
// Need to wait for pubsub reward notification
qCDebug(chatterinoTwitch) << "TwitchChannel reward added ADD "
"callback since reward is not known:"
<< rewardId;
channel->addQueuedRedemption(rewardId, originalContent, message);
return;
}
args.channelPointRewardId = rewardId;

QString content = originalContent;
int messageOffset = stripLeadingReplyMention(tags, content);
Expand Down
2 changes: 2 additions & 0 deletions src/providers/twitch/PubSubManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,8 @@ void PubSub::handleMessageResponse(const PubSubMessageMessage &message)

switch (innerMessage.type)
{
case PubSubCommunityPointsChannelV1Message::Type::
AutomaticRewardRedeemed:
case PubSubCommunityPointsChannelV1Message::Type::RewardRedeemed: {
auto redemption =
innerMessage.data.value("redemption").toObject();
Expand Down
15 changes: 15 additions & 0 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,15 @@ void TwitchMessageBuilder::appendChannelPointRewardMessage(
}
builder->emplace<TextElement>(redeemed,
MessageElementFlag::ChannelPointReward);
if (reward.id == "CELEBRATION")
{
const auto emotePtr =
getIApp()->getEmotes()->getTwitchEmotes()->getOrCreateEmote(
EmoteId{reward.emoteId}, EmoteName{reward.emoteName});
builder->emplace<EmoteElement>(emotePtr,
MessageElementFlag::ChannelPointReward,
MessageColor::Text);
}
builder->emplace<TextElement>(
reward.title, MessageElementFlag::ChannelPointReward,
MessageColor::Text, FontStyle::ChatMediumBold);
Expand All @@ -1602,6 +1611,12 @@ void TwitchMessageBuilder::appendChannelPointRewardMessage(
builder->emplace<TextElement>(
QString::number(reward.cost), MessageElementFlag::ChannelPointReward,
MessageColor::Text, FontStyle::ChatMediumBold);
if (reward.isBits)
{
builder->emplace<TextElement>(
"bits", MessageElementFlag::ChannelPointReward, MessageColor::Text,
FontStyle::ChatMediumBold);
}
if (reward.isUserInputRequired)
{
builder->emplace<LinebreakElement>(
Expand Down
4 changes: 4 additions & 0 deletions src/providers/twitch/pubsubmessages/ChannelPoints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace chatterino {

struct PubSubCommunityPointsChannelV1Message {
enum class Type {
AutomaticRewardRedeemed,
RewardRedeemed,

INVALID,
Expand All @@ -30,6 +31,9 @@ constexpr magic_enum::customize::customize_t magic_enum::customize::enum_name<
{
switch (value)
{
case chatterino::PubSubCommunityPointsChannelV1Message::Type::
AutomaticRewardRedeemed:
return "automatic-reward-redeemed";
case chatterino::PubSubCommunityPointsChannelV1Message::Type::
RewardRedeemed:
return "reward-redeemed";
Expand Down
Loading