From 6445244957d4c8bbb310bcafa533b0f5fa428cd7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 31 Jan 2022 01:53:27 -0500 Subject: [PATCH] save both event types to db --- .../twitch_pubsub/insert_channel_points.rs | 29 ++++++++---------- .../twitch_pubsub_user_subscriber_thread.rs | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/crates/lib/database_queries/src/twitch_pubsub/insert_channel_points.rs b/crates/lib/database_queries/src/twitch_pubsub/insert_channel_points.rs index 0a701affda..4973fb2b3b 100644 --- a/crates/lib/database_queries/src/twitch_pubsub/insert_channel_points.rs +++ b/crates/lib/database_queries/src/twitch_pubsub/insert_channel_points.rs @@ -52,29 +52,24 @@ impl TwitchPubsubChannelPointsInsertBuilder { } } - pub fn set_sender_twitch_user_id(mut self, value: Option<&str>) -> Self { - self.sender_twitch_user_id = value.map(|s| s.to_string()); + pub fn set_sender_twitch_user_id(mut self, value: &str) -> Self { + self.sender_twitch_user_id = Some(value.to_string()); self } - pub fn set_sender_twitch_username(mut self, value: Option<&str>) -> Self { - if let Some(username) = value { - self.sender_twitch_username = Some(username.to_string()); - self.sender_twitch_username_lowercase = Some(username.to_uppercase()); - } else { - self.sender_twitch_username = None; - self.sender_twitch_username_lowercase = None; - } + pub fn set_sender_twitch_username(mut self, value: &str) -> Self { + self.sender_twitch_username = Some(value.to_string()); + self.sender_twitch_username_lowercase = Some(value.to_uppercase()); self } - pub fn set_destination_channel_id(mut self, value: Option<&str>) -> Self { - self.destination_channel_id = value.map(|s| s.to_string()); + pub fn set_destination_channel_id(mut self, value: &str) -> Self { + self.destination_channel_id = Some(value.to_string()); self } - pub fn set_destination_channel_name(mut self, value: Option<&str>) -> Self { - self.destination_channel_name = value.map(|s| s.to_string()); + pub fn set_destination_channel_name(mut self, value: &str) -> Self { + self.destination_channel_name = Some(value.to_string()); self } @@ -88,8 +83,10 @@ impl TwitchPubsubChannelPointsInsertBuilder { self } - pub fn set_user_tet_input(mut self, value: &str) -> Self { - self.user_text_input = value.to_string(); + pub fn set_user_text_input(mut self, value: Option<&str>) -> Self { + self.user_text_input = value + .map(|s| s.to_string()) + .unwrap_or("".to_string()); self } diff --git a/crates/service/obs_gateway/src/threads/twitch_pubsub_user_subscriber_thread.rs b/crates/service/obs_gateway/src/threads/twitch_pubsub_user_subscriber_thread.rs index 8037bcc8b1..d782e9ff69 100644 --- a/crates/service/obs_gateway/src/threads/twitch_pubsub_user_subscriber_thread.rs +++ b/crates/service/obs_gateway/src/threads/twitch_pubsub_user_subscriber_thread.rs @@ -27,6 +27,7 @@ use std::time::Duration; use time::Instant; use twitch_api2::pubsub::{Response, TwitchResponse, TopicData}; use twitch_api2::pubsub::channel_bits::ChannelBitsEventsV2Reply; +use twitch_api2::pubsub::channel_points::ChannelPointsChannelV1Reply; // TODO: Publish events back to OBS thread // TODO: (cleanup) make the logic clearer to follow. @@ -330,6 +331,35 @@ impl TwitchPubsubUserSubscriberThreadStageTwo { } } TopicData::ChannelPointsChannelV1 { topic, reply } => { + let mut event_builder = TwitchPubsubChannelPointsInsertBuilder::new(); + match *reply { + // Unimplemented + ChannelPointsChannelV1Reply::CustomRewardUpdated { .. } => {} + ChannelPointsChannelV1Reply::RedemptionStatusUpdate { .. } => {} + ChannelPointsChannelV1Reply::UpdateRedemptionStatusesFinished { .. } => {} + ChannelPointsChannelV1Reply::UpdateRedemptionStatusProgress { .. } => {} + // Implemented + ChannelPointsChannelV1Reply::RewardRedeemed { timestamp, redemption } => { + let user_id = redemption.user.id.to_string(); + let user_name = redemption.user.login.to_string(); + let mut event_builder = event_builder.set_sender_twitch_user_id(&user_id) + .set_sender_twitch_username(&user_name) + .set_destination_channel_id(&redemption.channel_id.to_string()) + // TODO: + .set_destination_channel_name("todo: not available") + .set_title(&redemption.reward.title) + .set_prompt(&redemption.reward.prompt) + .set_user_text_input(redemption.user_input.as_deref()) + .set_redemption_id(&redemption.id.to_string()) + .set_reward_id(&redemption.reward.id.to_string()) + .set_is_sub_only(redemption.reward.is_sub_only); + // TODO: + // .set_max_per_stream(redemption.reward.max_per_stream as u64) + // .set_max_per_user_per_stream(redemption.reward.max_per_user_per_stream as u64); + event_builder.insert(&self.mysql_pool).await?; + } + _ => {}, + } } }