Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/playlists: add support for playlist related operations #523

Merged
merged 20 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nghe-api/src/id3/album/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod full;
use bon::Builder;
pub use full::Full;
use nghe_proc_macro::api_derive;
use time::OffsetDateTime;
use uuid::Uuid;

use super::{artist, date, genre};
Expand All @@ -17,6 +18,7 @@ pub struct Album {
pub cover_art: Option<Uuid>,
pub song_count: u16,
pub duration: u32,
pub created: OffsetDateTime,
pub year: Option<u16>,
pub music_brainz_id: Option<Uuid>,
#[builder(default)]
Expand Down
7 changes: 2 additions & 5 deletions nghe-api/src/id3/song/full.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

use super::Song;
use super::Short;
use crate::id3::genre;

#[api_derive]
pub struct Full {
#[serde(flatten)]
pub song: Song,
pub album: String,
pub album_id: Uuid,
pub short: Short,
pub genres: genre::Genres,
}
4 changes: 4 additions & 0 deletions nghe-api/src/id3/song/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod full;
mod short;

use std::borrow::Cow;

use bon::Builder;
pub use full::Full;
use nghe_proc_macro::api_derive;
pub use short::Short;
use time::OffsetDateTime;
use uuid::Uuid;

use super::artist;
Expand All @@ -28,6 +31,7 @@ pub struct Song {
pub sampling_rate: u32,
pub channel_count: u8,
pub disc_number: Option<u16>,
pub created: OffsetDateTime,
pub artists: Vec<artist::Required>,
pub music_brainz_id: Option<Uuid>,
}
12 changes: 12 additions & 0 deletions nghe-api/src/id3/song/short.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

use super::Song;

#[api_derive]
pub struct Short {
#[serde(flatten)]
pub song: Song,
pub album: String,
pub album_id: Uuid,
}
1 change: 1 addition & 0 deletions nghe-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod media_annotation;
pub mod media_retrieval;
pub mod music_folder;
pub mod permission;
pub mod playlists;
pub mod scan;
pub mod search;
pub mod system;
Expand Down
30 changes: 13 additions & 17 deletions nghe-api/src/lists/get_album_list2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ use crate::id3;

// TODO: Optimize this after https://github.com/serde-rs/serde/issues/1183
#[serde_as]
#[api_derive(request = true, test_only = false)]
#[derive(Clone, Copy)]
pub struct ByYear {
#[serde_as(as = "serde_with::DisplayFromStr")]
pub from_year: u16,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub to_year: u16,
}

#[api_derive(request = true, copy = false)]
#[api_derive(copy = false)]
#[serde(tag = "type")]
#[cfg_attr(test, derive(Default))]
pub enum Type {
Expand All @@ -25,7 +16,12 @@ pub enum Type {
Frequent,
Recent,
AlphabeticalByName,
ByYear(ByYear),
ByYear {
#[serde_as(as = "serde_with::DisplayFromStr")]
from_year: u16,
#[serde_as(as = "serde_with::DisplayFromStr")]
to_year: u16,
},
ByGenre {
genre: String,
},
Expand Down Expand Up @@ -75,17 +71,17 @@ mod tests {
#[case(
"type=byYear&fromYear=1000&toYear=2000",
Some(Request {
ty: Type::ByYear (
ByYear{ from_year: 1000, to_year: 2000 }
), size: None, ..Default::default()
ty: Type::ByYear {
from_year: 1000, to_year: 2000
}, size: None, ..Default::default()
})
)]
#[case(
"type=byYear&fromYear=1000&toYear=2000&size=10",
Some(Request {
ty: Type::ByYear (
ByYear{ from_year: 1000, to_year: 2000 }
), size: Some(10), ..Default::default()
ty: Type::ByYear {
from_year: 1000, to_year: 2000
}, size: Some(10), ..Default::default()
})
)]
#[case(
Expand Down
94 changes: 94 additions & 0 deletions nghe-api/src/playlists/create_playlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use nghe_proc_macro::api_derive;
use serde_with::serde_as;
use uuid::Uuid;

use super::playlist;

// TODO: Optimize this after https://github.com/serde-rs/serde/issues/1183
#[serde_as]
#[api_derive(request = true, copy = false)]
#[serde(untagged)]
pub enum CreateOrUpdate {
Create {
name: String,
},
Update {
#[serde_as(as = "serde_with::DisplayFromStr")]
playlist_id: Uuid,
},
}

#[api_derive]
#[endpoint(path = "createPlaylist")]
pub struct Request {
#[serde(flatten)]
pub create_or_update: CreateOrUpdate,
#[serde(rename = "songId")]
pub song_ids: Option<Vec<Uuid>>,
}

#[api_derive]
pub struct Response {
pub playlist: playlist::Full,
}

#[cfg(any(test, feature = "test"))]
mod test {
use super::*;

impl From<String> for CreateOrUpdate {
fn from(value: String) -> Self {
Self::Create { name: value }
}
}

impl From<Uuid> for CreateOrUpdate {
fn from(value: Uuid) -> Self {
Self::Update { playlist_id: value }
}
}
}

#[cfg(test)]
mod tests {
use rstest::rstest;
use uuid::uuid;

use super::*;

#[rstest]
#[case(
"name=ef14c42b-6efa-45f3-961c-74856fd431d5",
Some(Request {
create_or_update: "ef14c42b-6efa-45f3-961c-74856fd431d5".to_owned().into(),
song_ids: None,
})
)]
#[case(
"name=ef14c42b-6efa-45f3-961c-74856fd431d5&\
songId=2b839103-04ab-4b39-9b05-8c664590eda4",
Some(Request {
create_or_update: "ef14c42b-6efa-45f3-961c-74856fd431d5".to_owned().into(),
song_ids: Some(vec![uuid!("2b839103-04ab-4b39-9b05-8c664590eda4")]),
})
)]
#[case(
"playlistId=ef14c42b-6efa-45f3-961c-74856fd431d5",
Some(Request {
create_or_update: uuid!("ef14c42b-6efa-45f3-961c-74856fd431d5").into(),
song_ids: None,
})
)]
#[case(
"playlistId=ef14c42b-6efa-45f3-961c-74856fd431d5&\
songId=2b839103-04ab-4b39-9b05-8c664590eda4",
Some(Request {
create_or_update: uuid!("ef14c42b-6efa-45f3-961c-74856fd431d5").into(),
song_ids: Some(vec![uuid!("2b839103-04ab-4b39-9b05-8c664590eda4")]),
})
)]
#[case("playlistId=none", None)]
fn test_deserialize(#[case] url: &str, #[case] request: Option<Request>) {
assert_eq!(serde_html_form::from_str::<Request>(url).ok(), request);
}
}
11 changes: 11 additions & 0 deletions nghe-api/src/playlists/delete_playlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

#[api_derive]
#[endpoint(path = "deletePlaylist")]
pub struct Request {
pub id: Uuid,
}

#[api_derive]
pub struct Response;
15 changes: 15 additions & 0 deletions nghe-api/src/playlists/get_playlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

use super::playlist;

#[api_derive]
#[endpoint(path = "getPlaylist")]
pub struct Request {
pub id: Uuid,
}

#[api_derive]
pub struct Response {
pub playlist: playlist::Full,
}
17 changes: 17 additions & 0 deletions nghe-api/src/playlists/get_playlists.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use nghe_proc_macro::api_derive;

use super::playlist;

#[api_derive]
#[endpoint(path = "getPlaylists")]
pub struct Request {}

#[api_derive]
pub struct Playlists {
pub playlist: Vec<playlist::Playlist>,
}

#[api_derive]
pub struct Response {
pub playlists: Playlists,
}
6 changes: 6 additions & 0 deletions nghe-api/src/playlists/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod create_playlist;
pub mod delete_playlist;
pub mod get_playlist;
pub mod get_playlists;
pub mod playlist;
pub mod update_playlist;
33 changes: 33 additions & 0 deletions nghe-api/src/playlists/playlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use bon::Builder;
use nghe_proc_macro::api_derive;
use time::OffsetDateTime;
use uuid::Uuid;

use crate::id3;

#[api_derive]
#[derive(Builder)]
#[builder(on(_, required))]
#[builder(state_mod(vis = "pub"))]
pub struct Playlist {
pub id: Uuid,
pub name: String,
pub comment: Option<String>,
pub public: bool,
pub song_count: u16,
pub duration: u32,
pub created: OffsetDateTime,
pub changed: OffsetDateTime,
}

#[api_derive]
pub struct Full {
#[serde(flatten)]
pub playlist: Playlist,
pub entry: Vec<id3::song::Short>,
}

pub mod builder {
pub use super::playlist_builder::*;
pub use super::PlaylistBuilder as Builder;
}
19 changes: 19 additions & 0 deletions nghe-api/src/playlists/update_playlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use nghe_proc_macro::api_derive;
use uuid::Uuid;

#[api_derive]
#[endpoint(path = "updatePlaylist")]
#[cfg_attr(feature = "test", derive(Default))]
pub struct Request {
pub playlist_id: Uuid,
pub name: Option<String>,
pub comment: Option<String>,
pub public: Option<bool>,
#[serde(rename = "songIdToAdd")]
pub add_ids: Option<Vec<Uuid>>,
#[serde(rename = "songIndexToRemove")]
pub remove_indexes: Option<Vec<u16>>,
}

#[api_derive]
pub struct Response;
2 changes: 1 addition & 1 deletion nghe-api/src/search/search3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Request {
pub struct SearchResult3 {
pub artist: Vec<id3::artist::Artist>,
pub album: Vec<id3::album::Album>,
pub song: Vec<id3::song::Song>,
pub song: Vec<id3::song::Short>,
}

#[api_derive]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ create table playlists (

select add_updated_at('playlists');

-- access level:
-- 1 - read songs
-- 2 - add/remove songs
-- 3 - admin (add/remove users, edit, delete)
create table playlists_users (
playlist_id uuid not null,
user_id uuid not null,
access_level smallint not null constraint playlists_users_access_level check (
access_level between 1 and 3
),
write boolean not null,
owner boolean not null default false,
constraint playlists_users_pkey primary key (playlist_id, user_id),
constraint playlists_users_playlist_id_fkey foreign key (
playlist_id
Expand All @@ -29,6 +24,9 @@ create table playlists_users (
) references users (id) on delete cascade
);

create unique index playlists_users_owner on playlists_users (playlist_id)
where owner;

create table playlists_songs (
playlist_id uuid not null,
song_id uuid not null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ unique nulls not distinct (
source, file_hash, file_size
),
drop constraint cover_arts_format_file_hash_file_size_key;

select add_updated_at_leave_scanned_at('cover_arts');
1 change: 1 addition & 0 deletions nghe-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub async fn build(config: config::Config) -> Router {
.merge(route::browsing::router())
.merge(route::lists::router())
.merge(route::media_annotation::router())
.merge(route::playlists::router())
.merge(route::search::router())
.merge(route::system::router())
.with_state(database::Database::new(&config.database))
Expand Down
2 changes: 1 addition & 1 deletion nghe-backend/src/orm/id3/album/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Full {
#[diesel(select_expression = sql("bool_or(songs_album_artists.compilation) is_compilation"))]
#[diesel(select_expression_type = SqlLiteral::<sql_types::Bool>)]
pub is_compilation: bool,
#[diesel(select_expression = sql("array_agg(distinct(songs.id)) album_artists"))]
#[diesel(select_expression = sql("array_agg(distinct(songs.id)) song_ids"))]
#[diesel(select_expression_type = SqlLiteral::<sql_types::Array<sql_types::Uuid>>)]
pub songs: Vec<Uuid>,
}
Expand Down
Loading
Loading