Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/commands/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub async fn view(ctx: &Ctx, post: &str, depth: u32, parents: u32) -> anyhow::Re
};
let mut posts = Vec::new();
flatten_thread(thread, &mut posts);
if posts.is_empty() {
return Err(thread_unavailable(thread, uri.as_str()).into());
}
for post in &posts {
ctx.out.item(post, render_post);
}
Expand Down Expand Up @@ -81,6 +84,43 @@ fn flatten_replies(node: &Value, out: &mut Vec<Value>) {
}
}

/// `getPostThread` answers a blocked or missing post with a `2xx` whose
/// `thread` is a `#blockedPost` / `#notFoundPost` stub carrying no `post`
/// field, so flattening yields nothing and `view` used to print nothing
/// and exit 0 — the one silent face in the CLI. Turn the stub into the
/// same `ApiError::Api` the profile path raises, keeping the status the
/// server actually sent and letting `kind` carry the meaning
/// (`BlockedByActor` / `BlockedActor` → exit 1, `NotFound` → exit 4).
fn thread_unavailable(thread: &Value, uri: &str) -> ApiError {
let root = thread.get("$type").and_then(Value::as_str).unwrap_or("");
match root {
"app.bsky.feed.defs#blockedPost" => {
let blocked_by = thread
.pointer("/author/viewer/blockedBy")
.and_then(Value::as_bool)
.unwrap_or(false);
let kind = if blocked_by {
"BlockedByActor"
} else {
"BlockedActor"
};
ApiError::Api {
status: 200,
kind: kind.into(),
message: format!("getPostThread returned a blockedPost stub for {uri}"),
}
}
"app.bsky.feed.defs#notFoundPost" => ApiError::Api {
status: 200,
kind: "NotFound".into(),
message: format!("getPostThread returned a notFoundPost stub for {uri}"),
},
other => ApiError::Unexpected(format!(
"getPostThread thread for {uri} carried no posts (root $type {other:?})"
)),
}
}

/// `fulmar me` — the author feed of the session's own account,
/// straight from the session file (no resolution round-trip).
pub async fn me(ctx: &Ctx, filter: &str, page: &PageArgs) -> anyhow::Result<()> {
Expand Down
65 changes: 65 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,68 @@ async fn two_processes_racing_one_session_never_double_spend_a_refresh_token() {
"chain must have advanced cleanly, got {final_refresh:?}"
);
}

#[tokio::test(flavor = "multi_thread")]
async fn view_blocked_post_exits_1_not_silent_0() {
let server = MockServer::start().await;
let uri = format!("at://{DID}/app.bsky.feed.post/3wall");
Mock::given(method("GET"))
.and(path("/xrpc/app.bsky.feed.getPostThread"))
.and(query_param("uri", uri.as_str()))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"thread": {
"$type": "app.bsky.feed.defs#blockedPost",
"uri": uri,
"blocked": true,
"author": { "did": DID, "viewer": { "blockedBy": true } },
},
})))
.expect(1)
.mount(&server)
.await;

let dir = tempfile::tempdir().expect("tempdir");
let session = seed_session(&dir, &server.uri(), "access-1", "refresh-1");
let args_uri = uri.clone();
let (code, stdout, stderr) = tokio::task::spawn_blocking(move || {
run(fulmar(&session).args(["view", args_uri.as_str(), "--json"]))
})
.await
.expect("join");

assert_eq!(code, 1, "stdout: {stdout} stderr: {stderr}");
assert!(stdout.is_empty(), "stdout: {stdout}");
assert!(stderr.contains("BlockedByActor"), "stderr: {stderr}");
}

#[tokio::test(flavor = "multi_thread")]
async fn view_not_found_stub_exits_4() {
let server = MockServer::start().await;
let uri = format!("at://{DID}/app.bsky.feed.post/3gone");
Mock::given(method("GET"))
.and(path("/xrpc/app.bsky.feed.getPostThread"))
.and(query_param("uri", uri.as_str()))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"thread": {
"$type": "app.bsky.feed.defs#notFoundPost",
"uri": uri,
"notFound": true,
},
})))
.expect(1)
.mount(&server)
.await;

let dir = tempfile::tempdir().expect("tempdir");
let session = seed_session(&dir, &server.uri(), "access-1", "refresh-1");
let args_uri = uri.clone();
let (code, stdout, stderr) = tokio::task::spawn_blocking(move || {
run(fulmar(&session).args(["view", args_uri.as_str(), "--json"]))
})
.await
.expect("join");

assert_eq!(code, 4, "stdout: {stdout} stderr: {stderr}");
assert!(stdout.is_empty(), "stdout: {stdout}");
assert!(stderr.contains("NotFound"), "stderr: {stderr}");
}