Skip to main content

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

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: app.bsky.feed.searchPosts
3
4use serde::{Deserialize, Serialize};
5
6/// Find posts matching search criteria, returning views of those posts. Note that this API endpoint may require authentication (eg, not public) for some service providers and implementations.
7/// XRPC Query: app.bsky.feed.searchPosts
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Params {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub author: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub cursor: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub domain: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub lang: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub limit: Option<i64>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub mentions: Option<String>,
23    pub q: String,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub since: Option<String>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub sort: Option<String>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub tag: Option<Vec<String>>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub until: Option<String>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub url: Option<String>,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(rename_all = "camelCase")]
38pub struct Output {
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub cursor: Option<String>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub hits_total: Option<i64>,
43    pub posts: Vec<crate::app::bsky::feed::defs::PostView>,
44}
45
46/// Errors a `call()` on this method can return.
47#[derive(Debug, thiserror::Error)]
48pub enum CallError {
49    #[error("BadQueryString")]
50    BadQueryString,
51    #[error("{0}")]
52    Xrpc(proto_blue_xrpc::XrpcError),
53    #[error(transparent)]
54    Transport(#[from] proto_blue_xrpc::Error),
55    #[error(transparent)]
56    Json(#[from] serde_json::Error),
57}
58
59fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
60    match err.error.as_deref() {
61        Some("BadQueryString") => CallError::BadQueryString,
62        _ => CallError::Xrpc(err),
63    }
64}
65
66fn to_query_params(p: &Params) -> proto_blue_xrpc::QueryParams {
67    let mut qp = proto_blue_xrpc::QueryParams::new();
68    if let Some(v) = &p.author { qp.insert("author".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
69    if let Some(v) = &p.cursor { qp.insert("cursor".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
70    if let Some(v) = &p.domain { qp.insert("domain".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
71    if let Some(v) = &p.lang { qp.insert("lang".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
72    if let Some(v) = &p.limit { qp.insert("limit".to_string(), proto_blue_xrpc::QueryValue::Integer(*v)); }
73    if let Some(v) = &p.mentions { qp.insert("mentions".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
74    { let v = &p.q; qp.insert("q".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
75    if let Some(v) = &p.since { qp.insert("since".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
76    if let Some(v) = &p.sort { qp.insert("sort".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
77    if let Some(v) = &p.tag { qp.insert("tag".to_string(), proto_blue_xrpc::QueryValue::Array(v.iter().map(|x| proto_blue_xrpc::QueryValue::String(x.clone())).collect())); }
78    if let Some(v) = &p.until { qp.insert("until".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
79    if let Some(v) = &p.url { qp.insert("url".to_string(), proto_blue_xrpc::QueryValue::String(v.clone())); }
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.query("app.bsky.feed.searchPosts", qp.as_ref(), opts).await {
91        Ok(r) => r,
92        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
93        Err(e) => return Err(CallError::Transport(e)),
94    };
95    Ok(serde_json::from_value(response.data)?)
96}
97
98/// Register a typed handler for this method on an [`XrpcServer`].
99#[cfg(feature = "server")]
100pub fn register<F, Fut>(
101server: proto_blue_xrpc::XrpcServer,
102handler: F,
103) -> proto_blue_xrpc::XrpcServer
104where
105    F: Fn(proto_blue_xrpc::HandlerContext, Option<Params>) -> Fut + Send + Sync + 'static,
106    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
107{
108    let handler = std::sync::Arc::new(handler);
109    server.query("app.bsky.feed.searchPosts", move |ctx| {
110        let handler = handler.clone();
111        async move {
112            let params = params_from_ctx(&ctx);
113            let out = handler(ctx, params).await?;
114            let value = serde_json::to_value(&out)
115                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
116            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
117        }
118    })
119}
120
121#[cfg(feature = "server")]
122fn params_from_ctx(ctx: &proto_blue_xrpc::HandlerContext) -> Option<Params> {
123    // Always construct a `Params` — required fields are
124    // validated upstream by the lexicon validator when enabled;
125    // missing values surface as runtime errors from the handler.
126    Some(Params {
127        author: ctx.params.get("author").cloned(),
128        cursor: ctx.params.get("cursor").cloned(),
129        domain: ctx.params.get("domain").cloned(),
130        lang: ctx.params.get("lang").cloned(),
131        limit: ctx.params.get("limit").and_then(|v| v.parse::<i64>().ok()),
132        mentions: ctx.params.get("mentions").cloned(),
133        q: (ctx.params.get("q").cloned())?,
134        since: ctx.params.get("since").cloned(),
135        sort: ctx.params.get("sort").cloned(),
136        tag: Some(ctx.params.get("tag").map(|v| v.split(',').map(String::from).collect::<Vec<_>>()).unwrap_or_default()),
137        until: ctx.params.get("until").cloned(),
138        url: ctx.params.get("url").cloned(),
139    })
140}
141