twitch_api/eventsub/channel/guest_star_session/
end.rs

1#![doc(alias = "channel.guest_star_session.end")]
2//! a running Guest Star session is ended by the host, or automatically by the system.
3
4use super::*;
5/// [`channel.guest_star_session.end`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelguest_star_sessionend): a running Guest Star session is ended by the host, or automatically by the system.
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 ChannelGuestStarSessionEndBeta {
11    /// The broadcaster user ID for the channel you want to receive Guest Star session end notifications for.
12    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
13    pub broadcaster_user_id: types::UserId,
14    /// The user ID of the moderator or broadcaster of the specified channel.
15    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
16    pub moderator_user_id: types::UserId,
17}
18
19impl ChannelGuestStarSessionEndBeta {
20    /// Get notifications for guest star sessions in this channel as a moderator
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 ChannelGuestStarSessionEndBeta {
33    type Payload = ChannelGuestStarSessionEndBetaPayload;
34
35    const EVENT_TYPE: EventType = EventType::ChannelGuestStarSessionEnd;
36    #[cfg(feature = "twitch_oauth2")]
37    const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
38        twitch_oauth2::Scope::ChannelReadGuestStar,
39        twitch_oauth2::Scope::ChannelManageGuestStar,
40        twitch_oauth2::Scope::ModeratorReadGuestStar,
41        twitch_oauth2::Scope::ModeratorManageGuestStar,
42    )];
43    const VERSION: &'static str = "beta";
44}
45
46/// [`channel.guest_star_session.end`](ChannelGuestStarSessionEndBeta) response payload.
47#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
48#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
49#[non_exhaustive]
50pub struct ChannelGuestStarSessionEndBetaPayload {
51    /// The non-host broadcaster user ID.
52    pub broadcaster_user_id: types::UserId,
53    /// The non-host broadcaster display name.
54    pub broadcaster_user_login: types::UserName,
55    /// The non-host broadcaster login.
56    pub broadcaster_user_name: types::DisplayName,
57
58    /// ID representing the unique session that was started.
59    pub session_id: types::GuestStarSessionId,
60    /// RFC3339 timestamp indicating the time the session began.
61    pub started_at: types::Timestamp,
62    /// RFC3339 timestamp indicating the time the session ended.
63    pub ended_at: types::Timestamp,
64
65    /// User ID of the host channel.
66    pub host_user_id: types::UserId,
67    /// The host display name.
68    pub host_user_login: types::UserName,
69    /// The host login.
70    pub host_user_name: types::DisplayName,
71}
72
73#[cfg(test)]
74#[test]
75fn parse_payload() {
76    use crate::eventsub::{Event, Message};
77
78    let payload = r##"
79    {
80        "subscription": {
81            "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
82            "type": "channel.guest_star_session.end",
83            "version": "beta",
84            "status": "enabled",
85            "cost": 0,
86            "condition": {
87                "broadcaster_user_id": "1337",
88                "moderator_user_id": "1338"
89            },
90            "transport": {
91                "method": "webhook",
92                "callback": "https://example.com/webhooks/callback"
93            },
94            "created_at": "2023-04-11T10:11:22.123Z"
95        },
96        "event": {
97            "broadcaster_user_id": "1337",
98            "broadcaster_user_name": "Cool_User",
99            "broadcaster_user_login": "cool_user",
100            "host_user_id": "1338",
101            "host_user_name": "Cool_Mod",
102            "host_user_login": "cool_mod",
103            "session_id": "2KFRQbFtpmfyD3IevNRnCzOPRJI",
104            "started_at": "2023-04-11T16:20:03.17106713Z",
105            "ended_at": "2023-04-11T17:51:29.153485Z"
106        }
107    }
108    "##;
109
110    let val = Event::parse(payload).unwrap();
111    crate::tests::roundtrip(&val);
112
113    let Event::ChannelGuestStarSessionEndBeta(val) = val else {
114        panic!("invalid event type");
115    };
116    let Message::Notification(notif) = val.message else {
117        panic!("invalid settings type");
118    };
119
120    assert_eq!(notif.broadcaster_user_id.as_str(), "1337");
121    assert_eq!(notif.host_user_id.as_str(), "1338");
122    assert_eq!(notif.session_id.as_str(), "2KFRQbFtpmfyD3IevNRnCzOPRJI");
123    assert_eq!(notif.started_at.as_str(), "2023-04-11T16:20:03.17106713Z");
124    assert_eq!(notif.ended_at.as_str(), "2023-04-11T17:51:29.153485Z");
125}