diff options
author | Dennis Kobert <dennis@kobert.dev> | 2021-02-19 10:34:37 +0000 |
---|---|---|
committer | Dennis Kobert <dennis@kobert.dev> | 2021-02-19 10:34:37 +0000 |
commit | bc5da83a645c22e10971455b1a180cf62684b275 (patch) | |
tree | 3c0b7e38de935978aad83cb0ebb866c9611f53b1 | |
parent | 5ae2fdf0e941b3ce13ad35363398782381179dad (diff) |
Use the provided username
-rw-r--r-- | dist/callback.html | 2 | ||||
-rw-r--r-- | src/database.rs | 4 | ||||
-rw-r--r-- | src/errors.rs | 4 | ||||
-rw-r--r-- | src/serve.rs | 6 | ||||
-rw-r--r-- | 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<String, Error> 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<String> for Error { Error::Misc(error) } } -impl<'a> From<PoisonError<MutexGuard<'a, HashMap<String, Spotify>>>> for Error { - fn from(error: PoisonError<MutexGuard<'a, HashMap<String, Spotify>>>) -> Self { +impl<'a> From<PoisonError<MutexGuard<'a, HashMap<String, (String, Spotify)>>>> for Error { + fn from(error: PoisonError<MutexGuard<'a, HashMap<String, (String, Spotify)>>>) -> 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/<name>")] -pub fn token(name: String) -> Result<Redirect, Error> { - Ok(Redirect::to(spotify::token(name)?)) +#[get("/token/<name>/<token>")] +pub fn token(name: String, token: String) -> Result<Redirect, Error> { + Ok(Redirect::to(spotify::token(name, token)?)) } #[get("/match/<lobby>/<names>")] 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<Mutex<HashMap<String, Spotify>>> = Arc::new(Mutex::new(HashMap::new())); + static ref CACHE: Arc<Mutex<HashMap<String, (String, Spotify)>>> = + 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<String, Error> { +pub fn token(name: String, token: String) -> Result<String, Error> { 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<String, Error> { */ //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) } |