Skip to main content

proto_blue_api/generated/app/bsky/feed/
getPostThread.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: app.bsky.feed.getPostThread
3
4use serde::{Deserialize, Serialize};
5
6/// Get posts in a thread. Does not require auth, but additional metadata and filtering will be applied for authed requests.
7/// XRPC Query: app.bsky.feed.getPostThread
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Params {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub depth: Option<i64>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub parent_height: Option<i64>,
15    pub uri: String,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub struct Output {
21    pub thread: serde_json::Value,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub threadgate: Option<crate::app::bsky::feed::defs::ThreadgateView>,
24}
25
26/// Errors a `call()` on this method can return.
27#[derive(Debug, thiserror::Error)]
28pub enum CallError {
29    #[error("NotFound")]
30    NotFound,
31    #[error("{0}")]
32    Xrpc(proto_blue_xrpc::XrpcError),
33    #[error(transparent)]
34    Transport(#[from] proto_blue_xrpc::Error),
35    #[error(transparent)]
36    Json(#[from] serde_json::Error),
37}
38
39fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
40    match err.error.as_deref() {
41        Some("NotFound") => CallError::NotFound,
42        _ => CallError::Xrpc(err),
43    }
44}
45
46fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
47    let mut qp = proto_blue_xrpc::QueryParams::new();
48    if let Some(v) = &p.depth { qp.insert("depth".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
49    if let Some(v) = &p.parent_height { qp.insert("parentHeight".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
50    { let v = &p.uri; qp.insert("uri".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
51    qp
52}
53
54/// Execute the query.
55pub async fn call(
56    client: &proto_blue_xrpc::XrpcClient,
57    params: Option<&Params>,
58    opts: Option<&proto_blue_xrpc::CallOptions>,
59) -> Result<Output, CallError> {
60    let qp = params.map(to_query_params);
61    let response = match client.query("app.bsky.feed.getPostThread", qp.as_ref(), opts).await {
62        Ok(r) => r,
63        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
64        Err(e) => return Err(CallError::Transport(e)),
65    };
66    Ok(serde_json::from_value(response.data)?)
67}
68
69/// Register a typed handler for this method on an [`XrpcServer`].
70#[cfg(feature = "server")]
71pub fn register<F, Fut>(
72server: proto_blue_xrpc::XrpcServer,
73handler: F,
74) -> proto_blue_xrpc::XrpcServer
75where
76    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
77    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
78{
79    let handler = std::sync::Arc::new(handler);
80    server.query("app.bsky.feed.getPostThread", move |ctx| {
81        let handler = handler.clone();
82        async move {
83            let params = params_from_ctx(&ctx);
84            let out = handler(ctx, params).await?;
85            let value = serde_json::to_value(&out)
86                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
87            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
88        }
89    })
90}
91
92#[cfg(feature = "server")]
93fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
94    // Always construct a `Params` — required fields are
95    // validated upstream by the lexicon validator when enabled;
96    // missing values surface as runtime errors from the handler.
97    Some(Params {
98        depth: ctx.params.get("depth").and_then(|v| v.parse::<i64>().ok()),
99        parent_height: ctx.params.get("parentHeight").and_then(|v| v.parse::<i64>().ok()),
100        uri: (ctx.params.get("uri").cloned())?,
101    })
102}
103