twitch_api/eventsub/channel/shoutout/
receive.rs

1#![doc(alias = "channel.shoutout.receive")]
2//! A specified broadcaster receives a Shoutout.
3
4use super::*;
5/// [`channel.shoutout.receive`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelshoutoutreceive): a Prediction 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 ChannelShoutoutReceiveV1 {
11    /// The ID of the broadcaster that you want to receive notifications about when they receive a Shoutout.
12    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
13    pub broadcaster_user_id: types::UserId,
14    /// The ID of the broadcaster that received the Shoutout or one of the broadcaster’s moderators.
15    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
16    pub moderator_user_id: types::UserId,
17}
18
19impl ChannelShoutoutReceiveV1 {
20    /// Create a new [`ChannelShoutoutReceiveV1`]
21    pub fn new(
22        broadcaster_user_id: impl Into<types::UserId>,
23        moderator_user_id: impl Into<types::UserId>,
24    ) -> Self {
25        Self {
26            broadcaster_user_id: broadcaster_user_id.into(),
27            moderator_user_id: moderator_user_id.into(),
28        }
29    }
30}
31
32impl EventSubscription for ChannelShoutoutReceiveV1 {
33    type Payload = ChannelShoutoutReceiveV1Payload;
34
35    const EVENT_TYPE: EventType = EventType::ChannelShoutoutReceive;
36    #[cfg(feature = "twitch_oauth2")]
37    const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
38        twitch_oauth2::Scope::ModeratorReadShoutouts,
39        twitch_oauth2::Scope::ModeratorManageShoutouts
40    )];
41    const VERSION: &'static str = "1";
42}
43
44/// [`channel.shoutout.receive`](ChannelShoutoutReceiveV1) response payload.
45#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
46#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
47#[non_exhaustive]
48pub struct ChannelShoutoutReceiveV1Payload {
49    /// An ID that identifies the broadcaster that received the Shoutout.
50    pub broadcaster_user_id: types::UserId,
51    /// The broadcaster’s login name.
52    pub broadcaster_user_login: types::UserName,
53    /// The broadcaster’s display name.
54    pub broadcaster_user_name: types::DisplayName,
55    /// An ID that identifies the broadcaster that sent the Shoutout.
56    pub from_broadcaster_user_id: types::UserId,
57    /// The broadcaster’s login name.
58    pub from_broadcaster_user_login: types::UserName,
59    /// The broadcaster’s display name.
60    pub from_broadcaster_user_name: types::DisplayName,
61    /// The number of users that were watching the from-broadcaster’s stream at the time of the Shoutout.
62    pub viewer_count: i64,
63    /// The UTC timestamp (in RFC3339 format) of when the moderator sent the Shoutout.
64    pub started_at: types::Timestamp,
65}
66
67#[cfg(test)]
68#[test]
69fn parse_payload() {
70    let payload = r##"
71    {
72        "subscription": {
73          "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
74          "type": "channel.shoutout.receive",
75          "version": "1",
76          "status": "enabled",
77          "cost": 0,
78          "condition": {
79            "broadcaster_user_id": "626262",
80            "moderator_user_id": "98765"
81          },
82          "transport": {
83            "method": "webhook",
84            "callback": "https://example.com/webhooks/callback"
85          },
86          "created_at": "2022-07-25T10:11:12.1236739Z"
87        },
88        "event": {
89          "broadcaster_user_id": "626262",
90          "broadcaster_user_name": "SandySanderman",
91          "broadcaster_user_login": "sandysanderman",
92          "from_broadcaster_user_id": "12345",
93          "from_broadcaster_user_name": "SimplySimple",
94          "from_broadcaster_user_login": "simplysimple",
95          "viewer_count": 860,
96          "started_at": "2022-07-26T17:00:03.17106713Z"
97        }
98      }
99    "##;
100
101    let val = dbg!(crate::eventsub::Event::parse(payload).unwrap());
102    crate::tests::roundtrip(&val)
103}