Merge remote-tracking branch 'origin/main' into 33-create-component-for-displaying-music-on-dashboard
This commit is contained in:
44
src/api/history.rs
Normal file
44
src/api/history.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use std::time::SystemTime;
|
||||
use leptos::*;
|
||||
use crate::models::HistoryEntry;
|
||||
use crate::models::Song;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
use crate::database::get_db_conn;
|
||||
use crate::auth::get_user;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the history of the current user.
|
||||
#[server(endpoint = "history/get")]
|
||||
pub async fn get_history(limit: Option<i64>) -> Result<Vec<HistoryEntry>, ServerFnError> {
|
||||
let user = get_user().await?;
|
||||
let db_con = &mut get_db_conn();
|
||||
let history = user.get_history(limit, db_con)
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting history: {}", e)))?;
|
||||
Ok(history)
|
||||
}
|
||||
|
||||
/// Get the listen dates and songs of the current user.
|
||||
#[server(endpoint = "history/get_songs")]
|
||||
pub async fn get_history_songs(limit: Option<i64>) -> Result<Vec<(SystemTime, Song)>, ServerFnError> {
|
||||
let user = get_user().await?;
|
||||
let db_con = &mut get_db_conn();
|
||||
let songs = user.get_history_songs(limit, db_con)
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting history songs: {}", e)))?;
|
||||
Ok(songs)
|
||||
}
|
||||
|
||||
/// Add a song to the history of the current user.
|
||||
#[server(endpoint = "history/add")]
|
||||
pub async fn add_history(song_id: i32) -> Result<(), ServerFnError> {
|
||||
let user = get_user().await?;
|
||||
let db_con = &mut get_db_conn();
|
||||
user.add_history(song_id, db_con)
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error adding history: {}", e)))?;
|
||||
Ok(())
|
||||
}
|
3
src/api/mod.rs
Normal file
3
src/api/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod history;
|
||||
pub mod profile;
|
||||
pub mod songs;
|
49
src/api/profile.rs
Normal file
49
src/api/profile.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use leptos::*;
|
||||
use server_fn::codec::{MultipartData, MultipartFormData};
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::auth::get_user;
|
||||
use server_fn::error::NoCustomError;
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a user uploading a profile picture. Converts the image to webp and saves it to the server.
|
||||
#[server(input = MultipartFormData, endpoint = "/profile/upload_picture")]
|
||||
pub async fn upload_picture(data: MultipartData) -> Result<(), ServerFnError> {
|
||||
// Safe to unwrap - "On the server side, this always returns Some(_). On the client side, always returns None."
|
||||
let mut data = data.into_inner().unwrap();
|
||||
|
||||
let field = data.next_field().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting field: {}", e)))?
|
||||
.ok_or_else(|| ServerFnError::<NoCustomError>::ServerError("No field found".to_string()))?;
|
||||
|
||||
if field.name() != Some("picture") {
|
||||
return Err(ServerFnError::ServerError("Field name is not 'picture'".to_string()));
|
||||
}
|
||||
|
||||
// Get user id from session
|
||||
let user = get_user().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user: {}", e)))?;
|
||||
|
||||
let user_id = user.id.ok_or_else(|| ServerFnError::<NoCustomError>::ServerError("User has no id".to_string()))?;
|
||||
|
||||
// Read the image, and convert it to webp
|
||||
use image_convert::{to_webp, WEBPConfig, ImageResource};
|
||||
|
||||
let bytes = field.bytes().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting field bytes: {}", e)))?;
|
||||
|
||||
let reader = std::io::Cursor::new(bytes);
|
||||
let image_source = ImageResource::from_reader(reader)
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error creating image resource: {}", e)))?;
|
||||
|
||||
let profile_picture_path = format!("assets/images/profile/{}.webp", user_id);
|
||||
let mut image_target = ImageResource::from_path(&profile_picture_path);
|
||||
to_webp(&mut image_target, &image_source, &WEBPConfig::new())
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error converting image to webp: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
55
src/api/songs.rs
Normal file
55
src/api/songs.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use leptos::*;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use leptos::server_fn::error::NoCustomError;
|
||||
use crate::database::get_db_conn;
|
||||
use crate::auth::get_user;
|
||||
}
|
||||
}
|
||||
|
||||
/// Like or unlike a song
|
||||
#[server(endpoint = "songs/set_like")]
|
||||
pub async fn set_like_song(song_id: i32, like: bool) -> Result<(), ServerFnError> {
|
||||
let user = get_user().await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error getting user: {}", e)))?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
|
||||
user.set_like_song(song_id, like, db_con).await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error liking song: {}", e)))
|
||||
}
|
||||
|
||||
/// Dislike or remove dislike from a song
|
||||
#[server(endpoint = "songs/set_dislike")]
|
||||
pub async fn set_dislike_song(song_id: i32, dislike: bool) -> Result<(), ServerFnError> {
|
||||
let user = get_user().await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error getting user: {}", e)))?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
|
||||
user.set_dislike_song(song_id, dislike, db_con).await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error disliking song: {}", e)))
|
||||
}
|
||||
|
||||
/// Get the like and dislike status of a song
|
||||
#[server(endpoint = "songs/get_like_dislike")]
|
||||
pub async fn get_like_dislike_song(song_id: i32) -> Result<(bool, bool), ServerFnError> {
|
||||
let user = get_user().await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error getting user: {}", e)))?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
|
||||
// TODO this could probably be done more efficiently with a tokio::try_join, but
|
||||
// doing so is much more complicated than it would initially seem
|
||||
|
||||
let like = user.get_like_song(song_id, db_con).await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error getting song liked: {}", e)))?;
|
||||
let dislike = user.get_dislike_song(song_id, db_con).await.map_err(|e| ServerFnError::<NoCustomError>::
|
||||
ServerError(format!("Error getting song disliked: {}", e)))?;
|
||||
|
||||
Ok((like, dislike))
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
use crate::playbar::PlayBar;
|
||||
use crate::playbar::CustomTitle;
|
||||
use crate::playstatus::PlayStatus;
|
||||
use crate::queue::Queue;
|
||||
use leptos::*;
|
||||
@ -24,7 +25,7 @@ pub fn App() -> impl IntoView {
|
||||
<Stylesheet id="leptos" href="/pkg/libretunes.css"/>
|
||||
|
||||
// sets the document title
|
||||
<Title text="LibreTunes"/>
|
||||
<CustomTitle play_status=play_status/>
|
||||
|
||||
// content for this welcome page
|
||||
<Router fallback=|| {
|
||||
|
23
src/auth.rs
23
src/auth.rs
@ -122,6 +122,29 @@ pub async fn require_auth() -> Result<(), ServerFnError> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the current logged-in user
|
||||
/// Returns a Result with the user if they are logged in
|
||||
/// Returns an error if the user is not logged in, or if there is an error getting the user
|
||||
/// Intended to be used in a route to get the current user:
|
||||
/// ```rust
|
||||
/// use leptos::*;
|
||||
/// use libretunes::auth::get_user;
|
||||
/// #[server(endpoint = "user_route")]
|
||||
/// pub async fn user_route() -> Result<(), ServerFnError> {
|
||||
/// let user = get_user().await?;
|
||||
/// println!("Logged in as: {}", user.username);
|
||||
/// // Do something with the user
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn get_user() -> Result<User, ServerFnError> {
|
||||
let auth_session = extract::<AuthSession<AuthBackend>>().await
|
||||
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {}", e)))?;
|
||||
|
||||
auth_session.user.ok_or(ServerFnError::<NoCustomError>::ServerError("User not logged in".to_string()))
|
||||
}
|
||||
|
||||
/// Check if a user is an admin
|
||||
/// Returns a Result with a boolean indicating if the user is logged in and an admin
|
||||
#[server(endpoint = "check_admin")]
|
||||
|
@ -29,11 +29,11 @@ cfg_if! { if #[cfg(feature = "ssr")] {
|
||||
let req = Request::builder().uri(uri.clone()).body(Body::empty()).unwrap();
|
||||
// `ServeDir` implements `tower::Service` so we can call it with `tower::ServiceExt::oneshot`
|
||||
// This path is relative to the cargo root
|
||||
match ServeDir::new(root).oneshot(req).await {
|
||||
Ok(res) => Ok(res.into_response()),
|
||||
Err(err) => Err((
|
||||
match ServeDir::new(root).oneshot(req).await.ok() {
|
||||
Some(res) => Ok(res.into_response()),
|
||||
None => Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Something went wrong: {err}"),
|
||||
format!("Something went wrong"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ pub mod search;
|
||||
pub mod fileserv;
|
||||
pub mod error_template;
|
||||
pub mod media_type;
|
||||
pub mod api;
|
||||
pub mod upload;
|
||||
pub mod util;
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
// Needed for building in Docker container
|
||||
// See https://github.com/clux/muslrust?tab=readme-ov-file#diesel-and-pq-builds
|
||||
// See https://github.com/sgrif/pq-sys/issues/25
|
||||
#[cfg(target = "x86_64-unknown-linux-musl")]
|
||||
#[cfg(target_env = "musl")]
|
||||
extern crate openssl;
|
||||
|
||||
#[cfg(target = "x86_64-unknown-linux-musl")]
|
||||
#[cfg(target_env = "musl")]
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
|
286
src/models.rs
286
src/models.rs
@ -45,6 +45,275 @@ pub struct User {
|
||||
pub admin: bool,
|
||||
}
|
||||
|
||||
impl User {
|
||||
/// Get the history of songs listened to by this user from the database
|
||||
///
|
||||
/// The returned history will be ordered by date in descending order,
|
||||
/// and a limit of N will select the N most recent entries.
|
||||
/// The `id` field of this user must be present (Some) to get history
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `limit` - An optional limit on the number of history entries to return
|
||||
/// * `conn` - A mutable reference to a database connection
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<Vec<HistoryEntry>, Box<dyn Error>>` -
|
||||
/// A result indicating success with a vector of history entries, or an error
|
||||
///
|
||||
#[cfg(feature = "ssr")]
|
||||
pub fn get_history(self: &Self, limit: Option<i64>, conn: &mut PgPooledConn) ->
|
||||
Result<Vec<HistoryEntry>, Box<dyn Error>> {
|
||||
use crate::schema::song_history::dsl::*;
|
||||
|
||||
let my_id = self.id.ok_or("Artist id must be present (Some) to get history")?;
|
||||
|
||||
let my_history =
|
||||
if let Some(limit) = limit {
|
||||
song_history
|
||||
.filter(user_id.eq(my_id))
|
||||
.order(date.desc())
|
||||
.limit(limit)
|
||||
.load(conn)?
|
||||
} else {
|
||||
song_history
|
||||
.filter(user_id.eq(my_id))
|
||||
.load(conn)?
|
||||
};
|
||||
|
||||
Ok(my_history)
|
||||
}
|
||||
|
||||
/// Get the history of songs listened to by this user from the database
|
||||
///
|
||||
/// The returned history will be ordered by date in descending order,
|
||||
/// and a limit of N will select the N most recent entries.
|
||||
/// The `id` field of this user must be present (Some) to get history
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `limit` - An optional limit on the number of history entries to return
|
||||
/// * `conn` - A mutable reference to a database connection
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<Vec<(SystemTime, Song)>, Box<dyn Error>>` -
|
||||
/// A result indicating success with a vector of listen dates and songs, or an error
|
||||
///
|
||||
#[cfg(feature = "ssr")]
|
||||
pub fn get_history_songs(self: &Self, limit: Option<i64>, conn: &mut PgPooledConn) ->
|
||||
Result<Vec<(SystemTime, Song)>, Box<dyn Error>> {
|
||||
use crate::schema::songs::dsl::*;
|
||||
use crate::schema::song_history::dsl::*;
|
||||
|
||||
let my_id = self.id.ok_or("Artist id must be present (Some) to get history")?;
|
||||
|
||||
let my_history =
|
||||
if let Some(limit) = limit {
|
||||
song_history
|
||||
.inner_join(songs)
|
||||
.filter(user_id.eq(my_id))
|
||||
.order(date.desc())
|
||||
.limit(limit)
|
||||
.select((date, songs::all_columns()))
|
||||
.load(conn)?
|
||||
} else {
|
||||
song_history
|
||||
.inner_join(songs)
|
||||
.filter(user_id.eq(my_id))
|
||||
.order(date.desc())
|
||||
.select((date, songs::all_columns()))
|
||||
.load(conn)?
|
||||
};
|
||||
|
||||
Ok(my_history)
|
||||
}
|
||||
|
||||
/// Add a song to this user's history in the database
|
||||
///
|
||||
/// The date of the history entry will be the current time
|
||||
/// The `id` field of this user must be present (Some) to add history
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `song_id` - The id of the song to add to this user's history
|
||||
/// * `conn` - A mutable reference to a database connection
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<(), Box<dyn Error>>` - A result indicating success with an empty value, or an error
|
||||
///
|
||||
#[cfg(feature = "ssr")]
|
||||
pub fn add_history(self: &Self, song_id: i32, conn: &mut PgPooledConn) -> Result<(), Box<dyn Error>> {
|
||||
use crate::schema::song_history;
|
||||
|
||||
let my_id = self.id.ok_or("Artist id must be present (Some) to add history")?;
|
||||
|
||||
diesel::insert_into(song_history::table)
|
||||
.values((song_history::user_id.eq(my_id), song_history::song_id.eq(song_id)))
|
||||
.execute(conn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if this user has listened to a song
|
||||
///
|
||||
/// The `id` field of this user must be present (Some) to check history
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `song_id` - The id of the song to check if this user has listened to
|
||||
/// * `conn` - A mutable reference to a database connection
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<bool, Box<dyn Error>>` - A result indicating success with a boolean value, or an error
|
||||
///
|
||||
#[cfg(feature = "ssr")]
|
||||
pub fn has_listened_to(self: &Self, song_id: i32, conn: &mut PgPooledConn) -> Result<bool, Box<dyn Error>> {
|
||||
use crate::schema::song_history::{self, user_id};
|
||||
|
||||
let my_id = self.id.ok_or("Artist id must be present (Some) to check history")?;
|
||||
|
||||
let has_listened = song_history::table
|
||||
.filter(user_id.eq(my_id))
|
||||
.filter(song_history::song_id.eq(song_id))
|
||||
.first::<HistoryEntry>(conn)
|
||||
.optional()?
|
||||
.is_some();
|
||||
|
||||
Ok(has_listened)
|
||||
}
|
||||
|
||||
/// Like or unlike a song for this user
|
||||
/// If likeing a song, remove dislike if it exists
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn set_like_song(self: &Self, song_id: i32, like: bool, conn: &mut PgPooledConn) ->
|
||||
Result<(), Box<dyn Error>> {
|
||||
use log::*;
|
||||
debug!("Setting like for song {} to {}", song_id, like);
|
||||
|
||||
use crate::schema::song_likes;
|
||||
use crate::schema::song_dislikes;
|
||||
|
||||
let my_id = self.id.ok_or("User id must be present (Some) to like/un-like a song")?;
|
||||
|
||||
if like {
|
||||
diesel::insert_into(song_likes::table)
|
||||
.values((song_likes::song_id.eq(song_id), song_likes::user_id.eq(my_id)))
|
||||
.execute(conn)?;
|
||||
|
||||
// Remove dislike if it exists
|
||||
diesel::delete(song_dislikes::table.filter(song_dislikes::song_id.eq(song_id)
|
||||
.and(song_dislikes::user_id.eq(my_id))))
|
||||
.execute(conn)?;
|
||||
} else {
|
||||
diesel::delete(song_likes::table.filter(song_likes::song_id.eq(song_id).and(song_likes::user_id.eq(my_id))))
|
||||
.execute(conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the like status of a song for this user
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn get_like_song(self: &Self, song_id: i32, conn: &mut PgPooledConn) -> Result<bool, Box<dyn Error>> {
|
||||
use crate::schema::song_likes;
|
||||
|
||||
let my_id = self.id.ok_or("User id must be present (Some) to get like status of a song")?;
|
||||
|
||||
let like = song_likes::table
|
||||
.filter(song_likes::song_id.eq(song_id).and(song_likes::user_id.eq(my_id)))
|
||||
.first::<(i32, i32)>(conn)
|
||||
.optional()?
|
||||
.is_some();
|
||||
|
||||
Ok(like)
|
||||
}
|
||||
|
||||
/// Get songs liked by this user
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn get_liked_songs(self: &Self, conn: &mut PgPooledConn) -> Result<Vec<Song>, Box<dyn Error>> {
|
||||
use crate::schema::songs::dsl::*;
|
||||
use crate::schema::song_likes::dsl::*;
|
||||
|
||||
let my_id = self.id.ok_or("User id must be present (Some) to get liked songs")?;
|
||||
|
||||
let my_songs = songs
|
||||
.inner_join(song_likes)
|
||||
.filter(user_id.eq(my_id))
|
||||
.select(songs::all_columns())
|
||||
.load(conn)?;
|
||||
|
||||
Ok(my_songs)
|
||||
}
|
||||
|
||||
/// Dislike or remove dislike from a song for this user
|
||||
/// If disliking a song, remove like if it exists
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn set_dislike_song(self: &Self, song_id: i32, dislike: bool, conn: &mut PgPooledConn) ->
|
||||
Result<(), Box<dyn Error>> {
|
||||
use log::*;
|
||||
debug!("Setting dislike for song {} to {}", song_id, dislike);
|
||||
|
||||
use crate::schema::song_likes;
|
||||
use crate::schema::song_dislikes;
|
||||
|
||||
let my_id = self.id.ok_or("User id must be present (Some) to dislike/un-dislike a song")?;
|
||||
|
||||
if dislike {
|
||||
diesel::insert_into(song_dislikes::table)
|
||||
.values((song_dislikes::song_id.eq(song_id), song_dislikes::user_id.eq(my_id)))
|
||||
.execute(conn)?;
|
||||
|
||||
// Remove like if it exists
|
||||
diesel::delete(song_likes::table.filter(song_likes::song_id.eq(song_id)
|
||||
.and(song_likes::user_id.eq(my_id))))
|
||||
.execute(conn)?;
|
||||
} else {
|
||||
diesel::delete(song_dislikes::table.filter(song_dislikes::song_id.eq(song_id)
|
||||
.and(song_dislikes::user_id.eq(my_id))))
|
||||
.execute(conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the dislike status of a song for this user
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn get_dislike_song(self: &Self, song_id: i32, conn: &mut PgPooledConn) -> Result<bool, Box<dyn Error>> {
|
||||
use crate::schema::song_dislikes;
|
||||
|
||||
let my_id = self.id.ok_or("User id must be present (Some) to get dislike status of a song")?;
|
||||
|
||||
let dislike = song_dislikes::table
|
||||
.filter(song_dislikes::song_id.eq(song_id).and(song_dislikes::user_id.eq(my_id)))
|
||||
.first::<(i32, i32)>(conn)
|
||||
.optional()?
|
||||
.is_some();
|
||||
|
||||
Ok(dislike)
|
||||
}
|
||||
|
||||
/// Get songs disliked by this user
|
||||
#[cfg(feature = "ssr")]
|
||||
pub async fn get_disliked_songs(self: &Self, conn: &mut PgPooledConn) -> Result<Vec<Song>, Box<dyn Error>> {
|
||||
use crate::schema::songs::dsl::*;
|
||||
use crate::schema::song_likes::dsl::*;
|
||||
|
||||
let my_id = self.id.ok_or("User id must be present (Some) to get disliked songs")?;
|
||||
|
||||
let my_songs = songs
|
||||
.inner_join(song_likes)
|
||||
.filter(user_id.eq(my_id))
|
||||
.select(songs::all_columns())
|
||||
.load(conn)?;
|
||||
|
||||
Ok(my_songs)
|
||||
}
|
||||
}
|
||||
|
||||
/// Model for an artist
|
||||
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable, Insertable, Identifiable))]
|
||||
#[cfg_attr(feature = "ssr", diesel(table_name = crate::schema::artists))]
|
||||
@ -340,3 +609,20 @@ impl Song {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Model for a history entry
|
||||
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable, Insertable))]
|
||||
#[cfg_attr(feature = "ssr", diesel(table_name = crate::schema::song_history))]
|
||||
#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct HistoryEntry {
|
||||
/// A unique id for the history entry
|
||||
#[cfg_attr(feature = "ssr", diesel(deserialize_as = i32))]
|
||||
pub id: Option<i32>,
|
||||
/// The id of the user who listened to the song
|
||||
pub user_id: i32,
|
||||
/// The date the song was listened to
|
||||
pub date: SystemTime,
|
||||
/// The id of the song that was listened to
|
||||
pub song_id: i32,
|
||||
}
|
||||
|
177
src/playbar.rs
177
src/playbar.rs
@ -1,10 +1,14 @@
|
||||
use crate::models::Artist;
|
||||
use crate::playstatus::PlayStatus;
|
||||
use crate::songdata::SongData;
|
||||
use crate::api::songs;
|
||||
use leptos::ev::MouseEvent;
|
||||
use leptos::html::{Audio, Div};
|
||||
use leptos::leptos_dom::*;
|
||||
use leptos_meta::Title;
|
||||
use leptos::*;
|
||||
use leptos_icons::*;
|
||||
use leptos_use::{utils::Pausable, use_interval_fn};
|
||||
|
||||
/// Width and height of the forward/backward skip buttons
|
||||
const SKIP_BTN_SIZE: &str = "3.5em";
|
||||
@ -20,6 +24,9 @@ const MIN_SKIP_BACK_TIME: f64 = 5.0;
|
||||
/// How many seconds to skip forward/backward when the user presses the arrow keys
|
||||
const ARROW_KEY_SKIP_TIME: f64 = 5.0;
|
||||
|
||||
/// Threshold in seconds for considering when the user has listened to a song, for adding it to the history
|
||||
const HISTORY_LISTEN_THRESHOLD: u64 = MIN_SKIP_BACK_TIME as u64;
|
||||
|
||||
// TODO Handle errors better, when getting audio HTML element and when playing/pausing audio
|
||||
|
||||
/// Get the current time and duration of the current song, if available
|
||||
@ -269,13 +276,124 @@ fn MediaInfo(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
});
|
||||
|
||||
view! {
|
||||
<div class="media-info">
|
||||
<img class="media-info-img" align="left" src={image}/>
|
||||
<div class="media-info-text">
|
||||
{name}
|
||||
<br/>
|
||||
{artist} - {album}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
/// The like and dislike buttons
|
||||
#[component]
|
||||
fn LikeDislike(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
let like_icon = Signal::derive(move || {
|
||||
status.with(|status| {
|
||||
match status.queue.front() {
|
||||
Some(SongData { like_dislike: Some((true, _)), .. }) => icondata::TbThumbUpFilled,
|
||||
_ => icondata::TbThumbUp,
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let dislike_icon = Signal::derive(move || {
|
||||
status.with(|status| {
|
||||
match status.queue.front() {
|
||||
Some(SongData { like_dislike: Some((_, true)), .. }) => icondata::TbThumbDownFilled,
|
||||
_ => icondata::TbThumbDown,
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let toggle_like = move |_| {
|
||||
status.update(|status| {
|
||||
match status.queue.front_mut() {
|
||||
Some(SongData { id, like_dislike: Some((liked, disliked)), .. }) => {
|
||||
*liked = !*liked;
|
||||
|
||||
if *liked {
|
||||
*disliked = false;
|
||||
}
|
||||
|
||||
let id = *id;
|
||||
let liked = *liked;
|
||||
spawn_local(async move {
|
||||
if let Err(e) = songs::set_like_song(id, liked).await {
|
||||
error!("Error liking song: {:?}", e);
|
||||
}
|
||||
});
|
||||
},
|
||||
Some(SongData { id, like_dislike, .. }) => {
|
||||
// This arm should only be reached if like_dislike is None
|
||||
// In this case, the buttons will show up not filled, indicating that the song is not
|
||||
// liked or disliked. Therefore, clicking the like button should like the song.
|
||||
|
||||
*like_dislike = Some((true, false));
|
||||
|
||||
let id = *id;
|
||||
spawn_local(async move {
|
||||
if let Err(e) = songs::set_like_song(id, true).await {
|
||||
error!("Error liking song: {:?}", e);
|
||||
}
|
||||
});
|
||||
},
|
||||
_ => {
|
||||
log!("Unable to like song: No song in queue");
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
let toggle_dislike = move |_| {
|
||||
status.update(|status| {
|
||||
match status.queue.front_mut() {
|
||||
Some(SongData { id, like_dislike: Some((liked, disliked)), .. }) => {
|
||||
*disliked = !*disliked;
|
||||
|
||||
if *disliked {
|
||||
*liked = false;
|
||||
}
|
||||
|
||||
let id = *id;
|
||||
let disliked = *disliked;
|
||||
spawn_local(async move {
|
||||
if let Err(e) = songs::set_dislike_song(id, disliked).await {
|
||||
error!("Error disliking song: {:?}", e);
|
||||
}
|
||||
});
|
||||
},
|
||||
Some(SongData { id, like_dislike, .. }) => {
|
||||
// This arm should only be reached if like_dislike is None
|
||||
// In this case, the buttons will show up not filled, indicating that the song is not
|
||||
// liked or disliked. Therefore, clicking the dislike button should dislike the song.
|
||||
|
||||
*like_dislike = Some((false, true));
|
||||
|
||||
let id = *id;
|
||||
spawn_local(async move {
|
||||
if let Err(e) = songs::set_dislike_song(id, true).await {
|
||||
error!("Error disliking song: {:?}", e);
|
||||
}
|
||||
});
|
||||
},
|
||||
_ => {
|
||||
log!("Unable to dislike song: No song in queue");
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
view! {
|
||||
<div class="like-dislike">
|
||||
<button on:click=toggle_dislike>
|
||||
<Icon class="controlbtn hmirror" width=SKIP_BTN_SIZE height=SKIP_BTN_SIZE icon=dislike_icon />
|
||||
</button>
|
||||
<button on:click=toggle_like>
|
||||
<Icon class="controlbtn" width=SKIP_BTN_SIZE height=SKIP_BTN_SIZE icon=like_icon />
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@ -343,6 +461,21 @@ fn QueueToggle(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the title of the page based on the currently playing song
|
||||
#[component]
|
||||
pub fn CustomTitle(play_status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
let title = create_memo(move |_| {
|
||||
play_status.with(|play_status| {
|
||||
play_status.queue.front().map_or("LibreTunes".to_string(), |song_data| {
|
||||
format!("{} - {} | {}",song_data.title.clone(),Artist::display_list(&song_data.artists), "LibreTunes")
|
||||
})
|
||||
})
|
||||
});
|
||||
view! {
|
||||
<Title text=title />
|
||||
}
|
||||
}
|
||||
|
||||
/// The main play bar component, containing the progress bar, media info, play controls, and play duration
|
||||
#[component]
|
||||
pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
@ -419,6 +552,39 @@ pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
});
|
||||
});
|
||||
|
||||
let current_song_id = create_memo(move |_| {
|
||||
status.with(|status| {
|
||||
status.queue.front().map(|song| song.id)
|
||||
})
|
||||
});
|
||||
|
||||
// Track the last song that was added to the history to prevent duplicates
|
||||
let last_history_song_id = create_rw_signal(None);
|
||||
|
||||
let Pausable {
|
||||
is_active: hist_timeout_pending,
|
||||
resume: resume_hist_timeout,
|
||||
pause: pause_hist_timeout,
|
||||
..
|
||||
} = use_interval_fn(move || {
|
||||
if last_history_song_id.get_untracked() == current_song_id.get_untracked() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(current_song_id) = current_song_id.get_untracked() {
|
||||
last_history_song_id.set(Some(current_song_id));
|
||||
|
||||
spawn_local(async move {
|
||||
if let Err(e) = crate::api::history::add_history(current_song_id).await {
|
||||
error!("Error adding song {} to history: {}", current_song_id, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, HISTORY_LISTEN_THRESHOLD * 1000);
|
||||
|
||||
// Initially pause the timeout, since the audio starts off paused
|
||||
pause_hist_timeout();
|
||||
|
||||
let on_play = move |_| {
|
||||
log!("Audio playing");
|
||||
status.update(|status| status.playing = true);
|
||||
@ -427,6 +593,7 @@ pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
let on_pause = move |_| {
|
||||
log!("Audio paused");
|
||||
status.update(|status| status.playing = false);
|
||||
pause_hist_timeout();
|
||||
};
|
||||
|
||||
let on_time_update = move |_| {
|
||||
@ -444,6 +611,11 @@ pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
error!("Unable to update time: Audio element not available");
|
||||
}
|
||||
});
|
||||
|
||||
// If time is updated, audio is playing, so make sure the history timeout is running
|
||||
if !hist_timeout_pending.get_untracked() {
|
||||
resume_hist_timeout();
|
||||
}
|
||||
};
|
||||
|
||||
let on_end = move |_| {
|
||||
@ -488,7 +660,10 @@ pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
|
||||
on:timeupdate=on_time_update on:ended=on_end type="audio/mpeg" />
|
||||
<div class="playbar">
|
||||
<ProgressBar percentage=percentage.into() status=status />
|
||||
<div class="playbar-left-group">
|
||||
<MediaInfo status=status />
|
||||
<LikeDislike status=status />
|
||||
</div>
|
||||
<PlayControls status=status />
|
||||
<PlayDuration elapsed_secs=elapsed_secs.into() total_secs=total_secs.into() />
|
||||
<QueueToggle status=status />
|
||||
|
@ -23,6 +23,22 @@ diesel::table! {
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
friend_requests (from_id, to_id) {
|
||||
created_at -> Timestamp,
|
||||
from_id -> Int4,
|
||||
to_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
friendships (friend_1_id, friend_2_id) {
|
||||
created_at -> Timestamp,
|
||||
friend_1_id -> Int4,
|
||||
friend_2_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
song_artists (song_id, artist_id) {
|
||||
song_id -> Int4,
|
||||
@ -30,6 +46,29 @@ diesel::table! {
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
song_dislikes (song_id, user_id) {
|
||||
song_id -> Int4,
|
||||
user_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
song_history (id) {
|
||||
id -> Int4,
|
||||
user_id -> Int4,
|
||||
date -> Timestamp,
|
||||
song_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
song_likes (song_id, user_id) {
|
||||
song_id -> Int4,
|
||||
user_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
songs (id) {
|
||||
id -> Int4,
|
||||
@ -58,13 +97,24 @@ diesel::joinable!(album_artists -> albums (album_id));
|
||||
diesel::joinable!(album_artists -> artists (artist_id));
|
||||
diesel::joinable!(song_artists -> artists (artist_id));
|
||||
diesel::joinable!(song_artists -> songs (song_id));
|
||||
diesel::joinable!(song_dislikes -> songs (song_id));
|
||||
diesel::joinable!(song_dislikes -> users (user_id));
|
||||
diesel::joinable!(song_history -> songs (song_id));
|
||||
diesel::joinable!(song_history -> users (user_id));
|
||||
diesel::joinable!(song_likes -> songs (song_id));
|
||||
diesel::joinable!(song_likes -> users (user_id));
|
||||
diesel::joinable!(songs -> albums (album_id));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
album_artists,
|
||||
albums,
|
||||
artists,
|
||||
friend_requests,
|
||||
friendships,
|
||||
song_artists,
|
||||
song_dislikes,
|
||||
song_history,
|
||||
song_likes,
|
||||
songs,
|
||||
users,
|
||||
);
|
||||
|
@ -28,45 +28,10 @@ pub struct SongData {
|
||||
/// Path to song image, relative to the root of the web server.
|
||||
/// For example, `"/assets/images/Song.jpg"`
|
||||
pub image_path: String,
|
||||
/// Whether the song is liked by the user
|
||||
pub like_dislike: Option<(bool, bool)>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl TryInto<SongData> for Song {
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
|
||||
/// Convert a Song object into a SongData object
|
||||
///
|
||||
/// This conversion is expensive, as it requires database queries to get the artist and album objects.
|
||||
/// The SongData/Song conversions are also not truly reversible,
|
||||
/// due to the way the image_path, album, and artist data is handled.
|
||||
fn try_into(self) -> Result<SongData, Self::Error> {
|
||||
use crate::database;
|
||||
let mut db_con = database::get_db_conn();
|
||||
|
||||
let album = self.get_album(&mut db_con)?;
|
||||
|
||||
// Use the song's image path if it exists, otherwise use the album's image path, or fallback to the placeholder
|
||||
let image_path = self.image_path.clone().unwrap_or_else(|| {
|
||||
album
|
||||
.as_ref()
|
||||
.and_then(|album| album.image_path.clone())
|
||||
.unwrap_or_else(|| "/assets/images/placeholder.jpg".to_string())
|
||||
});
|
||||
|
||||
Ok(SongData {
|
||||
id: self.id.ok_or("Song id must be present (Some) to convert to SongData")?,
|
||||
title: self.title.clone(),
|
||||
artists: self.get_artists(&mut db_con)?,
|
||||
album: album,
|
||||
track: self.track,
|
||||
duration: self.duration,
|
||||
release_date: self.release_date,
|
||||
// TODO https://gitlab.mregirouard.com/libretunes/libretunes/-/issues/35
|
||||
song_path: self.storage_path,
|
||||
image_path: image_path,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TryInto<Song> for SongData {
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
@ -74,7 +39,7 @@ impl TryInto<Song> for SongData {
|
||||
/// Convert a SongData object into a Song object
|
||||
///
|
||||
/// The SongData/Song conversions are also not truly reversible,
|
||||
/// due to the way the image_path, album, and and artist data is handled.
|
||||
/// due to the way the image_path data is handled.
|
||||
fn try_into(self) -> Result<Song, Self::Error> {
|
||||
Ok(Song {
|
||||
id: Some(self.id),
|
||||
|
Reference in New Issue
Block a user