twitch_api/eventsub/channel/charity_campaign/
donate.rs

1#![doc(alias = "channel.charity_campaign.donate")]
2//! Sends an event notification when a user donates to the broadcaster’s charity campaign.
3
4use super::*;
5/// [`channel.charity_campaign.donate`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelcharity_campaigndonate): a user donates to the broadcaster’s charity campaign.
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7#[non_exhaustive]
8#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
9pub struct ChannelCharityCampaignDonateV1 {
10    /// The ID of the broadcaster that you want to receive notifications about when users donate to their campaign.
11    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
12    pub broadcaster_user_id: types::UserId,
13}
14
15impl ChannelCharityCampaignDonateV1 {
16    /// The ID of the broadcaster to get notified about.
17    pub fn broadcaster_user_id(broadcaster_user_id: impl Into<types::UserId>) -> Self {
18        Self {
19            broadcaster_user_id: broadcaster_user_id.into(),
20        }
21    }
22}
23
24impl EventSubscription for ChannelCharityCampaignDonateV1 {
25    type Payload = ChannelCharityCampaignDonateV1Payload;
26
27    const EVENT_TYPE: EventType = EventType::ChannelCharityCampaignDonate;
28    #[cfg(feature = "twitch_oauth2")]
29    const SCOPE: twitch_oauth2::Validator =
30        twitch_oauth2::validator![twitch_oauth2::Scope::ChannelReadCharity];
31    const VERSION: &'static str = "1";
32}
33
34/// [`channel.charity_campaign.donate`](ChannelCharityCampaignDonateV1) response payload.
35#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
36#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
37#[non_exhaustive]
38pub struct ChannelCharityCampaignDonateV1Payload {
39    /// An ID that identifies the donation. The ID is unique across campaigns.
40    pub id: types::CharityDonationId,
41    /// An ID that identifies the charity campaign.
42    pub campaign_id: types::CharityCampaignId,
43    /// An ID that identifies the broadcaster that’s running the campaign.
44    #[serde(alias = "broadcaster_user_id")]
45    pub broadcaster_id: types::UserId,
46    /// An ID that identifies the charity campaign.
47    #[serde(alias = "broadcaster_user_login")]
48    pub broadcaster_login: types::UserName,
49    /// An ID that identifies the broadcaster that’s running the campaign.
50    #[serde(alias = "broadcaster_user_name")]
51    pub broadcaster_name: types::DisplayName,
52    /// An ID that identifies the user that donated to the campaign.
53    pub user_id: types::UserId,
54    /// The user’s login name.
55    pub user_login: types::UserName,
56    /// The user’s display name.
57    pub user_name: types::DisplayName,
58    /// The charity’s name.
59    pub charity_name: String,
60    /// A description of the charity.
61    pub charity_description: String,
62    /// A URL to an image of the charity’s logo. The image’s type is PNG and its size is 100px X 100px.
63    pub charity_logo: String,
64    /// A URL to the charity’s website.
65    pub charity_website: String,
66    /// An object that contains the amount of money that the user donated.
67    pub amount: crate::extra::DonationAmount,
68}
69
70#[cfg(test)]
71#[test]
72fn parse_payload_correct_maybe() {
73    let payload = r##"
74    {
75      "subscription": {
76        "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
77        "type": "channel.charity_campaign.donate",
78        "version": "1",
79        "status": "enabled",
80        "cost": 0,
81        "condition": {
82          "broadcaster_user_id": "123456"
83        },
84        "transport": {
85          "method": "webhook",
86          "callback": "https://example.com/webhooks/callback"
87        },
88        "created_at": "2022-07-25T10:11:12.123Z"
89      },
90      "event": {
91        "id": "a1b2c3-aabb-4455-d1e2f3",
92        "campaign_id": "123-abc-456-def",
93        "broadcaster_user_id": "123456",
94        "broadcaster_user_name": "SunnySideUp",
95        "broadcaster_user_login": "sunnysideup",
96        "user_id": "654321",
97        "user_login": "generoususer1",
98        "user_name": "GenerousUser1",
99        "charity_name": "Example name",
100        "charity_description": "Example description",
101        "charity_logo": "https://abc.cloudfront.net/ppgf/1000/100.png",
102        "charity_website": "https://www.example.com",
103        "amount": {
104          "value": 10000,
105          "decimal_places": 2,
106          "currency": "USD"
107        }
108      }
109    }
110    "##;
111
112    let val = dbg!(crate::eventsub::Event::parse(payload).unwrap());
113    crate::tests::roundtrip(&val)
114}
115
116#[cfg(test)]
117#[test]
118fn parse_payload() {
119    let payload = r##"
120      {
121        "subscription": {
122          "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
123          "type": "channel.charity_campaign.donate",
124          "version": "1",
125          "status": "enabled",
126          "cost": 0,
127          "condition": {
128            "broadcaster_user_id": "123456"
129          },
130          "transport": {
131            "method": "webhook",
132            "callback": "https://example.com/webhooks/callback"
133          },
134          "created_at": "2022-07-25T10:11:12.123Z"
135        },
136        "event": {
137          "id": "a1b2c3-aabb-4455-d1e2f3",
138          "campaign_id": "123-abc-456-def",
139          "broadcaster_id": "123456",
140          "broadcaster_name": "SunnySideUp",
141          "broadcaster_login": "sunnysideup",
142          "user_id": "654321",
143          "user_login": "generoususer1",
144          "user_name": "GenerousUser1",
145          "charity_name": "Example name",
146          "charity_description": "Example description",
147          "charity_logo": "https://abc.cloudfront.net/ppgf/1000/100.png",
148          "charity_website": "https://www.example.com",
149          "amount": {
150            "value": 10000,
151            "decimal_places": 2,
152            "currency": "USD"
153          }
154        }
155      }
156    "##;
157
158    let val = dbg!(crate::eventsub::Event::parse(payload).unwrap());
159    crate::tests::roundtrip(&val)
160}