Skip to main content

proto_blue_api/generated/chat/bsky/convo/
sendMessage.rs

1// Generated by atproto-codegen. Do not edit.
2//! Lexicon: chat.bsky.convo.sendMessage
3
4use serde::{Deserialize, Serialize};
5
6/// XRPC Procedure: chat.bsky.convo.sendMessage
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct Input {
10    pub convo_id: String,
11    pub message: crate::chat::bsky::convo::defs::MessageInput,
12}
13
14pub type Output = crate::chat::bsky::convo::defs::MessageView;
15
16/// Errors a `call()` on this method can return.
17#[derive(Debug, thiserror::Error)]
18pub enum CallError {
19    #[error("{0}")]
20    Xrpc(proto_blue_xrpc::XrpcError),
21    #[error(transparent)]
22    Transport(#[from] proto_blue_xrpc::Error),
23    #[error(transparent)]
24    Json(#[from] serde_json::Error),
25}
26
27fn map_xrpc_error(err: proto_blue_xrpc::XrpcError) -> CallError {
28    CallError::Xrpc(err)
29}
30
31/// Execute the procedure.
32pub async fn call(
33    client: &proto_blue_xrpc::XrpcClient,
34    input: &Input,
35    opts: Option<&proto_blue_xrpc::CallOptions>,
36) -> Result<Output, CallError> {
37    let qp_ref: Option<&proto_blue_xrpc::QueryParams> = None;
38    let body = proto_blue_xrpc::XrpcBody::Json(serde_json::to_value(input)?);
39    let response = match client.procedure("chat.bsky.convo.sendMessage", qp_ref, Some(body), opts).await {
40        Ok(r) => r,
41        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
42        Err(e) => return Err(CallError::Transport(e)),
43    };
44    Ok(serde_json::from_value(response.data)?)
45}
46
47/// Register a typed handler for this procedure on an [`XrpcServer`].
48#[cfg(feature = "server")]
49pub fn register<F, Fut>(
50server: proto_blue_xrpc::XrpcServer,
51handler: F,
52) -> proto_blue_xrpc::XrpcServer
53where
54    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
55    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>> + Send + 'static,
56{
57    let handler = std::sync::Arc::new(handler);
58    server.procedure("chat.bsky.convo.sendMessage", move |ctx| {
59        let handler = handler.clone();
60        async move {
61            let input = match ctx.json_body()? {
62                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InvalidRequest, format!("input deserialize: {e}")))?),
63                None => None,
64            };
65            let out = handler(ctx, input).await?;
66            let value = serde_json::to_value(&out)
67                .map_err(|e| proto_blue_xrpc::XrpcServerError::new(proto_blue_xrpc::ResponseType::InternalServerError, format!("output serialize: {e}")))?;
68            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
69        }
70    })
71}
72