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.

10.6 KB
Raw Blame
use chrono::{DateTime, Utc};
use macro_user_id::user_id::MacroUserIdStr;
use serde_json::Value as JsonValue;
use uuid::Uuid;

use super::attachment::{AttachmentDraft, AttachmentForwarded, MessageAttachment};
use super::contact::{ContactInfo, RecipientType};
use super::label::MessageLabel;
use super::link::Link;

/// Input for creating a draft message. Mirrors the fields from `MessageToSend`.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CreateDraftInput {
    /// Signature captured at Send; an empty value explicitly freezes no signature.
    /// None preserves legacy server-default behavior.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub frozen_signature_html: Option<String>,
    /// Generation captured when editing; admission/cancellation fence earlier writes.
    #[serde(default)]
    pub draft_generation: i32,
    /// Requested sender. Absent selects the inbox's primary address. The domain
    /// verifies alternate addresses against the selected inbox's provider grant.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sender_email: Option<macro_user_id::email::EmailStr<'static>>,
    /// Existing message DB ID (for updating an existing draft).
    pub db_id: Option<Uuid>,
    /// Provider message ID.
    pub provider_id: Option<String>,
    /// ID of the message this draft is replying to.
    pub replying_to_id: Option<Uuid>,
    /// Provider thread ID.
    pub provider_thread_id: Option<String>,
    /// Thread DB ID (set if this draft belongs to an existing thread).
    pub thread_db_id: Option<Uuid>,
    /// Subject line of the draft.
    pub subject: String,
    /// To recipients.
    pub to: Vec<ContactInfo>,
    /// Cc recipients.
    pub cc: Vec<ContactInfo>,
    /// Bcc recipients.
    pub bcc: Vec<ContactInfo>,
    /// Plain text body.
    pub body_text: Option<String>,
    /// HTML body (base64 encoded from the client; decoded and sanitized
    /// before storage).
    pub body_html: Option<String>,
    /// Macro-specific body format.
    pub body_macro: Option<String>,
    /// Headers JSON (e.g. Macro-In-Reply-To).
    pub headers_json: Option<JsonValue>,
    /// Scheduled send time.
    pub send_time: Option<DateTime<Utc>>,
    /// Per-message override for signature inclusion: `Some(false)` to exclude
    /// (the user dismissed it), `Some(true)` to force-include, `None` to apply
    /// the inbox's default policy. Only consulted on send (ignored for drafts).
    pub include_signature: Option<bool>,
    /// The authenticated user performing the send, set by the transport
    /// layer (never from the request body). Distinct from the link owner on
    /// delegated inboxes; attribution (events, activity) follows the actor.
    /// Ignored for drafts.
    pub actor: Option<MacroUserIdStr<'static>>,
    /// Client draft handle to bind to the final message row. Set only by
    /// user-scoped (GraphQL) saves during handle resolution — never from a
    /// request body. The binding is upserted inside the insert transaction
    /// so replayed offline saves converge on one server-minted row.
    pub draft_client_binding: Option<Uuid>,
    /// Client thread handle to bind to the final thread row; same contract
    /// as `draft_client_binding`.
    pub thread_client_binding: Option<Uuid>,
}

/// A draft input with all IDs resolved, ready for database insertion.
/// Created from `CreateDraftInput` after validation and ID generation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ResolvedDraftInput {
    /// Generation captured when editing; admission/cancellation fence earlier writes.
    #[serde(default)]
    pub draft_generation: i32,
    /// The resolved message DB ID.
    pub db_id: Uuid,
    /// Provider message ID.
    pub provider_id: Option<String>,
    /// ID of the message this draft is replying to.
    pub replying_to_id: Option<Uuid>,
    /// Provider thread ID.
    pub provider_thread_id: Option<String>,
    /// The resolved thread DB ID.
    pub thread_db_id: Uuid,
    /// Subject line of the draft.
    pub subject: String,
    /// To recipients.
    pub to: Vec<ContactInfo>,
    /// Cc recipients.
    pub cc: Vec<ContactInfo>,
    /// Bcc recipients.
    pub bcc: Vec<ContactInfo>,
    /// Plain text body.
    pub body_text: Option<String>,
    /// HTML body (decoded).
    pub body_html: Option<String>,
    /// Macro-specific body format.
    pub body_macro: Option<String>,
    /// Headers JSON (e.g. Macro-In-Reply-To).
    pub headers_json: Option<JsonValue>,
    /// Scheduled send time.
    pub send_time: Option<DateTime<Utc>>,
    /// The sending user's principal string, persisted with the scheduled
    /// send so the eventual `message_sent` event can attribute the actor.
    pub actor_id: Option<String>,
    /// Client draft handle to bind to `db_id` in the insert transaction.
    pub draft_client_id: Option<Uuid>,
    /// Client thread handle to bind to `thread_db_id` in the insert
    /// transaction.
    pub thread_client_id: Option<Uuid>,
}

/// The row IDs a save actually settled on, reported back by the insert.
///
/// Usually these are the IDs the caller resolved, but a save carrying a
/// client draft handle can adopt a row a concurrent first save minted for
/// the same handle, so the caller must echo these — not its own candidates —
/// back to the client.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SettledDraftIds {
    /// The message row the save wrote.
    pub message_db_id: Uuid,
    /// The thread that message belongs to.
    pub thread_db_id: Uuid,
}

/// Simplified message info used for validation queries.
#[derive(Debug, Clone)]
pub struct SimpleMessageInfo {
    /// Immutable provider message identity, resolved from storage.
    pub provider_id: Option<String>,
    /// Database ID of the message.
    pub db_id: Uuid,
    /// The inbox (link) the message belongs to.
    pub link_id: Uuid,
    /// Thread database ID.
    pub thread_db_id: Uuid,
    /// Provider thread ID.
    pub provider_thread_id: Option<String>,
    /// Headers JSON.
    pub headers_json: Option<JsonValue>,
    /// Whether the message has been sent.
    pub is_sent: bool,
    /// Whether the message is a draft.
    pub is_draft: bool,
}

/// A draft saved on behalf of a user, paired with the sending inbox the save
/// resolved into — transports that build a message representation of the
/// draft (the GraphQL payload) need the inbox's address for the sender.
/// Metadata and attachment collections have been loaded successfully; a failed load
/// returns an error instead of an incomplete saved draft.
#[derive(Clone)]
pub struct SavedUserDraft {
    /// Persisted provider write state after this save.
    pub operation_status: Option<super::mailbox_operation::MessageOperationStatus>,
    /// Persisted sender, including an approved alias when selected.
    pub sender: ContactInfo,
    /// The created or updated draft.
    pub draft: CreatedDraft,
    /// The inbox the draft was saved into.
    pub link: Link,
    /// Persisted creation time; updating a draft does not reset it.
    pub created_at: DateTime<Utc>,
    /// Persisted modification time after the save.
    pub updated_at: DateTime<Utc>,
    /// Persisted labels, loaded after saving.
    pub labels: Vec<MessageLabel>,
    /// Persisted provider attachments, loaded after saving.
    pub attachments: Vec<MessageAttachment>,
    /// Persisted uploaded draft attachments, loaded after saving.
    pub attachments_draft: Vec<AttachmentDraft>,
    /// Persisted forwarded attachments, loaded after saving.
    pub attachments_forwarded: Vec<AttachmentForwarded>,
}

/// Persisted message timestamps used when returning an updated draft.
#[derive(Debug, Clone, Copy)]
pub struct MessageTimestamps {
    /// When the message was first stored.
    pub created_at: DateTime<Utc>,
    /// When the stored message was last updated.
    pub updated_at: DateTime<Utc>,
}

/// What an applied guarded draft delete removed beyond the draft row.
#[derive(Debug, Clone, Copy)]
pub struct DraftDeletion {
    /// Whether deleting the draft emptied its thread and removed it too.
    pub thread_deleted: bool,
}

/// Outcome of a user-scoped draft deletion. Deletes are idempotent: a replay
/// after the row is already gone reports `deleted: false` instead of failing,
/// so a queued offline delete lands cleanly no matter how late it arrives.
#[derive(Debug, Clone, Copy)]
pub struct DeletedUserDraft {
    /// Authorized thread identity, retained when the draft is removed.
    pub thread_id: Option<Uuid>,
    /// Whether a draft row was actually deleted.
    pub deleted: bool,
    /// Whether the delete emptied the draft's thread and removed it too.
    pub thread_deleted: bool,
}

/// The result of creating a draft.
#[derive(Debug, Clone)]
pub struct CreatedDraft {
    /// Draft edit generation; changes at send and cancellation boundaries.
    pub draft_generation: i32,
    /// The assigned or existing message DB ID.
    pub db_id: Uuid,
    /// Provider message ID.
    pub provider_id: Option<String>,
    /// ID of the message this draft is replying to.
    pub replying_to_id: Option<Uuid>,
    /// Provider thread ID.
    pub provider_thread_id: Option<String>,
    /// Thread DB ID.
    pub thread_db_id: Uuid,
    /// Link ID.
    pub link_id: Uuid,
    /// Subject.
    pub subject: String,
    /// To recipients.
    pub to: Vec<ContactInfo>,
    /// Cc recipients.
    pub cc: Vec<ContactInfo>,
    /// Bcc recipients.
    pub bcc: Vec<ContactInfo>,
    /// Plain text body.
    pub body_text: Option<String>,
    /// HTML body (decoded).
    pub body_html: Option<String>,
    /// Macro body.
    pub body_macro: Option<String>,
    /// Headers JSON.
    pub headers_json: Option<JsonValue>,
    /// Scheduled send time.
    pub send_time: Option<DateTime<Utc>>,
}

/// Parsed from/to/cc/bcc addresses for contact upsert.
#[derive(Debug, Clone)]
pub struct ParsedAddresses {
    /// The from email address.
    pub from_email: String,
    /// The from display name.
    pub from_name: Option<String>,
    /// To recipients.
    pub to: Vec<ContactInfo>,
    /// Cc recipients.
    pub cc: Vec<ContactInfo>,
    /// Bcc recipients.
    pub bcc: Vec<ContactInfo>,
}

/// Result of upserting contacts for a draft.
#[derive(Debug, Clone)]
pub struct UpsertedContacts {
    /// The contact ID of the sender.
    pub from_contact_id: Option<Uuid>,
    /// The upserted recipients.
    pub recipients: Vec<UpsertedRecipient>,
}

/// A single upserted recipient.
#[derive(Debug, Clone)]
pub struct UpsertedRecipient {
    /// Contact ID.
    pub contact_id: Uuid,
    /// Display name.
    pub name: Option<String>,
    /// Recipient type (to, cc, bcc).
    pub recipient_type: RecipientType,
}