Skip to main content

proto_blue_api/generated/com/atproto/admin/
sendEmail.rs

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