Skip to content

Commit

Permalink
fix(browsing): return full information about artist/album/song instea…
Browse files Browse the repository at this point in the history
…d of basic
  • Loading branch information
vnghia committed Apr 14, 2024
1 parent 49cca39 commit fc964ff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
21 changes: 9 additions & 12 deletions src/open_subsonic/browsing/get_artists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ async fn get_indexed_artists(
pool: &DatabasePool,
user_id: Uuid,
music_folder_ids: &Option<Vec<Uuid>>,
) -> Result<Vec<(String, BasicArtistId3Db)>> {
) -> Result<Vec<(String, ArtistId3Db)>> {
#[add_permission_filter]
get_basic_artist_id3_db()
.select((artists::index, BasicArtistId3Db::as_select()))
.get_results::<(String, BasicArtistId3Db)>(&mut pool.get().await?)
.select((artists::index, ArtistId3Db::as_select()))
.get_results::<(String, ArtistId3Db)>(&mut pool.get().await?)
.await
.map_err(anyhow::Error::from)
}
Expand All @@ -47,10 +47,7 @@ pub async fn get_artists(
.into_iter()
.into_group_map()
.into_iter()
.map(|(k, v)| Index {
name: k,
artists: v.into_iter().map(BasicArtistId3Db::into).collect(),
})
.map(|(k, v)| Index { name: k, artists: v.into_iter().map(ArtistId3Db::into).collect() })
.collect_vec();

Ok(Indexes { ignored_articles, index })
Expand Down Expand Up @@ -87,7 +84,7 @@ mod tests {
.await
.unwrap()
.into_iter()
.map(|(_, artist)| artist.id)
.map(|(_, artist)| artist.basic.id)
.sorted()
.collect_vec();
assert_eq!(artist_ids, infra.artist_ids(&infra.artist_no_ids(..)).await);
Expand All @@ -114,7 +111,7 @@ mod tests {
.await
.unwrap()
.into_iter()
.map(|(_, artist)| artist.id)
.map(|(_, artist)| artist.basic.id)
.sorted()
.collect_vec();
assert!(artist_ids.contains(&artist_id));
Expand All @@ -141,7 +138,7 @@ mod tests {
.await
.unwrap()
.into_iter()
.map(|(_, artist)| artist.id)
.map(|(_, artist)| artist.basic.id)
.sorted()
.collect_vec();
assert!(artist_ids.contains(&artist_id));
Expand Down Expand Up @@ -169,7 +166,7 @@ mod tests {
.await
.unwrap()
.into_iter()
.map(|(_, artist)| artist.id)
.map(|(_, artist)| artist.basic.id)
.sorted()
.collect_vec();
assert!(artist_ids.contains(&artist_id));
Expand Down Expand Up @@ -205,7 +202,7 @@ mod tests {
.await
.unwrap()
.into_iter()
.map(|(_, artist)| artist.id)
.map(|(_, artist)| artist.basic.id)
.sorted()
.collect_vec();
assert!(!artist_ids.contains(&artist_id));
Expand Down
6 changes: 5 additions & 1 deletion src/open_subsonic/common/id3/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use anyhow::Result;
use diesel::dsl::{count_distinct, sql, sum, AssumeNotNull};
use diesel::dsl::{count_distinct, max, sql, sum, AssumeNotNull};
use diesel::expression::SqlLiteral;
use diesel::{
helper_types, sql_types, ExpressionMethods, NullableExpressionMethods, QueryDsl, Queryable,
Expand Down Expand Up @@ -94,6 +94,9 @@ pub struct BasicSongId3Db {
pub struct SongId3Db {
#[diesel(embed)]
pub basic: BasicSongId3Db,
#[diesel(select_expression = max(albums::name).assume_not_null())]
#[diesel(select_expression_type = AssumeNotNull<helper_types::max<albums::name>>)]
pub album: String,
#[diesel(select_expression = sql("array_agg(distinct(songs_artists.artist_id)) artist_ids"))]
#[diesel(select_expression_type = SqlLiteral::<sql_types::Array<sql_types::Uuid>>)]
pub artist_ids: Vec<Uuid>,
Expand Down Expand Up @@ -244,6 +247,7 @@ impl SongId3Db {
.basic
.cover_art_id
.map(|v| MediaTypedId { t: Some(MediaType::Song), id: v }),
album: Some(self.album),
content_type: Some(
mime_guess::from_ext(&self.basic.format)
.first_or_octet_stream()
Expand Down
6 changes: 5 additions & 1 deletion src/open_subsonic/common/id3/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub type GetBasicSongId3Db = Select<songs::table, AsSelect<BasicSongId3Db, Datab

pub type GetSongId3Db = Select<
GroupBy<
LeftJoin<InnerJoin<GetBasicSongId3Db, songs_artists::table>, GetBasicGenreId3Db>,
LeftJoin<
InnerJoin<InnerJoin<GetBasicSongId3Db, songs_artists::table>, albums::table>,
GetBasicGenreId3Db,
>,
songs::id,
>,
AsSelect<SongId3Db, DatabaseType>,
Expand Down Expand Up @@ -82,6 +85,7 @@ pub fn get_basic_song_id3_db() -> GetBasicSongId3Db {
pub fn get_song_id3_db() -> GetSongId3Db {
get_basic_song_id3_db()
.inner_join(songs_artists::table)
.inner_join(albums::table)
.left_join(get_basic_genre_id3_db())
.group_by(songs::id)
.select(SongId3Db::as_select())
Expand Down
21 changes: 12 additions & 9 deletions src/open_subsonic/searching/search3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ async fn sync(
) -> Result<Search3Result> {
let artists = #[add_permission_filter]
#[add_count_offset(artist)]
get_basic_artist_id3_db()
get_artist_id3_db()
.order(artists::name.asc())
.get_results::<BasicArtistId3Db>(&mut pool.get().await?)
.get_results::<ArtistId3Db>(&mut pool.get().await?)
.await?;

let albums = #[add_permission_filter]
Expand All @@ -72,7 +72,7 @@ async fn sync(
.await?;

Ok(Search3Result {
artists: artists.into_iter().map(BasicArtistId3Db::into).collect(),
artists: artists.into_iter().map(ArtistId3Db::into).collect(),
albums: stream::iter(albums)
.then(|v| async move { v.into(pool).await })
.try_collect()
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn full_text_search(
) -> Result<Search3Result> {
let artists = #[add_permission_filter]
#[add_count_offset(artist)]
get_basic_artist_id3_db()
get_artist_id3_db()
.filter(
artists::ts
.matches(websearch_to_tsquery_with_search_config(USIMPLE_TS_CONFIGURATION, &query)),
Expand All @@ -112,7 +112,7 @@ async fn full_text_search(
)
.desc(),
)
.get_results::<BasicArtistId3Db>(&mut pool.get().await?)
.get_results::<ArtistId3Db>(&mut pool.get().await?)
.await?;

let albums = #[add_permission_filter]
Expand All @@ -134,7 +134,7 @@ async fn full_text_search(

let songs = #[add_permission_filter]
#[add_count_offset(song)]
get_basic_song_id3_db()
get_song_id3_db()
.filter(
songs::ts
.matches(websearch_to_tsquery_with_search_config(USIMPLE_TS_CONFIGURATION, &query)),
Expand All @@ -146,13 +146,16 @@ async fn full_text_search(
)
.desc(),
)
.get_results::<BasicSongId3Db>(&mut pool.get().await?)
.get_results::<SongId3Db>(&mut pool.get().await?)
.await?;

Ok(Search3Result {
artists: artists.into_iter().map(BasicArtistId3Db::into).collect(),
artists: artists.into_iter().map(ArtistId3Db::into).collect(),
albums: albums.into_iter().map(BasicAlbumId3Db::into).collect(),
songs: songs.into_iter().map(BasicSongId3Db::into).collect(),
songs: stream::iter(songs)
.then(|v| async move { v.into(pool).await })
.try_collect()
.await?,
})
}

Expand Down
3 changes: 3 additions & 0 deletions types/src/common/id3/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub struct SongId3 {
pub cover_art: Option<MediaTypedId>,
#[new(default)]
#[serde(skip_serializing_if = "Option::is_none", default)]
pub album: Option<String>,
#[new(default)]
#[serde(skip_serializing_if = "Option::is_none", default)]
pub content_type: Option<String>,
#[new(default)]
#[serde(skip_serializing_if = "Vec::is_empty", default)]
Expand Down

0 comments on commit fc964ff

Please sign in to comment.