Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user.whisper.message #471

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exclude = ["twitch_types", "twitch_oauth2"]

[workspace.dependencies]
twitch_api = { version = "0.7.0-rc.8", path = "." }
twitch_oauth2 = { version = "0.15.0", path = "twitch_oauth2/" }
twitch_oauth2 = { version = "0.15.1", path = "twitch_oauth2/" }
twitch_types = { version = "0.4.8", features = [
"serde",
], path = "./twitch_types" }
Expand Down
5 changes: 5 additions & 0 deletions src/eventsub/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ macro_rules! fill_events {
user::UserAuthorizationGrantV1;
user::UserAuthorizationRevokeV1;
user::UserUpdateV1;
user::UserWhisperMessageV1;
)
};
}
Expand Down Expand Up @@ -316,6 +317,8 @@ make_event_type!("Event Types": pub enum EventType {
UserAuthorizationRevoke => "user.authorization.revoke",
"a user’s authorization has been granted to your client id.":
UserAuthorizationGrant => "user.authorization.grant",
"a user receives a whisper.":
UserWhisperMessage => "user.whisper.message",
},
to_str: r#"Get the event string of this event.
```
Expand Down Expand Up @@ -499,6 +502,8 @@ pub enum Event {
UserAuthorizationGrantV1(Payload<user::UserAuthorizationGrantV1>),
/// User Authorization Revoke V1 Event
UserAuthorizationRevokeV1(Payload<user::UserAuthorizationRevokeV1>),
/// User Whisper Message V1 Event
UserWhisperMessageV1(Payload<user::UserWhisperMessageV1>),
/// Channel Subscription End V1 Event
ChannelSubscriptionEndV1(Payload<channel::ChannelSubscriptionEndV1>),
/// Channel Subscription Gift 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 @@ -197,14 +197,14 @@
//!
//! </details>
//!
//! <details><summary style="cursor: pointer"><code style="color: var(--link-color)">user.*</code> 🟡 3/4</summary>
//! <details><summary style="cursor: pointer"><code style="color: var(--link-color)">user.*</code> 🟢 4/4</summary>
//!
//! | Name | Subscription<br>Payload |
//! |---|:---|
//! | [`user.authorization.grant`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userauthorizationgrant) (v1) | [UserAuthorizationGrantV1](user::UserAuthorizationGrantV1)<br>[UserAuthorizationGrantV1Payload](user::UserAuthorizationGrantV1Payload) |
//! | [`user.authorization.revoke`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userauthorizationrevoke) (v1) | [UserAuthorizationRevokeV1](user::UserAuthorizationRevokeV1)<br>[UserAuthorizationRevokeV1Payload](user::UserAuthorizationRevokeV1Payload) |
//! | [`user.update`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userupdate) (v1) | [UserUpdateV1](user::UserUpdateV1)<br>[UserUpdateV1Payload](user::UserUpdateV1Payload) |
//! | [`user.whisper.message`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userwhispermessage) (v1) | -<br>- |
//! | [`user.whisper.message`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userwhispermessage) (v1) | [UserWhisperMessageV1](user::UserWhisperMessageV1)<br>[UserWhisperMessageV1Payload](user::UserWhisperMessageV1Payload) |
//!
//! </details>
//!
Expand Down
3 changes: 3 additions & 0 deletions src/eventsub/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use serde_derive::{Deserialize, Serialize};

pub mod authorization;
pub mod update;
pub mod whisper;

#[doc(inline)]
pub use authorization::{UserAuthorizationGrantV1, UserAuthorizationGrantV1Payload};
#[doc(inline)]
pub use authorization::{UserAuthorizationRevokeV1, UserAuthorizationRevokeV1Payload};
#[doc(inline)]
pub use update::{UserUpdateV1, UserUpdateV1Payload};
#[doc(inline)]
pub use whisper::{UserWhisperMessageV1, UserWhisperMessageV1Payload};
106 changes: 106 additions & 0 deletions src/eventsub/user/whisper/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#![doc(alias = "user.whisper.message")]
//! A user receives a whisper
use super::*;

/// [`user.whisper.message`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#userwhispermessage): a user receives a whisper.
#[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 UserWhisperMessageV1 {
/// The user ID of the person receiving whispers.
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
pub user_id: types::UserId,
}

impl UserWhisperMessageV1 {
/// The user ID of the person receiving whispers.
pub fn new(user_id: impl Into<types::UserId>) -> Self {
Self {
user_id: user_id.into(),
}
}

Check warning on line 22 in src/eventsub/user/whisper/message.rs

View check run for this annotation

Codecov / codecov/patch

src/eventsub/user/whisper/message.rs#L18-L22

Added lines #L18 - L22 were not covered by tests
}

impl EventSubscription for UserWhisperMessageV1 {
type Payload = UserWhisperMessageV1Payload;

const EVENT_TYPE: EventType = EventType::UserWhisperMessage;
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any(
twitch_oauth2::Scope::UserReadWhispers,
twitch_oauth2::Scope::UserManageWhispers
)];
const VERSION: &'static str = "1";
}

/// [`user.whisper.message`](UserWhisperMessageV1) response payload.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct UserWhisperMessageV1Payload {
/// The ID of the user sending the message.
pub from_user_id: types::UserId,
/// The name of the user sending the message.
pub from_user_name: types::DisplayName,
/// The login of the user sending the message.
pub from_user_login: types::UserName,
/// The ID of the user receiving the message.
pub to_user_id: types::UserId,
/// The name of the user receiving the message.
pub to_user_name: types::DisplayName,
/// The login of the user receiving the message.
pub to_user_login: types::UserName,
/// The whisper ID.
pub whisper_id: types::WhisperId,
/// Object containing whisper information.
pub whisper: Whisper,
}

/// Object containing whisper information.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Whisper {
/// The body of the whisper message.
pub text: String,
}

#[cfg(test)]
#[test]
fn parse_payload() {
let payload = r#"
{
"subscription": {
"id": "7297f7eb-3bf5-461f-8ae6-7cd7781ebce3",
"status": "enabled",
"type": "user.whisper.message",
"version": "1",
"condition": {
"user_id": "423374343"
},
"transport": {
"method": "webhook",
"callback": "https://example.com/webhooks/callback"
},
"created_at": "2024-02-23T21:12:33.771005262Z",
"cost": 0
},
"event": {
"from_user_id": "423374343",
"from_user_login": "glowillig",
"from_user_name": "glowillig",
"to_user_id": "424596340",
"to_user_login": "quotrok",
"to_user_name": "quotrok",
"whisper_id": "some-whisper-id",
"whisper": {
"text": "a secret"
}
}
}
"#;

let val = dbg!(crate::eventsub::Event::parse(payload).unwrap());
crate::tests::roundtrip(&val)
}
10 changes: 10 additions & 0 deletions src/eventsub/user/whisper/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![doc(alias = "user.whisper")]
//! Notifications for whispers (private messages)
use super::{EventSubscription, EventType};
use crate::types;
use serde_derive::{Deserialize, Serialize};

pub mod message;

#[doc(inline)]
pub use message::{UserWhisperMessageV1, UserWhisperMessageV1Payload, Whisper};
2 changes: 1 addition & 1 deletion twitch_oauth2
Loading