Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.6 KB
Raw Blame
use crate::api::context::{ApiContext, AuthorizationService, EmailSvc};
use axum::Json;
use axum::extract::{Path, Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use axum_extra::extract::Cached;
use email::domain::{
    models::EmailErr,
    scheduled::{EmailSchedulingService, ScheduleChange},
};
use email::inbound::axum::api_types::DraftEditPrecondition;
use email::inbound::axum::axum_impls::EmailLinkExtractor;
use email::inbound::axum::send_router::{SendMessageError, refuse_program_send};
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use model::response::ErrorResponse;
use sqlx_core::types::chrono::{DateTime, Utc};
use thiserror::Error;
use utoipa::ToSchema;
use uuid::Uuid;

#[derive(Debug, Error)]
pub enum UpsertScheduledError {
    #[error("{0}")]
    Email(#[from] EmailErr),
    /// A scheduled draft sends without anyone seeing it, so a program must
    /// quote the user's approval here.
    #[error("{0}")]
    Program(#[from] SendMessageError),
}

impl IntoResponse for UpsertScheduledError {
    fn into_response(self) -> Response {
        let error = match self {
            UpsertScheduledError::Email(error) => error,
            UpsertScheduledError::Program(refusal) => return refusal.into_response(),
        };
        let status = match &error {
            EmailErr::MessageNotFound(_) => StatusCode::NOT_FOUND,
            EmailErr::Unauthorized => StatusCode::FORBIDDEN,
            EmailErr::MessageDeliveryConflict(_) | EmailErr::InvalidScheduleTime => {
                StatusCode::BAD_REQUEST
            }
            _ => {
                tracing::error!(error=?error, "schedule transition failed");
                StatusCode::INTERNAL_SERVER_ERROR
            }
        };
        let message = if status == StatusCode::INTERNAL_SERVER_ERROR {
            "Failed to update scheduled message".to_owned()
        } else {
            error.to_string()
        };
        (
            status,
            Json(ErrorResponse {
                message: message.into(),
            }),
        )
            .into_response()
    }
}

#[derive(Debug, serde::Deserialize, ToSchema)]
pub struct UpsertScheduledRequest {
    /// The time to send the message (ISO 8601 format).
    pub send_time: DateTime<Utc>,
    /// Per-message signature override; absent uses the inbox's send defaults.
    pub include_signature: Option<bool>,
    /// The user's own reply approving this send, quoted verbatim. Required
    /// from an agent's program, which is refused without it; ignored for a
    /// person in the app, whose Send Later is their confirmation.
    #[serde(default)]
    pub user_confirmation: Option<String>,
}

#[derive(Debug, serde::Serialize, ToSchema)]
pub struct UpsertScheduledResponse {
    pub message_id: Uuid,
    pub send_time: DateTime<Utc>,
}

/// Schedule or update a scheduled draft.
#[utoipa::path(
    put, tag = "Draft Scheduling", path = "/email/drafts/scheduled/{id}",
    operation_id = "upsert_scheduled_message",
    params(DraftEditPrecondition, ("id" = Uuid, Path, description = "The ID of the draft message to schedule")),
    request_body = UpsertScheduledRequest,
    responses(
        (status = 200, body = UpsertScheduledResponse),
        (status = 400, body = ErrorResponse),
        (status = 401, body = ErrorResponse),
        (status = 403, body = ErrorResponse),
        (status = 404, body = ErrorResponse),
        (status = 500, body = ErrorResponse),
    )
)]
#[tracing::instrument(skip(ctx, authorization, headers, link), err)]
pub async fn handler(
    Query(precondition): Query<DraftEditPrecondition>,
    State(ctx): State<ApiContext>,
    Cached(authorization): Cached<
        MacroAuthorizationExtractor<AuthorizationService, UserOrInternal>,
    >,
    Cached(EmailLinkExtractor(link, _)): Cached<EmailLinkExtractor<EmailSvc, AuthorizationService>>,
    Path(draft_id): Path<Uuid>,
    headers: HeaderMap,
    Json(request): Json<UpsertScheduledRequest>,
) -> Result<Json<UpsertScheduledResponse>, UpsertScheduledError> {
    refuse_program_send(
        authorization.authorization.user.agent_id.is_some(),
        &headers,
        request.user_confirmation.as_deref(),
    )?;
    ctx.email_service
        .service()
        .change_schedule(
            authorization.authorization.user.macro_user_id.clone(),
            link.id,
            draft_id,
            ScheduleChange::Set(request.send_time),
            request.include_signature,
            precondition.draft_generation,
        )
        .await?;
    Ok(Json(UpsertScheduledResponse {
        message_id: draft_id,
        send_time: request.send_time,
    }))
}