use 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, pub truncated: bool, pub posts_read: usize, } pub async fn scan_identity( api: &Api, actor: &str, filter: &ThreadFilter, cutoff: DateTime, ) -> Result> { 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 = 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 = candidates .iter() .map(|candidate| candidate.root_uri.clone()) .collect(); let sample: Vec = 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, } }