|
| 1 | +#![doc(alias = "user.whisper.message")] |
| 2 | +//! A user receives a whisper |
| 3 | +use super::*; |
| 4 | + |
| 5 | +/// [`user.whisper.message`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#userwhispermessage): a user receives a whisper. |
| 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] |
| 10 | +pub struct UserWhisperMessageV1 { |
| 11 | + /// The user ID of the person receiving whispers. |
| 12 | + #[cfg_attr(feature = "typed-builder", builder(setter(into)))] |
| 13 | + pub user_id: types::UserId, |
| 14 | +} |
| 15 | + |
| 16 | +impl UserWhisperMessageV1 { |
| 17 | + /// The user ID of the person receiving whispers. |
| 18 | + pub fn new(user_id: impl Into<types::UserId>) -> Self { |
| 19 | + Self { |
| 20 | + user_id: user_id.into(), |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl EventSubscription for UserWhisperMessageV1 { |
| 26 | + type Payload = UserWhisperMessageV1Payload; |
| 27 | + |
| 28 | + const EVENT_TYPE: EventType = EventType::UserWhisperMessage; |
| 29 | + #[cfg(feature = "twitch_oauth2")] |
| 30 | + const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![any( |
| 31 | + twitch_oauth2::Scope::UserReadWhispers, |
| 32 | + twitch_oauth2::Scope::UserManageWhispers |
| 33 | + )]; |
| 34 | + const VERSION: &'static str = "1"; |
| 35 | +} |
| 36 | + |
| 37 | +/// [`user.whisper.message`](UserWhisperMessageV1) response payload. |
| 38 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 39 | +#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))] |
| 40 | +#[non_exhaustive] |
| 41 | +pub struct UserWhisperMessageV1Payload { |
| 42 | + /// The ID of the user sending the message. |
| 43 | + pub from_user_id: types::UserId, |
| 44 | + /// The name of the user sending the message. |
| 45 | + pub from_user_name: types::DisplayName, |
| 46 | + /// The login of the user sending the message. |
| 47 | + pub from_user_login: types::UserName, |
| 48 | + /// The ID of the user receiving the message. |
| 49 | + pub to_user_id: types::UserId, |
| 50 | + /// The name of the user receiving the message. |
| 51 | + pub to_user_name: types::DisplayName, |
| 52 | + /// The login of the user receiving the message. |
| 53 | + pub to_user_login: types::UserName, |
| 54 | + /// The whisper ID. |
| 55 | + pub whisper_id: types::WhisperId, |
| 56 | + /// Object containing whisper information. |
| 57 | + pub whisper: Whisper, |
| 58 | +} |
| 59 | + |
| 60 | +/// Object containing whisper information. |
| 61 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 62 | +#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))] |
| 63 | +#[non_exhaustive] |
| 64 | +pub struct Whisper { |
| 65 | + /// The body of the whisper message. |
| 66 | + pub text: String, |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +#[test] |
| 71 | +fn parse_payload() { |
| 72 | + let payload = r#" |
| 73 | + { |
| 74 | + "subscription": { |
| 75 | + "id": "7297f7eb-3bf5-461f-8ae6-7cd7781ebce3", |
| 76 | + "status": "enabled", |
| 77 | + "type": "user.whisper.message", |
| 78 | + "version": "1", |
| 79 | + "condition": { |
| 80 | + "user_id": "423374343" |
| 81 | + }, |
| 82 | + "transport": { |
| 83 | + "method": "webhook", |
| 84 | + "callback": "https://example.com/webhooks/callback" |
| 85 | + }, |
| 86 | + "created_at": "2024-02-23T21:12:33.771005262Z", |
| 87 | + "cost": 0 |
| 88 | + }, |
| 89 | + "event": { |
| 90 | + "from_user_id": "423374343", |
| 91 | + "from_user_login": "glowillig", |
| 92 | + "from_user_name": "glowillig", |
| 93 | + "to_user_id": "424596340", |
| 94 | + "to_user_login": "quotrok", |
| 95 | + "to_user_name": "quotrok", |
| 96 | + "whisper_id": "some-whisper-id", |
| 97 | + "whisper": { |
| 98 | + "text": "a secret" |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + "#; |
| 103 | + |
| 104 | + let val = dbg!(crate::eventsub::Event::parse(payload).unwrap()); |
| 105 | + crate::tests::roundtrip(&val) |
| 106 | +} |
0 commit comments