twitch_api/eventsub/user/
update.rs

1#![doc(alias = "user.update")]
2//! Specified user updates their account.
3use super::*;
4/// [`user.update`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userupdate): user updates their account.
5#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
6#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
7#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
8#[non_exhaustive]
9pub struct UserUpdateV1 {
10    /// The user ID for the user you want update notifications for.
11    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
12    pub user_id: types::UserId,
13}
14
15impl UserUpdateV1 {
16    /// The user ID for the user you want update notifications for.
17    pub fn new(user_id: impl Into<types::UserId>) -> Self {
18        Self {
19            user_id: user_id.into(),
20        }
21    }
22}
23
24impl EventSubscription for UserUpdateV1 {
25    type Payload = UserUpdateV1Payload;
26
27    const EVENT_TYPE: EventType = EventType::UserUpdate;
28    #[cfg(feature = "twitch_oauth2")]
29    const OPT_SCOPE: &'static [twitch_oauth2::Scope] = &[twitch_oauth2::Scope::UserReadEmail];
30    #[cfg(feature = "twitch_oauth2")]
31    const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
32    const VERSION: &'static str = "1";
33}
34
35/// [`user.update`](UserUpdateV1) response payload.
36#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
37#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
38#[non_exhaustive]
39pub struct UserUpdateV1Payload {
40    /// The user’s description.
41    pub description: String,
42    /// The user’s email. Only included if you have the [`user:read:email`](twitch_oauth2::Scope::UserReadEmail) scope for the user.
43    pub email: Option<String>,
44    /// The user’s user id.
45    pub user_id: types::UserId,
46    /// The user’s user login.
47    pub user_login: types::UserName,
48    /// The user’s user display name.
49    pub user_name: types::DisplayName,
50}
51
52#[cfg(test)]
53#[test]
54fn parse_payload() {
55    let payload = r#"
56    {
57        "subscription": {
58            "id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
59            "type": "user.update",
60            "version": "1",
61            "status": "enabled",
62            "cost": 0,
63            "condition": {
64               "user_id": "1337"
65            },
66             "transport": {
67                "method": "webhook",
68                "callback": "https://example.com/webhooks/callback"
69            },
70            "created_at": "2019-11-16T10:11:12.123Z"
71        },
72        "event": {
73            "user_id": "1337",
74            "user_login": "cool_user",
75            "user_name": "Cool_User",
76            "email": "user@email.com",
77            "description": "cool description"
78        }
79    }
80    "#;
81
82    let val = dbg!(crate::eventsub::Event::parse(payload).unwrap());
83    crate::tests::roundtrip(&val)
84}