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: proto_blue_syntax::AtUri,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(tag = "$type")]
20pub enum OutputThreadRefs {
21    #[serde(rename = "app.bsky.feed.defs#threadViewPost")]
22    BskyFeedDefsThreadViewPost(Box<crate::app::bsky::feed::defs::ThreadViewPost>),
23    #[serde(rename = "app.bsky.feed.defs#notFoundPost")]
24    BskyFeedDefsNotFoundPost(Box<crate::app::bsky::feed::defs::NotFoundPost>),
25    #[serde(rename = "app.bsky.feed.defs#blockedPost")]
26    BskyFeedDefsBlockedPost(Box<crate::app::bsky::feed::defs::BlockedPost>),
27    #[serde(other)]
28    Other,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct Output {
34    pub thread: OutputThreadRefs,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub threadgate: Option<crate::app::bsky::feed::defs::ThreadgateView>,
37}
38
39/// Errors a `call()` on this method can return.
40#[derive(Debug, thiserror::Error)]
41pub enum CallError {
42    #[error("NotFound")]
43    NotFound,
44    #[error("{0}")]
45    Xrpc(proto_blue_xrpc::XrpcError),
46    #[error(transparent)]
47    Transport(#[from] proto_blue_xrpc::Error),
48    #[error(transparent)]
49    Json(#[from] serde_json::Error),
50}
51
52fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
53    match err.error.as_deref() {
54        Some("NotFound") => CallError::NotFound,
55        _ => CallError::Xrpc(err),
56    }
57}
58
59fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
60    let mut qp = proto_blue_xrpc::QueryParams::new();
61    if let Some(v) = &p.depth {
62        qp.insert(
63            "depth".to_string(),
64            proto_blue_xrpc::QueryValue::Integer(*v),
65        );
66    }
67    if let Some(v) = &p.parent_height {
68        qp.insert(
69            "parentHeight".to_string(),
70            proto_blue_xrpc::QueryValue::Integer(*v),
71        );
72    }
73    {
74        let v = &p.uri;
75        qp.insert(
76            "uri".to_string(),
77            proto_blue_xrpc::QueryValue::String(v.to_string()),
78        );
79    }
80    qp
81}
82
83/// Execute the query.
84pub async fn call(
85    client: &proto_blue_xrpc::XrpcClient,
86    params: Option<&Params>,
87    opts: Option<&proto_blue_xrpc::CallOptions>,
88) -> Result<Output, CallError> {
89    let qp = params.map(to_query_params);
90    let response = match client
91        .query("app.bsky.feed.getPostThread", qp.as_ref(), opts)
92        .await
93    {
94        Ok(r) => r,
95        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
96        Err(e) => return Err(CallError::Transport(e)),
97    };
98    Ok(serde_json::from_value(response.data)?)
99}
100
101/// Register a typed handler for this method on an [`proto_blue_xrpc::XrpcServer`].
102#[cfg(feature = "server")]
103pub fn register<F, Fut>(
104    server: proto_blue_xrpc::XrpcServer,
105    handler: F,
106) -> proto_blue_xrpc::XrpcServer
107where
108    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
109    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
110        + Send
111        + 'static,
112{
113    let handler = std::sync::Arc::new(handler);
114    server.query("app.bsky.feed.getPostThread", move |ctx| {
115        let handler = handler.clone();
116        async move {
117            let params = params_from_ctx(&ctx);
118            let out = handler(ctx, params).await?;
119            let value = serde_json::to_value(&out).map_err(|e| {
120                proto_blue_xrpc::XrpcServerError::new(
121                    proto_blue_xrpc::ResponseType::InternalServerError,
122                    format!("output serialize: {e}"),
123                )
124            })?;
125            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
126        }
127    })
128}
129
130#[cfg(feature = "server")]
131fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
132    // Always construct a `Params` — required fields are
133    // validated upstream by the lexicon validator when enabled;
134    // missing values surface as runtime errors from the handler.
135    Some(Params {
136        depth: ctx.params.get("depth").and_then(|v| v.parse::<i64>().ok()),
137        parent_height: ctx
138            .params
139            .get("parentHeight")
140            .and_then(|v| v.parse::<i64>().ok()),
141        uri: (ctx
142            .params
143            .get("uri")
144            .and_then(|v| proto_blue_syntax::AtUri::new(v).ok()))?,
145    })
146}