cli-sky 1.0.0

A CLI AT protocol client
Documentation
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
use std::fs::File;
use std::io::Write;
use std::process;
use std::fs;
use std::io::{self, BufRead};
use std::str::FromStr;

use keyring::error::Error;
use bsky_sdk::BskyAgent;
use atrium_api::types::string::Datetime;
use atrium_api::app::bsky::feed::get_timeline::ParametersData;
use bsky_sdk::agent::config::{Config, FileStore};
use atrium_api::app::bsky::feed::post::RecordData as PostRecordData;
use serde_json::from_value;
use serde::{Deserialize, Serialize};

use atrium_api::com::atproto::repo::create_record::InputData;


async fn print_logo() {
    println!("{}[2J", 27 as char);
    println!("  /██████  /██       /██████        /██████  /██   /██ /██     /██");
    println!(" /██__  ██| ██      |_  ██_/       /██__  ██| ██  /██/|  ██   /██/");
    println!("| ██  |__/| ██        | ██        | ██  |__/| ██ /██/  |  ██ /██/ ");
    println!("| ██      | ██        | ██        |  ██████ | █████/    |  ████/  ");
    println!("| ██      | ██        | ██         |____  ██| ██  ██     |  ██/   ");
    println!("| ██    ██| ██        | ██         /██  | ██| ██|  ██     | ██    ");
    println!("|  ██████/| ████████ /██████      |  ██████/| ██ |  ██    | ██    ");
    println!(" |______/ |________/|______/       |______/ |__/  |__/    |__/   ");
    println!("");
    println!("");
}


#[derive(Serialize, Deserialize)]
struct BlogPost {
    title: String,
    text: String,
    tags: Option<Vec<String>>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    print_logo().await;

    ask_to_login().await?;
    Ok(())
}

async fn login() -> Result<(), Box<dyn std::error::Error>> {
    println!("Welcome!!! Please enter your AT Protocol Handle");

    loop {
        println!("Login:");

        let mut uname = String::new();
        std::io::stdin().read_line(&mut uname).expect("Failed to read");

        println!("");
        println!("what is your password?");

   //     std::io::stdout().flush().unwrap();
     //   let pwd = read_password().unwrap();
       // println!("{pwd}");

       let pwd = rpassword::prompt_password("Password: ").unwrap();
       println!("{pwd}");

        let agent = create_agent(uname.trim().to_string(), pwd.to_string()).await?;


        match start_session(agent).await {
            Ok(_) => {
                // Session started successfully
                return Ok(());
            }
            Err(e) => {
                println!("\nLogin failed. Please check your handle and password.");
                println!("{e}");
                // Optionally, you could log the detailed error for debugging:
                // eprintln!("Error details: {}", e);
                continue;
            }
        }
    }
}

async fn ask_to_login() -> Result<(), Box<dyn std::error::Error>> {
    let service = "cli_sky";
    let username = "user";
    let entry = keyring::Entry::new(service, username)?;

    let mut pwd = String::new();


    match entry.get_password() {
        Ok(secret) => {pwd = secret},
        Err(Error::NoEntry) => {
            login().await?;
        }
        Err(e) => {
            eprintln!("Failed to get password: {}", e);
        }
    }


    let mut file = File::create("config.json")?;
    file.write_all(pwd.as_bytes())?;

    let agent = BskyAgent::builder()
    .config(Config::load(&FileStore::new("config.json")).await?)
    .build()
    .await?;

    match start_session(agent).await {
        Ok(_) => {
            // Session started successfully
            return Ok(());
        }
        Err(e) => {
            println!("\nLogin failed. Please enter new details.");
            println!("{e}");
            login().await?;
            Ok(())
        }
    }
}

async fn create_agent(uname: String, pwd: String) -> Result<BskyAgent, Box<dyn std::error::Error>> {
    let agent = BskyAgent::builder().build().await?;
    let session = agent.login(&uname,&pwd).await?;
    println!("Logged in! DID = {}", session.did.to_string());
    return Ok(agent);
}

async fn start_session(agent: BskyAgent) -> Result<(), Box<dyn std::error::Error>> {
    save_session(&agent).await?;


    match menu(agent).await {
        Ok(_) => {
            // Session started successfully
            return Ok(());
        }
        Err(e) => {
            println!("there is an error here");
            return Err(e);
        }
    }

}

async fn save_session(agent: &BskyAgent) -> Result<(), Box<dyn std::error::Error>> {
    agent.to_config()
    .await
    .save(&FileStore::new("config.json"))
    .await?;
    println!("Session saved to config.json");

    //deserialize the json
    let config = fs::read_to_string("config.json")?;

    //create an entry
    let service = "cli_sky";
    let username = "user";
    let entry = keyring::Entry::new(service, username)?;
    entry.set_password(&config)?;

    //delete the config.json file
    fs::remove_file("config.json")?;

    Ok(())
}

fn menu(agent: BskyAgent) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>> {
    Box::pin(async move {
        print!("{}[2J", 27 as char);
        print_logo().await;
        println!("");
        println!("What would you like to do?");
        println!("0: Exit");
        println!("1: Text Post");
        println!("2: Following Feed");
        println!("3: Blog Post");

        println!("");

        let mut input = String::new();

        loop {
            std::io::stdin().read_line(&mut input).expect("Failed to read menu input");

            match input.trim().parse::<i32>() {
                Ok(0) => {
                    println!("Exiting...");
                    process::exit(0);
                }
                Ok(1) => {
                    println!("writing post");
                    make_post(agent.clone()).await?;  // Assuming agent is cloneable
                    break;
                }
                Ok(2) => {
                    println!("following feed");
                    following_feed(agent.clone()).await?;
                    break;
                }
                Ok(3) => {
                    println!("blog post");
                    write_blog(agent.clone()).await?;
                    break;
                }
                Ok(4) => {
                    println!("find a blog");
                    list_user_blog(&agent).await?;
                    break;
                }
                Ok(5) => {
                    println!("find a blog (experimental)");
                    list_user_blog_experimental(&agent).await?;
                    break;
                }
                _ => {
                    input.clear();
                    println!("Invalid input");
                    continue;
                }
            };
        }

        Ok(())
    })
}


async fn make_post(agent: BskyAgent) -> Result<(), Box<dyn std::error::Error>> {
    let mut content = String::new();
    io::stdin().read_line(&mut content).expect("Failed to read post content");


    agent
        .create_record(atrium_api::app::bsky::feed::post::RecordData {
            created_at: Datetime::now(),
            embed: None,
            entities: None,
            facets: None,
            labels: None,
            langs: None,
            reply: None,
            tags: None,
            text: content,
        })
        .await?;

    println!("Post sent to the Atmosphere!!!");
    menu(agent).await?;
    Ok(())
}

async fn following_feed(agent: BskyAgent)-> Result<(), Box<dyn std::error::Error>>{

    // Fetch the first page of timeline (default cursor, default limit)
    let output = agent.clone()
        .api.app.bsky.feed.get_timeline(
            ParametersData {
                cursor: None,
                limit: None,
                algorithm: Some("reverse-chronological".to_string()),
            }
            .into(),
        )
        .await?;

    // Iterate over posts
    for feed_post in &output.feed {
        let author = &feed_post.post.author;

        // Try deserializing the record field into a known post structure
        let record_value = serde_json::to_value::<atrium_api::types::Unknown>(feed_post.post.record.clone())?;
        let maybe_post: Result<PostRecordData, _> =
            from_value(record_value);

        match maybe_post {
            Ok(post_record) => {
                println!("{}[2J", 27 as char);
                println!(
                    "{} \n(@{})\n\n {}",
                    author.display_name.clone().unwrap_or_default(),
                    author.handle.to_string(),
                    post_record.text
                );
                println!("\nType 'exit' to quit.\n");


                let mut input = String::new();
                std::io::stdin().read_line( &mut input).expect("Failed to read");
                match input.trim() {
                    "exit" => {process::exit(0)}
                    "menu" => {menu(agent.clone()).await?},
                    _ => {}
                }
            }
            Err(e) => {
                eprintln!(
                    "Failed to parse post record from {} (@{}): {}",
                    author.display_name.clone().unwrap_or_default(),
                    author.handle.to_string(),
                    e
                );
            }
        }
    }
    process::exit(0);
}

async fn write_blog(agent: BskyAgent) -> Result<(), Box<dyn std::error::Error>> {
    println!("Enter blog title:");
    let mut title = String::new();
    io::stdin().read_line(&mut title)?;
    let title = title.trim().to_string();
    
    println!("Paste blog content in markdown (type END in all caps on a new line to finish):");
    let stdin = io::stdin();
    let mut content = String::new();
    
    for line in stdin.lock().lines() {
        let line = line?;
        if line.trim() == "END" { break; }
        content.push_str(&line);
        content.push('\n');
    }
    let content = content.trim().to_string();
    
    println!("Enter tags (comma-separated, or press Enter for none):");
    let mut tags_input = String::new();
    io::stdin().read_line(&mut tags_input)?;
    let tags: Option<Vec<String>> = if tags_input.trim().is_empty() {
        None
    } else {
        Some(tags_input.trim().split(',').map(|s| s.trim().to_string()).collect())
    };


    let record_json = serde_json::json!({
        "$type": "com.macroblog.blog.post",
        "title": title,
        "text": content,
        "tags": tags,
    });

    let session_info = agent.api.com.atproto.server.get_session().await?;
    let did = session_info.did.clone(); // or however you get the current user's DID

    let record_unknown: atrium_api::types::Unknown = serde_json::from_value(record_json)?;
    let input = InputData {
        collection: "com.macroblog.blog.post".parse()?, // Your lexicon's NSID
        repo: did.into(),
        rkey: None, // Let the server pick a key
        record: record_unknown,
        swap_commit: None, 
        validate: None,
    };
    agent.api.com.atproto.repo.create_record(input.into()).await?;

    println!("Blog post created successfully!");
    menu(agent).await?;
    Ok(())
} 


pub async fn list_user_blog(agent: &BskyAgent) -> Result<(), Box<dyn std::error::Error>> {
    println!("Enter blogger's handle: ");
    io::stdout().flush()?;

    let mut name = String::new();
    io::stdin().read_line(&mut name)?;
    let handle = name.trim();
    println!("Looking up handle: {}", handle);

    // Get the author's DID
    let did = match agent.api.com.atproto.identity.resolve_handle(
        atrium_api::com::atproto::identity::resolve_handle::ParametersData {
            handle: atrium_api::types::string::Handle::from_str(handle)?,
        }
        .into(),
    ).await {
        Ok(response) => {
            println!("Successfully resolved handle to DID: {:?}", response.did);
            response.did.clone()
        },
        Err(e) => {
            println!("Error: Could not find user with handle '{}'. Please check the handle and try again.", handle);
            println!("Error details: {}", e);
            menu(agent.clone()).await?;
            return Ok(());
        }
    };

    println!("Fetching blog posts for DID: {:?}", did);

    // Get the blog posts using listRecords
    let params = atrium_api::com::atproto::repo::list_records::ParametersData {
        repo: atrium_api::types::string::AtIdentifier::from_str(&format!("at://{}", did.to_string()))?,
        collection: "com.macroblog.blog.post".parse()?,
        limit: Some(50.try_into()?),
        cursor: None,
        reverse: None,
    };
    
    println!("List records parameters: {:?}", params);

    let response = match agent
        .api
        .com
        .atproto
        .repo
        .list_records(params.into())
        .await {
            Ok(response) => {
                println!("Successfully fetched records. Number of records: {}", response.records.len());
                if response.records.is_empty() {
                    println!("No records found in response");
                } else {
                    let record_value = serde_json::to_value::<atrium_api::types::Unknown>(response.records[0].value.clone())?;
                    println!("First record value: {:?}", record_value);
                }
                response
            },
            Err(e) => {
                println!("Error: Could not fetch blog posts. The user may not have any blog posts or they may be private.");
                println!("Error details: {}", e);
                println!("Error type: {:?}", e);
                println!("DID used: {:?}", did);
                println!("Collection: com.macroblog.blog.post");
                println!("Full repository identifier: at://{}", did.to_string());
                println!("Debug info - DID string: {}", did.to_string());
                println!("Debug info - Collection string: com.macroblog.blog.post");
                println!("Debug info - Raw error: {:?}", e);
                return Ok(());
            }
        };

    if response.records.is_empty() {
        println!("No blog posts found for user '{}'.", handle);
        menu(agent.clone()).await?;
        return Ok(());
    }

    // Print out blog posts
    for (i, record) in response.records.iter().enumerate() {
        println!("\nProcessing record {} of {}", i + 1, response.records.len());
        let record_value = serde_json::to_value::<atrium_api::types::Unknown>(record.value.clone())?;
        
        // Debug print the record type
        println!("Record value: {:?}", record_value);
        
        // Parse the blog post
        match serde_json::from_value::<BlogPost>(record_value.clone()) {
            Ok(blog_post) => {
                println!("\n{}", "=".repeat(50));
                println!(
                    "Title: {}\n\n{}",
                    blog_post.title,
                    blog_post.text
                );
                if let Some(tags) = blog_post.tags {
                    println!("\nTags: {}", tags.join(", "));
                }
                println!("{}", "=".repeat(50));
            }
            Err(e) => {
                println!("Error parsing blog post: {}", e);
                println!("Raw record value: {:?}", record_value);
                continue;
            }
        }
        println!("\nPress Enter to continue to next post...");
        let mut dummy = String::new();
        io::stdin().read_line(&mut dummy)?;
    }

    println!("\nPress Enter to return to menu...");
    let mut dummy = String::new();
    io::stdin().read_line(&mut dummy)?;  
    menu(agent.clone()).await?;  
    Ok(())
}

pub async fn list_user_blog_experimental(agent: &BskyAgent) -> Result<(), Box<dyn std::error::Error>> {
    println!("Enter blogger's handle: ");
    io::stdout().flush()?;

    let mut name = String::new();
    io::stdin().read_line(&mut name)?;
    let handle = name.trim();
    println!("Looking up handle: {}", handle);

    // Get the author's DID
    let did = match agent.api.com.atproto.identity.resolve_handle(
        atrium_api::com::atproto::identity::resolve_handle::ParametersData {
            handle: atrium_api::types::string::Handle::from_str(handle)?,
        }
        .into(),
    ).await {
        Ok(response) => {
            println!("Successfully resolved handle to DID: {:?}", response.did);
            response.did.clone()
        },
        Err(e) => {
            println!("Error: Could not find user with handle '{}'. Please check the handle and try again.", handle);
            println!("Error details: {}", e);
            return Ok(());
        }
    };

    println!("Fetching posts for DID: {:?}", did);

    // First try to get the user's profile to verify access
    match agent.api.app.bsky.actor.get_profile(
        atrium_api::app::bsky::actor::get_profile::ParametersData {
            actor: atrium_api::types::string::AtIdentifier::from_str(&did.to_string())?,
        }
        .into(),
    ).await {
        Ok(profile) => {
            println!("Successfully accessed profile for: {}", profile.display_name.as_ref().unwrap_or(&"Unknown".to_string()));
        },
        Err(e) => {
            println!("Warning: Could not access profile. This might indicate the account is private or suspended.");
            println!("Profile error details: {}", e);
            return Ok(());
        }
    }

    // Try to get the author's feed using the feed API first
    println!("\nAttempting to fetch author feed...");
    let feed_params = atrium_api::app::bsky::feed::get_author_feed::ParametersData {
        actor: atrium_api::types::string::AtIdentifier::from_str(&did.to_string())?,
        cursor: None,
        filter: None, // Try without filter first
        limit: Some(50.try_into()?),
        include_pins: None,
    };

    println!("Feed parameters: {:?}", feed_params);

    match agent
        .api
        .app
        .bsky
        .feed
        .get_author_feed(feed_params.into())
        .await {
            Ok(feed_response) => {
                println!("Successfully fetched feed. Number of posts: {}", feed_response.feed.len());
                println!("Feed response: {:?}", feed_response);
                
                if feed_response.feed.is_empty() {
                    println!("No posts found in feed, trying repository API...");
                    
                    // Try repository API as fallback
                    let params = atrium_api::com::atproto::repo::list_records::ParametersData {
                        repo: atrium_api::types::string::AtIdentifier::from_str(&format!("at://{}", did.to_string()))?,
                        collection: "app.bsky.feed.post".parse()?,
                        limit: Some(100.try_into()?),
                        cursor: None,
                        reverse: Some(true), // Try getting most recent posts first
                    };
                    
                    println!("List records parameters: {:?}", params);

                    match agent
                        .api
                        .com
                        .atproto
                        .repo
                        .list_records(params.into())
                        .await {
                            Ok(response) => {
                                println!("Successfully fetched records. Number of records: {}", response.records.len());
                                println!("Records response: {:?}", response);
                                
                                if response.records.is_empty() {
                                    println!("No posts found for user '{}'.", handle);
                                    return Ok(());
                                }

                                // Print out records
                                for (i, record) in response.records.iter().enumerate() {
                                    println!("\nProcessing record {} of {}", i + 1, response.records.len());
                                    
                                    // Convert the record value to a JSON value for easier access
                                    let record_value = match serde_json::to_value(&record.value) {
                                        Ok(value) => value,
                                        Err(e) => {
                                            println!("Error converting record to JSON: {}", e);
                                            continue;
                                        }
                                    };

                                    // Get the record type
                                    let record_type = record_value.get("$type").and_then(|t| t.as_str()).unwrap_or("unknown");
                                    println!("Record type: {}", record_type);

                                    // Try to parse as a post record
                                    match serde_json::from_value::<PostRecordData>(record_value.clone()) {
                                        Ok(post) => {
                                            println!("\n{}", "=".repeat(50));
                                            println!("Text: {}", post.text);
                                            
                                            // Print any embedded content
                                            if let Some(embed) = post.embed {
                                                println!("\nEmbedded content:");
                                                println!("{:?}", embed);
                                            }
                                            
                                            // Print any facets
                                            if let Some(facets) = post.facets {
                                                println!("\nFacets:");
                                                for facet in facets {
                                                    println!("{:?}", facet);
                                                }
                                            }
                                            
                                            // Print any tags
                                            if let Some(tags) = post.tags {
                                                println!("\nTags: {}", tags.join(", "));
                                            }
                                            
                                            println!("{}", "=".repeat(50));
                                        },
                                        Err(e) => {
                                            println!("Could not parse as post record: {}", e);
                                            // Fall back to displaying raw text if available
                                            if let Some(text) = record_value.get("text").and_then(|t| t.as_str()) {
                                                println!("\n{}", "=".repeat(50));
                                                println!("Text: {}", text);
                                                println!("{}", "=".repeat(50));
                                            }
                                        }
                                    }
                                    
                                    println!("\nPress Enter to continue to next post...");
                                    let mut dummy = String::new();
                                    io::stdin().read_line(&mut dummy)?;
                                }
                            },
                            Err(e) => {
                                println!("Error: Could not fetch records.");
                                println!("Error details: {}", e);
                                println!("Error type: {:?}", e);
                                println!("DID used: {:?}", did);
                                println!("Collection: app.bsky.feed.post");
                                println!("Full repository identifier: at://{}", did.to_string());
                                println!("Debug info - DID string: {}", did.to_string());
                                println!("Debug info - Raw error: {:?}", e);
                                
                                // Try one more time with a different collection
                                println!("\nTrying with different collection...");
                                let params = atrium_api::com::atproto::repo::list_records::ParametersData {
                                    repo: atrium_api::types::string::AtIdentifier::from_str(&format!("at://{}", did.to_string()))?,
                                    collection: "com.atproto.repo.strongRef".parse()?,
                                    limit: Some(100.try_into()?),
                                    cursor: None,
                                    reverse: Some(true),
                                };
                                
                                println!("List records parameters (second attempt): {:?}", params);
                                
                                match agent
                                    .api
                                    .com
                                    .atproto
                                    .repo
                                    .list_records(params.into())
                                    .await {
                                        Ok(response) => {
                                            println!("Successfully fetched records (second attempt). Number of records: {}", response.records.len());
                                            println!("Records response (second attempt): {:?}", response);
                                        },
                                        Err(e) => {
                                            println!("Error in second attempt: {}", e);
                                            println!("Error type: {:?}", e);
                                            
                                            // Try one last time with the relay service
                                            println!("\nTrying with relay service...");
                                            let params = atrium_api::com::atproto::sync::get_repo::ParametersData {
                                                did: did.clone(),
                                                since: None,
                                            };
                                            
                                            println!("Get repo parameters: {:?}", params);
                                            
                                            match agent
                                                .api
                                                .com
                                                .atproto
                                                .sync
                                                .get_repo(params.into())
                                                .await {
                                                    Ok(response) => {
                                                        println!("Successfully fetched repo from relay service");
                                                        println!("Repo response: {:?}", response);
                                                    },
                                                    Err(e) => {
                                                        println!("Error fetching from relay service: {}", e);
                                                        println!("Error type: {:?}", e);
                                                    }
                                                }
                                        }
                                    }
                                
                                return Ok(());
                            }
                        }
                } else {
                    // Print out feed posts
                    for (i, feed_view) in feed_response.feed.iter().enumerate() {
                        println!("\nProcessing post {} of {}", i + 1, feed_response.feed.len());
                        
                        // Get the record value and print it directly
                        let record_value = serde_json::to_value::<atrium_api::types::Unknown>(feed_view.post.record.clone())?;
                        println!("\n{}", "=".repeat(50));
                        println!("Record type: {:?}", record_value.get("$type"));
                        
                        // Try to get the text field directly
                        if let Some(text) = record_value.get("text").and_then(|t| t.as_str()) {
                            println!("Text: {}", text);
                        }
                        
                        // Try to get the title field if it exists
                        if let Some(title) = record_value.get("title").and_then(|t| t.as_str()) {
                            println!("Title: {}", title);
                        }
                        
                        // Try to get tags if they exist
                        if let Some(tags) = record_value.get("tags").and_then(|t| t.as_array()) {
                            let tag_strings: Vec<String> = tags.iter()
                                .filter_map(|tag| tag.as_str().map(String::from))
                                .collect();
                            if !tag_strings.is_empty() {
                                println!("Tags: {}", tag_strings.join(", "));
                            }
                        }
                        
                        println!("{}", "=".repeat(50));
                        println!("\nPress Enter to continue to next post...");
                        let mut dummy = String::new();
                        io::stdin().read_line(&mut dummy)?;
                    }
                }
            },
            Err(e) => {
                println!("Error fetching feed: {}", e);
                println!("Error type: {:?}", e);
                println!("Trying repository API as fallback...");
                
                // Fall back to repository API
                let params = atrium_api::com::atproto::repo::list_records::ParametersData {
                    repo: atrium_api::types::string::AtIdentifier::from_str(&format!("at://{}", did.to_string()))?,
                    collection: "app.bsky.feed.post".parse()?,
                    limit: Some(100.try_into()?),
                    cursor: None,
                    reverse: Some(true),
                };
                
                println!("List records parameters: {:?}", params);

                match agent
                    .api
                    .com
                    .atproto
                    .repo
                    .list_records(params.into())
                    .await {
                        Ok(response) => {
                            println!("Successfully fetched records. Number of records: {}", response.records.len());
                            println!("Records response: {:?}", response);
                            
                            if response.records.is_empty() {
                                println!("No posts found for user '{}'.", handle);
                                return Ok(());
                            }

                            // Print out records
                            for (i, record) in response.records.iter().enumerate() {
                                println!("\nProcessing record {} of {}", i + 1, response.records.len());
                                
                                // Convert the record value to a JSON value for easier access
                                let record_value = match serde_json::to_value(&record.value) {
                                    Ok(value) => value,
                                    Err(e) => {
                                        println!("Error converting record to JSON: {}", e);
                                        continue;
                                    }
                                };

                                // Get the record type
                                let record_type = record_value.get("$type").and_then(|t| t.as_str()).unwrap_or("unknown");
                                println!("Record type: {}", record_type);

                                // Try to parse as a post record
                                match serde_json::from_value::<PostRecordData>(record_value.clone()) {
                                    Ok(post) => {
                                        println!("\n{}", "=".repeat(50));
                                        println!("Text: {}", post.text);
                                        
                                        // Print any embedded content
                                        if let Some(embed) = post.embed {
                                            println!("\nEmbedded content:");
                                            println!("{:?}", embed);
                                        }
                                        
                                        // Print any facets
                                        if let Some(facets) = post.facets {
                                            println!("\nFacets:");
                                            for facet in facets {
                                                println!("{:?}", facet);
                                            }
                                        }
                                        
                                        // Print any tags
                                        if let Some(tags) = post.tags {
                                            println!("\nTags: {}", tags.join(", "));
                                        }
                                        
                                        println!("{}", "=".repeat(50));
                                    },
                                    Err(e) => {
                                        println!("Could not parse as post record: {}", e);
                                        // Fall back to displaying raw text if available
                                        if let Some(text) = record_value.get("text").and_then(|t| t.as_str()) {
                                            println!("\n{}", "=".repeat(50));
                                            println!("Text: {}", text);
                                            println!("{}", "=".repeat(50));
                                        }
                                    }
                                }
                                
                                println!("\nPress Enter to continue to next post...");
                                let mut dummy = String::new();
                                io::stdin().read_line(&mut dummy)?;
                            }
                        },
                        Err(e) => {
                            println!("Error: Could not fetch records.");
                            println!("Error details: {}", e);
                            println!("Error type: {:?}", e);
                            println!("DID used: {:?}", did);
                            println!("Collection: app.bsky.feed.post");
                            println!("Full repository identifier: at://{}", did.to_string());
                            println!("Debug info - DID string: {}", did.to_string());
                            println!("Debug info - Raw error: {:?}", e);
                            return Ok(());
                        }
                    }
            }
        }

    println!("\nPress Enter to return to menu...");
    let mut dummy = String::new();
    io::stdin().read_line(&mut dummy)?;  
    Ok(())
}