use clap::Parser; use url::Url; use jetstream::{ JetstreamCompression, JetstreamConfig, JetstreamConnector, events::{CommitOp, Cursor, EventKind, JetstreamEvent}, exports::Nsid, }; use jacquard::{ api::{ app_bsky::feed::post::Post, app_bsky::richtext::facet::{Facet, ByteSlice}, com_atproto::server::create_session::CreateSession, com_atproto::repo::create_record::CreateRecord, }, client::{BasicClient, Session}, types::{ datetime::Datetime, ident::AtIdentifier, language::Language, collection::Collection, value::Data, string::AtUri, }, }; use std::time::Duration; use serde::Deserialize; type Result = std::result::Result>; #[derive(Debug, Parser)] #[command(version, about, long_about = None)] struct Args { /// pds of the bot user #[arg(short, long, env = "BOT_PDS")] pds: Url, /// handle or did of the bot user #[arg(short, long, env = "BOT_HANDLE")] identifier: String, /// app password for the bot user #[arg(short, long, env = "BOT_APP_PASSWORD")] app_password: String, /// lightweight firehose #[arg(short, long, env = "BOT_JETSTREAM_URL")] #[clap(default_value = "wss://jetstream1.us-east.fire.hose.cam/subscribe")] jetstream_url: Url, /// optional: we can pick up from a past jetstream cursor /// /// the default is to just live-tail /// /// warning: setting this can lead to rapid bot posting #[arg(long)] jetstream_cursor: Option, /// don't actually post #[arg(long, action)] dry_run: bool, } async fn post( client: &BasicClient, identifier: &AtIdentifier<'_>, repo_name: &str, repo_url: &str, title: &str, repo_issues_url: &str, ) -> Result<()> { let message = format!(r#"good-first-issue added for {repo_name}: > {title}"#); let repo_feature = serde_json::json!({ "$type": "app.bsky.richtext.facet#link", "uri": repo_url, }); let repo_facet = Facet { features: vec![Data::from_json(&repo_feature)?], index: ByteSlice { byte_start: 27, byte_end: 29 + repo_name.len() as i64, extra_data: Default::default(), }, extra_data: Default::default(), }; let title_starts_at = (29 + repo_name.len() + 5) as i64; let repo_issues_feature = serde_json::json!({ "$type": "app.bsky.richtext.facet#link", "uri": repo_issues_url, }); let issues_facet = Facet { features: vec![Data::from_json(&repo_issues_feature)?], index: ByteSlice { byte_start: title_starts_at, byte_end: title_starts_at + title.len() as i64, extra_data: Default::default(), }, extra_data: Default::default(), }; // Make a post let post = Post { created_at: Datetime::now(), langs: Some(vec![Language::new("en")?]), text: message.into(), facets: Some(vec![repo_facet, issues_facet]), embed: Default::default(), entities: Default::default(), labels: Default::default(), reply: Default::default(), tags: Default::default(), extra_data: Default::default(), }; let json = serde_json::to_value(post)?; let data = Data::from_json(&json)?; println!("\nposting..."); client .send(CreateRecord::new() .repo(identifier.clone()) .collection(Post::nsid()) .record(data) .build()) .await? .into_output()?; Ok(()) } fn event_to_create_label Deserialize<'a>>(event: JetstreamEvent) -> Result<(T, Cursor)> { if event.kind != EventKind::Commit { return Err("not a commit".into()); } let commit = event.commit.ok_or("commit event missing commit data")?; if commit.operation != CommitOp::Create { return Err("not a create event".into()); } let raw = commit.record.ok_or("commit missing record")?; // todo: delete post if label is removed // delete sample: at://did:plc:hdhoaan3xa3jiuq4fg4mefid/sh.tangled.label.op/3m2jvx4c6wf22 // tldr: has a "delete" array just like "add" on the same op collection let t = serde_json::from_str(raw.get())?; Ok((t, event.cursor)) } /// com.bad-example.identity.resolveMiniDoc bit we care about #[derive(Deserialize)] struct MiniDocResponse { handle: String, } /// com.atproto.repo.getRecord wraps the record in a `value` key #[derive(Deserialize)] struct GetRecordResonse { value: T, } /// part of CreateLabelRecord: key is the label reference (ie for "good-first-issue") #[derive(Deserialize)] struct AddLabel { key: String, } /// tangled's record for adding labels to an issue #[derive(Deserialize)] struct CreateLabelRecord { add: Vec, subject: String, } /// tangled issue record #[derive(Deserialize)] struct IssueRecord { title: String, repo: String, } /// tangled repo record #[derive(Deserialize)] struct RepoRecord { name: String, } /// get some atproto record content (from slingshot) async fn get_record Deserialize<'a>>(client: &reqwest::Client, at_uri: &str) -> Result { let mut url: Url = "https://slingshot.microcosm.blue".parse()?; url.set_path("/xrpc/com.bad-example.repo.getUriRecord"); url.query_pairs_mut().append_pair("at_uri", at_uri); let GetRecordResonse { value } = client .get(url) .send() .await? .error_for_status()? .json() .await?; Ok(value) } /// try to resolve a bidirectionally verified handle from an identifier (via slingshot) async fn get_handle(client: &reqwest::Client, identifier: &str) -> Result> { let mut url: Url = "https://slingshot.microcosm.blue".parse()?; url.set_path("/xrpc/com.bad-example.identity.resolveMiniDoc"); url.query_pairs_mut().append_pair("identifier", identifier); let MiniDocResponse { handle } = client .get(url) .send() .await? .error_for_status()? .json() .await?; if handle == "handle.invalid" { Ok(None) } else { Ok(Some(handle)) } } #[tokio::main] async fn main() -> Result<()> { env_logger::init(); let args = Args::parse(); // Create HTTP client and session let client = BasicClient::new(args.pds); let bot_id = AtIdentifier::new(&args.identifier)?; let session = Session::from( client .send( CreateSession::new() .identifier(&bot_id.to_string()) .password(args.app_password) .build(), ) .await? .into_output()?, ); println!("logged in as {} ({})", session.handle, session.did); client.set_session(session).await?; let slingshot_client = reqwest::Client::builder() .user_agent("hacktober_bot") .timeout(Duration::from_secs(9)) .build()?; let jetstream_config: JetstreamConfig = JetstreamConfig { endpoint: args.jetstream_url.to_string(), wanted_collections: vec![Nsid::new("sh.tangled.label.op".to_string())?], user_agent: Some("hacktober_bot".to_string()), compression: JetstreamCompression::Zstd, replay_on_reconnect: true, channel_size: 1024, // buffer up to ~1s of jetstream events ..Default::default() }; let mut receiver = JetstreamConnector::new(jetstream_config)? .connect_cursor(args.jetstream_cursor.map(Cursor::from_raw_u64)) .await?; println!("receiving jetstream messages..."); loop { let Some(event) = receiver.recv().await else { eprintln!("consumer: could not receive event, bailing"); break; }; let Ok((CreateLabelRecord { add, subject }, cursor)) = event_to_create_label(event) else { continue; }; let mut added_good_first_issue = false; for added in add { if added.key == "at://did:plc:wshs7t2adsemcrrd4snkeqli/sh.tangled.label.definition/good-first-issue" { println!("found a good first issue label!! {:?}", cursor); added_good_first_issue = true; break; // inner } eprintln!("found a label but it wasn't good-first-issue, ignoring..."); } if !added_good_first_issue { continue; } let IssueRecord { title, repo } = match get_record(&slingshot_client, &subject).await { Ok(m) => m, Err(e) => { eprintln!("failed to get issue record: {e} for {subject}"); continue; } }; let Ok(repo_uri) = AtUri::new(&repo) else { eprintln!("failed to parse repo to aturi for {subject}"); continue; }; let RepoRecord { name: repo_name } = match get_record(&slingshot_client, &repo).await { Ok(m) => m, Err(e) => { eprintln!("failed to get repo record: {e} for {subject}"); continue; } }; let nice_tangled_repo_id = match repo_uri.authority() { AtIdentifier::Handle(h) => format!("@{h}"), AtIdentifier::Did(did) => match get_handle(&slingshot_client, did.as_str()).await { Err(e) => { eprintln!("failed to get mini doc from repo identifier: {e} for {subject}"); continue; } Ok(None) => did.to_string(), Ok(Some(h)) => format!("@{h}"), } }; let repo_full_name = format!("{nice_tangled_repo_id}/{repo_name}"); let repo_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}"); let issues_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}/issues"); if args.dry_run { println!("--dry-run, but would have posted:"); println!("good-first-issue label added for {repo_full_name} ({repo_url}):"); println!("> {title} ({issues_url})\n"); continue; } if let Err(e) = post( &client, &bot_id, &repo_full_name, &repo_url, &title, &issues_url, ).await { eprintln!("failed to post for {subject}: {e}"); }; break; } Ok(()) }