Skip to main content

proto_blue_api/generated/tools/ozone/moderation/
emitEvent.rs

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