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