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 {
49        qp.insert(
50            "depth".to_string(),
51            proto_blue_xrpc::QueryValue::Integer(*v),
52        );
53    }
54    if let Some(v) = &p.parent_height {
55        qp.insert(
56            "parentHeight".to_string(),
57            proto_blue_xrpc::QueryValue::Integer(*v),
58        );
59    }
60    {
61        let v = &p.uri;
62        qp.insert(
63            "uri".to_string(),
64            proto_blue_xrpc::QueryValue::String(v.clone()),
65        );
66    }
67    qp
68}
69
70/// Execute the query.
71pub async fn call(
72    client: &proto_blue_xrpc::XrpcClient,
73    params: Option<&Params>,
74    opts: Option<&proto_blue_xrpc::CallOptions>,
75) -> Result<Output, CallError> {
76    let qp = params.map(to_query_params);
77    let response = match client
78        .query("app.bsky.feed.getPostThread", qp.as_ref(), opts)
79        .await
80    {
81        Ok(r) => r,
82        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
83        Err(e) => return Err(CallError::Transport(e)),
84    };
85    Ok(serde_json::from_value(response.data)?)
86}
87
88/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
89#[cfg(feature = "server")]
90pub fn register<F, Fut>(
91    server: proto_blue_xrpc::XrpcServer,
92    handler: F,
93) -> proto_blue_xrpc::XrpcServer
94where
95    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
96    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
97        + Send
98        + 'static,
99{
100    let handler = std::sync::Arc::new(handler);
101    server.query("app.bsky.feed.getPostThread", move |ctx| {
102        let handler = handler.clone();
103        async move {
104            let params = params_from_ctx(&ctx);
105            let out = handler(ctx, params).await?;
106            let value = serde_json::to_value(&out).map_err(|e| {
107                proto_blue_xrpc::XrpcServerError::new(
108                    proto_blue_xrpc::ResponseType::InternalServerError,
109                    format!("output serialize: {e}"),
110                )
111            })?;
112            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
113        }
114    })
115}
116
117#[cfg(feature = "server")]
118fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
119    // Always construct a `Params` — required fields are
120    // validated upstream by the lexicon validator when enabled;
121    // missing values surface as runtime errors from the handler.
122    Some(Params {
123        depth: ctx.params.get("depth").and_then(|v| v.parse::<i64>().ok()),
124        parent_height: ctx
125            .params
126            .get("parentHeight")
127            .and_then(|v| v.parse::<i64>().ok()),
128        uri: (ctx.params.get("uri").cloned())?,
129    })
130}