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