Skip to content

Commit

Permalink
feat(media-retrieval): add get lyrics by song id
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Apr 6, 2024
1 parent 0bef5ac commit b444353
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/open_subsonic/common/id3/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ pub struct GenresId3Db {
#[diesel(table_name = lyrics)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct LyricId3Db {
pub description: String,
pub language: String,
pub external: bool,
pub line_values: Vec<Option<String>>,
pub line_starts: Option<Vec<Option<i32>>>,
}
Expand Down
5 changes: 4 additions & 1 deletion src/open_subsonic/extension/get_open_subsonic_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub struct GetOpenSubsonicExtensionsBody {

pub async fn get_open_subsonic_extensions_handler() -> GetOpenSubsonicExtensionsJsonResponse {
GetOpenSubsonicExtensionsBody {
open_subsonic_extensions: &[OSExtension { name: "transcodeOffset", versions: &[1] }],
open_subsonic_extensions: &[
OSExtension { name: "transcodeOffset", versions: &[1] },
OSExtension { name: "songLyrics", versions: &[1] },
],
}
.into()
}
59 changes: 59 additions & 0 deletions src/open_subsonic/media_retrieval/get_lyrics_by_song_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use anyhow::Result;
use axum::extract::State;
use diesel::{ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use nghe_proc_macros::{add_validate, wrap_subsonic_response};
use serde::Serialize;
use uuid::Uuid;

use crate::models::*;
use crate::open_subsonic::common::id3::db::*;
use crate::open_subsonic::common::id3::query::*;
use crate::open_subsonic::common::id3::response::*;
use crate::open_subsonic::permission::with_permission;
use crate::{Database, DatabasePool};

#[add_validate]
#[derive(Debug)]
pub struct GetLyricsBySongIdParams {
id: Uuid,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LyricList {
structured_lyrics: Vec<LyricId3>,
}

#[wrap_subsonic_response]
pub struct GetLyricsBySongIdBody {
lyrics_list: LyricList,
}

async fn get_lyrics_by_song_id(
pool: &DatabasePool,
user_id: Uuid,
song_id: Uuid,
) -> Result<Vec<LyricId3>> {
get_lyric_id3_db()
.filter(with_permission(user_id))
.filter(songs::id.eq(song_id))
.get_results(&mut pool.get().await.unwrap())
.await?
.into_iter()
.map(LyricId3Db::into_res)
.collect()
}

pub async fn get_lyrics_by_song_id_handler(
State(database): State<Database>,
req: GetLyricsBySongIdRequest,
) -> GetLyricsBySongIdJsonResponse {
GetLyricsBySongIdBody {
lyrics_list: LyricList {
structured_lyrics: get_lyrics_by_song_id(&database.pool, req.user_id, req.params.id)
.await?,
},
}
.into()
}
6 changes: 6 additions & 0 deletions src/open_subsonic/media_retrieval/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod download;
mod get_cover_art;
mod get_lyrics_by_song_id;
mod stream;
mod utils;

Expand All @@ -19,6 +20,11 @@ pub fn router(
.route("/rest/stream.view", get(stream::stream_handler))
.route("/rest/getCoverArt", get(get_cover_art::get_cover_art_handler))
.route("/rest/getCoverArt.view", get(get_cover_art::get_cover_art_handler))
.route("/rest/getLyricsBySongId", get(get_lyrics_by_song_id::get_lyrics_by_song_id_handler))
.route(
"/rest/getLyricsBySongId.view",
get(get_lyrics_by_song_id::get_lyrics_by_song_id_handler),
)
.layer(Extension(transcoding_config))
.layer(Extension(art_config))
}
Expand Down

0 comments on commit b444353

Please sign in to comment.