use jacquard_api::com_atproto::sync::request_crawl::{ RequestCrawlError, RequestCrawlRequest, RequestCrawlResponse, }; use url::Url; use super::*; pub async fn handle( State(hydrant): State, ExtractXrpc(req): ExtractXrpc, ) -> XrpcResult> { let nsid = RequestCrawlResponse::NSID; let url_str = format!("wss://{}/", req.hostname); let url = Url::parse(&url_str).map_err(|e| bad_request(nsid, e))?; if hydrant.pds.is_banned(&req.hostname) { return Err(XrpcErrorResponse { status: StatusCode::BAD_REQUEST, error: jacquard_common::xrpc::XrpcError::Xrpc(RequestCrawlError::HostBanned(Some( "host is banned".into(), ))), }); } // if this host is already running, everything is already ok if hydrant.firehose.is_source_running(&url) { return Ok(StatusCode::OK); } // enforce daily new pds limit on unknown hosts if !hydrant.firehose.is_source_known(&url) { let (allowed, to_persist) = hydrant.state.pds_daily_limit.try_increment(); if !allowed { return Err(rate_limited( nsid, "POST", "daily limit for new PDS sources reached", )); } // persist the new count before returning so a crash cannot reset the counter // and allow the budget to be replayed. if let Some((day, count)) = to_persist { hydrant .state .db .run(move |db| crate::db::save_pds_daily_adds(db, day, count)) .await .map_err(|e| internal_error(nsid, e))?; } } hydrant .firehose .add_source(url, true) .await .map_err(|e| internal_error(nsid, e))?; Ok(StatusCode::OK) }