This commit is contained in:
1 parent
bcb5d378c8
commit
4141562e33
51 files changed
+2235
-4754
No files matched your search
@@ -0,0 +1,167 @@
|
||||
use super::v2::{json_error, require_admin_json};
|
||||
use crate::{auth::Role, user::User};
|
||||
use cot::{db::Database, http::StatusCode, json::Json, response::IntoResponse, session::Session};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(super) struct UserRequest {
|
||||
action: String,
|
||||
id: Option<i64>,
|
||||
#[serde(default)]
|
||||
username: String,
|
||||
#[serde(default)]
|
||||
email: String,
|
||||
#[serde(default)]
|
||||
display_name: String,
|
||||
#[serde(default)]
|
||||
password: String,
|
||||
#[serde(default)]
|
||||
role: String,
|
||||
}
|
||||
|
||||
pub(super) async fn manage_user(
|
||||
session: Session,
|
||||
db: Database,
|
||||
Json(body): Json<UserRequest>,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
let admin = match require_admin_json(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(r) => return Ok(r),
|
||||
};
|
||||
if body.action == "delete" {
|
||||
let Some(id) = body.id.filter(|id| *id > 0) else {
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, "Invalid user"));
|
||||
};
|
||||
if id == admin.id {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"You cannot delete your own account",
|
||||
));
|
||||
}
|
||||
User::delete_by_id(&db, id).await?;
|
||||
} else {
|
||||
if !["create", "update"].contains(&body.action.as_str())
|
||||
|| body.username.trim().is_empty()
|
||||
|| body.username.chars().count() > 255
|
||||
|| Role::from_code(&body.role).is_none()
|
||||
|| (body.action == "create" && body.password.is_empty())
|
||||
{
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Provide a username, valid role, and a password for new accounts",
|
||||
));
|
||||
}
|
||||
let optional = |s: String| if s.trim().is_empty() { None } else { Some(s) };
|
||||
let email = optional(body.email);
|
||||
let display_name = optional(body.display_name);
|
||||
if body.action == "create" {
|
||||
if User::get_by_username(&db, body.username.trim())
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
return Ok(json_error(StatusCode::CONFLICT, "Username already exists"));
|
||||
}
|
||||
User::create(
|
||||
&db,
|
||||
body.username.trim(),
|
||||
email.as_deref(),
|
||||
display_name.as_deref(),
|
||||
&body.password,
|
||||
&body.role,
|
||||
)
|
||||
.await?;
|
||||
} else {
|
||||
let Some(id) = body.id.filter(|id| *id > 0) else {
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, "Invalid user"));
|
||||
};
|
||||
if id == admin.id && body.role != "admin" {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"You cannot remove your own administrator role",
|
||||
));
|
||||
}
|
||||
let Some(mut user) = User::get_by_id(&db, id).await? else {
|
||||
return Ok(json_error(StatusCode::NOT_FOUND, "User not found"));
|
||||
};
|
||||
if User::get_by_username(&db, body.username.trim())
|
||||
.await?
|
||||
.is_some_and(|u| u.id_val() != id)
|
||||
{
|
||||
return Ok(json_error(StatusCode::CONFLICT, "Username already exists"));
|
||||
}
|
||||
user.update_fields(
|
||||
&db,
|
||||
body.username.trim(),
|
||||
email.as_deref(),
|
||||
display_name.as_deref(),
|
||||
(!body.password.is_empty()).then_some(body.password.as_str()),
|
||||
&body.role,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
Json(serde_json::json!({"ok": true})).into_response()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(super) struct ScheduleRequest {
|
||||
cron: String,
|
||||
}
|
||||
|
||||
pub(super) async fn schedule(
|
||||
session: Session,
|
||||
db: Database,
|
||||
handle: &std::sync::Arc<
|
||||
tokio::sync::OnceCell<std::sync::Arc<crate::scheduler::SchedulerHandle>>,
|
||||
>,
|
||||
name: &str,
|
||||
Json(body): Json<ScheduleRequest>,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
if let Err(response) = require_admin_json(&session, &db).await {
|
||||
return Ok(response);
|
||||
}
|
||||
let cron = body.cron.trim();
|
||||
if !valid_schedule(cron) {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Invalid cron schedule: include the seconds field",
|
||||
));
|
||||
}
|
||||
if crate::scheduler::ScheduledJob::get_by_name(&db, name)
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
return Ok(json_error(StatusCode::NOT_FOUND, "Job not found"));
|
||||
}
|
||||
let Some(handle) = handle.get() else {
|
||||
return Ok(json_error(
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"Scheduler is not ready",
|
||||
));
|
||||
};
|
||||
handle
|
||||
.reschedule_job(name, cron)
|
||||
.await
|
||||
.map_err(|e| cot::Error::internal(e.to_string()))?;
|
||||
Json(serde_json::json!({"ok": true})).into_response()
|
||||
}
|
||||
|
||||
fn valid_schedule(cron: &str) -> bool {
|
||||
cron.len() <= 100
|
||||
&& croner::parser::CronParser::builder()
|
||||
.seconds(croner::parser::Seconds::Required)
|
||||
.build()
|
||||
.parse(cron)
|
||||
.is_ok()
|
||||
&& crate::scheduler::compute_next_run(cron).is_some()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn schedules_use_the_same_seconds_field_as_the_scheduler() {
|
||||
assert!(super::valid_schedule("0 17 3 * * *"));
|
||||
assert!(!super::valid_schedule("17 3 * * *"));
|
||||
assert!(!super::valid_schedule("invalid"));
|
||||
}
|
||||
}
|
||||
+47
-717
@@ -1,6 +1,8 @@
|
||||
mod management;
|
||||
mod metadata_fix;
|
||||
mod setup;
|
||||
mod statistics;
|
||||
mod v2;
|
||||
pub mod views;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -20,15 +22,7 @@ use crate::config::AppConfig;
|
||||
use crate::i18n::I18n;
|
||||
use crate::scheduler::{JobRegistry, SchedulerHandle};
|
||||
use crate::user::User;
|
||||
use views::{
|
||||
ArtistForm, CronForm, MetadataBackfillForm, OidcSettingsForm, ReleaseForm, ReviewApproveForm,
|
||||
ReviewsBulkForm, SetImageBody, SetupForm, UploadImageBody, UserForm,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReviewsQuery {
|
||||
status: Option<String>,
|
||||
}
|
||||
use setup::SetupForm;
|
||||
|
||||
/// Build-time metadata baked in by `build.rs` and Cargo env vars.
|
||||
#[derive(Debug)]
|
||||
@@ -72,11 +66,6 @@ impl AdminApp {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SettingsQuery {
|
||||
saved: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PathId {
|
||||
id: i64,
|
||||
@@ -98,11 +87,6 @@ struct PathNameRunId {
|
||||
run_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReleasesQuery {
|
||||
artist_id: Option<i64>,
|
||||
}
|
||||
|
||||
impl App for AdminApp {
|
||||
fn name(&self) -> &'static str {
|
||||
"admin"
|
||||
@@ -122,7 +106,7 @@ impl App for AdminApp {
|
||||
if count > 0 {
|
||||
return Ok(auth::redirect("/admin/"));
|
||||
}
|
||||
views::setup_page(i18n, String::new())
|
||||
setup::setup_page(i18n, String::new())
|
||||
.await?
|
||||
.into_response()
|
||||
})
|
||||
@@ -133,7 +117,7 @@ impl App for AdminApp {
|
||||
if count > 0 {
|
||||
return Ok(auth::redirect("/admin/"));
|
||||
}
|
||||
views::setup_submit(i18n, &db, &session, form).await
|
||||
setup::setup_submit(i18n, &db, &session, form).await
|
||||
},
|
||||
),
|
||||
"admin_setup",
|
||||
@@ -180,6 +164,26 @@ impl App for AdminApp {
|
||||
},
|
||||
"admin_v2_dashboard",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/statistics",
|
||||
{
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
get(move |session: Session, db: Database,
|
||||
query: UrlQuery<statistics::StatisticsQuery>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new().max_connections(5)
|
||||
.connect(&pool_config.database_url).await.expect("admin pool")
|
||||
}).await;
|
||||
statistics::statistics(session, db, pg_pool, query.0).await
|
||||
}
|
||||
})
|
||||
},
|
||||
"admin_statistics",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/reviews",
|
||||
{
|
||||
@@ -233,6 +237,25 @@ impl App for AdminApp {
|
||||
},
|
||||
"admin_v2_reviews_bulk",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/users/manage",
|
||||
cot::router::method::post(management::manage_user),
|
||||
"admin_manage_user",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/jobs/{name}/schedule",
|
||||
cot::router::method::post({
|
||||
let handle = Arc::clone(&self.scheduler_handle);
|
||||
move |session: Session, db: Database, path: Path<PathName>,
|
||||
json: Json<management::ScheduleRequest>| {
|
||||
let handle = Arc::clone(&handle);
|
||||
async move {
|
||||
management::schedule(session, db, &handle, &path.0.name, json).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_job_schedule",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/users",
|
||||
{
|
||||
@@ -584,7 +607,7 @@ impl App for AdminApp {
|
||||
.expect("admin pool")
|
||||
})
|
||||
.await;
|
||||
v2::job_run_detail(session, db, pg_pool, path.0.run_id).await
|
||||
v2::job_run_detail(session, db, pg_pool, &path.0.name, path.0.run_id).await
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -907,699 +930,6 @@ impl App for AdminApp {
|
||||
},
|
||||
"admin_index",
|
||||
),
|
||||
// -- Debug --------------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/debug",
|
||||
{
|
||||
let config = Arc::clone(&self.config);
|
||||
move |session: Session, db: Database, i18n: I18n| {
|
||||
let config = Arc::clone(&config);
|
||||
async move {
|
||||
let admin =
|
||||
match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::debug_handler(admin, i18n, &config, &db)
|
||||
.await?
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_debug",
|
||||
),
|
||||
// -- Settings -----------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/settings",
|
||||
get({
|
||||
let config = Arc::clone(&self.config);
|
||||
move |session: Session, db: Database, i18n: I18n,
|
||||
query: UrlQuery<SettingsQuery>| {
|
||||
let config = Arc::clone(&config);
|
||||
async move {
|
||||
let admin =
|
||||
match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let saved = query.0.saved.as_deref() == Some("1");
|
||||
views::settings_handler(admin, i18n, &config, &db, saved)
|
||||
.await?
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
})
|
||||
.post({
|
||||
let config = Arc::clone(&self.config);
|
||||
move |session: Session, db: Database, i18n: I18n,
|
||||
form: RequestForm<OidcSettingsForm>| {
|
||||
let config = Arc::clone(&config);
|
||||
async move {
|
||||
let admin =
|
||||
match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::settings_submit(admin, i18n, &config, &db, form).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_settings",
|
||||
),
|
||||
// -- Settings probe (HTMX fragment) -----------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/settings/probe",
|
||||
{
|
||||
let config = Arc::clone(&self.config);
|
||||
move |session: Session, db: Database, i18n: I18n| {
|
||||
let config = Arc::clone(&config);
|
||||
async move {
|
||||
let admin =
|
||||
match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::settings_probe_handler(admin, i18n, &config, &db)
|
||||
.await?
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_settings_probe",
|
||||
),
|
||||
// -- Users --------------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/users",
|
||||
|session: Session, db: Database, i18n: I18n| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::users_list(admin, i18n, &db).await?.into_response()
|
||||
},
|
||||
"admin_users",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/users/new",
|
||||
get(|session: Session, db: Database, i18n: I18n| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::users_new(admin, i18n).await?.into_response()
|
||||
})
|
||||
.post(
|
||||
|session: Session, db: Database, form: RequestForm<UserForm>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::users_create(admin, &db, form).await
|
||||
},
|
||||
),
|
||||
"admin_users_new",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/users/{id}/edit",
|
||||
get(
|
||||
|session: Session, db: Database, i18n: I18n,
|
||||
path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::users_edit(admin, i18n, &db, path.0.id)
|
||||
.await?
|
||||
.into_response()
|
||||
},
|
||||
)
|
||||
.post(
|
||||
|session: Session, db: Database, path: Path<PathId>,
|
||||
form: RequestForm<UserForm>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::users_update(admin, &db, path.0.id, form).await
|
||||
},
|
||||
),
|
||||
"admin_users_edit",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/users/{id}/delete",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database, path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::users_delete(admin, &db, path.0.id).await
|
||||
},
|
||||
),
|
||||
"admin_users_delete",
|
||||
),
|
||||
// -- Artists ------------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/artists",
|
||||
|session: Session, db: Database, i18n: I18n| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_list(admin, i18n, &db).await?.into_response()
|
||||
},
|
||||
"admin_artists",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/artists/new",
|
||||
get(|session: Session, db: Database, i18n: I18n| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_new(admin, i18n).await?.into_response()
|
||||
})
|
||||
.post(
|
||||
|session: Session, db: Database, form: RequestForm<ArtistForm>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_create(admin, &db, form).await
|
||||
},
|
||||
),
|
||||
"admin_artists_new",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/artists/{id}/edit",
|
||||
get(
|
||||
|session: Session, db: Database, i18n: I18n,
|
||||
path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_edit(admin, i18n, &db, path.0.id)
|
||||
.await?
|
||||
.into_response()
|
||||
},
|
||||
)
|
||||
.post(
|
||||
|session: Session, db: Database, path: Path<PathId>,
|
||||
form: RequestForm<ArtistForm>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_update(admin, &db, path.0.id, form).await
|
||||
},
|
||||
),
|
||||
"admin_artists_edit",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/artists/{id}/delete",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database, path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_delete(admin, &db, path.0.id).await
|
||||
},
|
||||
),
|
||||
"admin_artists_delete",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/artists/{id}/available-covers",
|
||||
get(
|
||||
|session: Session, db: Database, path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_available_covers(admin, &db, path.0.id).await
|
||||
},
|
||||
),
|
||||
"admin_artists_available_covers",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/artists/{id}/set-image",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database, path: Path<PathId>,
|
||||
json: Json<SetImageBody>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::artists_set_image(admin, &db, path.0.id, json.0).await
|
||||
},
|
||||
),
|
||||
"admin_artists_set_image",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/artists/{id}/upload-image",
|
||||
cot::router::method::post({
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
move |session: Session, db: Database, path: Path<PathId>,
|
||||
json: Json<UploadImageBody>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(3)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
}).await;
|
||||
let (live_config, _) = AppConfig::load_with_db(&db).await;
|
||||
views::artists_upload_image(admin, &db, pg_pool, &live_config, path.0.id, json.0).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_artists_upload_image",
|
||||
),
|
||||
// -- Releases -----------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/releases",
|
||||
|session: Session, db: Database, i18n: I18n,
|
||||
query: UrlQuery<ReleasesQuery>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::releases_list(admin, i18n, &db, query.0.artist_id)
|
||||
.await?
|
||||
.into_response()
|
||||
},
|
||||
"admin_releases",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/releases/new",
|
||||
get(|session: Session, db: Database, i18n: I18n| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::releases_new(admin, i18n, &db).await?.into_response()
|
||||
})
|
||||
.post(
|
||||
|session: Session, db: Database,
|
||||
form: RequestForm<ReleaseForm>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::releases_create(admin, &db, form).await
|
||||
},
|
||||
),
|
||||
"admin_releases_new",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/releases/{id}/edit",
|
||||
get(
|
||||
|session: Session, db: Database, i18n: I18n,
|
||||
path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::releases_edit(admin, i18n, &db, path.0.id)
|
||||
.await?
|
||||
.into_response()
|
||||
},
|
||||
)
|
||||
.post(
|
||||
|session: Session, db: Database, path: Path<PathId>,
|
||||
form: RequestForm<ReleaseForm>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::releases_update(admin, &db, path.0.id, form).await
|
||||
},
|
||||
),
|
||||
"admin_releases_edit",
|
||||
),
|
||||
{
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
Route::with_handler_and_name(
|
||||
"/releases/{id}/delete",
|
||||
cot::router::method::post(move |session: Session, db: Database, path: Path<PathId>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool
|
||||
.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
})
|
||||
.await;
|
||||
views::releases_delete(admin, &db, pg_pool, path.0.id).await
|
||||
}
|
||||
}),
|
||||
"admin_releases_delete",
|
||||
)
|
||||
},
|
||||
// -- Media Files --------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/media-files",
|
||||
|session: Session, db: Database, i18n: I18n| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::media_files_list(admin, i18n, &db).await?.into_response()
|
||||
},
|
||||
"admin_media_files",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/media-files/{id}/delete",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database, path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::media_files_delete(admin, &db, path.0.id).await
|
||||
},
|
||||
),
|
||||
"admin_media_files_delete",
|
||||
),
|
||||
// -- Jobs ---------------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/jobs",
|
||||
{
|
||||
let registry = Arc::clone(&self.registry);
|
||||
move |session: Session, db: Database, i18n: I18n| {
|
||||
let registry = Arc::clone(®istry);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::jobs_list(admin, i18n, &db, ®istry).await?.into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_jobs",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/metadata_backfill/run-options",
|
||||
cot::router::method::post({
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
move |session: Session, db: Database,
|
||||
form: RequestForm<MetadataBackfillForm>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(3)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
}).await;
|
||||
views::metadata_backfill_run(admin, &db, pg_pool, form).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_metadata_backfill_run",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/lastfm_popularity/run-options",
|
||||
cot::router::method::post({
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
move |session: Session, db: Database,
|
||||
form: RequestForm<views::LastfmPopularityForm>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(3)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
}).await;
|
||||
views::lastfm_popularity_run(admin, &db, pg_pool, form).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_lastfm_popularity_run",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/{name}/run",
|
||||
cot::router::method::post({
|
||||
let handle = Arc::clone(&self.scheduler_handle);
|
||||
move |session: Session, db: Database, path: Path<PathName>| {
|
||||
let handle = Arc::clone(&handle);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::job_run_now(admin, &handle, &path.0.name).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_job_run",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/{name}/toggle",
|
||||
cot::router::method::post({
|
||||
let handle = Arc::clone(&self.scheduler_handle);
|
||||
move |session: Session, db: Database, path: Path<PathName>| {
|
||||
let handle = Arc::clone(&handle);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::job_toggle_enabled(admin, &db, &handle, &path.0.name).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_job_toggle",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/{name}/cron",
|
||||
cot::router::method::post({
|
||||
let handle = Arc::clone(&self.scheduler_handle);
|
||||
move |session: Session, db: Database, path: Path<PathName>,
|
||||
form: RequestForm<CronForm>| {
|
||||
let handle = Arc::clone(&handle);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::job_update_cron(admin, &db, &handle, &path.0.name, form).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_job_cron",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/{name}/runs/{run_id}",
|
||||
{
|
||||
move |session: Session, db: Database, i18n: I18n,
|
||||
path: Path<PathNameRunId>| {
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::job_run_detail(admin, i18n, &db, &path.0.name, path.0.run_id)
|
||||
.await?
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_job_run_detail",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/jobs/{name}",
|
||||
{
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
move |session: Session, db: Database, i18n: I18n,
|
||||
path: Path<PathName>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(3)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
}).await;
|
||||
views::job_detail(admin, i18n, &db, pg_pool, &path.0.name)
|
||||
.await?
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_job_detail",
|
||||
),
|
||||
// -- Reviews: clear -----------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/reviews/clear",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database,
|
||||
query: UrlQuery<ReviewsQuery>| async move {
|
||||
let admin =
|
||||
match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::reviews_clear(admin, &db, query.0.status.as_deref()).await
|
||||
},
|
||||
),
|
||||
"admin_reviews_clear",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/reviews/bulk",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database,
|
||||
form: RequestForm<ReviewsBulkForm>| async move {
|
||||
let admin =
|
||||
match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::reviews_bulk(admin, &db, form).await
|
||||
},
|
||||
),
|
||||
"admin_reviews_bulk",
|
||||
),
|
||||
// -- Reviews ------------------------------------------------------
|
||||
Route::with_handler_and_name(
|
||||
"/reviews",
|
||||
{
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
move |session: Session, db: Database, i18n: I18n,
|
||||
query: UrlQuery<ReviewsQuery>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(3)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
}).await;
|
||||
views::reviews_list(admin, i18n, &db, pg_pool, query.0.status.as_deref())
|
||||
.await?
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
"admin_reviews",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/reviews/{id}",
|
||||
|session: Session, db: Database, i18n: I18n,
|
||||
path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::review_detail(admin, i18n, &db, path.0.id)
|
||||
.await?
|
||||
.into_response()
|
||||
},
|
||||
"admin_review_detail",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/reviews/{id}/approve",
|
||||
cot::router::method::post({
|
||||
let config = Arc::clone(&self.config);
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
move |session: Session, db: Database, path: Path<PathId>,
|
||||
form: RequestForm<ReviewApproveForm>| {
|
||||
let config = Arc::clone(&config);
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
let pg_pool = pool.get_or_init(|| async {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(3)
|
||||
.connect(&pool_config.database_url)
|
||||
.await
|
||||
.expect("admin pool")
|
||||
}).await;
|
||||
views::review_approve(admin, &config, &db, pg_pool, path.0.id, form).await
|
||||
}
|
||||
}
|
||||
}),
|
||||
"admin_review_approve",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/reviews/{id}/reject",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database, path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::review_reject(admin, &db, path.0.id).await
|
||||
},
|
||||
),
|
||||
"admin_review_reject",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/reviews/{id}/requeue",
|
||||
cot::router::method::post(
|
||||
|session: Session, db: Database, path: Path<PathId>| async move {
|
||||
let admin = match auth::require_admin_or_redirect(&session, &db).await {
|
||||
Ok(u) => u,
|
||||
Err(resp) => return Ok(resp),
|
||||
};
|
||||
views::review_requeue(admin, &db, path.0.id).await
|
||||
},
|
||||
),
|
||||
"admin_review_requeue",
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
@@ -1625,7 +955,7 @@ impl App for AdminApp {
|
||||
}
|
||||
}
|
||||
|
||||
/// Start an options-based run for both admin interfaces. Ordinary scheduler
|
||||
/// Start an options-based run for the admin interface. Ordinary scheduler
|
||||
/// entry points continue to call Job::run with light-mode defaults.
|
||||
async fn start_lastfm_popularity(
|
||||
db: &Database,
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
use crate::auth;
|
||||
use crate::i18n::{I18n, Translations};
|
||||
use crate::user::User;
|
||||
use cot::Template;
|
||||
use cot::db::Database;
|
||||
use cot::form::{Form, FormResult};
|
||||
use cot::html::Html;
|
||||
use cot::request::extractors::RequestForm;
|
||||
use cot::response::IntoResponse;
|
||||
use cot::session::Session;
|
||||
|
||||
#[derive(Debug, Template)]
|
||||
#[template(path = "admin/setup.html")]
|
||||
struct SetupTemplate {
|
||||
t: &'static Translations,
|
||||
message: String,
|
||||
}
|
||||
|
||||
pub async fn setup_page(i18n: I18n, message: String) -> cot::Result<Html> {
|
||||
let template = SetupTemplate { t: i18n.t, message };
|
||||
Ok(Html::new(template.render()?))
|
||||
}
|
||||
|
||||
#[derive(Debug, Form)]
|
||||
pub struct SetupForm {
|
||||
username: String,
|
||||
password: String,
|
||||
confirm_password: String,
|
||||
}
|
||||
|
||||
pub async fn setup_submit(
|
||||
i18n: I18n,
|
||||
db: &Database,
|
||||
session: &Session,
|
||||
form: RequestForm<SetupForm>,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
let RequestForm(result) = form;
|
||||
let data = match result {
|
||||
FormResult::Ok(data) => data,
|
||||
FormResult::ValidationError(_) => {
|
||||
return setup_page(i18n, String::new()).await?.into_response();
|
||||
}
|
||||
};
|
||||
|
||||
if data.password != data.confirm_password {
|
||||
let msg = i18n.t.setup_mismatch.to_owned();
|
||||
return setup_page(i18n, msg).await?.into_response();
|
||||
}
|
||||
|
||||
let user = User::create(db, &data.username, None, None, &data.password, "admin")
|
||||
.await
|
||||
.map_err(|e| cot::Error::internal(format!("failed to create admin: {e}")))?;
|
||||
|
||||
auth::login(session, user.id_val()).await?;
|
||||
Ok(auth::redirect("/admin/"))
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
WITH listens AS (
|
||||
SELECT le.*, COALESCE(NULLIF(u.display_name, ''), u.username::text, 'Deleted user #' || le.user_id) AS user_name,
|
||||
COALESCE(NULLIF(le.metadata_json->>'title', ''), 'Unknown track') AS title,
|
||||
COALESCE(le.metadata_json->>'release_title', '') AS release_title,
|
||||
ARRAY(SELECT DISTINCT trim(name) FROM jsonb_array_elements_text(
|
||||
COALESCE(le.metadata_json->'artist_names', '[]'::jsonb) ||
|
||||
COALESCE(le.metadata_json->'featured_artist_names', '[]'::jsonb)
|
||||
) AS names(name) WHERE trim(name) <> '' ORDER BY trim(name)) AS artist_names,
|
||||
CASE WHEN le.origin_device_id = ident.device_id THEN 'instance' ELSE 'synced' END AS source,
|
||||
COALESCE(NULLIF(d.name, ''), le.origin_device_id) AS device_name
|
||||
FROM furumusic__listen_event le
|
||||
LEFT JOIN furumusic__user u ON u.id = le.user_id
|
||||
LEFT JOIN furumusic__fed_device_identity ident ON ident.user_id = le.user_id
|
||||
LEFT JOIN furumusic__fed_device d ON d.user_id = le.user_id AND d.device_id = le.origin_device_id
|
||||
WHERE le.started_at_ms >= $1 AND le.started_at_ms < $2
|
||||
AND ($3::bigint IS NULL OR le.user_id = $3)
|
||||
AND ($4::text IS NULL OR le.content_id = $4)
|
||||
), filtered AS (
|
||||
SELECT * FROM listens
|
||||
WHERE ($5::text IS NULL OR EXISTS (SELECT 1 FROM unnest(artist_names) n WHERE lower(n) = lower($5)))
|
||||
AND ($6 = 'all' OR source = $6)
|
||||
)
|
||||
@@ -0,0 +1,403 @@
|
||||
use chrono::{NaiveDate, Utc};
|
||||
use cot::{db::Database, http::StatusCode, json::Json, response::IntoResponse, session::Session};
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use super::v2::{json_error, require_admin_json};
|
||||
|
||||
const LISTENS: &str = include_str!("sql/listens.sql");
|
||||
const PAGE_SIZE: i64 = 25;
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub(super) struct StatisticsQuery {
|
||||
pub from: Option<String>,
|
||||
pub to: Option<String>,
|
||||
pub user_id: Option<i64>,
|
||||
pub content_id: Option<String>,
|
||||
pub artist: Option<String>,
|
||||
pub source: Option<String>,
|
||||
pub kind: Option<String>,
|
||||
pub search: Option<String>,
|
||||
pub rank_page: Option<i64>,
|
||||
pub history_page: Option<i64>,
|
||||
pub history_kind: Option<String>,
|
||||
}
|
||||
|
||||
struct Filter {
|
||||
from: i64,
|
||||
to: i64,
|
||||
user_id: Option<i64>,
|
||||
content_id: Option<String>,
|
||||
artist: Option<String>,
|
||||
source: String,
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
fn parse(query: &StatisticsQuery) -> Result<Self, String> {
|
||||
fn date(value: &str) -> Result<i64, String> {
|
||||
NaiveDate::parse_from_str(value, "%Y-%m-%d")
|
||||
.ok()
|
||||
.and_then(|d| d.and_hms_opt(0, 0, 0))
|
||||
.map(|d| d.and_utc().timestamp_millis())
|
||||
.filter(|d| *d >= 0)
|
||||
.ok_or_else(|| "Use dates in YYYY-MM-DD format (1970 or later)".to_owned())
|
||||
}
|
||||
let to = match query.to.as_deref().filter(|s| !s.is_empty()) {
|
||||
Some(value) => date(value)?
|
||||
.checked_add(86_400_000)
|
||||
.ok_or("Invalid end date")?,
|
||||
None => Utc::now().timestamp_millis(),
|
||||
};
|
||||
let from = match query.from.as_deref().filter(|s| !s.is_empty()) {
|
||||
Some("all") => 0,
|
||||
Some(value) => date(value)?,
|
||||
None => to.saturating_sub(30 * 86_400_000),
|
||||
};
|
||||
if from >= to {
|
||||
return Err("Start date must not be after end date".into());
|
||||
}
|
||||
let source = query.source.as_deref().unwrap_or("all");
|
||||
if !["all", "instance", "synced"].contains(&source) {
|
||||
return Err("Unknown playback source".into());
|
||||
}
|
||||
if query.user_id.is_some_and(|id| id <= 0) {
|
||||
return Err("Invalid user".into());
|
||||
}
|
||||
if query.artist.as_ref().is_some_and(|s| s.len() > 1024)
|
||||
|| query.content_id.as_ref().is_some_and(|s| s.len() > 256)
|
||||
|| query.search.as_ref().is_some_and(|s| s.len() > 1024)
|
||||
{
|
||||
return Err("Filter is too long".into());
|
||||
}
|
||||
Ok(Self {
|
||||
from,
|
||||
to,
|
||||
user_id: query.user_id,
|
||||
content_id: query.content_id.clone(),
|
||||
artist: query.artist.clone(),
|
||||
source: source.into(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn query(&self, pool: &PgPool, suffix: &str) -> anyhow::Result<Value> {
|
||||
let sql = format!("{LISTENS}{suffix}");
|
||||
Ok(sqlx::query_scalar(&sql)
|
||||
.bind(self.from)
|
||||
.bind(self.to)
|
||||
.bind(self.user_id)
|
||||
.bind(&self.content_id)
|
||||
.bind(&self.artist)
|
||||
.bind(&self.source)
|
||||
.fetch_one(pool)
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn statistics(
|
||||
session: Session,
|
||||
db: Database,
|
||||
pool: &PgPool,
|
||||
query: StatisticsQuery,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
if let Err(response) = require_admin_json(&session, &db).await {
|
||||
return Ok(response);
|
||||
}
|
||||
let filter = match Filter::parse(&query) {
|
||||
Ok(filter) => filter,
|
||||
Err(message) => return Ok(json_error(StatusCode::BAD_REQUEST, &message)),
|
||||
};
|
||||
let kind = query.kind.as_deref().unwrap_or("artists");
|
||||
if !["artists", "tracks", "releases", "users"].contains(&kind) {
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, "Unknown ranking"));
|
||||
}
|
||||
let history_kind = query.history_kind.as_deref().unwrap_or("all");
|
||||
if !["all", "plays", "short"].contains(&history_kind) {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"Unknown history filter",
|
||||
));
|
||||
}
|
||||
let result = load(pool, &filter, &query, kind, history_kind)
|
||||
.await
|
||||
.map_err(|e| cot::Error::internal(e.to_string()))?;
|
||||
Json(result).into_response()
|
||||
}
|
||||
|
||||
async fn load(
|
||||
pool: &PgPool,
|
||||
filter: &Filter,
|
||||
query: &StatisticsQuery,
|
||||
kind: &str,
|
||||
history_kind: &str,
|
||||
) -> anyhow::Result<Value> {
|
||||
let summary_sql = r#"
|
||||
SELECT to_jsonb(s) FROM (
|
||||
SELECT COUNT(*) AS events, COUNT(*) FILTER (WHERE qualified) AS plays,
|
||||
COUNT(*) FILTER (WHERE NOT qualified) AS short_listens,
|
||||
COUNT(*) FILTER (WHERE trim(both '"' from ended_reason) = 'finished') AS completed,
|
||||
COALESCE(SUM(listened_ms), 0)::bigint / 1000 AS listened_seconds,
|
||||
COUNT(DISTINCT user_id) FILTER (WHERE qualified) AS listeners,
|
||||
COUNT(DISTINCT content_id) FILTER (WHERE qualified) AS tracks,
|
||||
COUNT(*) FILTER (WHERE qualified AND source = 'instance') AS instance_plays,
|
||||
COUNT(*) FILTER (WHERE qualified AND source = 'synced') AS synced_plays
|
||||
FROM filtered
|
||||
) s"#;
|
||||
let daily_sql = r#",
|
||||
bounds AS (
|
||||
SELECT COALESCE(MIN(started_at_ms), $2) AS first_ms FROM filtered
|
||||
), granularity AS (
|
||||
SELECT CASE WHEN $2 - GREATEST($1, first_ms) > 120::bigint * 86400000
|
||||
THEN 'month' ELSE 'day' END AS unit FROM bounds
|
||||
), daily AS (
|
||||
SELECT date_trunc(g.unit, to_timestamp(started_at_ms / 1000.0) AT TIME ZONE 'UTC') AS bucket,
|
||||
COUNT(*) FILTER (WHERE qualified) AS plays,
|
||||
COUNT(DISTINCT user_id) FILTER (WHERE qualified) AS listeners,
|
||||
COALESCE(SUM(listened_ms), 0)::bigint / 1000 AS listened_seconds
|
||||
FROM filtered CROSS JOIN granularity g GROUP BY bucket
|
||||
), series AS (
|
||||
SELECT generate_series((SELECT MIN(bucket) FROM daily), (SELECT MAX(bucket) FROM daily),
|
||||
CASE WHEN unit = 'month' THEN interval '1 month' ELSE interval '1 day' END) AS bucket
|
||||
FROM granularity
|
||||
), filled AS (
|
||||
SELECT s.bucket, COALESCE(d.plays, 0) AS plays, COALESCE(d.listeners, 0) AS listeners,
|
||||
COALESCE(d.listened_seconds, 0) AS listened_seconds
|
||||
FROM series s LEFT JOIN daily d USING (bucket)
|
||||
)
|
||||
SELECT jsonb_build_object('unit', (SELECT unit FROM granularity), 'items',
|
||||
COALESCE((SELECT jsonb_agg(to_jsonb(d) ORDER BY bucket) FROM filled d), '[]'::jsonb))"#;
|
||||
let (summary, daily, ranking, history, instance) = tokio::try_join!(
|
||||
filter.query(pool, summary_sql),
|
||||
filter.query(pool, daily_sql),
|
||||
ranking(pool, filter, query, kind),
|
||||
history(pool, filter, query, history_kind),
|
||||
instance(pool),
|
||||
)?;
|
||||
Ok(
|
||||
serde_json::json!({"summary": summary, "daily": daily, "ranking": ranking,
|
||||
"history": history, "instance": instance, "from_ms": filter.from, "to_ms": filter.to}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn ranking(
|
||||
pool: &PgPool,
|
||||
filter: &Filter,
|
||||
query: &StatisticsQuery,
|
||||
kind: &str,
|
||||
) -> anyhow::Result<Value> {
|
||||
// Only fixed SQL fragments are interpolated; every user filter is bound.
|
||||
let (key, label, extra, join) = match kind {
|
||||
"users" => ("user_id::text", "MIN(user_name)", "''::text", ""),
|
||||
"tracks" => (
|
||||
"content_id",
|
||||
"MIN(title)",
|
||||
"MIN(array_to_string(artist_names, ', '))",
|
||||
"",
|
||||
),
|
||||
"releases" => (
|
||||
"jsonb_build_array(release_title, artist_names)::text",
|
||||
"MIN(NULLIF(release_title, ''))",
|
||||
"MIN(array_to_string(artist_names, ', '))",
|
||||
"",
|
||||
),
|
||||
_ => (
|
||||
"lower(artist.name)",
|
||||
"MIN(artist.name)",
|
||||
"''::text",
|
||||
"CROSS JOIN LATERAL (SELECT DISTINCT ON (lower(n)) n AS name FROM unnest(artist_names) n ORDER BY lower(n), n) artist",
|
||||
),
|
||||
};
|
||||
let sql = format!(
|
||||
r#"{LISTENS}, ranked AS (
|
||||
SELECT {key} AS key, {label} AS label, {extra} AS detail, COUNT(*) AS plays,
|
||||
COUNT(DISTINCT user_id) AS listeners,
|
||||
SUM(listened_ms)::bigint / 1000 AS listened_seconds, MAX(started_at_ms) AS last_play_ms
|
||||
FROM filtered {join} WHERE qualified GROUP BY {key}
|
||||
), searched AS (
|
||||
SELECT * FROM ranked WHERE label IS NOT NULL
|
||||
AND ($7::text IS NULL OR strpos(lower(label || ' ' || detail), lower($7)) > 0)
|
||||
), page AS (SELECT * FROM searched ORDER BY plays DESC, key LIMIT $8 OFFSET $9)
|
||||
SELECT jsonb_build_object('total', (SELECT COUNT(*) FROM searched),
|
||||
'items', COALESCE((SELECT jsonb_agg(to_jsonb(p) ORDER BY plays DESC, key) FROM page p), '[]'::jsonb))"#
|
||||
);
|
||||
let page = query.rank_page.unwrap_or(1).clamp(1, 1_000_000);
|
||||
let mut result: Value = sqlx::query_scalar(&sql)
|
||||
.bind(filter.from)
|
||||
.bind(filter.to)
|
||||
.bind(filter.user_id)
|
||||
.bind(&filter.content_id)
|
||||
.bind(&filter.artist)
|
||||
.bind(&filter.source)
|
||||
.bind(query.search.as_deref().filter(|s| !s.is_empty()))
|
||||
.bind(PAGE_SIZE)
|
||||
.bind((page - 1) * PAGE_SIZE)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
result["page"] = page.into();
|
||||
result["limit"] = PAGE_SIZE.into();
|
||||
result["kind"] = kind.into();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn history(
|
||||
pool: &PgPool,
|
||||
filter: &Filter,
|
||||
query: &StatisticsQuery,
|
||||
kind: &str,
|
||||
) -> anyhow::Result<Value> {
|
||||
let sql = format!(
|
||||
r#"{LISTENS}, selected AS (
|
||||
SELECT user_id, user_name, listen_id, content_id, title, artist_names, release_title,
|
||||
started_at_ms, listened_ms, track_duration_ms, qualified,
|
||||
trim(both '"' from ended_reason) AS ended_reason, device_name, source
|
||||
FROM filtered WHERE $7 = 'all' OR ($7 = 'plays' AND qualified) OR ($7 = 'short' AND NOT qualified)
|
||||
), page AS (SELECT * FROM selected ORDER BY started_at_ms DESC, user_id, listen_id LIMIT $8 OFFSET $9)
|
||||
SELECT jsonb_build_object('total', (SELECT COUNT(*) FROM selected),
|
||||
'items', COALESCE((SELECT jsonb_agg(to_jsonb(p) ORDER BY started_at_ms DESC, user_id, listen_id) FROM page p), '[]'::jsonb))"#
|
||||
);
|
||||
let page = query.history_page.unwrap_or(1).clamp(1, 1_000_000);
|
||||
let mut result: Value = sqlx::query_scalar(&sql)
|
||||
.bind(filter.from)
|
||||
.bind(filter.to)
|
||||
.bind(filter.user_id)
|
||||
.bind(&filter.content_id)
|
||||
.bind(&filter.artist)
|
||||
.bind(&filter.source)
|
||||
.bind(kind)
|
||||
.bind(PAGE_SIZE)
|
||||
.bind((page - 1) * PAGE_SIZE)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
result["page"] = page.into();
|
||||
result["limit"] = PAGE_SIZE.into();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn instance(pool: &PgPool) -> anyhow::Result<Value> {
|
||||
Ok(sqlx::query_scalar(r#"SELECT jsonb_build_object(
|
||||
'users', (SELECT COUNT(*) FROM furumusic__user),
|
||||
'tracks', (SELECT COUNT(*) FROM furumusic__track),
|
||||
'releases', (SELECT COUNT(*) FROM furumusic__release),
|
||||
'artists', (SELECT COUNT(*) FROM furumusic__artist),
|
||||
'playlists', (SELECT COUNT(*) FROM furumusic__playlist),
|
||||
'media_bytes', (SELECT COALESCE(SUM(file_size_bytes), 0)::bigint FROM furumusic__media_file),
|
||||
'formats', (SELECT COALESCE(jsonb_agg(to_jsonb(f)), '[]'::jsonb) FROM (
|
||||
SELECT COALESCE(audio_format, 'unknown') AS label, COUNT(*) AS count
|
||||
FROM furumusic__media_file WHERE file_type = 'audio' GROUP BY audio_format ORDER BY count DESC
|
||||
) f),
|
||||
'reviews', (SELECT COALESCE(jsonb_agg(to_jsonb(r)), '[]'::jsonb) FROM (
|
||||
SELECT status::text AS label, COUNT(*) AS count FROM furumusic__pending_review GROUP BY status ORDER BY status
|
||||
) r),
|
||||
'jobs', (SELECT COALESCE(jsonb_agg(to_jsonb(j)), '[]'::jsonb) FROM (
|
||||
SELECT status::text AS label, COUNT(*) AS count FROM furumusic__job_run GROUP BY status ORDER BY status
|
||||
) j),
|
||||
'scrobbles', (SELECT COALESCE(jsonb_agg(to_jsonb(s)), '[]'::jsonb) FROM (
|
||||
SELECT status::text AS label, COUNT(*) AS count FROM furumusic__lastfm_scrobble_outbox GROUP BY status ORDER BY status
|
||||
) s)
|
||||
)"#).fetch_one(pool).await?)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn dates_are_inclusive_utc_and_filters_are_validated() {
|
||||
let q = StatisticsQuery {
|
||||
from: Some("2026-11-01".into()),
|
||||
to: Some("2026-11-01".into()),
|
||||
..Default::default()
|
||||
};
|
||||
let f = Filter::parse(&q).unwrap();
|
||||
assert_eq!(f.to - f.from, 86_400_000);
|
||||
assert!(
|
||||
Filter::parse(&StatisticsQuery {
|
||||
from: Some("broken".into()),
|
||||
..q
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
Filter::parse(&StatisticsQuery {
|
||||
source: Some("invalid".into()),
|
||||
..Default::default()
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
#[tokio::test]
|
||||
#[ignore = "requires FURU_STATISTICS_TEST_DATABASE_URL pointing at a disposable PostgreSQL"]
|
||||
async fn postgres_statistics_drilldown_and_pagination() {
|
||||
let url = std::env::var("FURU_STATISTICS_TEST_DATABASE_URL").expect("test database URL");
|
||||
// Temporary tables are isolated to this one connection and disappear on close.
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect(&url)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::raw_sql(include_str!("../../tests/statistics_fixture.sql"))
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut q = StatisticsQuery {
|
||||
from: Some("2026-11-01".into()),
|
||||
to: Some("2026-11-03".into()),
|
||||
..Default::default()
|
||||
};
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "artists", "all")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["summary"]["plays"], 32);
|
||||
assert_eq!(result["summary"]["short_listens"], 1);
|
||||
assert_eq!(result["summary"]["listeners"], 2);
|
||||
assert_eq!(result["summary"]["listened_seconds"], 5618);
|
||||
assert_eq!(result["summary"]["synced_plays"], 1);
|
||||
assert_eq!(result["ranking"]["items"][0]["label"], "Artist A");
|
||||
assert_eq!(result["ranking"]["items"][0]["plays"], 31); // duplicate artist credits never multiply plays
|
||||
assert_eq!(result["ranking"]["items"][0]["listeners"], 2);
|
||||
assert_eq!(result["history"]["total"], 33);
|
||||
assert_eq!(result["history"]["items"].as_array().unwrap().len(), 25);
|
||||
assert_eq!(result["daily"]["items"].as_array().unwrap().len(), 3);
|
||||
assert_eq!(result["daily"]["items"][1]["plays"], 0); // fill the silent day
|
||||
q.artist = Some("artist a".into());
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "users", "all")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["ranking"]["total"], 2);
|
||||
assert_eq!(result["ranking"]["items"][0]["label"], "Alice");
|
||||
assert_eq!(result["ranking"]["items"][0]["plays"], 30);
|
||||
q.user_id = Some(2);
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "tracks", "short")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["summary"]["plays"], 1);
|
||||
assert_eq!(result["history"]["total"], 1);
|
||||
assert_eq!(result["history"]["items"][0]["ended_reason"], "skipped");
|
||||
assert_eq!(result["ranking"]["items"][0]["label"], "First song"); // no local track exists
|
||||
q.user_id = None;
|
||||
q.artist = None;
|
||||
q.history_page = Some(2);
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "releases", "all")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["history"]["items"].as_array().unwrap().len(), 8);
|
||||
q.source = Some("synced".into());
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "users", "all")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["summary"]["plays"], 1);
|
||||
q.source = Some("instance".into());
|
||||
q.search = Some("' OR 1=1 --".into());
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "artists", "all")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["summary"]["plays"], 31);
|
||||
assert_eq!(result["ranking"]["total"], 0);
|
||||
q.user_id = Some(3);
|
||||
let result = load(&pool, &Filter::parse(&q).unwrap(), &q, "users", "all")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result["summary"]["plays"], 0);
|
||||
assert_eq!(result["history"]["total"], 0);
|
||||
pool.close().await;
|
||||
}
|
||||
}
|
||||
+54
-42
@@ -232,6 +232,9 @@ struct AdminUserRowDto {
|
||||
is_active: bool,
|
||||
is_online: bool,
|
||||
last_seen_ms: Option<i64>,
|
||||
plays: i64,
|
||||
listened_seconds: i64,
|
||||
last_play_ms: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
@@ -257,7 +260,7 @@ struct AdminUserStatsDto {
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
struct AdminUserPlayDto {
|
||||
history_id: i64,
|
||||
history_id: String,
|
||||
played_at: String,
|
||||
duration_listened: Option<i32>,
|
||||
completed: bool,
|
||||
@@ -1958,6 +1961,7 @@ pub async fn job_run_detail(
|
||||
session: Session,
|
||||
db: Database,
|
||||
pool: &PgPool,
|
||||
job_name: &str,
|
||||
run_id: i64,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
if let Err(response) = require_admin_json(&session, &db).await {
|
||||
@@ -1967,9 +1971,10 @@ pub async fn job_run_detail(
|
||||
"SELECT id, job_name::text AS job_name, status::text AS status, started_at::text AS started_at, \
|
||||
finished_at, duration_ms, trigger::text AS trigger, error_message, \
|
||||
LEFT(COALESCE(log_output, ''), 1600) AS log_excerpt, log_output \
|
||||
FROM furumusic__job_run WHERE id = $1",
|
||||
FROM furumusic__job_run WHERE id = $1 AND job_name = $2",
|
||||
)
|
||||
.bind(run_id)
|
||||
.bind(job_name)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(|e| cot::Error::internal(e.to_string()))?;
|
||||
@@ -2662,6 +2667,9 @@ struct AdminUserSqlRow {
|
||||
email: Option<String>,
|
||||
role: String,
|
||||
is_active: bool,
|
||||
plays: i64,
|
||||
listened_seconds: i64,
|
||||
last_play_ms: Option<i64>,
|
||||
}
|
||||
|
||||
async fn load_admin_users_page(
|
||||
@@ -2692,7 +2700,11 @@ async fn load_admin_users_page(
|
||||
let total: i64 = count_qb.build_query_scalar().fetch_one(pool).await?;
|
||||
|
||||
let mut qb = QueryBuilder::<Postgres>::new(
|
||||
"SELECT id, username::text, display_name, email, role::text, is_active FROM furumusic__user WHERE 1=1",
|
||||
"SELECT id, username::text, display_name, email, role::text, is_active, \
|
||||
(SELECT COUNT(*) FROM furumusic__listen_event le WHERE le.user_id = furumusic__user.id AND qualified) AS plays, \
|
||||
(SELECT (COALESCE(SUM(listened_ms), 0) / 1000)::bigint FROM furumusic__listen_event le WHERE le.user_id = furumusic__user.id) AS listened_seconds, \
|
||||
(SELECT MAX(started_at_ms) FROM furumusic__listen_event le WHERE le.user_id = furumusic__user.id AND qualified) AS last_play_ms \
|
||||
FROM furumusic__user WHERE 1=1",
|
||||
);
|
||||
if let Some(pattern) = pattern.as_ref() {
|
||||
qb.push(" AND (username ILIKE ");
|
||||
@@ -2735,7 +2747,11 @@ async fn load_admin_user_detail(
|
||||
user_id: i64,
|
||||
) -> anyhow::Result<Option<AdminUserDetailDto>> {
|
||||
let row = sqlx::query_as::<_, AdminUserSqlRow>(
|
||||
"SELECT id, username::text, display_name, email, role::text, is_active FROM furumusic__user WHERE id = $1",
|
||||
"SELECT id, username::text, display_name, email, role::text, is_active, \
|
||||
(SELECT COUNT(*) FROM furumusic__listen_event le WHERE le.user_id = furumusic__user.id AND qualified) AS plays, \
|
||||
(SELECT (COALESCE(SUM(listened_ms), 0) / 1000)::bigint FROM furumusic__listen_event le WHERE le.user_id = furumusic__user.id) AS listened_seconds, \
|
||||
(SELECT MAX(started_at_ms) FROM furumusic__listen_event le WHERE le.user_id = furumusic__user.id AND qualified) AS last_play_ms \
|
||||
FROM furumusic__user WHERE id = $1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_optional(pool)
|
||||
@@ -2759,17 +2775,17 @@ async fn load_admin_user_detail(
|
||||
lastfm_connected,
|
||||
) = tokio::try_join!(
|
||||
sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM furumusic__play_history WHERE user_id = $1"
|
||||
"SELECT COUNT(*) FROM furumusic__listen_event WHERE user_id = $1 AND qualified"
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool),
|
||||
sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM furumusic__play_history WHERE user_id = $1 AND completed"
|
||||
"SELECT COUNT(*) FROM furumusic__listen_event WHERE user_id = $1 AND qualified AND ended_reason = '\"finished\"'"
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool),
|
||||
sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COALESCE(SUM(duration_listened), 0)::bigint FROM furumusic__play_history WHERE user_id = $1"
|
||||
"SELECT (COALESCE(SUM(listened_ms), 0) / 1000)::bigint FROM furumusic__listen_event WHERE user_id = $1"
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool),
|
||||
@@ -2835,34 +2851,27 @@ async fn load_admin_user_recent_plays(
|
||||
limit: i64,
|
||||
) -> anyhow::Result<Vec<AdminUserPlayDto>> {
|
||||
let rows = sqlx::query_as::<_, AdminUserPlaySqlRow>(
|
||||
r#"SELECT ph.id AS history_id,
|
||||
ph.played_at::text AS played_at,
|
||||
ph.duration_listened,
|
||||
ph.completed,
|
||||
t.id AS track_id,
|
||||
t.title::text AS title,
|
||||
COALESCE(NULLIF(STRING_AGG(a.name::text, ', ' ORDER BY ta.position), ''), 'Unknown artist') AS artists,
|
||||
t.release_id,
|
||||
COALESCE(r.title::text, '') AS release_title,
|
||||
r.year AS release_year,
|
||||
t.cover_file_id,
|
||||
r.cover_file_id AS release_cover_file_id,
|
||||
t.duration_seconds AS track_duration_seconds,
|
||||
COALESCE(mf.uploader_name, 'UFO')::text AS uploader_name,
|
||||
mf.audio_format,
|
||||
mf.audio_bitrate
|
||||
FROM furumusic__play_history ph
|
||||
JOIN furumusic__track t ON t.id = ph.track_id
|
||||
r#"SELECT le.listen_id AS history_id,
|
||||
to_timestamp(le.started_at_ms / 1000.0)::text AS played_at,
|
||||
LEAST(le.listened_ms / 1000, 2147483647)::integer AS duration_listened,
|
||||
le.ended_reason = '"finished"' AS completed,
|
||||
COALESCE(t.id, 0) AS track_id,
|
||||
COALESCE(le.metadata_json->>'title', 'Unknown track') AS title,
|
||||
COALESCE((SELECT string_agg(name, ', ') FROM jsonb_array_elements_text(
|
||||
COALESCE(le.metadata_json->'artist_names', '[]'::jsonb)) names(name)), 'Unknown artist') AS artists,
|
||||
COALESCE(t.release_id, 0) AS release_id,
|
||||
COALESCE(le.metadata_json->>'release_title', '') AS release_title,
|
||||
r.year AS release_year, t.cover_file_id, r.cover_file_id AS release_cover_file_id,
|
||||
COALESCE(le.track_duration_ms / 1000.0, 0)::float8 AS track_duration_seconds,
|
||||
COALESCE(mf.uploader_name, '')::text AS uploader_name,
|
||||
mf.audio_format, mf.audio_bitrate
|
||||
FROM furumusic__listen_event le
|
||||
LEFT JOIN furumusic__track_ref tr ON tr.content_id = le.content_id
|
||||
LEFT JOIN furumusic__track t ON t.id = COALESCE(le.local_track_id, tr.local_track_id)
|
||||
LEFT JOIN furumusic__release r ON r.id = t.release_id
|
||||
LEFT JOIN furumusic__media_file mf ON mf.id = t.audio_file_id
|
||||
LEFT JOIN furumusic__track_artist ta ON ta.track_id = t.id AND ta.role = 'main'
|
||||
LEFT JOIN furumusic__artist a ON a.id = ta.artist_id
|
||||
WHERE ph.user_id = $1
|
||||
GROUP BY ph.id, ph.played_at, ph.duration_listened, ph.completed, t.id, t.title,
|
||||
t.release_id, r.title, r.year, t.cover_file_id, r.cover_file_id,
|
||||
t.duration_seconds, mf.uploader_name, mf.audio_format, mf.audio_bitrate
|
||||
ORDER BY ph.played_at DESC, ph.id DESC
|
||||
LIMIT $2"#,
|
||||
WHERE le.user_id = $1 AND le.qualified
|
||||
ORDER BY le.started_at_ms DESC, le.listen_id DESC LIMIT $2"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(limit)
|
||||
@@ -2893,7 +2902,7 @@ async fn load_admin_user_recent_plays(
|
||||
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
struct AdminUserPlaySqlRow {
|
||||
history_id: i64,
|
||||
history_id: String,
|
||||
played_at: String,
|
||||
duration_listened: Option<i32>,
|
||||
completed: bool,
|
||||
@@ -2930,6 +2939,9 @@ fn admin_user_row(
|
||||
email: row.email,
|
||||
role: row.role,
|
||||
is_active: row.is_active,
|
||||
plays: row.plays,
|
||||
listened_seconds: row.listened_seconds,
|
||||
last_play_ms: row.last_play_ms,
|
||||
is_online: last_seen_ms.is_some_and(|value| value <= online_cutoff_ms),
|
||||
last_seen_ms,
|
||||
}
|
||||
@@ -3549,13 +3561,13 @@ async fn fetch_library_item(
|
||||
(COALESCE(NULLIF(STRING_AGG(DISTINCT a.name::text, ', '), ''), 'Unknown artist') || COALESCE(' / ' || r.year::text, '')) AS subtitle, \
|
||||
r.is_hidden, COUNT(DISTINCT t.id)::bigint AS primary_count, \
|
||||
COUNT(DISTINCT ra.artist_id)::bigint AS secondary_count, \
|
||||
COUNT(DISTINCT ph.id)::bigint AS tertiary_count, \
|
||||
COUNT(DISTINCT (ph.user_id, ph.listen_id)) FILTER (WHERE ph.listen_id IS NOT NULL)::bigint AS tertiary_count, \
|
||||
r.updated_at::text AS updated_at \
|
||||
FROM furumusic__release r \
|
||||
LEFT JOIN furumusic__track t ON t.release_id = r.id \
|
||||
LEFT JOIN furumusic__release_artist ra ON ra.release_id = r.id \
|
||||
LEFT JOIN furumusic__artist a ON a.id = ra.artist_id \
|
||||
LEFT JOIN furumusic__play_history ph ON ph.track_id = t.id \
|
||||
LEFT JOIN furumusic__track_ref playback_ref ON playback_ref.local_track_id = t.id LEFT JOIN furumusic__listen_event ph ON ph.content_id = playback_ref.content_id AND ph.qualified \
|
||||
WHERE r.id = $1 \
|
||||
GROUP BY r.id",
|
||||
)
|
||||
@@ -3585,13 +3597,13 @@ async fn fetch_library_item(
|
||||
"SELECT t.id, t.title::text AS title, \
|
||||
CONCAT(COALESCE(r.title::text, 'No release'), COALESCE(' / #' || t.track_number::text, '')) AS subtitle, \
|
||||
t.is_hidden, COUNT(DISTINCT ta.artist_id)::bigint AS primary_count, \
|
||||
COUNT(DISTINCT ph.id)::bigint AS secondary_count, \
|
||||
COUNT(DISTINCT (ph.user_id, ph.listen_id)) FILTER (WHERE ph.listen_id IS NOT NULL)::bigint AS secondary_count, \
|
||||
COUNT(DISTINCT pt.playlist_id)::bigint AS tertiary_count, \
|
||||
t.updated_at::text AS updated_at \
|
||||
FROM furumusic__track t \
|
||||
LEFT JOIN furumusic__release r ON r.id = t.release_id \
|
||||
LEFT JOIN furumusic__track_artist ta ON ta.track_id = t.id \
|
||||
LEFT JOIN furumusic__play_history ph ON ph.track_id = t.id \
|
||||
LEFT JOIN furumusic__track_ref playback_ref ON playback_ref.local_track_id = t.id LEFT JOIN furumusic__listen_event ph ON ph.content_id = playback_ref.content_id AND ph.qualified \
|
||||
LEFT JOIN furumusic__playlist_track pt ON pt.track_id = t.id \
|
||||
WHERE t.id = $1 \
|
||||
GROUP BY t.id, r.title",
|
||||
@@ -4362,13 +4374,13 @@ async fn load_release_items(
|
||||
(COALESCE(NULLIF(STRING_AGG(DISTINCT a.name::text, ', '), ''), 'Unknown artist') || COALESCE(' / ' || r.year::text, '')) AS subtitle, \
|
||||
r.is_hidden, COUNT(DISTINCT t.id)::bigint AS primary_count, \
|
||||
COUNT(DISTINCT ra.artist_id)::bigint AS secondary_count, \
|
||||
COUNT(DISTINCT ph.id)::bigint AS tertiary_count, \
|
||||
COUNT(DISTINCT (ph.user_id, ph.listen_id)) FILTER (WHERE ph.listen_id IS NOT NULL)::bigint AS tertiary_count, \
|
||||
r.updated_at::text AS updated_at \
|
||||
FROM furumusic__release r \
|
||||
LEFT JOIN furumusic__track t ON t.release_id = r.id \
|
||||
LEFT JOIN furumusic__release_artist ra ON ra.release_id = r.id \
|
||||
LEFT JOIN furumusic__artist a ON a.id = ra.artist_id \
|
||||
LEFT JOIN furumusic__play_history ph ON ph.track_id = t.id \
|
||||
LEFT JOIN furumusic__track_ref playback_ref ON playback_ref.local_track_id = t.id LEFT JOIN furumusic__listen_event ph ON ph.content_id = playback_ref.content_id AND ph.qualified \
|
||||
WHERE 1=1",
|
||||
);
|
||||
if let Some(pattern) = search_pattern {
|
||||
@@ -4398,14 +4410,14 @@ async fn load_track_items(
|
||||
"SELECT t.id, t.title::text AS title, \
|
||||
CONCAT(COALESCE(r.title::text, 'No release'), COALESCE(' / #' || t.track_number::text, '')) AS subtitle, \
|
||||
t.is_hidden, COUNT(DISTINCT ta.artist_id)::bigint AS primary_count, \
|
||||
COUNT(DISTINCT ph.id)::bigint AS secondary_count, \
|
||||
COUNT(DISTINCT (ph.user_id, ph.listen_id)) FILTER (WHERE ph.listen_id IS NOT NULL)::bigint AS secondary_count, \
|
||||
COUNT(DISTINCT pt.playlist_id)::bigint AS tertiary_count, \
|
||||
t.updated_at::text AS updated_at \
|
||||
FROM furumusic__track t \
|
||||
LEFT JOIN furumusic__release r ON r.id = t.release_id \
|
||||
LEFT JOIN furumusic__track_artist ta ON ta.track_id = t.id \
|
||||
LEFT JOIN furumusic__artist a ON a.id = ta.artist_id \
|
||||
LEFT JOIN furumusic__play_history ph ON ph.track_id = t.id \
|
||||
LEFT JOIN furumusic__track_ref playback_ref ON playback_ref.local_track_id = t.id LEFT JOIN furumusic__listen_event ph ON ph.content_id = playback_ref.content_id AND ph.qualified \
|
||||
LEFT JOIN furumusic__playlist_track pt ON pt.track_id = t.id \
|
||||
WHERE 1=1",
|
||||
);
|
||||
|
||||
-2170
File diff suppressed because it is too large.
Load diff
+8
-2
@@ -443,6 +443,9 @@ pub async fn merge_existing(
|
||||
}
|
||||
// A repeated YouTube import must still resolve to this existing media ID.
|
||||
if let Some(sha) = context["sha256"].as_str() {
|
||||
if let Err(error) = crate::import_playlists::link_media(pool, sha, candidate.media_id).await {
|
||||
tracing::warn!(%error, "could not link deduplicated import to destination playlists");
|
||||
}
|
||||
if let Err(error) = sqlx::query(
|
||||
r#"INSERT INTO furumusic__youtube_import_media (item_id, media_file_id)
|
||||
SELECT DISTINCT item.id, $1
|
||||
@@ -794,7 +797,7 @@ mod tests {
|
||||
CREATE TABLE furumusic__track_embedding (track_id bigint);
|
||||
CREATE TABLE test_likes (track_id bigint REFERENCES furumusic__track(id));
|
||||
CREATE TABLE test_playlist_tracks (track_id bigint REFERENCES furumusic__track(id));
|
||||
CREATE TABLE furumusic__local_upload (id text PRIMARY KEY, user_id bigint,
|
||||
CREATE TABLE furumusic__local_upload (target_playlist_id bigint, id text PRIMARY KEY, user_id bigint,
|
||||
filename text, size_bytes bigint, status text, inbox_path text, error text,
|
||||
created_at text, updated_at text, completed_at text);
|
||||
INSERT INTO furumusic__artist VALUES (1, 'Artist', 'artist');
|
||||
@@ -969,6 +972,7 @@ mod tests {
|
||||
"song.mp3",
|
||||
3,
|
||||
"user_uploads/6/batch/song.mp3",
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -979,6 +983,7 @@ mod tests {
|
||||
"song.mp3",
|
||||
3,
|
||||
"user_uploads/6/batch/song.mp3",
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1014,7 +1019,8 @@ mod tests {
|
||||
7,
|
||||
"song.mp3",
|
||||
3,
|
||||
"user_uploads/7/song.mp3"
|
||||
"user_uploads/7/song.mp3",
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
|
||||
@@ -408,6 +408,8 @@ translations! {
|
||||
player_youtube_select_all: "Select all" , "Отметить все";
|
||||
player_youtube_clear_selection: "Clear selection" , "Снять все";
|
||||
player_youtube_selected_count: "selected" , "выбрано";
|
||||
player_import_destination: "Add downloads to a playlist" , "Добавить загруженное в плейлист";
|
||||
player_import_destination_hint: "Selected audio tracks will be added automatically after processing, including tracks already in the library." , "Выбранные аудиотреки будут добавлены автоматически после обработки, включая уже имеющиеся в библиотеке.";
|
||||
player_youtube_destination: "Add imported tracks to playlist" , "Добавить импортированные треки в плейлист";
|
||||
player_youtube_no_destination: "Do not add to a playlist" , "Не добавлять в плейлист";
|
||||
player_youtube_create_playlist: "Create a new playlist" , "Создать новый плейлист";
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
//! Durable playlist destinations for file and torrent imports.
|
||||
use anyhow::{Context, bail};
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub async fn validate_target(
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
playlist_id: Option<i64>,
|
||||
) -> anyhow::Result<()> {
|
||||
if let Some(id) = playlist_id {
|
||||
let owned: bool = sqlx::query_scalar(
|
||||
"SELECT EXISTS(SELECT 1 FROM furumusic__playlist WHERE id=$1 AND owner_id=$2)",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(user_id)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
if id <= 0 || !owned {
|
||||
bail!("selected playlist does not exist or is not yours");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn register(
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
playlist_id: i64,
|
||||
input_path: &str,
|
||||
sha256: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
validate_target(pool, user_id, Some(playlist_id)).await?;
|
||||
let result = sqlx::query(
|
||||
r#"INSERT INTO furumusic__import_playlist_target
|
||||
(user_id, playlist_id, input_path, source_sha256)
|
||||
VALUES ($1,$2,$3,$4)
|
||||
ON CONFLICT (user_id, input_path) DO UPDATE SET source_sha256=EXCLUDED.source_sha256
|
||||
WHERE furumusic__import_playlist_target.playlist_id=EXCLUDED.playlist_id
|
||||
AND furumusic__import_playlist_target.source_sha256=EXCLUDED.source_sha256"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(playlist_id)
|
||||
.bind(input_path)
|
||||
.bind(sha256)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
if result.rows_affected() == 0 {
|
||||
bail!("this upload already has a different playlist destination");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Fingerprint deduplication may resolve a differently encoded file to an existing media ID.
|
||||
pub async fn link_media(pool: &PgPool, source_sha256: &str, media_id: i64) -> anyhow::Result<()> {
|
||||
if source_sha256.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
sqlx::query("UPDATE furumusic__import_playlist_target SET media_file_id=$2 WHERE source_sha256=$1 AND fulfilled_at IS NULL")
|
||||
.bind(source_sha256).bind(media_id).execute(pool).await?;
|
||||
reconcile(pool, None).await
|
||||
}
|
||||
|
||||
/// Runs after imports and from background discovery, including after a restart.
|
||||
/// Fulfilled requests are retained as receipts so later user removals stay removed.
|
||||
pub async fn reconcile(pool: &PgPool, user_id: Option<i64>) -> anyhow::Result<()> {
|
||||
let requests: Vec<(i64, i64, String)> = sqlx::query_as(
|
||||
"SELECT user_id,playlist_id,input_path FROM furumusic__import_playlist_target WHERE fulfilled_at IS NULL AND ($1::bigint IS NULL OR user_id=$1) ORDER BY user_id,playlist_id,input_path"
|
||||
).bind(user_id).fetch_all(pool).await?;
|
||||
for (user_id, playlist_id, path) in requests {
|
||||
let mut transaction = pool.begin().await?;
|
||||
let pending: Option<String> = sqlx::query_scalar(
|
||||
"SELECT input_path FROM furumusic__import_playlist_target WHERE user_id=$1 AND input_path=$2 AND fulfilled_at IS NULL FOR UPDATE"
|
||||
).bind(user_id).bind(&path).fetch_optional(&mut *transaction).await?;
|
||||
if pending.is_none() {
|
||||
continue;
|
||||
}
|
||||
let track_ids: Vec<i64> = sqlx::query_scalar(r#"
|
||||
SELECT DISTINCT track.id FROM furumusic__import_playlist_target request
|
||||
JOIN furumusic__media_file media ON media.id=request.media_file_id
|
||||
OR (request.media_file_id IS NULL AND media.sha256_hash=request.source_sha256 AND media.file_type='audio')
|
||||
JOIN furumusic__track track ON track.audio_file_id=media.id
|
||||
WHERE request.user_id=$1 AND request.input_path=$2 ORDER BY track.id
|
||||
"#).bind(user_id).bind(&path).fetch_all(&mut *transaction).await?;
|
||||
if track_ids.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut added = Vec::new();
|
||||
for track_id in track_ids {
|
||||
if append_in_transaction(&mut transaction, user_id, playlist_id, track_id).await? {
|
||||
added.push(track_id);
|
||||
}
|
||||
}
|
||||
sqlx::query("UPDATE furumusic__import_playlist_target SET fulfilled_at=$3 WHERE user_id=$1 AND input_path=$2")
|
||||
.bind(user_id).bind(path).bind(now_string()).execute(&mut *transaction).await?;
|
||||
transaction.commit().await?;
|
||||
record_added(pool, user_id, playlist_id, &added).await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn now_string() -> String {
|
||||
chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string()
|
||||
}
|
||||
|
||||
pub(crate) async fn append_track_to_target_playlist(
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
playlist_id: i64,
|
||||
track_id: i64,
|
||||
) -> anyhow::Result<bool> {
|
||||
let mut transaction = pool.begin().await?;
|
||||
let added = append_in_transaction(&mut transaction, user_id, playlist_id, track_id).await?;
|
||||
transaction.commit().await?;
|
||||
if added {
|
||||
record_added(pool, user_id, playlist_id, &[track_id]).await;
|
||||
}
|
||||
Ok(added)
|
||||
}
|
||||
|
||||
async fn append_in_transaction(
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
user_id: i64,
|
||||
playlist_id: i64,
|
||||
track_id: i64,
|
||||
) -> anyhow::Result<bool> {
|
||||
let playlist: Option<i64> = sqlx::query_scalar(
|
||||
r#"SELECT id FROM furumusic__playlist
|
||||
WHERE id = $1 AND owner_id = $2
|
||||
FOR UPDATE"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.bind(user_id)
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await?;
|
||||
if playlist.is_none() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let already_present: bool = sqlx::query_scalar(
|
||||
r#"SELECT EXISTS (
|
||||
SELECT 1 FROM furumusic__playlist_track
|
||||
WHERE playlist_id = $1 AND track_id = $2
|
||||
)"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.bind(track_id)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await?;
|
||||
if already_present {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let position: i64 = sqlx::query_scalar(
|
||||
r#"SELECT COALESCE(MAX(position), -1)::bigint + 1
|
||||
FROM furumusic__playlist_track WHERE playlist_id = $1"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await?;
|
||||
let position = i32::try_from(position).context("target playlist has too many tracks")?;
|
||||
let now = now_string();
|
||||
sqlx::query(
|
||||
r#"INSERT INTO furumusic__playlist_track
|
||||
(playlist_id, track_id, position, added_at, added_by_user_id)
|
||||
VALUES ($1, $2, $3, $4, $5)"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.bind(track_id)
|
||||
.bind(position)
|
||||
.bind(&now)
|
||||
.bind(user_id)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
sqlx::query("UPDATE furumusic__playlist SET updated_at = $1 WHERE id = $2")
|
||||
.bind(&now)
|
||||
.bind(playlist_id)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn record_added(pool: &PgPool, user_id: i64, playlist_id: i64, track_ids: &[i64]) {
|
||||
if track_ids.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Err(error) = crate::federation::devices::record_playlist_tracks_added(
|
||||
pool,
|
||||
user_id,
|
||||
playlist_id,
|
||||
track_ids,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(playlist_id, %error, "federation operation for an automatic import playlist addition was not recorded");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires disposable PostgreSQL in FURU_IMPORT_TEST_DATABASE_URL"]
|
||||
async fn import_destinations_are_durable_owned_and_idempotent() {
|
||||
let url = std::env::var("FURU_IMPORT_TEST_DATABASE_URL").unwrap();
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect(&url)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::raw_sql(r#"
|
||||
CREATE TEMP TABLE furumusic__playlist(id bigint PRIMARY KEY,owner_id bigint,updated_at text);
|
||||
CREATE TEMP TABLE furumusic__media_file(id bigint PRIMARY KEY,sha256_hash text,file_type text);
|
||||
CREATE TEMP TABLE furumusic__track(id bigint PRIMARY KEY,audio_file_id bigint);
|
||||
CREATE TEMP TABLE furumusic__playlist_track(playlist_id bigint,track_id bigint,position int,added_at text,added_by_user_id bigint);
|
||||
CREATE TEMP TABLE furumusic__import_playlist_target(user_id bigint,playlist_id bigint REFERENCES furumusic__playlist ON DELETE CASCADE,input_path text,source_sha256 text,media_file_id bigint,fulfilled_at text,PRIMARY KEY(user_id,input_path));
|
||||
INSERT INTO furumusic__playlist VALUES (10,1,''),(20,2,'');
|
||||
"#).execute(&pool).await.unwrap();
|
||||
assert!(validate_target(&pool, 1, Some(20)).await.is_err());
|
||||
assert!(validate_target(&pool, 1, Some(999)).await.is_err());
|
||||
validate_target(&pool, 1, None).await.unwrap();
|
||||
register(&pool, 1, 10, "batch/a.flac", "hash-a")
|
||||
.await
|
||||
.unwrap();
|
||||
register(&pool, 1, 10, "batch/a.flac", "hash-a")
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
register(&pool, 1, 10, "batch/a.flac", "changed-hash")
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
reconcile(&pool, None).await.unwrap();
|
||||
let count = || {
|
||||
sqlx::query_scalar::<_, i64>("SELECT count(*) FROM furumusic__playlist_track")
|
||||
.fetch_one(&pool)
|
||||
};
|
||||
assert_eq!(count().await.unwrap(), 0);
|
||||
sqlx::raw_sql("INSERT INTO furumusic__media_file VALUES (100,'hash-a','audio'); INSERT INTO furumusic__track VALUES (101,100),(102,100);").execute(&pool).await.unwrap();
|
||||
reconcile(&pool, Some(2)).await.unwrap();
|
||||
assert_eq!(count().await.unwrap(), 0);
|
||||
reconcile(&pool, Some(1)).await.unwrap();
|
||||
assert_eq!(count().await.unwrap(), 2);
|
||||
// A second upload of identical audio must not append duplicates.
|
||||
register(&pool, 1, 10, "torrent/b.flac", "hash-a")
|
||||
.await
|
||||
.unwrap();
|
||||
reconcile(&pool, None).await.unwrap();
|
||||
assert_eq!(count().await.unwrap(), 2);
|
||||
sqlx::query("DELETE FROM furumusic__playlist_track WHERE track_id=101")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
reconcile(&pool, None).await.unwrap();
|
||||
assert_eq!(count().await.unwrap(), 1);
|
||||
// Fingerprint matching can map a differently encoded upload to the same media.
|
||||
register(&pool, 2, 20, "batch/a.mp3", "different-encoding")
|
||||
.await
|
||||
.unwrap();
|
||||
link_media(&pool, "different-encoding", 100).await.unwrap();
|
||||
assert_eq!(count().await.unwrap(), 3);
|
||||
let positions: Vec<i32> = sqlx::query_scalar(
|
||||
"SELECT position FROM furumusic__playlist_track WHERE playlist_id=20 ORDER BY position",
|
||||
)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(positions, vec![0, 1]);
|
||||
// Deleting a destination removes pending work without blocking future imports.
|
||||
register(&pool, 2, 20, "batch/pending.wav", "pending")
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("DELETE FROM furumusic__playlist WHERE id=20")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
reconcile(&pool, None).await.unwrap();
|
||||
pool.close().await;
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,9 @@ impl Job for InboxDiscoverJob {
|
||||
outcome: run_outcome,
|
||||
};
|
||||
|
||||
if let Err(error) = crate::import_playlists::reconcile(&ctx.pool, None).await {
|
||||
log.warn(&format!("Could not reconcile import playlists: {error}"));
|
||||
}
|
||||
let config = &ctx.config;
|
||||
|
||||
if config.agent_inbox_dir.is_empty() {
|
||||
|
||||
@@ -1271,6 +1271,10 @@ pub async fn finalize_approved(
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(error) = crate::import_playlists::link_media(pool, sha256, media_file.id_val()).await {
|
||||
tracing::warn!(%error, "could not add imported tracks to destination playlists; will retry");
|
||||
}
|
||||
|
||||
if let Err(error) =
|
||||
crate::youtube::sync_target_playlists_for_imported_media(pool, media_file.id_val()).await
|
||||
{
|
||||
|
||||
+14
-7
@@ -103,6 +103,7 @@ pub struct LocalUploadDto {
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
pub completed_at: Option<String>,
|
||||
pub target_playlist_id: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow)]
|
||||
@@ -117,6 +118,7 @@ struct LocalUploadRow {
|
||||
created_at: String,
|
||||
updated_at: String,
|
||||
completed_at: Option<String>,
|
||||
target_playlist_id: Option<i64>,
|
||||
}
|
||||
|
||||
impl LocalUploadRow {
|
||||
@@ -130,6 +132,7 @@ impl LocalUploadRow {
|
||||
created_at: self.created_at.clone(),
|
||||
updated_at: self.updated_at.clone(),
|
||||
completed_at: self.completed_at.clone(),
|
||||
target_playlist_id: self.target_playlist_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,18 +144,20 @@ pub async fn create(
|
||||
filename: &str,
|
||||
size_bytes: u64,
|
||||
inbox_path: &str,
|
||||
target_playlist_id: Option<i64>,
|
||||
) -> anyhow::Result<()> {
|
||||
let now = now_string();
|
||||
let created = sqlx::query(
|
||||
r#"INSERT INTO furumusic__local_upload
|
||||
(id, user_id, filename, size_bytes, status, inbox_path, error,
|
||||
created_at, updated_at, completed_at)
|
||||
VALUES ($1, $2, $3, $4, 'uploading', $5, NULL, $6, $6, NULL)
|
||||
created_at, updated_at, completed_at, target_playlist_id)
|
||||
VALUES ($1, $2, $3, $4, 'uploading', $5, NULL, $6, $6, NULL, $7)
|
||||
ON CONFLICT (id) DO UPDATE SET status='uploading', error=NULL, updated_at=$6
|
||||
WHERE furumusic__local_upload.user_id=$2
|
||||
AND furumusic__local_upload.inbox_path=$5
|
||||
AND furumusic__local_upload.size_bytes=$4
|
||||
AND furumusic__local_upload.status IN ('uploading', 'failed')"#,
|
||||
AND furumusic__local_upload.status IN ('uploading', 'failed')
|
||||
AND furumusic__local_upload.target_playlist_id IS NOT DISTINCT FROM $7"#,
|
||||
)
|
||||
.bind(id)
|
||||
.bind(user_id)
|
||||
@@ -160,6 +165,7 @@ pub async fn create(
|
||||
.bind(i64::try_from(size_bytes).unwrap_or(i64::MAX))
|
||||
.bind(inbox_path)
|
||||
.bind(now)
|
||||
.bind(target_playlist_id)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
if created.rows_affected() == 0 {
|
||||
@@ -178,7 +184,7 @@ pub async fn successful_receipt(
|
||||
) -> anyhow::Result<Option<LocalUploadDto>> {
|
||||
let row = sqlx::query_as::<_, LocalUploadRow>(
|
||||
r#"SELECT id, user_id, filename, size_bytes, status, inbox_path, error,
|
||||
created_at, updated_at, completed_at
|
||||
created_at, updated_at, completed_at, target_playlist_id
|
||||
FROM furumusic__local_upload WHERE id=$1 AND user_id=$2 AND inbox_path=$3
|
||||
AND status NOT IN ('uploading','failed')"#,
|
||||
)
|
||||
@@ -205,9 +211,10 @@ pub async fn list(
|
||||
inbox_dir: &str,
|
||||
) -> anyhow::Result<Vec<LocalUploadDto>> {
|
||||
sync_statuses(pool, user_id, inbox_dir).await?;
|
||||
crate::import_playlists::reconcile(pool, Some(user_id)).await?;
|
||||
let rows: Vec<LocalUploadRow> = sqlx::query_as(
|
||||
r#"SELECT id, user_id, filename, size_bytes, status, inbox_path, error,
|
||||
created_at, updated_at, completed_at
|
||||
created_at, updated_at, completed_at, target_playlist_id
|
||||
FROM furumusic__local_upload
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at DESC, id DESC
|
||||
@@ -244,7 +251,7 @@ async fn sync_statuses(pool: &PgPool, user_id: i64, inbox_dir: &str) -> anyhow::
|
||||
|
||||
let rows: Vec<LocalUploadRow> = sqlx::query_as(
|
||||
r#"SELECT id, user_id, filename, size_bytes, status, inbox_path, error,
|
||||
created_at, updated_at, completed_at
|
||||
created_at, updated_at, completed_at, target_playlist_id
|
||||
FROM furumusic__local_upload
|
||||
WHERE user_id = $1 AND status <> 'complete'"#,
|
||||
)
|
||||
@@ -353,7 +360,7 @@ async fn update_status(
|
||||
async fn load(pool: &PgPool, user_id: i64, id: &str) -> anyhow::Result<LocalUploadRow> {
|
||||
sqlx::query_as(
|
||||
r#"SELECT id, user_id, filename, size_bytes, status, inbox_path, error,
|
||||
created_at, updated_at, completed_at
|
||||
created_at, updated_at, completed_at, target_playlist_id
|
||||
FROM furumusic__local_upload WHERE id = $1 AND user_id = $2"#,
|
||||
)
|
||||
.bind(id)
|
||||
|
||||
@@ -5,6 +5,7 @@ mod auth;
|
||||
mod config;
|
||||
mod federation;
|
||||
mod i18n;
|
||||
mod import_playlists;
|
||||
mod jobs;
|
||||
mod lastfm;
|
||||
mod library_cleanup;
|
||||
|
||||
+11
-37
@@ -607,7 +607,7 @@ async fn render_db_metrics(out: &mut String, pool: &PgPool) {
|
||||
out,
|
||||
pool,
|
||||
"furumusic_play_history_total",
|
||||
"SELECT COUNT(*) FROM furumusic__play_history",
|
||||
"SELECT COUNT(*) FROM furumusic__listen_event WHERE qualified",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -821,6 +821,9 @@ const KNOWN_HTTP_ROUTES: &[&str] = &[
|
||||
"/admin/setup",
|
||||
"/admin/v2",
|
||||
"/admin/v2/api/dashboard",
|
||||
"/admin/v2/api/statistics",
|
||||
"/admin/v2/api/users/manage",
|
||||
"/admin/v2/api/jobs/{name}/schedule",
|
||||
"/admin/v2/api/reviews",
|
||||
"/admin/v2/api/reviews/bulk",
|
||||
"/admin/v2/api/users",
|
||||
@@ -844,40 +847,6 @@ const KNOWN_HTTP_ROUTES: &[&str] = &[
|
||||
"/admin/v2/api/library/item/upload-image",
|
||||
"/admin/v2/api/library/bulk",
|
||||
"/admin/v2/api/library/releases/merge",
|
||||
"/admin/debug",
|
||||
"/admin/settings",
|
||||
"/admin/settings/probe",
|
||||
"/admin/users",
|
||||
"/admin/users/new",
|
||||
"/admin/users/{id}/edit",
|
||||
"/admin/users/{id}/delete",
|
||||
"/admin/artists",
|
||||
"/admin/artists/new",
|
||||
"/admin/artists/{id}/edit",
|
||||
"/admin/artists/{id}/delete",
|
||||
"/admin/artists/{id}/available-covers",
|
||||
"/admin/artists/{id}/set-image",
|
||||
"/admin/artists/{id}/upload-image",
|
||||
"/admin/releases",
|
||||
"/admin/releases/new",
|
||||
"/admin/releases/{id}/edit",
|
||||
"/admin/releases/{id}/delete",
|
||||
"/admin/media-files",
|
||||
"/admin/media-files/{id}/delete",
|
||||
"/admin/jobs",
|
||||
"/admin/jobs/metadata_backfill/run-options",
|
||||
"/admin/jobs/{name}/run",
|
||||
"/admin/jobs/{name}/toggle",
|
||||
"/admin/jobs/{name}/cron",
|
||||
"/admin/jobs/{name}/runs/{run_id}",
|
||||
"/admin/jobs/{name}",
|
||||
"/admin/reviews/clear",
|
||||
"/admin/reviews/bulk",
|
||||
"/admin/reviews",
|
||||
"/admin/reviews/{id}",
|
||||
"/admin/reviews/{id}/approve",
|
||||
"/admin/reviews/{id}/reject",
|
||||
"/admin/reviews/{id}/requeue",
|
||||
"/api/player/me",
|
||||
"/api/player/lastfm/status",
|
||||
"/api/player/lastfm/connect",
|
||||
@@ -946,13 +915,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn known_http_route_matches_declared_dynamic_routes() {
|
||||
assert_eq!(known_http_route("/admin/debug"), None);
|
||||
assert_eq!(
|
||||
known_http_route("/admin/v2/api/statistics"),
|
||||
Some("/admin/v2/api/statistics")
|
||||
);
|
||||
assert_eq!(
|
||||
known_http_route("/api/player/stream/42"),
|
||||
Some("/api/player/stream/{track_id}")
|
||||
);
|
||||
assert_eq!(
|
||||
known_http_route("/admin/jobs/metadata_backfill/runs/123"),
|
||||
Some("/admin/jobs/{name}/runs/{run_id}")
|
||||
known_http_route("/admin/v2/api/jobs/metadata_backfill/runs/123"),
|
||||
Some("/admin/v2/api/jobs/{name}/runs/{run_id}")
|
||||
);
|
||||
assert_eq!(
|
||||
known_http_route("/share/playlist/abcDEF123"),
|
||||
|
||||
+49
-84
@@ -78,10 +78,6 @@ impl Artist {
|
||||
Self::objects().all(db).await
|
||||
}
|
||||
|
||||
pub async fn get_by_id(db: &Database, artist_id: i64) -> cot::db::Result<Option<Self>> {
|
||||
Self::get_by_primary_key(db, Auto::Fixed(artist_id)).await
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
@@ -102,41 +98,9 @@ impl Artist {
|
||||
Ok(artist)
|
||||
}
|
||||
|
||||
pub async fn update_name(&mut self, db: &Database, name: &str) -> cot::db::Result<()> {
|
||||
self.name = LimitedString::new(name).unwrap();
|
||||
self.name_sort = LimitedString::new(&normalize_name(name)).unwrap();
|
||||
self.updated_at = now_iso();
|
||||
self.save(db).await
|
||||
}
|
||||
|
||||
pub async fn set_image_file_id(
|
||||
&mut self,
|
||||
db: &Database,
|
||||
file_id: Option<i64>,
|
||||
) -> cot::db::Result<()> {
|
||||
self.image_file_id = file_id;
|
||||
self.updated_at = now_iso();
|
||||
self.save(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_by_id(db: &Database, artist_id: i64) -> cot::db::Result<()> {
|
||||
cot::db::query!(Artist, $id == Auto::Fixed(artist_id))
|
||||
.delete(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn id_val(&self) -> i64 {
|
||||
self.id.unwrap()
|
||||
}
|
||||
|
||||
pub fn name_str(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn is_hidden(&self) -> bool {
|
||||
self.is_hidden
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -286,47 +250,9 @@ impl ReleaseArtist {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_by_artist(db: &Database, artist_id: i64) -> cot::db::Result<Vec<Self>> {
|
||||
cot::db::query!(ReleaseArtist, $artist_id == artist_id)
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn count_by_artist(db: &Database, artist_id: i64) -> cot::db::Result<u64> {
|
||||
cot::db::query!(ReleaseArtist, $artist_id == artist_id)
|
||||
.count(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn set_artists(
|
||||
db: &Database,
|
||||
release_id: i64,
|
||||
artist_ids: &[i64],
|
||||
) -> cot::db::Result<()> {
|
||||
// Remove existing links
|
||||
cot::db::query!(ReleaseArtist, $release_id == release_id)
|
||||
.delete(db)
|
||||
.await?;
|
||||
// Insert new links
|
||||
for (pos, &aid) in artist_ids.iter().enumerate() {
|
||||
let mut link = Self {
|
||||
id: Auto::auto(),
|
||||
release_id,
|
||||
artist_id: aid,
|
||||
position: pos as i32,
|
||||
};
|
||||
link.insert(db).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn artist_id(&self) -> i64 {
|
||||
self.artist_id
|
||||
}
|
||||
|
||||
pub fn release_id(&self) -> i64 {
|
||||
self.release_id
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -377,12 +303,6 @@ pub struct TrackArtist {
|
||||
}
|
||||
|
||||
impl TrackArtist {
|
||||
pub async fn count_by_artist(db: &Database, artist_id: i64) -> cot::db::Result<u64> {
|
||||
cot::db::query!(TrackArtist, $artist_id == artist_id)
|
||||
.count(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
db: &Database,
|
||||
track_id: i64,
|
||||
@@ -556,10 +476,6 @@ pub struct PlaybackState {
|
||||
}
|
||||
|
||||
impl Track {
|
||||
pub async fn list_all(db: &Database) -> cot::db::Result<Vec<Self>> {
|
||||
Self::objects().all(db).await
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
db: &Database,
|
||||
title: &str,
|
||||
@@ -2875,6 +2791,53 @@ pub mod db_migrations {
|
||||
&[Operation::custom(create_metadata_fix_history).build()];
|
||||
}
|
||||
|
||||
#[cot::db::migrations::migration_op]
|
||||
async fn add_listen_statistics_indexes(
|
||||
ctx: migrations::MigrationContext<'_>,
|
||||
) -> cot::db::Result<()> {
|
||||
ctx.db.raw("CREATE INDEX IF NOT EXISTS idx_listen_event_time ON furumusic__listen_event (started_at_ms DESC)").await?;
|
||||
ctx.db.raw("CREATE INDEX IF NOT EXISTS idx_listen_event_content_plays ON furumusic__listen_event (content_id) WHERE qualified").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct M0051AddListenStatisticsIndexes;
|
||||
impl migrations::Migration for M0051AddListenStatisticsIndexes {
|
||||
const APP_NAME: &'static str = "furumusic";
|
||||
const MIGRATION_NAME: &'static str = "m_0051_add_listen_statistics_indexes";
|
||||
const DEPENDENCIES: &'static [migrations::MigrationDependency] =
|
||||
&[migrations::MigrationDependency::migration(
|
||||
"furumusic",
|
||||
"m_0050_create_metadata_fix_history",
|
||||
)];
|
||||
const OPERATIONS: &'static [Operation] =
|
||||
&[Operation::custom(add_listen_statistics_indexes).build()];
|
||||
}
|
||||
|
||||
#[cot::db::migrations::migration_op]
|
||||
async fn add_import_playlist_destinations(
|
||||
ctx: migrations::MigrationContext<'_>,
|
||||
) -> cot::db::Result<()> {
|
||||
ctx.db.raw("ALTER TABLE furumusic__torrent_session ADD COLUMN target_playlist_id BIGINT REFERENCES furumusic__playlist(id) ON DELETE SET NULL").await?;
|
||||
ctx.db.raw("ALTER TABLE furumusic__local_upload ADD COLUMN target_playlist_id BIGINT REFERENCES furumusic__playlist(id) ON DELETE SET NULL").await?;
|
||||
ctx.db.raw("CREATE TABLE furumusic__import_playlist_target (user_id BIGINT NOT NULL, playlist_id BIGINT NOT NULL REFERENCES furumusic__playlist(id) ON DELETE CASCADE, input_path TEXT NOT NULL, source_sha256 TEXT NOT NULL, media_file_id BIGINT REFERENCES furumusic__media_file(id) ON DELETE SET NULL, fulfilled_at TEXT, PRIMARY KEY(user_id,input_path))").await?;
|
||||
ctx.db.raw("CREATE INDEX idx_import_playlist_pending_hash ON furumusic__import_playlist_target(source_sha256) WHERE fulfilled_at IS NULL").await?;
|
||||
Ok(())
|
||||
}
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct M0052AddImportPlaylistDestinations;
|
||||
impl migrations::Migration for M0052AddImportPlaylistDestinations {
|
||||
const APP_NAME: &'static str = "furumusic";
|
||||
const MIGRATION_NAME: &'static str = "m_0052_add_import_playlist_destinations";
|
||||
const DEPENDENCIES: &'static [migrations::MigrationDependency] =
|
||||
&[migrations::MigrationDependency::migration(
|
||||
"furumusic",
|
||||
"m_0051_add_listen_statistics_indexes",
|
||||
)];
|
||||
const OPERATIONS: &'static [Operation] =
|
||||
&[Operation::custom(add_import_playlist_destinations).build()];
|
||||
}
|
||||
|
||||
pub const MIGRATIONS: &[&SyncDynMigration] = &[
|
||||
&M0006CreateMediaFile,
|
||||
&M0007CreateArtist,
|
||||
@@ -2916,5 +2879,7 @@ pub mod db_migrations {
|
||||
&M0048AddPlaybackCoordinationColumns,
|
||||
&M0049AddLastfmLookupState,
|
||||
&M0050CreateMetadataFixHistory,
|
||||
&M0051AddListenStatisticsIndexes,
|
||||
&M0052AddImportPlaylistDestinations,
|
||||
];
|
||||
}
|
||||
@@ -5157,6 +5157,26 @@ async fn local_upload_handler(
|
||||
));
|
||||
}
|
||||
|
||||
let target_playlist_id = match request.headers().get("x-furumusic-target-playlist-id") {
|
||||
None => None,
|
||||
Some(header) => match header
|
||||
.to_str()
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<i64>().ok())
|
||||
.filter(|id| *id > 0)
|
||||
{
|
||||
Some(id) => Some(id),
|
||||
None => {
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, "invalid target playlist ID"));
|
||||
}
|
||||
},
|
||||
};
|
||||
if let Err(error) =
|
||||
crate::import_playlists::validate_target(pool, user.id, target_playlist_id).await
|
||||
{
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, &error.to_string()));
|
||||
}
|
||||
|
||||
let batch_header_id = request
|
||||
.headers()
|
||||
.get("x-furumusic-upload-batch-id")
|
||||
@@ -5273,6 +5293,12 @@ async fn local_upload_handler(
|
||||
.await
|
||||
.map_err(|err| cot::Error::internal(err.to_string()))?
|
||||
{
|
||||
if upload.target_playlist_id != target_playlist_id {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"upload playlist destination cannot change on retry",
|
||||
));
|
||||
}
|
||||
return Json(LocalUploadResponse { ok: true, upload }).into_response();
|
||||
}
|
||||
if let Err(err) = crate::local_uploads::create(
|
||||
@@ -5282,12 +5308,23 @@ async fn local_upload_handler(
|
||||
&display_name,
|
||||
bytes.len() as u64,
|
||||
&inbox_path,
|
||||
target_playlist_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, &err.to_string()));
|
||||
}
|
||||
|
||||
if let Some(playlist_id) = target_playlist_id {
|
||||
use sha2::{Digest, Sha256};
|
||||
let sha256 = format!("{:x}", Sha256::digest(&bytes));
|
||||
if let Err(error) =
|
||||
crate::import_playlists::register(pool, user.id, playlist_id, &inbox_path, &sha256).await
|
||||
{
|
||||
return Ok(json_error(StatusCode::BAD_REQUEST, &error.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
let write_result = crate::local_uploads::save_file(&destination, &upload_id, &bytes).await;
|
||||
if let Err(err) = write_result {
|
||||
let message = format!("could not save uploaded file: {err}");
|
||||
@@ -9772,6 +9809,7 @@ impl App for PlayerApp {
|
||||
pg_pool,
|
||||
&path.0.id,
|
||||
json.0.selected_files,
|
||||
json.0.target_playlist_id,
|
||||
live_config.agent_inbox_dir,
|
||||
user.id,
|
||||
proxy_url.as_deref(),
|
||||
|
||||
+9
-82
@@ -80,19 +80,8 @@ impl ScheduledJob {
|
||||
cron_expression: &str,
|
||||
) -> cot::db::Result<Self> {
|
||||
if let Some(mut existing) = Self::get_by_name(db, name).await? {
|
||||
// Update cron expression and description if they changed
|
||||
// The registered schedule is a default for new jobs; retain admin overrides.
|
||||
let mut changed = false;
|
||||
if existing.cron_expression.as_str() != cron_expression {
|
||||
tracing::info!(
|
||||
job = name,
|
||||
old = existing.cron_expression.as_str(),
|
||||
new = cron_expression,
|
||||
"Updating cron expression"
|
||||
);
|
||||
existing.cron_expression = LimitedString::new(cron_expression).unwrap();
|
||||
existing.next_run_at = compute_next_run(cron_expression);
|
||||
changed = true;
|
||||
}
|
||||
if existing.description != description {
|
||||
existing.description = description.to_owned();
|
||||
changed = true;
|
||||
@@ -131,10 +120,6 @@ impl ScheduledJob {
|
||||
&self.cron_expression
|
||||
}
|
||||
|
||||
pub fn enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
|
||||
pub fn last_run_at_str(&self) -> &str {
|
||||
self.last_run_at.as_deref().unwrap_or("")
|
||||
}
|
||||
@@ -683,69 +668,6 @@ impl ProcessingStats {
|
||||
.await?;
|
||||
Ok(all.into_iter().next())
|
||||
}
|
||||
|
||||
pub async fn list_by_review_ids(
|
||||
pool: &sqlx::PgPool,
|
||||
ids: &[i64],
|
||||
) -> anyhow::Result<HashMap<i64, ProcessingStatsRow>> {
|
||||
if ids.is_empty() {
|
||||
return Ok(HashMap::new());
|
||||
}
|
||||
// Build comma-separated ID list
|
||||
let id_list: String = ids
|
||||
.iter()
|
||||
.map(|id| id.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let query = format!(
|
||||
"SELECT pending_review_id, model_name, llm_duration_ms, prompt_tokens, completion_tokens \
|
||||
FROM furumusic__processing_stats WHERE pending_review_id IN ({id_list})"
|
||||
);
|
||||
let rows = sqlx::query_as::<_, ProcessingStatsRow>(&query)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
let map = rows.into_iter().map(|r| (r.pending_review_id, r)).collect();
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
pub fn model_name_str(&self) -> &str {
|
||||
&self.model_name
|
||||
}
|
||||
|
||||
pub fn duration_display(&self) -> String {
|
||||
if self.llm_duration_ms >= 1000 {
|
||||
format!("{:.1}s", self.llm_duration_ms as f64 / 1000.0)
|
||||
} else {
|
||||
format!("{}ms", self.llm_duration_ms)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tokens_display(&self) -> String {
|
||||
format!("{}/{}", self.prompt_tokens, self.completion_tokens)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, sqlx::FromRow)]
|
||||
pub struct ProcessingStatsRow {
|
||||
pub pending_review_id: i64,
|
||||
pub model_name: String,
|
||||
pub llm_duration_ms: i64,
|
||||
pub prompt_tokens: i64,
|
||||
pub completion_tokens: i64,
|
||||
}
|
||||
|
||||
impl ProcessingStatsRow {
|
||||
pub fn duration_display(&self) -> String {
|
||||
if self.llm_duration_ms >= 1000 {
|
||||
format!("{:.1}s", self.llm_duration_ms as f64 / 1000.0)
|
||||
} else {
|
||||
format!("{}ms", self.llm_duration_ms)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tokens_display(&self) -> String {
|
||||
format!("{}/{}", self.prompt_tokens, self.completion_tokens)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1288,8 +1210,13 @@ impl SchedulerHandle {
|
||||
}
|
||||
}
|
||||
|
||||
// Add new cron job
|
||||
self.add_cron_job(job_name, new_cron).await?;
|
||||
// Editing a disabled job must not start it.
|
||||
let job = ScheduledJob::get_by_name(&self.shared_db, job_name)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("job not found: {job_name}"))?;
|
||||
if job.enabled {
|
||||
self.add_cron_job(job_name, new_cron).await?;
|
||||
}
|
||||
|
||||
// Update DB
|
||||
if let Ok(Some(mut sched_job)) = ScheduledJob::get_by_name(&self.shared_db, job_name).await
|
||||
@@ -1297,7 +1224,7 @@ impl SchedulerHandle {
|
||||
sched_job.cron_expression = LimitedString::new(new_cron).unwrap();
|
||||
sched_job.next_run_at = compute_next_run(new_cron);
|
||||
sched_job.updated_at = now_iso();
|
||||
let _ = sched_job.save(&self.shared_db).await;
|
||||
sched_job.save(&self.shared_db).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
+178
-4
@@ -58,6 +58,7 @@ pub struct TorrentJobDto {
|
||||
pub created_at: Option<String>,
|
||||
pub updated_at: Option<String>,
|
||||
pub completed_at: Option<String>,
|
||||
pub target_playlist_id: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
@@ -93,6 +94,7 @@ pub struct TorrentPreviewRequest {
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct TorrentStartRequest {
|
||||
pub target_playlist_id: Option<i64>,
|
||||
pub selected_files: Vec<usize>,
|
||||
}
|
||||
|
||||
@@ -153,6 +155,7 @@ struct TorrentJob {
|
||||
created_at: String,
|
||||
updated_at: String,
|
||||
completed_at: Option<String>,
|
||||
target_playlist_id: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromRow)]
|
||||
@@ -176,6 +179,7 @@ struct TorrentSessionRow {
|
||||
created_at: String,
|
||||
updated_at: String,
|
||||
completed_at: Option<String>,
|
||||
target_playlist_id: Option<i64>,
|
||||
}
|
||||
|
||||
impl TorrentSessionRow {
|
||||
@@ -226,6 +230,7 @@ impl TorrentSessionRow {
|
||||
let peer_stats = live.map(|l| &l.snapshot.peer_stats);
|
||||
|
||||
TorrentJobDto {
|
||||
target_playlist_id: self.target_playlist_id,
|
||||
id: self.id.clone(),
|
||||
name: self.name.clone(),
|
||||
info_hash: self.info_hash.clone(),
|
||||
@@ -275,6 +280,7 @@ impl TorrentSessionRow {
|
||||
status: TorrentJobStatus::from_str(&self.status),
|
||||
output_dir: temp_root.join(&id).join("download"),
|
||||
selected_files,
|
||||
target_playlist_id: self.target_playlist_id,
|
||||
handle: None,
|
||||
downloaded_bytes: i64_to_u64(self.downloaded_bytes),
|
||||
uploaded_bytes: i64_to_u64(self.uploaded_bytes),
|
||||
@@ -341,6 +347,7 @@ impl TorrentJob {
|
||||
let peer_stats = live.map(|l| &l.snapshot.peer_stats);
|
||||
|
||||
TorrentJobDto {
|
||||
target_playlist_id: self.target_playlist_id,
|
||||
id: self.id.clone(),
|
||||
name: self.name.clone(),
|
||||
info_hash: self.info_hash.clone(),
|
||||
@@ -430,7 +437,7 @@ impl TorrentService {
|
||||
r#"SELECT id, user_id, name, info_hash, source_kind, source_label, torrent_bytes,
|
||||
files_json, selected_files_json, status, total_size, selected_size,
|
||||
downloaded_bytes, uploaded_bytes, progress_percent, error,
|
||||
created_at, updated_at, completed_at
|
||||
created_at, updated_at, completed_at, target_playlist_id
|
||||
FROM furumusic__torrent_session
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at DESC, id DESC
|
||||
@@ -441,6 +448,7 @@ impl TorrentService {
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
crate::import_playlists::reconcile(pool, Some(user_id)).await?;
|
||||
let handles = {
|
||||
let jobs = self.jobs.lock().await;
|
||||
jobs.iter()
|
||||
@@ -604,6 +612,7 @@ impl TorrentService {
|
||||
status: TorrentJobStatus::Preview,
|
||||
output_dir,
|
||||
selected_files,
|
||||
target_playlist_id: None,
|
||||
handle: None,
|
||||
downloaded_bytes: 0,
|
||||
uploaded_bytes: 0,
|
||||
@@ -727,6 +736,7 @@ impl TorrentService {
|
||||
status: TorrentJobStatus::Preview,
|
||||
output_dir,
|
||||
selected_files,
|
||||
target_playlist_id: None,
|
||||
handle: None,
|
||||
downloaded_bytes: 0,
|
||||
uploaded_bytes: 0,
|
||||
@@ -794,6 +804,7 @@ impl TorrentService {
|
||||
pool: &PgPool,
|
||||
id: &str,
|
||||
selected_files: Vec<usize>,
|
||||
target_playlist_id: Option<i64>,
|
||||
inbox_dir: String,
|
||||
uploader_user_id: i64,
|
||||
proxy_url: Option<&str>,
|
||||
@@ -806,6 +817,8 @@ impl TorrentService {
|
||||
}
|
||||
let inbox_dir = validate_inbox_dir(&inbox_dir)?;
|
||||
|
||||
crate::import_playlists::validate_target(pool, uploader_user_id, target_playlist_id)
|
||||
.await?;
|
||||
self.ensure_memory_job(pool, uploader_user_id, id).await?;
|
||||
|
||||
let (torrent_bytes, output_dir, selected_size) = {
|
||||
@@ -823,7 +836,13 @@ impl TorrentService {
|
||||
bail!("torrent job is already running");
|
||||
}
|
||||
validate_selection(&job.files, &selected_files)?;
|
||||
if job.status != TorrentJobStatus::Preview
|
||||
&& job.target_playlist_id != target_playlist_id
|
||||
{
|
||||
bail!("playlist destination cannot change after a torrent has started");
|
||||
}
|
||||
job.status = TorrentJobStatus::Downloading;
|
||||
job.target_playlist_id = target_playlist_id;
|
||||
job.selected_files = selected_files.clone();
|
||||
job.downloaded_bytes = 0;
|
||||
job.uploaded_bytes = 0;
|
||||
@@ -1032,9 +1051,14 @@ impl TorrentService {
|
||||
inbox_dir: &Path,
|
||||
uploader_user_id: i64,
|
||||
) -> anyhow::Result<()> {
|
||||
let (name, files, selected_files, output_dir) = {
|
||||
// The playlist may have been deleted while this torrent was downloading.
|
||||
let persisted_target = load_row(pool, uploader_user_id, id)
|
||||
.await?
|
||||
.target_playlist_id;
|
||||
let (name, files, selected_files, output_dir, target_playlist_id) = {
|
||||
let mut jobs = self.jobs.lock().await;
|
||||
let job = jobs.get_mut(id).context("torrent job not found")?;
|
||||
job.target_playlist_id = persisted_target;
|
||||
job.refresh_progress();
|
||||
job.status = TorrentJobStatus::Moving;
|
||||
job.updated_at = now_string();
|
||||
@@ -1043,6 +1067,7 @@ impl TorrentService {
|
||||
job.files.clone(),
|
||||
job.selected_files.clone(),
|
||||
job.output_dir.clone(),
|
||||
job.target_playlist_id,
|
||||
)
|
||||
};
|
||||
|
||||
@@ -1052,6 +1077,7 @@ impl TorrentService {
|
||||
let destination_root = inbox_dir
|
||||
.join("user_uploads")
|
||||
.join(uploader_user_id.to_string())
|
||||
.join(format!("torrent-{id}"))
|
||||
.join(sanitize_path_component(&name));
|
||||
tokio::fs::create_dir_all(&destination_root).await?;
|
||||
|
||||
@@ -1064,6 +1090,22 @@ impl TorrentService {
|
||||
if let Some(parent) = destination.parent() {
|
||||
tokio::fs::create_dir_all(parent).await?;
|
||||
}
|
||||
if let Some(playlist_id) = target_playlist_id
|
||||
&& crate::jobs::inbox_discover::is_audio_file(&file.name)
|
||||
{
|
||||
let input_path =
|
||||
crate::media_paths::path_for_root(&inbox_dir.to_string_lossy(), &destination)
|
||||
.context("torrent destination escaped inbox")?;
|
||||
let sha = crate::agent::mover::sha256_file(&source).await?;
|
||||
crate::import_playlists::register(
|
||||
pool,
|
||||
uploader_user_id,
|
||||
playlist_id,
|
||||
&input_path,
|
||||
&sha,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
move_file(&source, &destination).await?;
|
||||
}
|
||||
|
||||
@@ -1103,7 +1145,7 @@ async fn load_row(pool: &PgPool, user_id: i64, id: &str) -> anyhow::Result<Torre
|
||||
r#"SELECT id, user_id, name, info_hash, source_kind, source_label, torrent_bytes,
|
||||
files_json, selected_files_json, status, total_size, selected_size,
|
||||
downloaded_bytes, uploaded_bytes, progress_percent, error,
|
||||
created_at, updated_at, completed_at
|
||||
created_at, updated_at, completed_at, target_playlist_id
|
||||
FROM furumusic__torrent_session
|
||||
WHERE id = $1 AND user_id = $2"#,
|
||||
)
|
||||
@@ -1244,13 +1286,15 @@ async fn mark_job_started(
|
||||
progress_percent = 0,
|
||||
error = NULL,
|
||||
updated_at = $4,
|
||||
completed_at = NULL
|
||||
completed_at = NULL,
|
||||
target_playlist_id = $5
|
||||
WHERE id = $1"#,
|
||||
)
|
||||
.bind(id)
|
||||
.bind(serde_json::to_string(selected_files)?)
|
||||
.bind(u64_to_i64(dto.selected_size))
|
||||
.bind(now_string())
|
||||
.bind(dto.target_playlist_id)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
@@ -1434,3 +1478,133 @@ async fn move_file(source: &Path, destination: &Path) -> anyhow::Result<()> {
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod playlist_tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires disposable PostgreSQL in FURU_IMPORT_TEST_DATABASE_URL"]
|
||||
async fn torrent_playlist_survives_restore_and_registers_selected_audio() {
|
||||
let pool = sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect(&std::env::var("FURU_IMPORT_TEST_DATABASE_URL").unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::raw_sql(r#"
|
||||
CREATE TEMP TABLE furumusic__playlist(id bigint PRIMARY KEY,owner_id bigint);
|
||||
INSERT INTO furumusic__playlist VALUES (10,1);
|
||||
CREATE TEMP TABLE furumusic__import_playlist_target(user_id bigint,playlist_id bigint,input_path text,source_sha256 text,media_file_id bigint,fulfilled_at text,PRIMARY KEY(user_id,input_path));
|
||||
CREATE TEMP TABLE furumusic__torrent_session(
|
||||
id text PRIMARY KEY,user_id bigint,name text,info_hash text,source_kind text,source_label text,torrent_bytes bytea,
|
||||
files_json text,selected_files_json text,status text,total_size bigint DEFAULT 0,selected_size bigint DEFAULT 0,
|
||||
downloaded_bytes bigint DEFAULT 0,uploaded_bytes bigint DEFAULT 0,progress_percent float8 DEFAULT 0,
|
||||
error text,created_at text,updated_at text,completed_at text,target_playlist_id bigint);
|
||||
"#).execute(&pool).await.unwrap();
|
||||
let root = std::env::temp_dir().join(format!("furumi-torrent-test-{}", Uuid::new_v4()));
|
||||
let mut service = TorrentService::new(Arc::new(OnceCell::new()));
|
||||
service.temp_root = root.join("torrents");
|
||||
let output = service.temp_root.join("job/download");
|
||||
tokio::fs::create_dir_all(&output).await.unwrap();
|
||||
let files: Vec<_> = ["a.flac", "b.mp3", "cover.jpg"]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, name)| TorrentFileDto {
|
||||
index,
|
||||
name: name.to_string(),
|
||||
components: vec![name.to_string()],
|
||||
length: 3,
|
||||
selected: index != 1,
|
||||
})
|
||||
.collect();
|
||||
for file in &files {
|
||||
tokio::fs::write(output.join(&file.name), b"abc")
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
let mut job = TorrentJob {
|
||||
id: "job".into(),
|
||||
user_id: 1,
|
||||
name: "Soundtrack".into(),
|
||||
info_hash: "hash".into(),
|
||||
source_kind: "torrent_file".into(),
|
||||
source_label: None,
|
||||
torrent_bytes: vec![],
|
||||
files,
|
||||
selected_files: vec![0, 2],
|
||||
target_playlist_id: None,
|
||||
output_dir: output,
|
||||
status: TorrentJobStatus::Preview,
|
||||
handle: None,
|
||||
downloaded_bytes: 0,
|
||||
uploaded_bytes: 0,
|
||||
progress_percent: 0.0,
|
||||
error: None,
|
||||
created_at: now_string(),
|
||||
updated_at: now_string(),
|
||||
completed_at: None,
|
||||
};
|
||||
sqlx::query("INSERT INTO furumusic__torrent_session (id,user_id,name,info_hash,source_kind,torrent_bytes,files_json,selected_files_json,status,created_at,updated_at) VALUES ('job',1,'Soundtrack','hash','torrent_file',$1,$2,'[0,2]','resolving',$3,$3)")
|
||||
.bind(Vec::<u8>::new()).bind(serde_json::to_string(&job.files).unwrap()).bind(now_string()).execute(&pool).await.unwrap();
|
||||
update_resolved_job(&pool, &job).await.unwrap();
|
||||
job.target_playlist_id = Some(10);
|
||||
mark_job_started(&pool, "job", &[0, 2], &job.dto())
|
||||
.await
|
||||
.unwrap();
|
||||
let restored = load_row(&pool, 1, "job")
|
||||
.await
|
||||
.unwrap()
|
||||
.into_job(&service.temp_root)
|
||||
.unwrap();
|
||||
assert_eq!(restored.target_playlist_id, Some(10));
|
||||
assert_eq!(restored.dto().target_playlist_id, Some(10));
|
||||
service.jobs.lock().await.insert("job".into(), restored);
|
||||
let inbox = root.join("inbox");
|
||||
service
|
||||
.finalize_completed(&pool, "job", &inbox, 1)
|
||||
.await
|
||||
.unwrap();
|
||||
let paths: Vec<String> =
|
||||
sqlx::query_scalar("SELECT input_path FROM furumusic__import_playlist_target")
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(paths, vec!["user_uploads/1/torrent-job/Soundtrack/a.flac"]);
|
||||
assert!(inbox.join(&paths[0]).exists());
|
||||
assert!(
|
||||
!inbox
|
||||
.join("user_uploads/1/torrent-job/Soundtrack/b.mp3")
|
||||
.exists()
|
||||
);
|
||||
assert_eq!(load_row(&pool, 1, "job").await.unwrap().status, "complete");
|
||||
// Deleting the destination while downloading clears its persisted FK;
|
||||
// an older in-memory choice must not prevent the audio from being imported.
|
||||
sqlx::query("UPDATE furumusic__torrent_session SET target_playlist_id=NULL WHERE id='job'")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("DELETE FROM furumusic__playlist WHERE id=10")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let source = service.temp_root.join("job/download");
|
||||
tokio::fs::create_dir_all(&source).await.unwrap();
|
||||
tokio::fs::write(source.join("a.flac"), b"abc")
|
||||
.await
|
||||
.unwrap();
|
||||
service
|
||||
.finalize_completed(&pool, "job", &inbox, 1)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
service
|
||||
.memory_job_dto("job")
|
||||
.await
|
||||
.unwrap()
|
||||
.target_playlist_id,
|
||||
None
|
||||
);
|
||||
tokio::fs::remove_dir_all(root).await.unwrap();
|
||||
pool.close().await;
|
||||
}
|
||||
}
|
||||
-14
@@ -26,11 +26,6 @@ pub struct User {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
impl User {
|
||||
/// List all users.
|
||||
pub async fn list_all(db: &Database) -> cot::db::Result<Vec<Self>> {
|
||||
Self::objects().all(db).await
|
||||
}
|
||||
|
||||
/// Get a user by primary key.
|
||||
pub async fn get_by_id(db: &Database, user_id: i64) -> cot::db::Result<Option<Self>> {
|
||||
Self::get_by_primary_key(db, Auto::Fixed(user_id)).await
|
||||
@@ -121,21 +116,12 @@ impl User {
|
||||
pub fn username_str(&self) -> &str {
|
||||
&self.username
|
||||
}
|
||||
pub fn email_str(&self) -> String {
|
||||
self.email
|
||||
.as_ref()
|
||||
.map(|e| e.to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
pub fn display_name_str(&self) -> String {
|
||||
self.display_name
|
||||
.as_ref()
|
||||
.map(|d| d.to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
pub fn role_str(&self) -> &str {
|
||||
&self.role
|
||||
}
|
||||
pub fn is_active(&self) -> bool {
|
||||
self.is_active
|
||||
}
|
||||
|
||||
+6
-82
@@ -2089,91 +2089,15 @@ async fn sync_target_playlist_tracks_for_user(pool: &PgPool, user_id: i64) -> an
|
||||
.await?;
|
||||
|
||||
for (playlist_id, owner_id, track_id, _, _) in candidates {
|
||||
append_track_to_target_playlist(pool, owner_id, playlist_id, track_id).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn append_track_to_target_playlist(
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
playlist_id: i64,
|
||||
track_id: i64,
|
||||
) -> anyhow::Result<bool> {
|
||||
let mut transaction = pool.begin().await?;
|
||||
let playlist: Option<i64> = sqlx::query_scalar(
|
||||
r#"SELECT id FROM furumusic__playlist
|
||||
WHERE id = $1 AND owner_id = $2
|
||||
FOR UPDATE"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.bind(user_id)
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await?;
|
||||
if playlist.is_none() {
|
||||
transaction.rollback().await?;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let already_present: bool = sqlx::query_scalar(
|
||||
r#"SELECT EXISTS (
|
||||
SELECT 1 FROM furumusic__playlist_track
|
||||
WHERE playlist_id = $1 AND track_id = $2
|
||||
)"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.bind(track_id)
|
||||
.fetch_one(&mut *transaction)
|
||||
.await?;
|
||||
if already_present {
|
||||
transaction.commit().await?;
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let position: i64 = sqlx::query_scalar(
|
||||
r#"SELECT COALESCE(MAX(position), -1)::bigint + 1
|
||||
FROM furumusic__playlist_track WHERE playlist_id = $1"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.fetch_one(&mut *transaction)
|
||||
.await?;
|
||||
let position = i32::try_from(position).context("target playlist has too many tracks")?;
|
||||
let now = now_string();
|
||||
sqlx::query(
|
||||
r#"INSERT INTO furumusic__playlist_track
|
||||
(playlist_id, track_id, position, added_at, added_by_user_id)
|
||||
VALUES ($1, $2, $3, $4, $5)"#,
|
||||
)
|
||||
.bind(playlist_id)
|
||||
.bind(track_id)
|
||||
.bind(position)
|
||||
.bind(&now)
|
||||
.bind(user_id)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
sqlx::query("UPDATE furumusic__playlist SET updated_at = $1 WHERE id = $2")
|
||||
.bind(&now)
|
||||
.bind(playlist_id)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
transaction.commit().await?;
|
||||
|
||||
if let Err(error) = crate::federation::devices::record_playlist_tracks_added(
|
||||
pool,
|
||||
user_id,
|
||||
playlist_id,
|
||||
&[track_id],
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(
|
||||
crate::import_playlists::append_track_to_target_playlist(
|
||||
pool,
|
||||
owner_id,
|
||||
playlist_id,
|
||||
track_id,
|
||||
%error,
|
||||
"federation operation for an automatic YouTube playlist addition was not recorded"
|
||||
);
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
Ok(true)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn refresh_parent(pool: &PgPool, job_id: &str) -> anyhow::Result<()> {
|
||||
|
||||
Reference in new issue
Block a user