Use feature-specific Client in server functions
Some checks failed
Push Workflows / rustfmt (push) Successful in 10s
Push Workflows / docs (push) Successful in 1m43s
Push Workflows / clippy (push) Successful in 2m12s
Push Workflows / test (push) Successful in 4m17s
Push Workflows / leptos-test (push) Successful in 4m24s
Push Workflows / build (push) Successful in 6m2s
Push Workflows / docker-build (push) Failing after 12m7s
Push Workflows / nix-build (push) Successful in 15m46s

This commit is contained in:
2025-05-03 04:23:20 +00:00
parent ff1b7401f2
commit 9cd1e8291a
9 changed files with 40 additions and 31 deletions

View File

@ -1,4 +1,5 @@
use crate::models::frontend;
use crate::util::serverfn_client::Client;
use leptos::prelude::*;
use cfg_if::cfg_if;
@ -12,7 +13,7 @@ cfg_if! {
}
}
#[server(endpoint = "album/get")]
#[server(endpoint = "album/get", client = Client)]
pub async fn get_album(id: i32) -> Result<Option<frontend::Album>, ServerFnError> {
use crate::models::backend::Album;
use crate::schema::*;
@ -50,7 +51,7 @@ pub async fn get_album(id: i32) -> Result<Option<frontend::Album>, ServerFnError
Ok(Some(album))
}
#[server(endpoint = "album/get_songs")]
#[server(endpoint = "album/get_songs", client = Client)]
pub async fn get_songs(id: i32) -> Result<Vec<frontend::Song>, ServerFnError> {
use crate::api::auth::get_logged_in_user;
use crate::schema::*;

View File

@ -1,3 +1,4 @@
use crate::util::serverfn_client::Client;
use leptos::prelude::*;
use cfg_if::cfg_if;
@ -21,7 +22,7 @@ cfg_if! {
/// # Returns
/// * `Result<(), Box<dyn Error>>` - A empty result if successful, or an error
///
#[server(endpoint = "albums/add-album")]
#[server(endpoint = "albums/add-album", client = Client)]
pub async fn add_album(
album_title: String,
release_date: Option<String>,

View File

@ -4,6 +4,7 @@ use cfg_if::cfg_if;
use crate::models::backend::Artist;
use crate::models::frontend;
use crate::util::serverfn_client::Client;
cfg_if! {
if #[cfg(feature = "ssr")] {
@ -24,7 +25,7 @@ cfg_if! {
/// # Returns
/// * `Result<(), Box<dyn Error>>` - A empty result if successful, or an error
///
#[server(endpoint = "artists/add-artist")]
#[server(endpoint = "artists/add-artist", client = Client)]
pub async fn add_artist(artist_name: String) -> Result<(), ServerFnError> {
use crate::schema::artists::dsl::*;
use leptos::server_fn::error::NoCustomError;
@ -45,7 +46,7 @@ pub async fn add_artist(artist_name: String) -> Result<(), ServerFnError> {
Ok(())
}
#[server(endpoint = "artists/get")]
#[server(endpoint = "artists/get", client = Client)]
pub async fn get_artist_by_id(artist_id: i32) -> Result<Option<Artist>, ServerFnError> {
use crate::schema::artists::dsl::*;
use leptos::server_fn::error::NoCustomError;
@ -62,7 +63,7 @@ pub async fn get_artist_by_id(artist_id: i32) -> Result<Option<Artist>, ServerFn
Ok(artist)
}
#[server(endpoint = "artists/top_songs")]
#[server(endpoint = "artists/top_songs", client = Client)]
pub async fn top_songs_by_artist(
artist_id: i32,
limit: Option<i64>,
@ -195,7 +196,7 @@ pub async fn top_songs_by_artist(
Ok(top_songs)
}
#[server(endpoint = "artists/albums")]
#[server(endpoint = "artists/albums", client = Client)]
pub async fn albums_by_artist(
artist_id: i32,
limit: Option<i64>,

View File

@ -13,11 +13,12 @@ cfg_if! {
use crate::api::users::UserCredentials;
use crate::models::backend::User;
use crate::util::serverfn_client::Client;
/// Create a new user and log them in
/// Takes in a NewUser struct, with the password in plaintext
/// Returns a Result with the error message if the user could not be created
#[server(endpoint = "signup")]
#[server(endpoint = "signup", client = Client)]
pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
// Check LIBRETUNES_DISABLE_SIGNUP env var
if std::env::var("LIBRETUNES_DISABLE_SIGNUP").is_ok_and(|v| v == "true") {
@ -64,7 +65,7 @@ pub async fn signup(new_user: User) -> Result<(), ServerFnError> {
/// Log a user in
/// Takes in a username or email and a password in plaintext
/// Returns a Result with a boolean indicating if the login was successful
#[server(endpoint = "login")]
#[server(endpoint = "login", client = Client)]
pub async fn login(credentials: UserCredentials) -> Result<Option<User>, ServerFnError> {
use crate::api::users::validate_user;
@ -90,7 +91,7 @@ pub async fn login(credentials: UserCredentials) -> Result<Option<User>, ServerF
/// Log a user out
/// Returns a Result with the error message if the user could not be logged out
#[server(endpoint = "logout")]
#[server(endpoint = "logout", client = Client)]
pub async fn logout() -> Result<(), ServerFnError> {
let mut auth_session = extract::<AuthSession<AuthBackend>>().await.map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {e}"))
@ -106,7 +107,7 @@ pub async fn logout() -> Result<(), ServerFnError> {
/// Check if a user is logged in
/// Returns a Result with a boolean indicating if the user is logged in
#[server(endpoint = "check_auth")]
#[server(endpoint = "check_auth", client = Client)]
pub async fn check_auth() -> Result<bool, ServerFnError> {
let auth_session = extract::<AuthSession<AuthBackend>>().await.map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {e}"))
@ -169,7 +170,7 @@ pub async fn get_user() -> Result<User, ServerFnError> {
))
}
#[server(endpoint = "get_logged_in_user")]
#[server(endpoint = "get_logged_in_user", client = Client)]
pub async fn get_logged_in_user() -> Result<Option<User>, ServerFnError> {
let auth_session = extract::<AuthSession<AuthBackend>>().await.map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {e}"))
@ -185,7 +186,7 @@ pub async fn get_logged_in_user() -> Result<Option<User>, ServerFnError> {
/// 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")]
#[server(endpoint = "check_admin", client = Client)]
pub async fn check_admin() -> Result<bool, ServerFnError> {
let auth_session = extract::<AuthSession<AuthBackend>>().await.map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error getting auth session: {e}"))

View File

@ -1,5 +1,6 @@
use crate::models::backend::HistoryEntry;
use crate::models::backend::Song;
use crate::util::serverfn_client::Client;
use chrono::NaiveDateTime;
use leptos::prelude::*;
@ -14,7 +15,7 @@ cfg_if! {
}
/// Get the history of the current user.
#[server(endpoint = "history/get")]
#[server(endpoint = "history/get", client = Client)]
pub async fn get_history(limit: Option<i64>) -> Result<Vec<HistoryEntry>, ServerFnError> {
let user = get_user().await?;
let db_con = &mut get_db_conn();
@ -25,7 +26,7 @@ pub async fn get_history(limit: Option<i64>) -> Result<Vec<HistoryEntry>, Server
}
/// Get the listen dates and songs of the current user.
#[server(endpoint = "history/get_songs")]
#[server(endpoint = "history/get_songs", client = Client)]
pub async fn get_history_songs(
limit: Option<i64>,
) -> Result<Vec<(NaiveDateTime, Song)>, ServerFnError> {
@ -38,7 +39,7 @@ pub async fn get_history_songs(
}
/// Add a song to the history of the current user.
#[server(endpoint = "history/add")]
#[server(endpoint = "history/add", client = Client)]
pub async fn add_history(song_id: i32) -> Result<(), ServerFnError> {
let user = get_user().await?;
let db_con = &mut get_db_conn();

View File

@ -4,6 +4,7 @@ use server_fn::codec::{MultipartData, MultipartFormData};
use cfg_if::cfg_if;
use crate::models::frontend;
use crate::util::serverfn_client::Client;
use chrono::NaiveDateTime;
cfg_if! {
@ -76,7 +77,7 @@ pub async fn upload_picture(data: MultipartData) -> Result<(), ServerFnError> {
/// If not provided, all songs ever listend to are returned.
/// Returns a list of tuples with the date the song was listened to
/// and the song data, sorted by date (most recent first).
#[server(endpoint = "/profile/recent_songs")]
#[server(endpoint = "/profile/recent_songs", client = Client)]
pub async fn recent_songs(
for_user_id: i32,
limit: Option<i64>,
@ -198,7 +199,7 @@ pub async fn recent_songs(
/// Optionally takes a limit parameter to limit the number of songs returned.
/// If not provided, all songs listened to in the date range are returned.
/// Returns a list of tuples with the play count and the song data, sorted by play count (most played first).
#[server(endpoint = "/profile/top_songs")]
#[server(endpoint = "/profile/top_songs", client = Client)]
pub async fn top_songs(
for_user_id: i32,
start_date: NaiveDateTime,
@ -334,7 +335,7 @@ pub async fn top_songs(
/// Optionally takes a limit parameter to limit the number of artists returned.
/// If not provided, all artists listened to in the date range are returned.
/// Returns a list of tuples with the play count and the artist data, sorted by play count (most played first).
#[server(endpoint = "/profile/top_artists")]
#[server(endpoint = "/profile/top_artists", client = Client)]
pub async fn top_artists(
for_user_id: i32,
start_date: NaiveDateTime,

View File

@ -1,4 +1,5 @@
use crate::models::backend::{Album, Artist, Song};
use crate::util::serverfn_client::Client;
use leptos::prelude::*;
use cfg_if::cfg_if;
@ -38,7 +39,7 @@ if #[cfg(feature = "ssr")] {
///
/// # Returns
/// A Result containing a vector of albums if the search was successful, or an error if the search failed
#[server(endpoint = "search_albums")]
#[server(endpoint = "search_albums", client = Client)]
pub async fn search_albums(query: String, limit: i64) -> Result<Vec<Album>, ServerFnError> {
use crate::schema::albums::dsl::*;
@ -57,7 +58,7 @@ pub async fn search_albums(query: String, limit: i64) -> Result<Vec<Album>, Serv
///
/// # Returns
/// A Result containing a vector of artists if the search was successful, or an error if the search failed
#[server(endpoint = "search_artists")]
#[server(endpoint = "search_artists", client = Client)]
pub async fn search_artists(query: String, limit: i64) -> Result<Vec<Artist>, ServerFnError> {
use crate::schema::artists::dsl::*;
@ -76,7 +77,7 @@ pub async fn search_artists(query: String, limit: i64) -> Result<Vec<Artist>, Se
///
/// # Returns
/// A Result containing a vector of songs if the search was successful, or an error if the search failed
#[server(endpoint = "search_songs")]
#[server(endpoint = "search_songs", client = Client)]
pub async fn search_songs(query: String, limit: i64) -> Result<Vec<Song>, ServerFnError> {
use crate::schema::songs::dsl::*;
@ -96,7 +97,7 @@ pub async fn search_songs(query: String, limit: i64) -> Result<Vec<Song>, Server
///
/// # Returns
/// A Result containing a tuple of vectors of albums, artists, and songs if the search was successful,
#[server(endpoint = "search")]
#[server(endpoint = "search", client = Client)]
pub async fn search(
query: String,
limit: i64,

View File

@ -3,6 +3,7 @@ use leptos::prelude::*;
use cfg_if::cfg_if;
use crate::models::frontend;
use crate::util::serverfn_client::Client;
cfg_if! {
if #[cfg(feature = "ssr")] {
@ -15,7 +16,7 @@ cfg_if! {
}
/// Like or unlike a song
#[server(endpoint = "songs/set_like")]
#[server(endpoint = "songs/set_like", client = Client)]
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}"))
@ -29,7 +30,7 @@ pub async fn set_like_song(song_id: i32, like: bool) -> Result<(), ServerFnError
}
/// Dislike or remove dislike from a song
#[server(endpoint = "songs/set_dislike")]
#[server(endpoint = "songs/set_dislike", client = Client)]
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}"))
@ -45,7 +46,7 @@ pub async fn set_dislike_song(song_id: i32, dislike: bool) -> Result<(), ServerF
}
/// Get the like and dislike status of a song
#[server(endpoint = "songs/get_like_dislike")]
#[server(endpoint = "songs/get_like_dislike", client = Client)]
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}"))
@ -66,7 +67,7 @@ pub async fn get_like_dislike_song(song_id: i32) -> Result<(bool, bool), ServerF
Ok((like, dislike))
}
#[server(endpoint = "songs/get")]
#[server(endpoint = "songs/get", client = Client)]
pub async fn get_song_by_id(song_id: i32) -> Result<Option<frontend::Song>, ServerFnError> {
use crate::schema::*;
@ -147,7 +148,7 @@ pub async fn get_song_by_id(song_id: i32) -> Result<Option<frontend::Song>, Serv
}
}
#[server(endpoint = "songs/plays")]
#[server(endpoint = "songs/plays", client = Client)]
pub async fn get_song_plays(song_id: i32) -> Result<i64, ServerFnError> {
use crate::schema::*;
@ -164,7 +165,7 @@ pub async fn get_song_plays(song_id: i32) -> Result<i64, ServerFnError> {
Ok(plays)
}
#[server(endpoint = "songs/my-plays")]
#[server(endpoint = "songs/my-plays", client = Client)]
pub async fn get_my_song_plays(song_id: i32) -> Result<i64, ServerFnError> {
use crate::schema::*;

View File

@ -14,6 +14,7 @@ cfg_if::cfg_if! {
}
use crate::models::backend::User;
use crate::util::serverfn_client::Client;
use leptos::prelude::*;
use serde::{Deserialize, Serialize};
@ -158,7 +159,7 @@ pub async fn validate_user(credentials: UserCredentials) -> Result<Option<User>,
/// Get a user from the database by username or email
/// Returns a Result with the user if found, None if not found, or an error if there was a problem
#[server(endpoint = "find_user")]
#[server(endpoint = "find_user", client = Client)]
pub async fn get_user(username_or_email: String) -> Result<Option<User>, ServerFnError> {
let mut user = find_user(username_or_email).await?;
@ -170,7 +171,7 @@ pub async fn get_user(username_or_email: String) -> Result<Option<User>, ServerF
Ok(user)
}
#[server(endpoint = "get_user_by_id")]
#[server(endpoint = "get_user_by_id", client = Client)]
pub async fn get_user_by_id(user_id: i32) -> Result<Option<User>, ServerFnError> {
let mut user = find_user_by_id(user_id).await?;