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
#![doc(alias = "cheer")]
#![doc(alias = "channel-cheer-events-public-v1")]
//! PubSub messages for cheer events
use crate::{pubsub, types};
use serde::{Deserialize, Serialize};

/// A user redeems a cheer with shared rewards.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(into = "String", try_from = "String")]
pub struct ChannelCheerEventsPublicV1 {
    /// 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!(
    ChannelCheerEventsPublicV1,
    "channel-cheer-events-public-v1",
    channel_id // FIXME: add trailing comma
);

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

/// Reply from [ChannelCheerEventsPublicV1]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type", content = "data")]
#[non_exhaustive]
pub enum ChannelCheerEventsPublicV1Reply {
    /// A cheer bomb happened
    #[serde(rename = "cheerbomb")]
    CheerBomb {
        /// Display name of user
        #[serde(rename = "displayName")]
        display_name: types::DisplayName,
        /// Domain of cheer reward. Name of active twitch event
        domain: String,
        /// Selected count for cheer. e.g How many that will receive rewards
        #[serde(rename = "selectedCount")]
        selected_count: i64,
        /// Unknown
        #[serde(rename = "totalRewardCount")]
        total_reward_count: i64,
        /// Unknown
        #[serde(rename = "triggerAmount")]
        trigger_amount: i64,
        /// Type of cheerbomb.
        #[serde(rename = "triggerType")]
        trigger_type: TriggerType,
        /// Id of the user
        #[serde(rename = "userID")]
        user_id: types::UserId,
        /// Login name of the user, not capitalized
        #[serde(rename = "userLogin")]
        user_login: types::UserName,
    },
}

/// Trigger for cheer event/cheer bomb
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
#[non_exhaustive]
pub enum TriggerType {
    /// Subscription
    Subscription,
    /// Subscription gift
    SubGift,
    /// Cheer
    Cheer,
}

#[cfg(test)]
mod tests {
    use super::super::{Response, TopicData};
    use super::*;
    #[test]
    fn channel_cheer_event_cheerbomb_sub() {
        let message = r##"
{
    "type": "cheerbomb",
    "data": {
        "userID": "1234",
        "displayName": "TMI",
        "userLogin": "tmi",
        "selectedCount": 5,
        "triggerType": "SUBSCRIPTION",
        "triggerAmount": 1,
        "totalRewardCount": 5,
        "domain": "kpop_megacommerce"
    }
}
"##;

        let source = format!(
            r#"{{"type": "MESSAGE","data": {{ "topic": "channel-cheer-events-public-v1.27620241", "message": {:?} }}}}"#,
            message
        );
        let actual = dbg!(Response::parse(&source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::ChannelCheerEventsPublicV1 { .. },
            }
        ));
    }

    #[test]
    fn channel_cheer_event_cheerbomb_subgift() {
        let message = r##"
{
    "type": "cheerbomb",
    "data": {
        "userID": "1234",
        "displayName": "tmi",
        "userLogin": "TMI",
        "selectedCount": 25,
        "triggerType": "SUBGIFT",
        "triggerAmount": 5,
        "totalRewardCount": 25,
        "domain": "kpop_megacommerce"
    }
}
"##;

        let source = format!(
            r#"{{"type": "MESSAGE","data": {{ "topic": "channel-cheer-events-public-v1.27620241", "message": {:?} }}}}"#,
            message
        );
        let actual = dbg!(Response::parse(&source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::ChannelCheerEventsPublicV1 { .. },
            }
        ));
    }

    #[test]
    fn channel_cheer_event_cheerbomb_cheer() {
        let message = r##"
{
    "type": "cheerbomb",
    "data": {
        "userID": "1234",
        "displayName": "tmi",
        "userLogin": "TMI",
        "selectedCount": 10,
        "triggerType": "CHEER",
        "triggerAmount": 600,
        "totalRewardCount": 10,
        "domain": "kpop_megacommerce"
    }
}
"##;

        let source = format!(
            r#"{{"type": "MESSAGE","data": {{ "topic": "channel-cheer-events-public-v1.27620241", "message": {:?} }}}}"#,
            message
        );
        let actual = dbg!(Response::parse(&source).unwrap());
        assert!(matches!(
            actual,
            Response::Message {
                data: TopicData::ChannelCheerEventsPublicV1 { .. },
            }
        ));
    }

    #[test]
    fn check_deser() {
        use std::convert::TryInto as _;
        let s = "channel-cheer-events-public-v1.1234";
        assert_eq!(
            ChannelCheerEventsPublicV1 { channel_id: 1234 },
            s.to_string().try_into().unwrap()
        );
    }

    #[test]
    fn check_ser() {
        let s = "channel-cheer-events-public-v1.1234";
        let right: String = ChannelCheerEventsPublicV1 { channel_id: 1234 }.into();
        assert_eq!(s.to_string(), right);
    }
}