1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);