use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use chrono::Utc;
use serde::Deserialize;
use serde_json::{json, Value};
use crate::{
error::AppError, git, hooks::HookJob, order, routes::AuthorRequest, state::AppState,
util::run_blocking, validate,
};
#[derive(Deserialize)]
pub struct WriteOrderRequest {
pub author: AuthorRequest,
pub order: Vec<String>,
pub allow_hidden_files: Option<bool>,
pub message: Option<String>,
}
#[derive(Deserialize)]
pub struct DeleteOrderRequest {
pub author: AuthorRequest,
pub message: Option<String>,
}
pub async fn read_order_root(
State(state): State<AppState>,
Path((collection_id, tenant_id)): Path<(String, String)>,
) -> Result<impl IntoResponse, AppError> {
read(state, collection_id, tenant_id, String::new()).await
}
pub async fn read_order(
State(state): State<AppState>,
Path((collection_id, tenant_id, directory)): Path<(String, String, String)>,
) -> Result<impl IntoResponse, AppError> {
read(state, collection_id, tenant_id, directory).await
}
pub async fn write_order_root(
State(state): State<AppState>,
Path((collection_id, tenant_id)): Path<(String, String)>,
Json(body): Json<WriteOrderRequest>,
) -> Result<impl IntoResponse, AppError> {
write(state, collection_id, tenant_id, String::new(), body).await
}
pub async fn write_order(
State(state): State<AppState>,
Path((collection_id, tenant_id, directory)): Path<(String, String, String)>,
Json(body): Json<WriteOrderRequest>,
) -> Result<impl IntoResponse, AppError> {
write(state, collection_id, tenant_id, directory, body).await
}
pub async fn delete_order_root(
State(state): State<AppState>,
Path((collection_id, tenant_id)): Path<(String, String)>,
Json(body): Json<DeleteOrderRequest>,
) -> Result<impl IntoResponse, AppError> {
delete(state, collection_id, tenant_id, String::new(), body).await
}
pub async fn delete_order(
State(state): State<AppState>,
Path((collection_id, tenant_id, directory)): Path<(String, String, String)>,
Json(body): Json<DeleteOrderRequest>,
) -> Result<impl IntoResponse, AppError> {
delete(state, collection_id, tenant_id, directory, body).await
}
async fn read(
state: AppState,
collection_id: String,
tenant_id: String,
directory: String,
) -> Result<(StatusCode, Json<Value>), AppError> {
let collection_id = validate::collection_id(&collection_id)?.to_string();
let tenant_id = validate::tenant_id(&tenant_id)?.to_string();
let directory = validate::folder_path(&directory)?.to_string();
tracing::debug!(collection_id = %collection_id, tenant_id = %tenant_id, directory = %order::display_directory(&directory), "handling read order request");
let repo_path = state
.config
.server
.repos_path
.join(&collection_id)
.join(&tenant_id);
let directory_for_task = directory.clone();
let stored_order = run_blocking(move || {
git::GitOrder::read_order(&repo_path, &tenant_id, &directory_for_task)
})
.await?;
Ok((
StatusCode::OK,
Json(json!({
"directory": directory,
"order": stored_order,
})),
))
}
async fn write(
state: AppState,
collection_id: String,
tenant_id: String,
directory: String,
body: WriteOrderRequest,
) -> Result<(StatusCode, Json<Value>), AppError> {
let collection_id = validate::collection_id(&collection_id)?.to_string();
let tenant_id = validate::tenant_id(&tenant_id)?.to_string();
let directory = validate::folder_path(&directory)?.to_string();
let allow_hidden_files = body.allow_hidden_files.unwrap_or(false);
order::validate_order(&body.order, allow_hidden_files)?;
tracing::debug!(collection_id = %collection_id, tenant_id = %tenant_id, directory = %order::display_directory(&directory), entry_count = body.order.len(), allow_hidden_files = allow_hidden_files, "handling write order request");
let repo_path = state
.config
.server
.repos_path
.join(&collection_id)
.join(&tenant_id);
let lock_key = format!("{}/{}", collection_id, tenant_id);
let lock = state.get_repo_lock(&lock_key);
let _lock_guard = lock.lock().await;
let WriteOrderRequest {
author,
order: order_entries,
allow_hidden_files: _,
message,
} = body;
let repo_path_for_maintenance = repo_path.clone();
let tenant_id_for_task = tenant_id.clone();
let (commit_sha, change) = run_blocking(move || {
git::GitOrder::write_order(
&repo_path,
&tenant_id_for_task,
&directory,
&order_entries,
message.as_deref(),
&author.name,
&author.email,
)
})
.await?;
let Some(change) = change else {
tracing::debug!(tenant_id = %tenant_id, sha = %commit_sha, "order unchanged, no commit created");
return Ok((StatusCode::OK, Json(json!({ "commit_sha": commit_sha }))));
};
tracing::debug!(tenant_id = %tenant_id, sha = %commit_sha, "order write committed, enqueuing hook delivery");
state
.replication
.repository_updated(&collection_id, &tenant_id, &commit_sha);
state.hook_queue.enqueue(
&lock_key,
HookJob::new(
collection_id,
tenant_id,
commit_sha.clone(),
Utc::now(),
vec![change],
),
);
state
.maintenance
.schedule(&lock_key, repo_path_for_maintenance, lock.clone());
Ok((StatusCode::OK, Json(json!({ "commit_sha": commit_sha }))))
}
async fn delete(
state: AppState,
collection_id: String,
tenant_id: String,
directory: String,
body: DeleteOrderRequest,
) -> Result<(StatusCode, Json<Value>), AppError> {
let collection_id = validate::collection_id(&collection_id)?.to_string();
let tenant_id = validate::tenant_id(&tenant_id)?.to_string();
let directory = validate::folder_path(&directory)?.to_string();
tracing::debug!(collection_id = %collection_id, tenant_id = %tenant_id, directory = %order::display_directory(&directory), "handling delete order request");
let repo_path = state
.config
.server
.repos_path
.join(&collection_id)
.join(&tenant_id);
let lock_key = format!("{}/{}", collection_id, tenant_id);
let lock = state.get_repo_lock(&lock_key);
let _lock_guard = lock.lock().await;
let DeleteOrderRequest { author, message } = body;
let repo_path_for_maintenance = repo_path.clone();
let tenant_id_for_task = tenant_id.clone();
let (commit_sha, change) = run_blocking(move || {
git::GitOrder::delete_order(
&repo_path,
&tenant_id_for_task,
&directory,
message.as_deref(),
&author.name,
&author.email,
)
})
.await?;
tracing::debug!(tenant_id = %tenant_id, sha = %commit_sha, "order deletion committed, enqueuing hook delivery");
state
.replication
.repository_updated(&collection_id, &tenant_id, &commit_sha);
state.hook_queue.enqueue(
&lock_key,
HookJob::new(
collection_id,
tenant_id,
commit_sha.clone(),
Utc::now(),
vec![change],
),
);
state
.maintenance
.schedule(&lock_key, repo_path_for_maintenance, lock.clone());
Ok((StatusCode::OK, Json(json!({ "commit_sha": commit_sha }))))
}