tangled.org trending bluesky account
1use atrium_api::types::Collection;
2use reqwest::Url;
3use serde::Deserialize;
4
5#[derive(Deserialize, Debug)]
6struct ConstellationCountResponse {
7 total: i64,
8}
9
10pub async fn fetch_constellation_count(target: &str) -> anyhow::Result<i64> {
11 let base = std::env::var("CONSTELLATION_BASE_URL")
12 .unwrap_or_else(|_| "https://constellation.microcosm.blue".to_string());
13 let endpoint = if base.ends_with('/') {
14 format!("{}links/count", base)
15 } else {
16 format!("{}/links/count", base)
17 };
18
19 // Build URL with query params in one go to avoid non-Send lifetimes
20 let url = Url::parse_with_params(
21 &endpoint,
22 &[
23 ("target", target.to_string()),
24 ("collection", atproto_api::sh::tangled::feed::Star::NSID.to_string()),
25 ("path", ".subject".to_string()),
26 ],
27 )?;
28
29 let resp = reqwest::Client::new()
30 .get(url)
31 .send()
32 .await?
33 .error_for_status()?;
34
35 let parsed: ConstellationCountResponse = resp.json().await?;
36 Ok(parsed.total)
37}