From bc5da83a645c22e10971455b1a180cf62684b275 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 19 Feb 2021 10:34:37 +0000 Subject: Use the provided username --- dist/callback.html | 2 +- src/database.rs | 4 ++-- src/errors.rs | 4 ++-- src/serve.rs | 6 +++--- src/spotify.rs | 21 +++++++++++---------- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/dist/callback.html b/dist/callback.html index 555bede..c013416 100644 --- a/dist/callback.html +++ b/dist/callback.html @@ -105,7 +105,7 @@ sessionStorage.setItem('token', token); sessionStorage.setItem('lobby', lobby); document.getElementById("Connect_button").disabled = true; - window.location = api + "/token/" + token; + window.location = api + "/token/" + document.getElementById("username").value + "/" + token; } function populate_users() { lobby = getParameterByName("lobby"); diff --git a/src/database.rs b/src/database.rs index ecb88d0..a5204a1 100644 --- a/src/database.rs +++ b/src/database.rs @@ -156,9 +156,9 @@ pub async fn match_users(lobby: String, names: &[&str]) -> Result JOIN suser USING (user_id) JOIN track USING (track_id) JOIN lobby USING (lobby_id) - WHERE suser.user_name IN (SELECT * FROM users) AND token = $1 + WHERE suser.user_name IN (SELECT user_name FROM users) AND token = $1 GROUP BY track_id - HAVING COUNT(track_id) = (SELECT COUNT(*) FROM users) + ---HAVING COUNT(track_id) = (SELECT COUNT(*) FROM users) ORDER BY SUM(score) DESC ) AS _ USING (track_id) ; diff --git a/src/errors.rs b/src/errors.rs index d935c49..51ac8fd 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -34,8 +34,8 @@ impl From for Error { Error::Misc(error) } } -impl<'a> From>>> for Error { - fn from(error: PoisonError>>) -> Self { +impl<'a> From>>> for Error { + fn from(error: PoisonError>>) -> Self { Error::Misc(format!("failed to lock the client mutex: {:?}", error)) } } diff --git a/src/serve.rs b/src/serve.rs index ab07607..0c4e83e 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -10,9 +10,9 @@ pub async fn get_tracks(name: String, lobby: String, url: String) -> Result<(), spotify::load_profile(uid, spotify_uid.as_ref(), spotify_client).await } -#[get("/token/")] -pub fn token(name: String) -> Result { - Ok(Redirect::to(spotify::token(name)?)) +#[get("/token//")] +pub fn token(name: String, token: String) -> Result { + Ok(Redirect::to(spotify::token(name, token)?)) } #[get("/match//")] diff --git a/src/spotify.rs b/src/spotify.rs index ea05b14..04a82bc 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -10,7 +10,8 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; lazy_static! { - static ref CACHE: Arc>> = Arc::new(Mutex::new(HashMap::new())); + static ref CACHE: Arc>> = + Arc::new(Mutex::new(HashMap::new())); } static CHUNK_SIZE: u32 = 50; @@ -113,7 +114,7 @@ fn generate_random_uuid(length: usize) -> String { } pub async fn auth_user(name: &str, code: &str) -> Result<(String, Spotify), Error> { - let mut spotify = { + let (user, mut spotify) = { let mut guard = (*CACHE).lock()?; guard.remove(name)? }; @@ -127,15 +128,15 @@ pub async fn auth_user(name: &str, code: &str) -> Result<(String, Spotify), Erro let spotify = Spotify::default() .client_credentials_manager(client_credential) .build();*/ - let user_id = spotify - .current_user() - .await - .map_err(|e| format!("failed to load currentuser {:?}", e))? - .id; - Ok((user_id, spotify)) + /*let user_id = spotify + .current_user() + .await + .map_err(|e| format!("failed to load currentuser {:?}", e))? + .id;*/ + Ok((user, spotify)) } -pub fn token(name: String) -> Result { +pub fn token(name: String, token: String) -> Result { let scope = "playlist-read-private playlist-read-collaborative user-read-private user-follow-read user-library-read"; let oauth = OAuthBuilder::from_env() @@ -162,6 +163,6 @@ pub fn token(name: String) -> Result { */ //let auth_url = oauth.get_authorize_url(Some(&state), None); let mut guard = (*CACHE).lock()?; - guard.insert(name, spotify); + guard.insert(token, (name, spotify)); Ok(auth_url) } -- cgit v1.2.3-54-g00ecf