playlists

This commit is contained in:
JMARyA 2024-08-11 04:07:02 +02:00
parent 74b32003d0
commit 428aa6e066
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 59 additions and 3 deletions

View file

@ -5,7 +5,7 @@ use mongod::{
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::library::{track::Track, user::User}; use crate::{library::{track::Track, user::User}, route::ToAPI};
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] #[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
pub struct Playlist { pub struct Playlist {
@ -33,3 +33,14 @@ impl Validate for Playlist {
Ok(()) Ok(())
} }
} }
impl ToAPI for Playlist {
async fn api(&self) -> serde_json::Value {
serde_json::json!({
"id": self._id,
"owner": self.owner.id(),
"title": self.title,
"tracks": self.tracks.iter().map(|x| x.id()).collect::<Vec<_>>()
})
}
}

View file

@ -59,7 +59,9 @@ async fn rocket() -> _ {
route::user::passwd_route, route::user::passwd_route,
route::user::user_create_route, route::user::user_create_route,
route::user::users_route, route::user::users_route,
route::track::track_audio_opus128_route route::track::track_audio_opus128_route,
route::playlist::playlists_route,
route::playlist::playlist_route
], ],
) )
.manage(lib) .manage(lib)

View file

@ -9,6 +9,7 @@ pub mod album;
pub mod artist; pub mod artist;
pub mod track; pub mod track;
pub mod user; pub mod user;
pub mod playlist;
// todo : rework api // todo : rework api

43
src/route/playlist.rs Normal file
View file

@ -0,0 +1,43 @@
use super::api_error;
use super::FallibleApiResponse;
use super::ToAPI;
use mongod::Model;
use mongod::Referencable;
use mongodb::bson::doc;
use rocket::*;
use serde_json::json;
use crate::library::playlist::Playlist;
use crate::library::playlist::Visibility;
use crate::library::user::User;
#[get("/playlists")]
pub async fn playlists_route(u: User) -> FallibleApiResponse {
let mut playlists = vec![
"recent".to_string()
];
let own_playlists = Playlist::find(doc! { "owner": u.reference()}, None).await.unwrap();
for playlist in own_playlists {
playlists.push(playlist._id);
}
Ok(json!(playlists))
}
#[get("/playlist/<id>")]
pub async fn playlist_route(id: &str, u: User) -> FallibleApiResponse {
if id == "recents" {
// todo : recently played
}
let playlist = Playlist::get(id).await.ok_or_else(|| api_error("No playlist with that ID found"))?;
if matches!(playlist.visibility, Visibility::Private) && u.username != playlist.owner.get::<User>().await.username {
return Err(api_error("Forbidden"));
}
Ok(playlist.api().await)
}

View file

@ -1,7 +1,6 @@
use crate::library::user::Session; use crate::library::user::Session;
use crate::library::user::User; use crate::library::user::User;
use crate::route::to_api; use crate::route::to_api;
use crate::route::ToAPI;
use mongod::Model; use mongod::Model;
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket::get; use rocket::get;