#![doc(alias = "raids")]
use crate::{pubsub, types};
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(into = "String", try_from = "String")]
pub struct Raid {
pub channel_id: u32,
}
impl_de_ser!(Raid, "raid", channel_id);
impl pubsub::Topic for Raid {
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator = twitch_oauth2::validator![];
fn into_topic(self) -> pubsub::Topics { super::Topics::Raid(self) }
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct RaidGoV2 {
pub creator_id: types::UserId,
pub force_raid_now_seconds: i64,
pub id: String,
pub source_id: types::UserId,
pub target_display_name: types::DisplayName,
pub target_id: types::UserId,
pub target_login: types::UserName,
pub target_profile_image: String,
pub transition_jitter_seconds: i64,
pub viewer_count: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct RaidUpdateV2 {
pub creator_id: types::UserId,
pub force_raid_now_seconds: i64,
pub id: String,
pub source_id: types::UserId,
pub target_display_name: types::DisplayName,
pub target_id: types::UserId,
pub target_login: types::UserName,
pub target_profile_image: String,
pub transition_jitter_seconds: i64,
pub viewer_count: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct RaidCancelV2 {
pub creator_id: types::UserId,
pub force_raid_now_seconds: i64,
pub id: String,
pub source_id: types::UserId,
pub target_display_name: types::DisplayName,
pub target_id: types::UserId,
pub target_login: types::UserName,
pub target_profile_image: String,
pub transition_jitter_seconds: i64,
pub viewer_count: i64,
}
#[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 {
#[serde(rename = "raid_go_v2")]
RaidGoV2(RaidGoV2),
#[serde(rename = "raid_update_v2")]
RaidUpdateV2(RaidUpdateV2),
#[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);
}
}