diff --git a/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs b/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs index e011ce535b..4569db9188 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/obs_gateway_websocket_handler.rs @@ -16,6 +16,8 @@ use tokio::runtime::Handle; use twitch_api2::pubsub::Topic; use twitch_api2::pubsub; use crate::twitch::pubsub::build_pubsub_topics_for_user::build_pubsub_topics_for_user; +use crate::twitch::oauth::oauth_token_refresher::OauthTokenRefresher; +use crate::twitch::websocket_client::TwitchWebsocketClient; /// Endpoint pub async fn obs_gateway_websocket_handler( @@ -28,7 +30,9 @@ pub async fn obs_gateway_websocket_handler( let user_id = server_state.twitch_oauth_temp.temp_oauth_user_id.parse::().unwrap(); let auth_token = server_state.twitch_oauth_temp.temp_oauth_access_token.clone(); - let mut client = PollingTwitchWebsocketClient::new().unwrap(); + let mut client = TwitchWebsocketClient::new().unwrap(); + + let token_refresher = OauthTokenRefresher::new(user_id, &auth_token, None); info!("Connecting to Twitch PubSub..."); client.connect().await.unwrap(); @@ -66,7 +70,7 @@ struct ObsGatewayWebSocket { impl ObsGatewayWebSocket { - fn new(twitch_user_id: u32, twitch_client: PollingTwitchWebsocketClient) -> Self { + fn new(twitch_user_id: u32, twitch_client: TwitchWebsocketClient) -> Self { let twitch_thread = ObsTwitchThread::new(twitch_user_id, twitch_client); Self { twitch_user_id, diff --git a/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs b/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs index de9d76fe91..6e792d1cf1 100644 --- a/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs +++ b/crates/service/obs_gateway/src/endpoints_ws/obs_twitch_thread.rs @@ -1,4 +1,5 @@ use crate::twitch::polling_websocket_client::PollingTwitchWebsocketClient; +use crate::twitch::websocket_client::TwitchWebsocketClient; use log::error; use log::info; use std::sync::atomic::AtomicBool; @@ -19,12 +20,12 @@ use tokio::runtime::Handle; pub struct ObsTwitchThread { twitch_user_id: u32, - twitch_client: Arc>, + twitch_client: Arc>, last_requested: Arc>, } impl ObsTwitchThread { - pub fn new(twitch_user_id: u32, twitch_client: PollingTwitchWebsocketClient) -> Self { + pub fn new(twitch_user_id: u32, twitch_client: TwitchWebsocketClient) -> Self { let now = Instant::now(); Self { twitch_user_id, diff --git a/crates/service/obs_gateway/src/twitch/oauth/mod.rs b/crates/service/obs_gateway/src/twitch/oauth/mod.rs index c406e2678c..46b2d4c4f9 100644 --- a/crates/service/obs_gateway/src/twitch/oauth/mod.rs +++ b/crates/service/obs_gateway/src/twitch/oauth/mod.rs @@ -1 +1,2 @@ -pub mod oauth_token_builder; \ No newline at end of file +pub mod oauth_token_builder; +pub mod oauth_token_refresher; \ No newline at end of file diff --git a/crates/service/obs_gateway/src/twitch/oauth/oauth_token_refresher.rs b/crates/service/obs_gateway/src/twitch/oauth/oauth_token_refresher.rs new file mode 100644 index 0000000000..35d5c86149 --- /dev/null +++ b/crates/service/obs_gateway/src/twitch/oauth/oauth_token_refresher.rs @@ -0,0 +1,24 @@ +// TODO: I know this is something we'll need, but I haven't built it yet. +// We'll probably want to write back to the database. + +#[derive(Clone)] +pub struct OauthTokenRefresher { + user_id: u32, + auth_token: String, + refresh_token: Option, +} + +impl OauthTokenRefresher { + + pub fn new(user_id: u32, auth_token: &str, refresh_token: Option<&str>) -> Self { + Self { + user_id, + auth_token: auth_token.to_string(), + refresh_token: refresh_token.map(|str| str.to_string()), + } + } + + pub fn get_auth_token(&self) -> String { + self.auth_token.clone() + } +}