~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs

0
0
main
31 lines · 1.2 KB Raw History
use abi::{Call, Did, Event, Handle, Run, entries};
use serde::Deserialize;
use serde_json::json;

// Every event gets the action's input plus did and now; Run adds the event and the last state.
#[derive(Deserialize)]
struct Args {
    did: Did,
    #[serde(default)]
    collection: Option<String>,
}

fn count_handler(run: Run<Args>) -> Result<Handle, String> {
    let collection = run.args.collection.ok_or("collection is required")?;
    Ok(match run.event {
        // First event: list the records. service = the account's DID: a public read of its PDS.
        Event::Start => Handle::new().call(
            Call::new("list", "com.atproto.repo.listRecords")
                .params(json!({ "repo": run.args.did, "collection": collection, "limit": "100" }))
                .service(run.args.did.as_str()),
        ),
        // The result comes back with the id we gave it. A failed call is the run's error.
        Event::Result { id, result, .. } if id == "list" => {
            let records = result.output()?["records"].as_array().map_or(0, Vec::len);
            Handle::new().result(json!({ "collection": collection, "records": records })).done()
        }
        _ => Handle::new(),
    })
}

entries!(count => count_handler);