aboutsummaryrefslogtreecommitdiffstats
path: root/adenosine-cli/src/bin/adenosine.rs
blob: 7f811914a13630753d5ede47740d5b4fe3e4240a (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
use adenosine::app_bsky;
use adenosine::auth::parse_did_from_jwt;
use adenosine::created_at_now;
use adenosine::identifiers::*;
use adenosine::xrpc::{XrpcClient, XrpcMethod};
use adenosine_cli::*;
use anyhow::anyhow;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::str::FromStr;

use colored_json::to_colored_json_auto;
use log::{self, debug};
use std::io::Write;
use structopt::StructOpt;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

#[derive(StructOpt)]
#[structopt(
    rename_all = "kebab-case",
    about = "command-line client for AT protocol (atproto.com)"
)]
struct Opt {
    /// HTTP(S) URL of Personal Data Server to connect to
    #[structopt(
        global = true,
        long = "--pds-host",
        env = "ATP_PDS_HOST",
        default_value = "http://localhost:2583"
    )]
    pds_host: String,

    /// Authentication session token (JWT), for operations that need it
    #[structopt(
        global = true,
        long = "--auth-token",
        env = "ATP_AUTH_TOKEN",
        hide_env_values = true
    )]
    auth_token: Option<String>,

    /// Authentication handle (username), for commands that need it
    #[structopt(
        global = true,
        long = "--auth-handle",
        env = "ATP_AUTH_HANDLE",
        hide_env_values = true
    )]
    auth_handle: Option<String>,

    /// Authentication password, for commands that need it
    #[structopt(
        global = true,
        long = "--auth-password",
        env = "ATP_AUTH_PASSWORD",
        hide_env_values = true
    )]
    auth_password: Option<String>,

    /// Admin password in auth, if needed
    #[structopt(
        global = true,
        long = "--admin-password",
        env = "ATP_AUTH_ADMIN_PASSWORD",
        hide_env_values = true
    )]
    admin_password: Option<String>,

    /// Log more messages. Pass multiple times for ever more verbosity
    ///
    /// By default, it'll only report errors. Passing `-v` one time also prints
    /// warnings, `-vv` enables info logging, `-vvv` debug, and `-vvvv` trace.
    #[structopt(global = true, long, short = "v", parse(from_occurrences))]
    verbose: i8,

    #[structopt(long = "--shell-completions", hidden = true)]
    shell_completions: Option<structopt::clap::Shell>,

    #[structopt(subcommand)]
    cmd: Command,
}

#[derive(StructOpt)]
enum AccountCommand {
    /// Register a new account
    ///
    /// Does not (yet) support invite codes or email verification.
    ///
    /// This will return a JWT token that you should assign to the `ATP_AUTH_TOKEN` environment
    /// variable.
    Register {
        #[structopt(long, short)]
        email: String,

        #[structopt(long = "--username", short = "-u")]
        handle: String,

        #[structopt(long, short)]
        password: String,

        #[structopt(long, short)]
        recovery_key: Option<String>,

        #[structopt(long, short)]
        invite_code: Option<String>,
    },
    /// Delete the currently logged-in account (danger!)
    Delete,
    /// Create a new authenticated session
    ///
    /// This will return a JWT token that you should assign to the `ATP_AUTH_TOKEN` environment
    /// variable
    Login {
        #[structopt(long = "--username", short = "-u")]
        handle: String,

        #[structopt(long, short)]
        password: String,
    },
    /// Refreshes JWT token
    Refresh,
    /// Deletes the current login session
    Logout,
    /// Fetches account metadata for the current session
    Info,
    // TODO: CreateRevocationKey or CreateDid
    CreateInvite {
        #[structopt(short = "-u", default_value = "1")]
        uses: u64,
    },
}

#[derive(StructOpt)]
enum RepoCommand {
    /// Get the current 'root' commit for a DID
    Root {
        /// Repository DID, or uses the current session account
        did: Option<DidOrHost>,
    },
    /// Dump raw binary repository as CAR format to stdout
    Export {
        /// Repository DID, or uses the current session account
        did: Option<DidOrHost>,
        /// CID of a prior commit; only newer updates are included
        #[structopt(long)]
        from: Option<String>,
    },
    /// Read raw binary repository as CAR format from stdin, and import to PDS
    Import {
        // TODO: could accept either path or stdin?
        /// Repository DID, or uses the current session account
        #[structopt(long)]
        did: Option<DidOrHost>,
    },
}

#[derive(StructOpt)]
enum BskyCommand {
    /// Fetch the account feed for a specific user (or self, by default)
    Feed { name: Option<DidOrHost> },
    /// Fetch timeline for currently logged-in account
    Timeline,
    /// View post thread (parent and child replies)
    Thread {
        uri: AtUri,

        /// How far to recurse along the parent and child reply chains
        #[structopt(long)]
        depth: Option<u64>,
    },
    /// Fetch notification feed
    Notifications,
    /// Create a new 'post' record
    Post { text: String },
    /// Create a 'repost' record for the target by AT URI
    Repost { uri: AtUri },
    /// Create a 'like' record for the target by AT URI
    Like { uri: AtUri },
    /// Create a 'follow' record for the target by AT URI
    Follow { uri: DidOrHost },
    // TODO: Unlike { uri: String, },
    // TODO: Unfollow { uri: String, },
    /* TODO:
    Follows {
        name: String,
    },
    Followers {
        name: String,
    },
    */
    /// Display a profile record (or self if not provided)
    Profile { name: Option<DidOrHost> },
    /// Query by partial handle
    SearchUsers { query: String },
}

#[derive(StructOpt)]
enum Command {
    /// Summarize connection and authentication with API
    Status,

    /// List all collections for a user, or all records for a collection
    Ls { uri: AtUri },
    /// Fetch and display a generic record by full AT URI
    Get {
        uri: AtUri,

        /// Specific version of record to fetch
        #[structopt(long)]
        cid: Option<String>,
    },
    /// Generic record creation
    Create {
        collection: Nsid,

        /// Set of object fields (keys) and values to construct the object from
        fields: Vec<ArgField>,
    },
    /// Generic mutation of an existing record
    Update {
        uri: AtUri,

        /// Set of object fields (keys) and values to update in the record
        fields: Vec<ArgField>,
    },
    /// Generic record deletion
    Delete { uri: AtUri },

    /// Print user/repository-level description (including DID document)
    Describe { name: Option<DidOrHost> },

    /// Have PDS resolve the DID for a handle
    Resolve { name: DidOrHost },

    /// Generic HTTP XRPC helper, printing any result
    Xrpc {
        /// 'get' or 'post'
        method: XrpcMethod,
        /// Name of method to call
        nsid: Nsid,
        /// Set of query parameters and body fields for the request
        fields: Vec<ArgField>,
    },

    /// Manage user account and sessions
    Account {
        #[structopt(subcommand)]
        cmd: AccountCommand,
    },

    /// Direct access to binary repository content
    Repo {
        #[structopt(subcommand)]
        cmd: RepoCommand,
    },

    /// Helper commands for bsky.app Lexicon
    Bsky {
        #[structopt(subcommand)]
        cmd: BskyCommand,
    },
}

fn main() -> Result<()> {
    dotenvy::dotenv().ok();
    let opt = Opt::from_args();

    let log_level = match opt.verbose {
        std::i8::MIN..=-1 => "none",
        0 => "error",
        1 => "warn",
        2 => "info",
        3 => "debug",
        4..=std::i8::MAX => "trace",
    };
    // hyper logging is very verbose, so crank that down even if everything else is more verbose
    let log_filter = format!("{log_level},hyper=error");
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_filter))
        .format_timestamp(None)
        .init();

    debug!("Args parsed, starting up");

    #[cfg(windows)]
    colored_json::enable_ansi_support();

    if let Some(shell) = opt.shell_completions {
        Opt::clap().gen_completions_to("adenosine", shell, &mut std::io::stdout());
        std::process::exit(0);
    }

    if let Err(err) = run(opt) {
        // Be graceful about some errors
        if let Some(io_err) = err.root_cause().downcast_ref::<std::io::Error>() {
            if let std::io::ErrorKind::BrokenPipe = io_err.kind() {
                // presumably due to something like writing to stdout and piped to `head -n10` and
                // stdout was closed
                debug!("got BrokenPipe error, assuming stdout closed as expected and exiting with success");
                std::process::exit(0);
            }
        }
        let mut color_stderr = StandardStream::stderr(if atty::is(atty::Stream::Stderr) {
            ColorChoice::Auto
        } else {
            ColorChoice::Never
        });
        color_stderr.set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_bold(true))?;
        eprintln!("Error: {err:?}");
        color_stderr.set_color(&ColorSpec::new())?;
        std::process::exit(1);
    }
    Ok(())
}

fn print_result_json(result: Option<Value>) -> Result<()> {
    if let Some(val) = result {
        writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&val)?)?
    };
    Ok(())
}

/// Helper for endpoints that require authentication.
///
/// If an author token already exists, use it to refresh the session. If no auth token is provided,
/// tries using handle/password to login, resulting in a session auth token.
///
/// Returns DID passed from session token if auth was successful, otherwise an Error.
fn require_auth_did(opt: &Opt, xrpc_client: &mut XrpcClient) -> Result<Did> {
    if opt.auth_token.is_some() {
        // TODO: currently would session (old refresh gets deleted)
        //xrpc_client.auth_refresh()?;
    } else if let (Some(handle), Some(passwd)) = (&opt.auth_handle, &opt.auth_password) {
        xrpc_client.auth_login(handle, passwd)?;
    } else {
        return Err(anyhow!(
            "command requires auth, but have neither token or handle/password"
        ));
    }
    xrpc_client.auth_did()
}

fn run(opt: Opt) -> Result<()> {
    let mut xrpc_client = XrpcClient::new(
        opt.pds_host.clone(),
        opt.auth_token.clone(),
        opt.admin_password.clone(),
    )?;
    let mut params: HashMap<String, String> = HashMap::new();
    let jwt_did: Option<String> = if let Some(ref token) = opt.auth_token {
        Some(parse_did_from_jwt(token)?)
    } else {
        None
    };

    let result = match opt.cmd {
        Command::Status => {
            println!("Configuration");
            println!("  ATP_PDS_HOST: {}", opt.pds_host);
            if opt.auth_token.is_some() {
                println!("  ATP_AUTH_TOKEN: <configured>");
            } else {
                println!("  ATP_AUTH_TOKEN:");
            }
            // TODO: parse JWT?
            // TODO: connection, auth check
            // TODO: account username, did, etc
            None
        }
        Command::Describe { ref name } => {
            let name = name
                .as_ref()
                .map(|v| v.to_string())
                .unwrap_or(require_auth_did(&opt, &mut xrpc_client)?.to_string());
            params.insert("user".to_string(), name);
            xrpc_client.get(&Nsid::from_str("com.atproto.repo.describe")?, Some(params))?
        }
        Command::Resolve { name } => {
            params.insert("name".to_string(), name.to_string());
            xrpc_client.get(&Nsid::from_str("com.atproto.handle.resolve")?, Some(params))?
        }
        Command::Get { uri, cid } => {
            params.insert("user".to_string(), uri.repository.to_string());
            params.insert(
                "collection".to_string(),
                uri.collection.ok_or(anyhow!("collection required"))?,
            );
            params.insert(
                "rkey".to_string(),
                uri.record.ok_or(anyhow!("record key required"))?,
            );
            if let Some(c) = cid {
                params.insert("cid".to_string(), c);
            }
            xrpc_client.get(&Nsid::from_str("com.atproto.repo.getRecord")?, Some(params))?
        }
        Command::Ls { uri } => {
            // TODO: option to print fully-qualified path?
            params.insert("user".to_string(), uri.repository.to_string());
            if uri.collection.is_none() {
                // if a repository, but no collection, list the collections
                let describe = xrpc_client
                    .get(&Nsid::from_str("com.atproto.repo.describe")?, Some(params))?
                    .ok_or(anyhow!("expected a repo.describe response"))?;
                for c in describe["collections"]
                    .as_array()
                    .ok_or(anyhow!("expected collection list"))?
                {
                    println!(
                        "at://{}/{}",
                        uri.repository,
                        c.as_str()
                            .ok_or(anyhow!("expected collection as a JSON string"))?
                    );
                }
            } else if uri.collection.is_some() && uri.record.is_none() {
                // if a collection, but no record, list the records (with extracted timestamps)
                params.insert("collection".to_string(), uri.collection.unwrap());
                let records = xrpc_client
                    .get(
                        &Nsid::from_str("com.atproto.repo.listRecords")?,
                        Some(params),
                    )?
                    .ok_or(anyhow!("expected a repoListRecords response"))?;
                for r in records["records"].as_array().unwrap_or(&vec![]).iter() {
                    println!("{}", r["uri"].as_str().unwrap());
                }
            } else {
                return Err(anyhow!("got too much of a URI to 'ls'"));
            }
            None
        }
        Command::Create {
            ref collection,
            ref fields,
        } => {
            let did = require_auth_did(&opt, &mut xrpc_client)?;
            let val = value_from_fields(fields.clone());
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.createRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": collection,
                    // TODO: "validate" (boolean)
                    "record": val
                })),
            )?
        }
        Command::Update {
            ref uri,
            ref fields,
        } => {
            require_auth_did(&opt, &mut xrpc_client)?;
            let did = uri.repository.to_string();
            let collection = uri
                .collection
                .clone()
                .ok_or(anyhow!("collection required"))?;
            let rkey = uri.record.clone().ok_or(anyhow!("record key required"))?;
            params.insert("did".to_string(), did.clone());
            params.insert("collection".to_string(), collection.clone());
            params.insert("rkey".to_string(), rkey.clone());
            // fetch existing, extend map with fields, put the updated value
            let mut record = xrpc_client
                .get(&Nsid::from_str("com.atproto.repo.getRecord")?, Some(params))?
                .unwrap_or(json!({}));
            update_value_from_fields(fields.clone(), &mut record);
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.putRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": collection,
                    "rkey": rkey,
                    "record": record,
                })),
            )?
        }
        Command::Delete { ref uri } => {
            require_auth_did(&opt, &mut xrpc_client)?;
            let did = uri.repository.to_string();
            let collection = uri
                .collection
                .clone()
                .ok_or(anyhow!("collection required"))?;
            let rkey = uri.record.clone().ok_or(anyhow!("record key required"))?;
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.deleteRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": collection,
                    "rkey": rkey,
                })),
            )?
        }
        Command::Xrpc {
            ref method,
            ref nsid,
            ref fields,
        } => {
            update_params_from_fields(fields, &mut params);
            let body = value_from_fields(fields.clone());
            match method {
                XrpcMethod::Get => xrpc_client.get(nsid, Some(params))?,
                XrpcMethod::Post => {
                    require_auth_did(&opt, &mut xrpc_client)?;
                    xrpc_client.post(nsid, Some(params), Some(body))?
                }
            }
        }
        Command::Account {
            cmd:
                AccountCommand::Register {
                    email,
                    handle,
                    password,
                    recovery_key,
                    invite_code,
                },
        } => {
            let mut body = json!({
                "email": email,
                "handle": handle,
                "password": password,
            });
            if let Some(key) = recovery_key {
                body["recoveryKey"] = json!(key);
            }
            if let Some(code) = invite_code {
                body["inviteCode"] = json!(code);
            }
            xrpc_client.post(
                &Nsid::from_str("com.atproto.account.create")?,
                None,
                Some(body),
            )?
        }
        Command::Account {
            cmd: AccountCommand::Login { handle, password },
        } => xrpc_client.post(
            &Nsid::from_str("com.atproto.session.create")?,
            None,
            Some(json!({
                "handle": handle,
                "password": password,
            })),
        )?,
        Command::Account {
            cmd: AccountCommand::Refresh,
        } => xrpc_client.post(&Nsid::from_str("com.atproto.session.refresh")?, None, None)?,
        Command::Account {
            cmd: AccountCommand::Logout,
        } => xrpc_client.post(&Nsid::from_str("com.atproto.session.delete")?, None, None)?,
        Command::Account {
            cmd: AccountCommand::Delete,
        } => xrpc_client.post(&Nsid::from_str("com.atproto.account.delete")?, None, None)?,
        Command::Account {
            cmd: AccountCommand::Info,
        } => xrpc_client.get(&Nsid::from_str("com.atproto.account.get")?, None)?,
        Command::Account {
            cmd: AccountCommand::CreateInvite { uses },
        } => xrpc_client.post(
            &Nsid::from_str("com.atproto.account.createInviteCode")?,
            None,
            Some(json!({ "useCount": uses })),
        )?,
        Command::Repo {
            cmd: RepoCommand::Root { did },
        } => {
            let did = match did {
                Some(DidOrHost::Host(_)) => return Err(anyhow!("expected a DID, not a hostname")),
                Some(v) => v.to_string(),
                None => jwt_did.ok_or(anyhow!("expected a DID"))?,
            };
            params.insert("did".to_string(), did);
            xrpc_client.get(&Nsid::from_str("com.atproto.sync.getRoot")?, Some(params))?
        }
        Command::Repo {
            cmd: RepoCommand::Export { did, from },
        } => {
            let did = match did {
                Some(DidOrHost::Host(_)) => return Err(anyhow!("expected a DID, not a hostname")),
                Some(v) => v.to_string(),
                None => jwt_did.ok_or(anyhow!("expected a DID"))?,
            };
            params.insert("did".to_string(), did);
            if let Some(from) = from {
                params.insert("from".to_string(), from);
            };
            xrpc_client.get_to_writer(
                &Nsid::from_str("com.atproto.sync.getRepo")?,
                Some(params),
                &mut std::io::stdout(),
            )?;
            None
        }
        Command::Repo {
            cmd: RepoCommand::Import { did },
        } => {
            let did = match did {
                Some(DidOrHost::Host(_)) => return Err(anyhow!("expected a DID, not a hostname")),
                Some(v) => v.to_string(),
                None => jwt_did.ok_or(anyhow!("expected a DID"))?,
            };
            params.insert("did".to_string(), did);
            xrpc_client.post_cbor_from_reader(
                &Nsid::from_str("com.atproto.sync.updateRepo")?,
                Some(params),
                &mut std::io::stdin(),
            )?
        }
        Command::Bsky {
            cmd: BskyCommand::Feed { ref name },
        } => {
            let name = name
                .as_ref()
                .map(|v| v.to_string())
                .unwrap_or(require_auth_did(&opt, &mut xrpc_client)?.to_string());
            params.insert("author".to_string(), name);
            let resp = xrpc_client.get(
                &Nsid::from_str("app.bsky.feed.getAuthorFeed")?,
                Some(params),
            )?;
            let resp = resp.ok_or(anyhow!("expected resp from getAuthorFeed"))?;
            if atty::is(atty::Stream::Stdout) {
                for val in resp["feed"]
                    .as_array()
                    .ok_or(anyhow!("expected feed from getAuthorFeed"))?
                    .iter()
                {
                    let val: serde_json::Value = val.clone();
                    let fi: app_bsky::FeedPostView = serde_json::from_value(val)?;
                    pretty::pp_feed_post_view(&fi)?;
                }
                None
            } else {
                Some(resp)
            }
        }
        Command::Bsky {
            cmd: BskyCommand::Timeline,
        } => {
            require_auth_did(&opt, &mut xrpc_client)?;
            let resp = xrpc_client.get(&Nsid::from_str("app.bsky.feed.getTimeline")?, None)?;
            let resp = resp.ok_or(anyhow!("expected resp from getTimeline"))?;
            if atty::is(atty::Stream::Stdout) {
                for val in resp["feed"]
                    .as_array()
                    .ok_or(anyhow!("expected feed from getTimeline"))?
                    .iter()
                {
                    let val: serde_json::Value = val.clone();
                    //print_result_json(Some(val.clone()))?;
                    let fi: app_bsky::FeedPostView = serde_json::from_value(val)?;
                    pretty::pp_feed_post_view(&fi)?;
                }
                None
            } else {
                Some(resp)
            }
        }
        Command::Bsky {
            cmd: BskyCommand::Thread { ref uri, depth },
        } => {
            require_auth_did(&opt, &mut xrpc_client)?;
            params.insert("uri".to_string(), uri.to_string());
            if let Some(d) = depth {
                params.insert("depth".to_string(), d.to_string());
            }
            let resp = xrpc_client.get(
                &Nsid::from_str("app.bsky.feed.getPostThread")?,
                Some(params),
            )?;
            let resp = resp.ok_or(anyhow!("expected resp from getPostThread"))?;
            if atty::is(atty::Stream::Stdout) {
                resp["thread"]
                    .as_object()
                    .ok_or(anyhow!("expected thread from getPostThread"))?;
                let tpv: app_bsky::ThreadPostView = serde_json::from_value(resp["thread"].clone())?;
                pretty::pp_thread_post_view(&tpv)?;
                None
            } else {
                Some(resp)
            }
        }
        Command::Bsky {
            cmd: BskyCommand::Notifications,
        } => {
            require_auth_did(&opt, &mut xrpc_client)?;
            xrpc_client.get(&Nsid::from_str("app.bsky.notification.list")?, None)?
        }
        Command::Bsky {
            cmd: BskyCommand::Post { ref text },
        } => {
            let did = require_auth_did(&opt, &mut xrpc_client)?;
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.createRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": "app.bsky.feed.post",
                    "record": {
                        "text": text,
                        "createdAt": created_at_now(),
                    },
                })),
            )?
        }
        Command::Bsky {
            cmd: BskyCommand::Repost { ref uri },
        } => {
            let did = require_auth_did(&opt, &mut xrpc_client)?;
            params.insert("user".to_string(), uri.repository.to_string());
            params.insert(
                "collection".to_string(),
                uri.collection
                    .clone()
                    .ok_or(anyhow!("collection required"))?,
            );
            params.insert(
                "rkey".to_string(),
                uri.record.clone().ok_or(anyhow!("record key required"))?,
            );
            let existing =
                xrpc_client.get(&Nsid::from_str("com.atproto.repo.getRecord")?, Some(params))?;
            let existing = existing.ok_or(anyhow!("expected record in reponse"))?;
            let cid = existing["cid"]
                .as_str()
                .ok_or(anyhow!("expected 'cid' in record response"))?;
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.createRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": "app.bsky.feed.repost",
                    "record": {
                        "subject": {
                            "uri": uri.to_string(),
                            "cid": cid,
                        },
                        "createdAt": created_at_now(),
                    }
                })),
            )?
        }
        Command::Bsky {
            cmd: BskyCommand::Like { ref uri },
        } => {
            let did = require_auth_did(&opt, &mut xrpc_client)?;
            params.insert("user".to_string(), uri.repository.to_string());
            params.insert(
                "collection".to_string(),
                uri.collection
                    .clone()
                    .ok_or(anyhow!("collection required"))?,
            );
            params.insert(
                "rkey".to_string(),
                uri.record.clone().ok_or(anyhow!("record key required"))?,
            );
            let existing =
                xrpc_client.get(&Nsid::from_str("com.atproto.repo.getRecord")?, Some(params))?;
            let existing = existing.ok_or(anyhow!("expected record in reponse"))?;
            let cid = existing["cid"]
                .as_str()
                .ok_or(anyhow!("expected 'cid' in record response"))?;
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.createRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": "app.bsky.feed.vote",
                    "record": {
                        "subject": { "uri": uri.to_string(), "cid": cid },
                        "direction": "up",
                        "createdAt": created_at_now(),
                    },
                })),
            )?
        }
        Command::Bsky {
            cmd: BskyCommand::Follow { ref uri },
        } => {
            let did = require_auth_did(&opt, &mut xrpc_client)?;
            xrpc_client.post(
                &Nsid::from_str("com.atproto.repo.createRecord")?,
                None,
                Some(json!({
                    "did": did,
                    "collection": "app.bsky.graph.follow",
                    "record": {
                        "subject": { "did": uri.to_string() },
                        "createdAt": created_at_now(),
                    }
                })),
            )?
        }
        Command::Bsky {
            cmd: BskyCommand::Profile { ref name },
        } => {
            let name = name
                .as_ref()
                .map(|v| v.to_string())
                .unwrap_or(require_auth_did(&opt, &mut xrpc_client)?.to_string());
            params.insert("actor".to_string(), name);
            xrpc_client.get(&Nsid::from_str("app.bsky.actor.getProfile")?, Some(params))?
        }
        Command::Bsky {
            cmd: BskyCommand::SearchUsers { query },
        } => {
            params.insert("term".to_string(), query);
            xrpc_client.get(&Nsid::from_str("app.bsky.actor.search")?, Some(params))?
        }
    };
    print_result_json(result)?;
    Ok(())
}