twitch_api/eventsub/channel/hypetrain/
begin.rs

1#![doc(alias = "channel.hype_train.begin")]
2//! A hype train begins on the specified channel.
3
4use super::*;
5/// [`channel.hype_train.begin`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelhype_trainbegin): a hype train begins on the specified channel.
6#[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    // FIXME: Twitch docs say "want to hype train"
12    /// The broadcaster user ID for the channel you want hype train begin notifications for.
13    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
14    pub broadcaster_user_id: types::UserId,
15}
16
17impl ChannelHypeTrainBeginV1 {
18    /// The broadcaster user ID for the channel you want hype train begin notifications for.
19    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/// [`channel.hype_train.begin`](ChannelHypeTrainBeginV1) response payload.
37#[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    /// The Hype Train ID.
42    pub id: types::HypeTrainId,
43    /// The requested broadcaster ID.
44    pub broadcaster_user_id: types::UserId,
45    /// The requested broadcaster login.
46    pub broadcaster_user_login: types::UserName,
47    /// The requested broadcaster display name.
48    pub broadcaster_user_name: types::DisplayName,
49    /// The time at which the hype train expires. The expiration is extended when the hype train reaches a new level.
50    pub expires_at: types::Timestamp,
51    /// The number of points required to reach the next level.
52    pub goal: i64,
53    /// The most recent contribution.
54    pub last_contribution: Contribution,
55    /// The number of points contributed to the hype train at the current level.
56    pub progress: i64,
57    /// The timestamp at which the hype train started.
58    pub started_at: types::Timestamp,
59    // FIXME: Contains a maximum of two user objects
60    /// The contributors with the most points contributed.
61    pub top_contributions: Vec<Contribution>,
62    /// Total points contributed to the hype train.
63    pub total: i64,
64    /// The starting level of the Hype Train.
65    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}