twitch_api/eventsub/channel/hypetrain/
begin.rs1#![doc(alias = "channel.hype_train.begin")]
2use super::*;
5#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
8#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
9#[non_exhaustive]
10pub struct ChannelHypeTrainBeginV1 {
11 #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
14 pub broadcaster_user_id: types::UserId,
15}
16
17impl ChannelHypeTrainBeginV1 {
18 pub fn broadcaster_user_id(broadcaster_user_id: impl Into<types::UserId>) -> Self {
20 Self {
21 broadcaster_user_id: broadcaster_user_id.into(),
22 }
23 }
24}
25
26impl EventSubscription for ChannelHypeTrainBeginV1 {
27 type Payload = ChannelHypeTrainBeginV1Payload;
28
29 const EVENT_TYPE: EventType = EventType::ChannelHypeTrainBegin;
30 #[cfg(feature = "twitch_oauth2")]
31 const SCOPE: twitch_oauth2::Validator =
32 twitch_oauth2::validator![twitch_oauth2::Scope::ChannelReadHypeTrain];
33 const VERSION: &'static str = "1";
34}
35
36#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
38#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
39#[non_exhaustive]
40pub struct ChannelHypeTrainBeginV1Payload {
41 pub id: types::HypeTrainId,
43 pub broadcaster_user_id: types::UserId,
45 pub broadcaster_user_login: types::UserName,
47 pub broadcaster_user_name: types::DisplayName,
49 pub expires_at: types::Timestamp,
51 pub goal: i64,
53 pub last_contribution: Contribution,
55 pub progress: i64,
57 pub started_at: types::Timestamp,
59 pub top_contributions: Vec<Contribution>,
62 pub total: i64,
64 pub level: i64,
66}
67
68#[cfg(test)]
69#[test]
70fn parse_payload() {
71 let payload = r##"
72 {
73 "subscription": {
74 "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
75 "type": "channel.hype_train.begin",
76 "version": "1",
77 "status": "enabled",
78 "cost": 0,
79 "condition": {
80 "broadcaster_user_id": "1337"
81 },
82 "transport": {
83 "method": "webhook",
84 "callback": "https://example.com/webhooks/callback"
85 },
86 "created_at": "2019-11-16T10:11:12.123Z"
87 },
88 "event": {
89 "id": "1b0AsbInCHZW2SQFQkCzqN07Ib2",
90 "broadcaster_user_id": "1337",
91 "broadcaster_user_login": "cool_user",
92 "broadcaster_user_name": "Cool_User",
93 "total": 137,
94 "progress": 137,
95 "goal": 500,
96 "top_contributions": [
97 { "user_id": "123", "user_login": "pogchamp", "user_name": "PogChamp", "type": "bits", "total": 50 },
98 { "user_id": "456", "user_login": "kappa", "user_name": "Kappa", "type": "subscription", "total": 45 }
99 ],
100 "last_contribution": { "user_id": "123", "user_login": "pogchamp", "user_name": "PogChamp", "type": "bits", "total": 50 },
101 "level": 2,
102 "started_at": "2020-07-15T17:16:03.17106713Z",
103 "expires_at": "2020-07-15T17:16:11.17106713Z"
104 }
105 }
106 "##;
107
108 let val = dbg!(crate::eventsub::Event::parse(payload).unwrap());
109 crate::tests::roundtrip(&val)
110}