1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#![doc(alias = "raids")]
//! PubSub messages for raids
use crate::{pubsub, types};
use serde::{Deserialize, Serialize};

/// A user raids the channel
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(into = "String", try_from = "String")]
pub struct Raid {
    /// The channel_id to watch. Can be fetched with the [Get Users](crate::helix::users::get_users) endpoint
    pub channel_id: u32,
}

impl_de_ser!(Raid, "raid", channel_id);

impl pubsub::Topic for Raid {
    #[cfg(feature = "twitch_oauth2")]
    const SCOPE: &'static [twitch_oauth2::Scope] = &[];

    fn into_topic(self) -> pubsub::Topics { super::Topics::Raid(self) }
}

/// Raid go
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct RaidGoV2 {
    /// ID of user would be raided
    pub creator_id: types::UserId,
    /// Raid will be force started in this many seconds
    pub force_raid_now_seconds: i64,
    /// ID of raid
    pub id: String,
    /// ID of broadcaster doing raid
    pub source_id: types::UserId,
    /// Display name of targeted broadcaster/user
    pub target_display_name: types::DisplayName,
    /// ID of targeted broadcaster/user
    pub target_id: types::UserId,
    /// Username of targeted broadcaster/user
    pub target_login: types::UserName,
    /// Profile picture of targeted broadcaster/user
    pub target_profile_image: String,
    /// Jitter amount
    pub transition_jitter_seconds: i64,
    /// Amount of viewers that will join raid
    pub viewer_count: i64,
}

/// Raid update
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct RaidUpdateV2 {
    /// ID of user would be raided
    pub creator_id: types::UserId,
    /// Raid will be force started in this many seconds
    pub force_raid_now_seconds: i64,
    /// ID of raid
    pub id: String,
    /// ID of broadcaster doing raid
    pub source_id: types::UserId,
    /// Display name of targeted broadcaster/user
    pub target_display_name: types::DisplayName,
    /// ID of targeted broadcaster/user
    pub target_id: types::UserId,
    /// Username of targeted broadcaster/user
    pub target_login: types::UserName,
    /// Profile picture of targeted broadcaster/user
    pub target_profile_image: String,
    /// Jitter amount
    pub transition_jitter_seconds: i64,
    /// Amount of viewers that will join raid
    pub viewer_count: i64,
}

/// Raid canceled
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct RaidCancelV2 {
    /// ID of user would be raided
    pub creator_id: types::UserId,
    /// Raid would have be force started in this many seconds
    pub force_raid_now_seconds: i64,
    /// ID of raid
    pub id: String,
    /// ID of broadcaster doing raid
    pub source_id: types::UserId,
    /// Display name of targeted broadcaster/user
    pub target_display_name: types::DisplayName,
    /// ID of targeted broadcaster/user
    pub target_id: types::UserId,
    /// Username of targeted broadcaster/user
    pub target_login: types::UserName,
    /// Profile picture of targeted broadcaster/user
    pub target_profile_image: String,
    /// Jitter amount
    pub transition_jitter_seconds: i64,
    /// Amount of viewers that would join raid
    pub viewer_count: i64,
}

/// Reply from [Raid]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type", content = "raid")]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub enum RaidReply {
    /// Raid go
    #[serde(rename = "raid_go_v2")]
    RaidGoV2(RaidGoV2),
    /// Raid update
    #[serde(rename = "raid_update_v2")]
    RaidUpdateV2(RaidUpdateV2),
    /// Raid canceled
    #[serde(rename = "raid_cancel_v2")]
    RaidCancelV2(RaidCancelV2),
}

#[cfg(test)]
mod tests {
    use super::super::{Response, TopicData};
    use super::*;

    #[test]
    fn raid() {
        let message = r##"
{
    "type": "raid_go_v2",
    "raid": {
        "id": "7fbea6d0-1337-4c61-8c92-b7510e639010",
        "creator_id": "27620241",
        "source_id": "27620241",
        "target_id": "1234",
        "target_login": "tmi",
        "target_display_name": "TMI",
        "target_profile_image": "https://static-cdn.jtvnw.net/jtv_user_pictures/tmi-profile_image-deadbeef1234-70x70.jpeg",
        "transition_jitter_seconds": 5,
        "force_raid_now_seconds": 90,
        "viewer_count": 5238
    }
}
"##;

        let source = format!(
            r#"{{"type": "MESSAGE", "data": {{ "topic": "raid.27620241", "message": {:?} }}}}"#,
            message
        );
        let actual = dbg!(Response::parse(&source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::Raid { .. },
            }
        ));
    }

    #[test]
    fn raid_update() {
        let message = r##"
{
    "type": "raid_update_v2",
    "raid": {
        "id": "7fbea6d0-4087-48e7-a395-c0ecd15ef551",
        "creator_id": "123455",
        "source_id": "123455",
        "target_id": "123",
        "target_login": "tmi",
        "target_display_name": "tmi",
        "target_profile_image": "https://static-cdn.jtvnw.net/jtv_user_pictures/deadbeef-profile_image-70x70.png",
        "transition_jitter_seconds": 0,
        "force_raid_now_seconds": 90,
        "viewer_count": 143
    }
}
"##;

        let source = format!(
            r#"{{"type": "MESSAGE", "data": {{ "topic": "raid.27620241", "message": {:?} }}}}"#,
            message
        );
        let actual = dbg!(Response::parse(&source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::Raid { .. },
            }
        ));
    }

    #[test]
    fn raid_cancel() {
        let message = r##"
{
    "type": "raid_cancel_v2",
    "raid": {
        "id": "7fbea6d0-5b4b-4b8f-a79d-c9e94bf88bbf",
        "creator_id": "123455",
        "source_id": "123455",
        "target_id": "123",
        "target_login": "tmi",
        "target_display_name": "TMI",
        "target_profile_image": "https://static-cdn.jtvnw.net/jtv_user_pictures/deadbeef-profile_image-70x70.png",
        "transition_jitter_seconds": 4,
        "force_raid_now_seconds": 90,
        "viewer_count": 4463
    }
}
"##;

        let source = format!(
            r#"{{"type": "MESSAGE", "data": {{ "topic": "raid.27620241", "message": {:?} }}}}"#,
            message
        );
        let actual = dbg!(Response::parse(&source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::Raid { .. },
            }
        ));
    }

    #[test]
    fn check_deser() {
        use std::convert::TryInto as _;
        let s = "raid.1234";
        assert_eq!(Raid { channel_id: 1234 }, s.to_string().try_into().unwrap());
    }

    #[test]
    fn check_ser() {
        let s = "raid.1234";
        let right: String = Raid { channel_id: 1234 }.into();
        assert_eq!(s.to_string(), right);
    }
}