#![doc(alias = "channel.channel_points_automatic_reward_redemption.add")]
use super::*;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChannelPointsAutomaticRewardRedemptionAddV1 {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
pub broadcaster_user_id: types::UserId,
}
impl ChannelPointsAutomaticRewardRedemptionAddV1 {
pub fn broadcaster_user_id(broadcaster_user_id: impl Into<types::UserId>) -> Self {
Self {
broadcaster_user_id: broadcaster_user_id.into(),
}
}
}
impl EventSubscription for ChannelPointsAutomaticRewardRedemptionAddV1 {
type Payload = ChannelPointsAutomaticRewardRedemptionAddV1Payload;
const EVENT_TYPE: EventType = EventType::ChannelPointsAutomaticRewardRedemptionAdd;
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
twitch_oauth2::Scope::ChannelReadRedemptions,
twitch_oauth2::Scope::ChannelManageRedemptions
)];
const VERSION: &'static str = "1";
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChannelPointsAutomaticRewardRedemptionAddV1Payload {
pub broadcaster_user_id: types::UserId,
pub broadcaster_user_login: types::UserName,
pub broadcaster_user_name: types::DisplayName,
pub user_id: types::UserId,
pub user_login: types::UserName,
pub user_name: types::DisplayName,
pub id: types::RedemptionId,
pub reward: AutomaticReward,
pub message: RedemptionMessage,
pub user_input: String,
pub redeemed_at: types::Timestamp,
}
#[cfg(test)]
#[test]
fn parse_payload() {
use crate::eventsub::{Event, Message};
let payload = r##"
{
"subscription": {
"id": "7297f7eb-3bf5-461f-8ae6-7cd7781ebce3",
"status": "enabled",
"type": "channel.channel_points_automatic_reward_redemption.add",
"version": "1",
"condition": {
"broadcaster_user_id": "12826"
},
"transport": {
"method": "webhook",
"callback": "https://example.com/webhooks/callback"
},
"created_at": "2024-02-23T21:12:33.771005262Z",
"cost": 0
},
"event": {
"broadcaster_user_id": "12826",
"broadcaster_user_name": "Twitch",
"broadcaster_user_login": "twitch",
"user_id": "141981764",
"user_name": "TwitchDev",
"user_login": "twitchdev",
"id": "f024099a-e0fe-4339-9a0a-a706fb59f353",
"reward": {
"type": "send_highlighted_message",
"cost": 100,
"unlocked_emote": null
},
"message": {
"text": "Hello world! VoHiYo",
"emotes": [
{
"id": "81274",
"begin": 13,
"end": 18
}
]
},
"user_input": "Hello world! VoHiYo ",
"redeemed_at": "2024-02-23T21:14:34.260398045Z"
}
}
"##;
let val = Event::parse(payload).unwrap();
crate::tests::roundtrip(&val);
let Event::ChannelPointsAutomaticRewardRedemptionAddV1(val) = val else {
panic!("invalid event type");
};
let Message::Notification(notif) = val.message else {
panic!("invalid message type");
};
assert_eq!(notif.broadcaster_user_id.as_str(), "12826");
assert_eq!(notif.user_id.as_str(), "141981764");
assert_eq!(notif.id.as_str(), "f024099a-e0fe-4339-9a0a-a706fb59f353");
assert_eq!(notif.reward.cost, 100);
assert!(notif.reward.unlocked_emote.is_none());
assert_eq!(
notif.reward.type_,
AutomaticRewardType::SendHighlightedMessage
);
assert_eq!(notif.message.emotes.len(), 1);
}