Added user attribution

This commit is contained in:
2026-05-25 23:04:58 +03:00
parent 8530016d35
commit 5f925be29b
13 changed files with 901 additions and 443 deletions
+5 -1
View File
@@ -140,7 +140,9 @@ impl Job for InboxDiscoverJob {
// Parse path hints
let relative = file_path.strip_prefix(inbox).unwrap_or(file_path);
let hints = crate::agent::path_hints::parse(relative);
let uploader = crate::jobs::uploader_from_relative_path(&ctx.pool, relative).await;
let hinted_relative = crate::jobs::strip_user_upload_prefix(relative);
let hints = crate::agent::path_hints::parse(&hinted_relative);
// Build context JSON
let context = serde_json::json!({
@@ -156,6 +158,8 @@ impl Job for InboxDiscoverJob {
"audio_bitrate": raw_meta.audio_bitrate,
"audio_sample_rate": raw_meta.audio_sample_rate,
"audio_bit_depth": raw_meta.audio_bit_depth,
"uploaded_by_user_id": uploader.user_id,
"uploader_name": uploader.name,
"path_title": hints.title,
"path_artist": hints.artist,
"path_album": hints.album,
+20 -1
View File
@@ -337,7 +337,9 @@ async fn process_folder_batch(
// Parse path hints
let relative = file_path.strip_prefix(inbox_path).unwrap_or(file_path);
let hints = crate::agent::path_hints::parse(relative);
let uploader = crate::jobs::uploader_from_relative_path(pool, relative).await;
let hinted_relative = crate::jobs::strip_user_upload_prefix(relative);
let hints = crate::agent::path_hints::parse(&hinted_relative);
if let Some(context_obj) = context.as_object_mut() {
context_obj.insert(
"audio_bitrate".to_owned(),
@@ -351,6 +353,15 @@ async fn process_folder_batch(
"audio_bit_depth".to_owned(),
serde_json::json!(raw_meta.audio_bit_depth),
);
if !context_obj.contains_key("uploaded_by_user_id") {
context_obj.insert(
"uploaded_by_user_id".to_owned(),
serde_json::json!(uploader.user_id),
);
}
if !context_obj.contains_key("uploader_name") {
context_obj.insert("uploader_name".to_owned(), serde_json::json!(uploader.name));
}
}
prepared.push(PreparedFile {
@@ -737,6 +748,12 @@ pub async fn finalize_approved(
.get("audio_bit_depth")
.and_then(|v| v.as_i64())
.and_then(|v| i32::try_from(v).ok());
let uploaded_by_user_id = context.get("uploaded_by_user_id").and_then(|v| v.as_i64());
let uploader_name = context
.get("uploader_name")
.and_then(|v| v.as_str())
.filter(|value| !value.trim().is_empty())
.unwrap_or("UFO");
let source_path = Path::new(input_path_str);
let original_filename = source_path
@@ -805,6 +822,8 @@ pub async fn finalize_approved(
audio_bitrate,
audio_sample_rate,
audio_bit_depth,
uploaded_by_user_id,
Some(uploader_name),
)
.await
.map_err(|e| anyhow::anyhow!("failed to create media file: {e}"))?;
+70
View File
@@ -4,3 +4,73 @@ pub mod cover_backfill;
pub mod inbox_discover;
pub mod inbox_process;
pub mod metadata_backfill;
use std::path::{Component, Path, PathBuf};
#[derive(Debug, Clone)]
pub struct UploaderAttribution {
pub user_id: Option<i64>,
pub name: String,
}
impl UploaderAttribution {
pub fn unknown() -> Self {
Self {
user_id: None,
name: "UFO".to_string(),
}
}
}
pub fn strip_user_upload_prefix(relative_path: &Path) -> PathBuf {
let components: Vec<_> = relative_path.components().collect();
if components.len() >= 3
&& matches!(components[0], Component::Normal(value) if value == "user_uploads")
{
components[2..].iter().collect()
} else {
relative_path.to_path_buf()
}
}
pub async fn uploader_from_relative_path(
pool: &sqlx::PgPool,
relative_path: &Path,
) -> UploaderAttribution {
let components: Vec<_> = relative_path.components().collect();
let Some(Component::Normal(root)) = components.first() else {
return UploaderAttribution::unknown();
};
if *root != "user_uploads" {
return UploaderAttribution::unknown();
}
let Some(Component::Normal(user_id_os)) = components.get(1) else {
return UploaderAttribution::unknown();
};
let Some(user_id_str) = user_id_os.to_str() else {
return UploaderAttribution::unknown();
};
let Ok(user_id) = user_id_str.parse::<i64>() else {
return UploaderAttribution::unknown();
};
let name: Option<String> = sqlx::query_scalar(
r#"SELECT COALESCE(NULLIF(display_name, ''), username)::text
FROM furumusic__user
WHERE id = $1 AND is_active = true"#,
)
.bind(user_id)
.fetch_optional(pool)
.await
.ok()
.flatten();
match name {
Some(name) if !name.trim().is_empty() => UploaderAttribution {
user_id: Some(user_id),
name,
},
_ => UploaderAttribution::unknown(),
}
}