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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#![doc(alias = "vod")]
#![doc(alias = "videos")]
#![doc(alias = "live")]
#![doc(alias = "stream")]
#![doc(alias = "viewers")]
#![doc(alias = "video-playback")]
//! PubSub messages for (live) stream playback information
use crate::{pubsub, types};
use serde::{Deserialize, Serialize};

/// Statistics about stream
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(into = "String", try_from = "String")]
pub struct VideoPlayback {
    /// The channel_login to watch.
    pub channel_login: types::DisplayName,
}

impl_de_ser!(VideoPlayback, "video-playback", channel_login);

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

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

/// Statistics about stream
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(into = "String", try_from = "String")]
pub struct VideoPlaybackById {
    /// The channel_login to watch.
    pub channel_id: u32,
}

impl_de_ser!(VideoPlaybackById, "video-playback-by-id", channel_id);

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

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

/// Reply from [VideoPlayback] and [VideoPlaybackById]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum VideoPlaybackReply {
    /// Commercial started
    ///
    /// Commercial started in full-screen or PiP
    #[serde(rename = "commercial")]
    Commercial {
        /// Length of commercial
        length: i64,
        /// Epoch Server time when commercial started
        server_time: f64,
        /// Commercial is scheduled or not.
        #[doc(hidden)]
        scheduled: Option<bool>,
    },
    /// Current viewcount on playback
    #[serde(rename = "viewcount")]
    ViewCount {
        /// Epoch Server time
        server_time: f64,
        /// Current viewers
        viewers: i64,
    },
    /// VOD Watchparty.
    #[serde(rename = "watchparty-vod")]
    WatchPartyVod {
        /// information about VOD.
        vod: Vod,
    },
    /// Stream started
    #[serde(rename = "stream-up")]
    StreamUp {
        /// Epoch Server time
        server_time: f64,
        /// Delay as set in broadcaster settings.
        play_delay: i64,
    },
    /// Stream ended
    #[serde(rename = "stream-down")]
    StreamDown {
        /// Epoch Server time
        server_time: f64,
    },
    /// Channel hit by TOS strike, meaning it will end
    #[serde(rename = "tos-strike")]
    TosStrike {
        /// Epoch Server time
        server_time: f64,
    },
}

/// Video on Demand
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Vod {
    /// Type of broadcast
    pub broadcast_type: BroadcastType,
    /// Url increment picture. Unknown usage
    pub increment_url: String,
    /// Title of VOD
    pub title: String,
    /// Availability of VOD
    pub viewable: types::VideoPrivacy,
    /// ID of current VOD
    pub vod_id: types::VideoId,
    /// ID of current watch party
    pub wp_id: String,
    /// Type of current watch party
    pub wp_type: WatchpartyType,
}

/// Watch party type
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum WatchpartyType {
    /// A rerun, i.e a highlight or saved broadcast
    Rerun,
    /// A premiere, i.e a uploaded video
    Premiere,
}

/// Type of broadcast
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum BroadcastType {
    /// Archive
    Archive,
}

#[cfg(test)]
mod tests {
    use super::super::{Response, TopicData};
    use super::*;
    #[test]
    fn video_playback() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback.tmi",
        "message": "{\"type\":\"viewcount\",\"server_time\":1603127341.505835,\"viewers\":2}"
    }
}"#;
        let actual = dbg!(Response::parse(source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::VideoPlayback { .. },
            }
        ));
    }

    #[test]
    fn video_playback_by_id() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback-by-id.1234",
        "message": "{\"type\":\"viewcount\",\"server_time\":1603127341.505835,\"viewers\":2}"
    }
}"#;
        let actual = dbg!(Response::parse(source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::VideoPlaybackById { .. },
            }
        ));
    }

    #[test]
    fn stream_up() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback.tmi",
        "message": "{\"server_time\":1603291436,\"play_delay\":0,\"type\":\"stream-up\"}"
    }
}"#;
        if let Response::Message { data } = dbg!(Response::parse(source).unwrap()) {
            if let TopicData::VideoPlayback { reply, .. } = data {
                assert!(matches!(*reply, VideoPlaybackReply::StreamUp { .. }))
            } else {
                panic!("not a videoplayback")
            }
        } else {
            panic!("not a message")
        }
    }

    #[test]
    fn stream_down() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback.tmi",
        "message": "{\"server_time\":1603141689,\"type\":\"stream-down\"}"
    }
}"#;
        if let Response::Message { data } = dbg!(Response::parse(source).unwrap()) {
            if let TopicData::VideoPlayback { reply, .. } = data {
                assert!(matches!(*reply, VideoPlaybackReply::StreamDown { .. }))
            } else {
                panic!("not a videoplayback")
            }
        } else {
            panic!("not a message")
        }
    }

    #[test]
    fn watch_party_vod_rerun() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback-by-id.1234",
        "message": "{\"type\":\"watchparty-vod\",\"vod\":{\"wp_id\":\"\",\"wp_type\":\"rerun\",\"increment_url\":\"https://countess.twitch.tv/ping.gif?u=%7B%22id%22%3A%22711110781%22%2C%22type%22%3A%22vod%22%7D\",\"vod_id\":\"1337\",\"title\":\"hi\",\"broadcast_type\":\"archive\",\"viewable\":\"public\"}}"
    }
}"#;
        if let Response::Message { data } = dbg!(Response::parse(source).unwrap()) {
            if let TopicData::VideoPlaybackById { reply, .. } = data {
                assert!(matches!(
                    *reply,
                    VideoPlaybackReply::WatchPartyVod { vod: Vod { .. } }
                ))
            } else {
                panic!("not a videoplayback")
            }
        } else {
            panic!("not a message")
        }
    }

    #[test]
    fn commercial1() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback-by-id.1234",
        "message": "{\"type\":\"commercial\",\"server_time\":1603209658.186545,\"length\":60}"
    }
}"#;
        if let Response::Message { data } = dbg!(Response::parse(source).unwrap()) {
            if let TopicData::VideoPlaybackById { reply, .. } = data {
                assert!(matches!(*reply, VideoPlaybackReply::Commercial { .. }))
            } else {
                panic!("not a videoplayback")
            }
        } else {
            panic!("not a message")
        }
    }

    #[test]
    fn commercial2() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback-by-id.1234",
        "message": "{\"type\":\"commercial\",\"server_time\":1604022504.517951,\"length\":175}"
    }
}"#;
        if let Response::Message { data } = dbg!(Response::parse(source).unwrap()) {
            if let TopicData::VideoPlaybackById { reply, .. } = data {
                assert!(matches!(*reply, VideoPlaybackReply::Commercial { .. }))
            } else {
                panic!("not a videoplayback")
            }
        } else {
            panic!("not a message")
        }
    }

    #[test]
    fn commercial_scheduled() {
        let source = r#"
{
    "type": "MESSAGE",
    "data": {
        "topic": "video-playback-by-id.1234",
        "message": "{\"type\":\"commercial\",\"server_time\":1604087214.932556,\"length\":180,\"scheduled\":false}"
    }
}"#;
        if let Response::Message { data } = dbg!(Response::parse(source).unwrap()) {
            if let TopicData::VideoPlaybackById { reply, .. } = data {
                assert!(matches!(*reply, VideoPlaybackReply::Commercial { .. }))
            } else {
                panic!("not a videoplayback")
            }
        } else {
            panic!("not a message")
        }
    }

    #[test]
    fn check_deser() {
        use std::convert::TryInto as _;
        let s = "video-playback.tmi";
        assert_eq!(
            VideoPlayback {
                channel_login: "tmi".into()
            },
            s.to_string().try_into().unwrap()
        );
        let s = "video-playback-by-id.1234";
        assert_eq!(
            VideoPlaybackById { channel_id: 1234 },
            s.to_string().try_into().unwrap()
        );
    }

    #[test]
    fn check_ser() {
        let s = "video-playback.tmi";
        let right: String = VideoPlayback {
            channel_login: "tmi".into(),
        }
        .into();
        assert_eq!(s.to_string(), right);
        let s = "video-playback-by-id.1234";
        let right: String = VideoPlaybackById { channel_id: 1234 }.into();
        assert_eq!(s.to_string(), right);
    }
}