use axum::{ Json, extract::{Query, State}, }; use chrono::Utc; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use tracing::{error, warn}; use tranquil_pds::api::error::ApiError; use tranquil_pds::auth::{Admin, Auth}; use tranquil_pds::state::AppState; use tranquil_pds::types::{CidLink, Did}; #[derive(Deserialize)] pub struct GetSubjectStatusParams { pub did: Option, pub uri: Option, pub blob: Option, } #[derive(Serialize)] pub struct SubjectStatus { pub subject: serde_json::Value, pub takedown: Option, pub deactivated: Option, } #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct StatusAttr { pub applied: bool, pub r#ref: Option, } pub async fn get_subject_status( State(state): State, _auth: Auth, Query(params): Query, ) -> Result, ApiError> { if params.did.is_none() && params.uri.is_none() && params.blob.is_none() { return Err(ApiError::InvalidRequest( "Must provide did, uri, or blob".into(), )); } if let Some(did_str) = ¶ms.did { let did: Did = did_str .parse() .map_err(|_| ApiError::InvalidDid("Invalid DID format".into()))?; match state.repos.user.get_status_by_did(&did).await { Ok(Some(status)) => { let deactivated = status.deactivated_at.map(|_| StatusAttr { applied: true, r#ref: None, }); let takedown = status.takedown_ref.as_ref().map(|r| StatusAttr { applied: true, r#ref: Some(r.clone()), }); return Ok(Json(SubjectStatus { subject: json!({ "$type": "com.atproto.admin.defs#repoRef", "did": did_str }), takedown, deactivated, })); } Ok(None) => { return Err(ApiError::SubjectNotFound); } Err(e) => { error!("DB error in get_subject_status: {:?}", e); return Err(ApiError::InternalError(None)); } } } if let Some(uri_str) = ¶ms.uri { let cid: CidLink = uri_str .parse() .map_err(|_| ApiError::InvalidRequest("Invalid CID format".into()))?; match state.repos.repo.get_record_by_cid(&cid).await { Ok(Some(record)) => { let takedown = record.takedown_ref.as_ref().map(|r| StatusAttr { applied: true, r#ref: Some(r.clone()), }); return Ok(Json(SubjectStatus { subject: json!({ "$type": "com.atproto.repo.strongRef", "uri": uri_str, "cid": uri_str }), takedown, deactivated: None, })); } Ok(None) => { return Err(ApiError::RecordNotFound); } Err(e) => { error!("DB error in get_subject_status: {:?}", e); return Err(ApiError::InternalError(None)); } } } if let Some(blob_cid_str) = ¶ms.blob { let blob_cid: CidLink = blob_cid_str .parse() .map_err(|_| ApiError::InvalidRequest("Invalid CID format".into()))?; let did = params.did.as_ref().ok_or_else(|| { ApiError::InvalidRequest("Must provide a did to request blob state".into()) })?; match state.repos.blob.get_blob_with_takedown(&blob_cid).await { Ok(Some(blob)) => { let takedown = blob.takedown_ref.as_ref().map(|r| StatusAttr { applied: true, r#ref: Some(r.clone()), }); return Ok(Json(SubjectStatus { subject: json!({ "$type": "com.atproto.admin.defs#repoBlobRef", "did": did, "cid": blob.cid }), takedown, deactivated: None, })); } Ok(None) => { return Err(ApiError::BlobNotFound(None)); } Err(e) => { error!("DB error in get_subject_status: {:?}", e); return Err(ApiError::InternalError(None)); } } } Err(ApiError::InvalidRequest("Invalid subject type".into())) } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] pub struct UpdateSubjectStatusInput { pub subject: serde_json::Value, pub takedown: Option, pub deactivated: Option, } #[derive(Deserialize)] pub struct StatusAttrInput { pub applied: bool, pub r#ref: Option, } pub async fn update_subject_status( State(state): State, _auth: Auth, Json(input): Json, ) -> Result, ApiError> { let subject_type = input.subject.get("$type").and_then(Value::as_str); match subject_type { Some("com.atproto.admin.defs#repoRef") => { let did_str = input.subject.get("did").and_then(Value::as_str); if let Some(did_str) = did_str { if input.takedown.as_ref().is_some_and(|status| status.applied) && input .deactivated .as_ref() .is_some_and(|status| !status.applied) { return Err(ApiError::InvalidRequest( "Cannot activate and takedown an account at the same time".into(), )); } let did: Did = match did_str.parse() { Ok(d) => d, Err(_) => return Err(ApiError::InvalidDid("Invalid DID format".into())), }; let takedown_ref = input.takedown.as_ref().and_then(|takedown| { takedown.applied.then(|| { takedown .r#ref .clone() .unwrap_or_else(|| Utc::now().to_rfc3339()) }) }); if input.takedown.is_some() { state .repos .user .set_user_takedown(&did, takedown_ref.as_deref()) .await .map_err(|e| { error!("Failed to update user takedown status for {}: {:?}", did, e); ApiError::InternalError(Some("Failed to update takedown status".into())) })?; } if let Some(deactivated) = &input.deactivated { let result = if deactivated.applied { state.repos.user.deactivate_account(&did, None).await } else { let status = state .repos .user .get_status_by_did(&did) .await .map_err(|e| { error!("Failed to read account status for {}: {:?}", did, e); ApiError::InternalError(None) })? .ok_or(ApiError::AccountNotFound)?; if status.takedown_ref.is_some() { return Err(ApiError::AccountNotFound); } state.repos.user.activate_account(&did).await }; result.map_err(|e| { error!( "Failed to update user deactivation status for {}: {:?}", did, e ); ApiError::InternalError(Some("Failed to update deactivation status".into())) })?; } let takedown_update = input.takedown.as_ref().map(|t| t.applied); let deactivated_update = input.deactivated.as_ref().map(|d| d.applied); if takedown_update.is_some() || deactivated_update.is_some() { state .repos .repo .update_repo_status( &did, takedown_update, takedown_ref.as_deref(), deactivated_update, ) .await .map_err(|e| { error!("Failed to sync account status to repo backend: {e:?}"); ApiError::InternalError(Some("Failed to update account status".into())) })?; } if input.takedown.as_ref().is_some_and(|status| status.applied) { state .repos .session .delete_sessions_by_did(&did) .await .map_err(|e| { error!("Failed to revoke sessions for taken-down account: {e:?}"); ApiError::InternalError(Some( "Failed to revoke account sessions".into(), )) })?; state .repos .oauth .delete_sessions_by_did(&did) .await .map_err(|e| { error!("Failed to revoke OAuth sessions for taken-down account: {e:?}"); ApiError::InternalError(Some("Failed to revoke OAuth sessions".into())) })?; } tranquil_pds::auth::invalidate_auth_cache(state.cache.as_ref(), &did).await; if let Ok(Some(handle)) = state.repos.user.get_handle_by_did(&did).await { let _ = state .cache .delete(&tranquil_pds::cache_keys::handle_key(&handle)) .await; } if takedown_update.is_some() || deactivated_update.is_some() { let status = tranquil_pds::sync::get_account_with_status( state.repos.repo.as_ref(), &did, ) .await .map_err(|e| { error!("Failed to read updated account status: {e:?}"); ApiError::InternalError(Some("Failed to read account status".into())) })? .ok_or(ApiError::SubjectNotFound)? .status; if let Err(e) = tranquil_pds::repo_ops::sequence_account_event(&state, &did, status).await { warn!("Failed to sequence account status event: {}", e); } } return Ok(Json(json!({ "subject": input.subject, "takedown": input.takedown.as_ref().map(|t| json!({ "applied": t.applied, "ref": t.r#ref })), "deactivated": input.deactivated.as_ref().map(|d| json!({ "applied": d.applied })) }))); } } Some("com.atproto.repo.strongRef") => { let uri_str = input.subject.get("uri").and_then(Value::as_str); if let Some(uri_str) = uri_str { let cid_str = input .subject .get("cid") .and_then(Value::as_str) .ok_or_else(|| { ApiError::InvalidRequest("Record subject must include a CID".into()) })?; let cid: CidLink = cid_str .parse() .map_err(|_| ApiError::InvalidRequest("Invalid CID format".into()))?; let mut applied_ref = None; if let Some(takedown) = &input.takedown { applied_ref = takedown.applied.then(|| { takedown .r#ref .clone() .unwrap_or_else(|| Utc::now().to_rfc3339()) }); state .repos .repo .set_record_takedown(&cid, applied_ref.as_deref()) .await .map_err(|e| { error!( "Failed to update record takedown status for {}: {:?}", uri_str, e ); ApiError::InternalError(Some("Failed to update takedown status".into())) })?; } return Ok(Json(json!({ "subject": input.subject, "takedown": input.takedown.as_ref().map(|t| json!({ "applied": t.applied, "ref": applied_ref })) }))); } } Some("com.atproto.admin.defs#repoBlobRef") => { let cid_str = input.subject.get("cid").and_then(Value::as_str); if let Some(cid_str) = cid_str { let cid: CidLink = cid_str .parse() .map_err(|_| ApiError::InvalidRequest("Invalid CID format".into()))?; let mut applied_ref = None; if let Some(takedown) = &input.takedown { applied_ref = takedown.applied.then(|| { takedown .r#ref .clone() .unwrap_or_else(|| Utc::now().to_rfc3339()) }); state .repos .blob .update_blob_takedown(&cid, applied_ref.as_deref()) .await .map_err(|e| { error!( "Failed to update blob takedown status for {}: {:?}", cid_str, e ); ApiError::InternalError(Some("Failed to update takedown status".into())) })?; } return Ok(Json(json!({ "subject": input.subject, "takedown": input.takedown.as_ref().map(|t| json!({ "applied": t.applied, "ref": applied_ref })) }))); } } _ => {} } Err(ApiError::InvalidRequest("Invalid subject type".into())) }