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
50        .procedure("com.atproto.admin.sendEmail", qp_ref, Some(body), opts)
51        .await
52    {
53        Ok(r) => r,
54        Err(proto_blue_xrpc::Error::Xrpc(x)) => return Err(map_xrpc_error(x)),
55        Err(e) => return Err(CallError::Transport(e)),
56    };
57    Ok(serde_json::from_value(response.data)?)
58}
59
60/// Register a typed handler for this procedure on an [`proto_blue_xrpc::XrpcServer`].
61#[cfg(feature = "server")]
62pub fn register<F, Fut>(
63    server: proto_blue_xrpc::XrpcServer,
64    handler: F,
65) -> proto_blue_xrpc::XrpcServer
66where
67    F: Fn(proto_blue_xrpc::HandlerContext, Option<Input>) -> Fut + Send + Sync + 'static,
68    Fut: std::future::Future<Output = Result<Output, proto_blue_xrpc::XrpcServerError>>
69        + Send
70        + 'static,
71{
72    let handler = std::sync::Arc::new(handler);
73    server.procedure("com.atproto.admin.sendEmail", move |ctx| {
74        let handler = handler.clone();
75        async move {
76            let input = match ctx.json_body()? {
77                Some(v) => Some(serde_json::from_value::<Input>(v).map_err(|e| {
78                    proto_blue_xrpc::XrpcServerError::new(
79                        proto_blue_xrpc::ResponseType::InvalidRequest,
80                        format!("input deserialize: {e}"),
81                    )
82                })?),
83                None => None,
84            };
85            let out = handler(ctx, input).await?;
86            let value = serde_json::to_value(&out).map_err(|e| {
87                proto_blue_xrpc::XrpcServerError::new(
88                    proto_blue_xrpc::ResponseType::InternalServerError,
89                    format!("output serialize: {e}"),
90                )
91            })?;
92            Ok::<_, proto_blue_xrpc::XrpcServerError>(value)
93        }
94    })
95}