Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140use std::collections::HashSet;use std::sync::Arc;
use anyhow::Result;use chrono::{DateTime, Utc};use tokio::sync::Semaphore;use tokio::task::JoinSet;
use crate::api::{Api, Identity};use crate::cli::{FollowsArgs, ThreadFilter};use crate::model::{AuthorRow, ThreadRow, avatar_url, has_self_label};use crate::{output, thread};
#[derive(Debug, Clone)]pub struct ScanOutcome { pub rows: Vec<ThreadRow>, pub truncated: bool, pub posts_read: usize,}
pub async fn scan_identity( api: &Api, actor: &str, filter: &ThreadFilter, cutoff: DateTime<Utc>,) -> Result<Option<ScanOutcome>> { let identity = api.identity(actor).await?; let profile = api.profile(&identity).await?; if profile .as_ref() .is_some_and(|profile| has_self_label(profile, "!no-unauthenticated")) { return Ok(None); }
let page = api.fetch_posts_since(&identity, cutoff).await?; let truncated = page.truncated; let posts = page.posts; let posts_read = posts.len(); let published: HashSet<String> = api.published_roots(&identity).await?.into_iter().collect(); let candidates = thread::candidates( &posts, filter.min_posts, filter.min_graphemes, filter.include_replies, ); let roots: Vec<String> = candidates .iter() .map(|candidate| candidate.root_uri.clone()) .collect(); let sample: Vec<String> = posts.iter().take(50).map(|post| post.uri.clone()).collect(); let mut engagement_uris = roots; for uri in &sample { if !engagement_uris.contains(uri) { engagement_uris.push(uri.clone()); } } let engagement = api.get_engagement(&engagement_uris).await?; let author_median = thread::median_engagement(&engagement, &sample); let rows = thread::build_rows( &author_row(&identity, profile.as_ref()), &identity.pds, candidates, &engagement, &published, filter.include_published, author_median, ); Ok(Some(ScanOutcome { rows, truncated, posts_read, }))}
pub async fn follows(api: &Api, args: FollowsArgs) -> Result<()> { let cutoff = crate::cli::since_cutoff(&args.filter.since)?; let identity = api.identity(&args.handle).await?; if api.opted_out(&identity).await? { return Ok(()); }
let follows = api.get_follows(&identity.did, args.limit).await?; let permits = Arc::new(Semaphore::new(4)); let mut tasks = JoinSet::new(); for follow in follows { let api = api.clone(); let filter = args.filter.clone(); let permits = permits.clone(); let actor = if follow.handle.is_empty() { follow.did } else { follow.handle }; tasks.spawn(async move { let _permit = permits .acquire_owned() .await .expect("the semaphore is never closed"); let result = scan_identity(&api, &actor, &filter, cutoff).await; (actor, result) }); }
let mut rows = Vec::new(); while let Some(joined) = tasks.join_next().await { match joined { Ok((actor, Ok(Some(mut outcome)))) => { if outcome.truncated { tracing::warn!(actor, "account post history truncated"); } rows.append(&mut outcome.rows); } Ok((actor, Ok(None))) => { tracing::debug!(actor, "account opted out; skipped"); } Ok((actor, Err(error))) => { tracing::warn!(actor, %error, "account scan failed"); } Err(error) => tracing::warn!(%error, "account scan task failed"), } } ThreadRow::sort_best_first(&mut rows); output::print_rows(&rows, true, args.json)}
pub fn author_row(identity: &Identity, profile: Option<&serde_json::Value>) -> AuthorRow { let display_name = profile .and_then(|profile| profile.get("displayName")) .and_then(serde_json::Value::as_str) .map(str::to_string); let avatar = profile.and_then(|profile| avatar_url(profile, &identity.pds, &identity.did)); AuthorRow { did: identity.did.clone(), handle: identity.handle.clone(), display_name, avatar, }}