1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![doc(alias = "subscription")]
#![doc(alias = "subscriptions")]
#![doc(alias = "channel.subscribe")]
//! A specified channel receives a subscriber. This does not include resubscribes.
use super::*;

/// [`channel.subscribe`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelsubscribe): a specified channel receives a subscriber. This does not include resubscribes.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(not(feature = "allow_unknown_fields"), serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChannelSubscribeV1 {
    /// The broadcaster user ID for the channel you want to get subscribe notifications for.
    pub broadcaster_user_id: types::UserId,
}

impl EventSubscription for ChannelSubscribeV1 {
    type Payload = ChannelSubscribeV1Payload;

    const EVENT_TYPE: EventType = EventType::ChannelSubscribe;
    #[cfg(feature = "twitch_oauth2")]
    const SCOPE: &'static [twitch_oauth2::Scope] =
        &[twitch_oauth2::Scope::ChannelReadSubscriptions];
    const VERSION: &'static str = "1";
}

/// [`channel.subscribe`](ChannelSubscribeV1) response payload.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(not(feature = "allow_unknown_fields"), serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChannelSubscribeV1Payload {
    /// The requested broadcaster ID.
    pub broadcaster_user_id: types::UserId,
    /// The requested broadcaster login.
    pub broadcaster_user_login: types::UserName,
    /// The requested broadcaster display name.
    pub broadcaster_user_name: types::DisplayName,
    /// Whether the subscription is a gift.
    pub is_gift: bool,
    /// The tier of the subscription. Valid values are 1000, 2000, and 3000.
    pub tier: types::SubscriptionTier,
    /// The user ID for the user who subscribed to the specified channel.
    pub user_id: types::UserId,
    /// The user login for the user who subscribed to the specified channel.
    pub user_login: types::UserName,
    /// The user display name for the user who subscribed to the specified channel.
    pub user_name: types::DisplayName,
}

#[test]
fn parse_payload() {
    let payload = r#"
    {
        "subscription": {
            "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
            "type": "channel.subscribe",
            "version": "1",
            "condition": {
               "broadcaster_user_id": "1337"
            },
             "transport": {
                "method": "webhook",
                "callback": "https://example.com/webhooks/callback"
            },
            "created_at": "2019-11-16T10:11:12.123Z"
        },
        "event": {
            "user_id": "1234",
            "user_login": "cool_user",
            "user_name": "Cool_User",
            "broadcaster_user_id": "1337",
            "broadcaster_user_login": "cooler_user",
            "broadcaster_user_name": "Cooler_User",
            "tier": "1000",
            "is_gift": false
        }
    }
    "#;

    dbg!(crate::eventsub::Payload::parse(payload).unwrap());
}