Skip to content

Commit

Permalink
feat: add chat.shared_chat.end
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Nov 8, 2024
1 parent 9fb9718 commit e28d8f3
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/eventsub/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub use raid::{ChannelRaidV1, ChannelRaidV1Payload};
#[doc(inline)]
pub use shared_chat::{ChannelSharedChatBeginV1, ChannelSharedChatBeginV1Payload};
#[doc(inline)]
pub use shared_chat::{ChannelSharedChatEndV1, ChannelSharedChatEndV1Payload};
#[doc(inline)]
pub use shared_chat::{ChannelSharedChatUpdateV1, ChannelSharedChatUpdateV1Payload};
#[doc(inline)]
pub use shield_mode::{ChannelShieldModeBeginV1, ChannelShieldModeBeginV1Payload};
Expand Down
114 changes: 114 additions & 0 deletions src/eventsub/channel/shared_chat/end.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#![doc(alias = "channel.shared_chat.end")]
//! A channel leaves a shared chat session or the session ends.

use super::*;

/// [`channel.shared_chat.end`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelshared_chatend): a channel leaves a shared chat session or the session ends.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChannelSharedChatEndV1 {
/// The User ID of the channel to receive shared chat session end events for.
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
pub broadcaster_user_id: types::UserId,
}

impl ChannelSharedChatEndV1 {
/// The User ID of the channel to receive shared chat session end events for.
pub fn broadcaster_user_id(broadcaster_user_id: impl Into<types::UserId>) -> Self {
Self {
broadcaster_user_id: broadcaster_user_id.into(),
}
}

Check warning on line 23 in src/eventsub/channel/shared_chat/end.rs

View check run for this annotation

Codecov / codecov/patch

src/eventsub/channel/shared_chat/end.rs#L19-L23

Added lines #L19 - L23 were not covered by tests
}

impl EventSubscription for ChannelSharedChatEndV1 {
type Payload = ChannelSharedChatEndV1Payload;

const EVENT_TYPE: EventType = EventType::ChannelSharedChatEnd;
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
const VERSION: &'static str = "1";
}

/// [`channel.shared_chat.end`](ChannelSharedChatEndV1) response payload.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChannelSharedChatEndV1Payload {
/// The unique identifier for the shared chat session.
pub session_id: types::SharedChatSessionId,
/// The User ID of the channel in the subscription condition which is no longer active in the shared chat session.
pub broadcaster_user_id: types::UserId,
/// The display name of the channel in the subscription condition which is no longer active in the shared chat session.
pub broadcaster_user_name: types::DisplayName,
/// The user login of the channel in the subscription condition which is no longer active in the shared chat session.
pub broadcaster_user_login: types::UserName,
/// The User ID of the host channel.
pub host_broadcaster_user_id: types::UserId,
/// The display name of the host channel.
pub host_broadcaster_user_name: types::DisplayName,
/// The user login of the host channel.
pub host_broadcaster_user_login: types::UserName,
}

#[cfg(test)]
#[test]
fn parse_payload() {
use crate::eventsub::{Event, Message};

let payload = r##"
{
"subscription": {
"id": "84a875f1-1dc0-43b2-8ed3-d7db4d650c37",
"status": "enabled",
"type": "channel.shared_chat.end",
"version": "1",
"condition": {
"broadcaster_user_id": "112233"
},
"transport": {
"method": "websocket",
"session_id": "AgoQOtgGkFvXRlSkij343CndhIGY2VsbC1h"
},
"created_at": "2023-10-06T18:04:38.807682738Z",
"cost": 0
},
"event": {
"session_id": "2b64a92a-dbb8-424e-b1c3-304423ba1b6f",
"broadcaster_user_id": "1971641",
"broadcaster_user_login": "streamer",
"broadcaster_user_name": "streamer",
"host_broadcaster_user_id": "1971641",
"host_broadcaster_user_login": "streamer",
"host_broadcaster_user_name": "streamer"
}
}
"##;

let val = Event::parse(payload).unwrap();
crate::tests::roundtrip(&val);

let Event::ChannelSharedChatEndV1(val) = val else {
panic!("invalid event type");

Check warning on line 94 in src/eventsub/channel/shared_chat/end.rs

View check run for this annotation

Codecov / codecov/patch

src/eventsub/channel/shared_chat/end.rs#L94

Added line #L94 was not covered by tests
};
let Message::Notification(notif) = val.message else {
panic!("invalid message type");

Check warning on line 97 in src/eventsub/channel/shared_chat/end.rs

View check run for this annotation

Codecov / codecov/patch

src/eventsub/channel/shared_chat/end.rs#L97

Added line #L97 was not covered by tests
};

assert_eq!(notif.broadcaster_user_id.as_str(), "1971641");
assert_eq!(notif.broadcaster_user_id, notif.host_broadcaster_user_id);
assert_eq!(
notif.broadcaster_user_login,
notif.host_broadcaster_user_login
);
assert_eq!(
notif.broadcaster_user_name,
notif.host_broadcaster_user_name
);
assert_eq!(
notif.session_id.as_str(),
"2b64a92a-dbb8-424e-b1c3-304423ba1b6f"
);
}
3 changes: 3 additions & 0 deletions src/eventsub/channel/shared_chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ use crate::types;
use serde_derive::{Deserialize, Serialize};

pub mod begin;
pub mod end;
pub mod update;

#[doc(inline)]
pub use begin::{ChannelSharedChatBeginV1, ChannelSharedChatBeginV1Payload};
#[doc(inline)]
pub use end::{ChannelSharedChatEndV1, ChannelSharedChatEndV1Payload};
#[doc(inline)]
pub use update::{ChannelSharedChatUpdateV1, ChannelSharedChatUpdateV1Payload};

/// A participant in a shared chat session
Expand Down
5 changes: 5 additions & 0 deletions src/eventsub/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ macro_rules! fill_events {
channel::ChannelPredictionProgressV1;
channel::ChannelRaidV1;
channel::ChannelSharedChatBeginV1;
channel::ChannelSharedChatEndV1;
channel::ChannelSharedChatUpdateV1;
channel::ChannelShieldModeBeginV1;
channel::ChannelShieldModeEndV1;
Expand Down Expand Up @@ -213,6 +214,8 @@ make_event_type!("Event Types": pub enum EventType {
ChannelRaid => "channel.raid",
"a channel becomes active in an active shared chat session.":
ChannelSharedChatBegin => "channel.shared_chat.begin",
"a channel leaves a shared chat session or the session ends.":
ChannelSharedChatEnd => "channel.shared_chat.end",
"the active shared chat session the channel is in changed.":
ChannelSharedChatUpdate => "channel.shared_chat.update",
"a subscription to the specified channel expires.":
Expand Down Expand Up @@ -346,6 +349,8 @@ pub enum Event {
ChannelRaidV1(Payload<channel::ChannelRaidV1>),
/// Channel SharedChat Begin V1 Event
ChannelSharedChatBeginV1(Payload<channel::ChannelSharedChatBeginV1>),
/// Channel SharedChat End V1 Event
ChannelSharedChatEndV1(Payload<channel::ChannelSharedChatEndV1>),
/// Channel SharedChat Update V1 Event
ChannelSharedChatUpdateV1(Payload<channel::ChannelSharedChatUpdateV1>),
/// Channel ShieldMode Begin V1 Event
Expand Down
4 changes: 2 additions & 2 deletions src/eventsub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
//!
//! </details>
//!
//! <details><summary style="cursor: pointer"><code style="color: var(--link-color)">channel.*</code> 🟡 44/65</summary>
//! <details><summary style="cursor: pointer"><code style="color: var(--link-color)">channel.*</code> 🟡 45/65</summary>
//!
//! | Name | Subscription<br>Payload |
//! |---|:---|
Expand Down Expand Up @@ -139,7 +139,7 @@
//! | [`channel.prediction.progress`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpredictionprogress) | [ChannelPredictionProgressV1](channel::ChannelPredictionProgressV1)<br>[ChannelPredictionProgressV1Payload](channel::ChannelPredictionProgressV1Payload) |
//! | [`channel.raid`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelraid) | [ChannelRaidV1](channel::ChannelRaidV1)<br>[ChannelRaidV1Payload](channel::ChannelRaidV1Payload) |
//! | [`channel.shared_chat.begin`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshared_chatbegin) | [ChannelSharedChatBeginV1](channel::ChannelSharedChatBeginV1)<br>[ChannelSharedChatBeginV1Payload](channel::ChannelSharedChatBeginV1Payload) |
//! | [`channel.shared_chat.end`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshared_chatend) | -<br>- |
//! | [`channel.shared_chat.end`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshared_chatend) | [ChannelSharedChatEndV1](channel::ChannelSharedChatEndV1)<br>[ChannelSharedChatEndV1Payload](channel::ChannelSharedChatEndV1Payload) |
//! | [`channel.shared_chat.update`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshared_chatupdate) | [ChannelSharedChatUpdateV1](channel::ChannelSharedChatUpdateV1)<br>[ChannelSharedChatUpdateV1Payload](channel::ChannelSharedChatUpdateV1Payload) |
//! | [`channel.shield_mode.begin`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshield_modebegin) | [ChannelShieldModeBeginV1](channel::ChannelShieldModeBeginV1)<br>[ChannelShieldModeBeginV1Payload](channel::ChannelShieldModeBeginV1Payload) |
//! | [`channel.shield_mode.end`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelshield_modeend) | [ChannelShieldModeEndV1](channel::ChannelShieldModeEndV1)<br>[ChannelShieldModeEndV1Payload](channel::ChannelShieldModeEndV1Payload) |
Expand Down

0 comments on commit e28d8f3

Please sign in to comment.