From eed3edfa59c81c54a2adb5893570c43b6000260c Mon Sep 17 00:00:00 2001 From: KoffeinFlummi Date: Sun, 28 Apr 2019 10:54:50 +0200 Subject: [PATCH] Differentiate between followed and created playlists Fix #56 --- Cargo.toml | 4 ++-- src/library.rs | 11 +++++++++++ src/playlist.rs | 4 ++-- src/spotify.rs | 5 +++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 40293ee..9a44c7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,8 +35,8 @@ rand = "0.6.5" webbrowser = "0.5" [dependencies.rspotify] -git = "https://github.com/samrayleung/rspotify" -rev = "9d9cf7c" +git = "https://github.com/KoffeinFlummi/rspotify" +rev = "9d6c544" [dependencies.librespot] git = "https://github.com/librespot-org/librespot.git" diff --git a/src/library.rs b/src/library.rs index 0aec8d0..8914faf 100644 --- a/src/library.rs +++ b/src/library.rs @@ -29,6 +29,7 @@ pub struct Library { pub artists: Arc>>, pub playlists: Arc>>, is_done: Arc>, + user_id: Option, ev: EventManager, spotify: Arc, pub use_nerdfont: bool, @@ -36,12 +37,15 @@ pub struct Library { impl Library { pub fn new(ev: &EventManager, spotify: Arc, use_nerdfont: bool) -> Self { + let user_id = spotify.current_user().map(|u| u.id); + let library = Self { tracks: Arc::new(RwLock::new(Vec::new())), albums: Arc::new(RwLock::new(Vec::new())), artists: Arc::new(RwLock::new(Vec::new())), playlists: Arc::new(RwLock::new(Vec::new())), is_done: Arc::new(RwLock::new(false)), + user_id, ev: ev.clone(), spotify, use_nerdfont, @@ -699,6 +703,13 @@ impl Library { playlists.iter().any(|p| p.id == playlist.id) } + pub fn is_followed_playlist(&self, playlist: &Playlist) -> bool { + self.user_id + .as_ref() + .map(|id| id != &playlist.owner_id) + .unwrap_or(false) + } + pub fn follow_playlist(&self, playlist: &Playlist) { if !*self.is_done.read().unwrap() { return; diff --git a/src/playlist.rs b/src/playlist.rs index b9cf5f1..f71e7cb 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -104,7 +104,7 @@ impl ListItem for Playlist { } fn display_right(&self, library: Arc) -> String { - let saved = if library.is_saved_playlist(self) { + let followed = if library.is_followed_playlist(self) { if library.use_nerdfont { "\u{f62b} " } else { @@ -113,7 +113,7 @@ impl ListItem for Playlist { } else { "" }; - format!("{}{:>3} tracks", saved, self.tracks.len()) + format!("{}{:>3} tracks", followed, self.tracks.len()) } fn play(&mut self, queue: Arc) { diff --git a/src/spotify.rs b/src/spotify.rs index 081f569..1c0220a 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -21,6 +21,7 @@ use rspotify::spotify::model::search::{ SearchAlbums, SearchArtists, SearchPlaylists, SearchTracks, }; use rspotify::spotify::model::track::{FullTrack, SavedTrack}; +use rspotify::spotify::model::user::PrivateUser; use failure::Error; @@ -590,6 +591,10 @@ impl Spotify { .map(|fa| fa.artists.iter().map(|a| a.into()).collect()) } + pub fn current_user(&self) -> Option { + self.api_with_retry(|api| api.current_user()) + } + pub fn load(&self, track: &Track) { info!("loading track: {:?}", track); self.channel