fix: identity binding + JSON endpoint + Phase 5 test script
- identity_binding.rs: fix i32->i64 type mismatch, COALESCE name column - identity_api.rs: get_identity_json fallback to DB if file missing - test_m5api_phase5.sh: fixed variable expansion, updated request bodies - Phase 5: 21/23 passed (2 known: multipart + proxy 404)
This commit is contained in:
@@ -792,6 +792,7 @@ async fn get_profile_image(
|
||||
}
|
||||
|
||||
async fn get_identity_json(
|
||||
State(state): State<crate::api::server::AppState>,
|
||||
Path(identity_uuid): Path<String>,
|
||||
) -> Result<(StatusCode, [(String, String); 1], Vec<u8>), StatusCode> {
|
||||
let clean = identity_uuid.replace('-', "");
|
||||
@@ -800,6 +801,8 @@ async fn get_identity_json(
|
||||
} else {
|
||||
identity_uuid.clone()
|
||||
};
|
||||
|
||||
// 1. Try file system first
|
||||
for u in [&clean, &identity_uuid, &with_hyphens] {
|
||||
let p = crate::core::identity::storage::identity_file_path(u);
|
||||
if p.exists() {
|
||||
@@ -811,7 +814,46 @@ async fn get_identity_json(
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(StatusCode::NOT_FOUND)
|
||||
|
||||
// 2. Fallback: Generate JSON from DB
|
||||
use crate::core::identity::storage::{IdentityFile, FileBinding};
|
||||
let record = state.db.get_identity_by_uuid(&clean).await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::NOT_FOUND)?;
|
||||
|
||||
let id = record.id as i64;
|
||||
let bindings: Vec<FileBinding> = {
|
||||
let fd_table = crate::core::db::schema::table_name("face_detections");
|
||||
let rows = sqlx::query_as::<_, (String, Vec<i32>, i64)>(
|
||||
&format!("SELECT fd.file_uuid, COALESCE(array_agg(DISTINCT fd.trace_id) FILTER (WHERE fd.trace_id IS NOT NULL), '{{}}'::int[]), COUNT(*)::bigint FROM {} fd WHERE fd.identity_id = $1 GROUP BY fd.file_uuid ORDER BY fd.file_uuid", fd_table)
|
||||
).bind(id).fetch_all(state.db.pool()).await.unwrap_or_default();
|
||||
rows.into_iter().map(|(fu, tids, cnt)| FileBinding {
|
||||
file_uuid: fu, trace_ids: tids, face_count: cnt,
|
||||
}).collect()
|
||||
};
|
||||
|
||||
let fmt_time = |dt: Option<chrono::DateTime<chrono::Utc>>| -> String {
|
||||
dt.map(|d| d.to_rfc3339()).unwrap_or_else(|| chrono::Utc::now().to_rfc3339())
|
||||
};
|
||||
|
||||
let file = IdentityFile {
|
||||
version: 1,
|
||||
identity_uuid: record.uuid.clone(),
|
||||
name: record.name.clone(),
|
||||
identity_type: record.identity_type.clone(),
|
||||
source: record.source.clone(),
|
||||
status: record.status.clone(),
|
||||
tmdb_id: record.tmdb_id,
|
||||
tmdb_profile: record.tmdb_profile.clone(),
|
||||
metadata: record.metadata.clone(),
|
||||
file_bindings: bindings,
|
||||
created_at: fmt_time(record.created_at),
|
||||
updated_at: fmt_time(record.updated_at),
|
||||
};
|
||||
|
||||
let json = serde_json::to_string_pretty(&file).unwrap_or_default();
|
||||
let bytes = json.into_bytes();
|
||||
Ok((StatusCode::OK, [("content-type".to_string(), "application/json".to_string())], bytes))
|
||||
}
|
||||
|
||||
// ── Experiment: Identity Text Search ──────────────────────────
|
||||
|
||||
@@ -76,8 +76,8 @@ pub async fn bind_identity(
|
||||
})?;
|
||||
|
||||
// Get identity_id from identity_uuid
|
||||
let identity_row: Option<(i32, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, name FROM {} WHERE uuid = $1::uuid",
|
||||
let identity_row: Option<(i64, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, COALESCE(real_name, actor_name) AS name FROM {} WHERE uuid = $1::uuid",
|
||||
id_table
|
||||
))
|
||||
.bind(&identity_uuid)
|
||||
@@ -170,7 +170,7 @@ pub async fn unbind_identity(
|
||||
|
||||
/// V4.0 合併:將 identity A 合併入 identity B,A 被刪除
|
||||
pub async fn merge_identities(
|
||||
Path(from_uuid): Path<String>,
|
||||
Path(identity_uuid): Path<String>,
|
||||
Json(req): Json<MergeIdentitiesRequest>,
|
||||
) -> Result<Json<ApiResponse<serde_json::Value>>, (StatusCode, Json<serde_json::Value>)> {
|
||||
let face_table = crate::core::db::schema::table_name("face_detections");
|
||||
@@ -186,11 +186,11 @@ pub async fn merge_identities(
|
||||
})?;
|
||||
|
||||
// Get IDs for both identities
|
||||
let from_row: Option<(i32, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, name FROM {} WHERE uuid = $1::uuid",
|
||||
let from_row: Option<(i64, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, COALESCE(real_name, actor_name) AS name FROM {} WHERE uuid = $1::uuid",
|
||||
id_table
|
||||
))
|
||||
.bind(&from_uuid)
|
||||
.bind(&identity_uuid)
|
||||
.fetch_optional(&db)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
@@ -204,8 +204,8 @@ pub async fn merge_identities(
|
||||
Json(serde_json::json!({"error": "Source identity not found"})),
|
||||
))?;
|
||||
|
||||
let into_row: Option<(i32, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, name FROM {} WHERE uuid = $1::uuid",
|
||||
let into_row: Option<(i64, String)> = sqlx::query_as(&format!(
|
||||
"SELECT id, COALESCE(real_name, actor_name) AS name FROM {} WHERE uuid = $1::uuid",
|
||||
id_table
|
||||
))
|
||||
.bind(&req.into_uuid)
|
||||
@@ -301,7 +301,7 @@ pub fn identity_binding_routes() -> Router<crate::api::server::AppState> {
|
||||
post(unbind_identity),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/identity/:from_uuid/mergeinto",
|
||||
"/api/v1/identity/:identity_uuid/mergeinto",
|
||||
post(merge_identities),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user