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.

14.3 KB
Raw Blame
pub mod list;
pub mod response;
#[cfg(test)]
mod test;
use document_sub_type::DocumentSubType;
use macro_user_id::user_id::MacroUserIdStr;
use model_owner::Owner;
use schemars::JsonSchema;
use utoipa::ToSchema;

mod file_type;
pub use file_type::{ContentType, ContentTypeExt, FileAssociation, FileType, FileTypeExt};
mod docx;
pub use docx::{BomPart, BomPartWithContent, SaveBomPart};
mod pdf;
pub use pdf::modification_data;
mod basic;
pub use basic::{
    Document, DocumentBasic, DocumentInfo, ID, IDWithTimeStamps, VersionID,
    VersionIDWithTimeStamps, VersionIDWithTimeStampsNoSha, VersionIDWithTimeStampsOptionalSha,
};
mod document_family;
pub use document_family::DocumentFamily;
use models_permissions::share_permission::access_level::AccessLevel;

/// The longest document name DSS accepts, counted in extended grapheme clusters.
pub const MAX_DOCUMENT_NAME_GRAPHEMES: usize = 200;

#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct DocumentPermissionsToken {
    /// The users id if present
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<MacroUserIdStr<'static>>,
    /// The document id
    pub document_id: String,
    /// The access level of the user for the document
    pub access_level: AccessLevel,
    /// The expiration time of the token
    pub exp: usize,
    /// The issuer of the token
    pub iss: String,
}

/// Sub type of a document with associated properties encoded in each variant.
/// This ensures type-safety: task properties only exist when the document is a task.
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone, ToSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BasicDocumentSubType {
    /// A task document with its associated properties
    Task {
        /// Whether the task is completed.
        /// True if the Status property is set to "Completed".
        is_completed: bool,
    },
    /// A snippet document — reusable markdown
    Snippet {},
    /// A skill document — markdown instructions for AI
    Skill {},
    /// The description document of an initiative
    InitiativeDescription {},
}

impl BasicDocumentSubType {
    /// Converts from DB representation (separate sub_type and is_completed columns)
    /// to the domain enum.
    pub fn from_db(sub_type: Option<DocumentSubType>, is_completed: Option<bool>) -> Option<Self> {
        match sub_type? {
            DocumentSubType::Task => Some(Self::Task {
                is_completed: is_completed.unwrap_or_default(),
            }),
            DocumentSubType::Snippet => Some(Self::Snippet {}),
            DocumentSubType::Skill => Some(Self::Skill {}),
            DocumentSubType::InitiativeDescription => Some(Self::InitiativeDescription {}),
        }
    }
}

#[derive(
    sqlx::FromRow, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone, ToSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct BasicDocument {
    /// The document id
    #[serde(rename = "id", alias = "documentId")]
    pub document_id: String,
    /// The version of the document
    /// This could be the document_instance_id or document_bom_id depending on
    /// the file type
    pub document_version_id: i64,
    /// The owner of the document
    #[schema(value_type = String)]
    #[sqlx(try_from = "String")]
    pub owner: Owner,
    /// The name of the document
    #[serde(rename = "name", alias = "documentName")]
    pub document_name: String,
    /// The file type of the document (e.g. pdf, docx)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_type: Option<String>,
    /// If the document is a PDF, this is the SHA of the pdf
    /// If the document is a DOCX, this will not be present
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha: Option<String>,
    /// The id of the project that this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    /// The id of the document this document branched from
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branched_from_id: Option<String>,
    /// The id of the version this document branched from
    /// This could be either DocumentInstance or DocumentBom id depending on
    /// the file type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branched_from_version_id: Option<i64>,
    /// The id of the document family this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_family_id: Option<i64>,
    /// The time the document was created
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,
    /// The time the document instance / document BOM was updated
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,

    /// The time the document was deleted
    pub deleted_at: Option<chrono::DateTime<chrono::Utc>>,

    /// The sub type of the document if present.
    /// Task-related properties are encoded within the variant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_type: Option<BasicDocumentSubType>,
}

#[derive(sqlx::FromRow, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct BackfillDocumentInformation {
    /// The document id
    pub document_id: String,
    /// The version of the document
    /// This could be the document_instance_id or document_bom_id depending on
    /// the file type
    pub document_version_id: i64,
    /// The owner of the document
    pub owner: String,
    /// The name of the document
    pub document_name: String,
    /// The file type of the document (file extension)
    pub file_type: String,
    /// If the document is a DOCX document and unzipped, the document_bom will be present
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_bom: Option<serde_json::Value>,
}

#[derive(sqlx::FromRow, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct BackfillSearchDocumentInformation {
    pub document_id: String,
    pub document_version_id: i64,
    pub owner: String,
    pub file_type: FileType,
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}
#[derive(
    sqlx::FromRow,
    serde::Serialize,
    serde::Deserialize,
    Eq,
    PartialEq,
    Debug,
    Clone,
    ToSchema,
    JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct DocumentMetadata {
    /// The document id
    pub document_id: String,
    /// The version of the document
    /// This could be the document_instance_id or document_bom_id depending on
    /// the file type
    pub document_version_id: i64,
    /// The owner of the document
    #[schema(value_type = String)]
    #[sqlx(try_from = "String")]
    #[schemars(with = "String")]
    pub owner: Owner,
    /// The name of the document
    pub document_name: String,
    /// The file type of the document (file extension)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_type: Option<String>,
    /// If the document is a PDF, this is the SHA of the pdf
    /// If the document is a DOCX, this will not be present
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha: Option<String>,
    /// The id of the project that this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    /// The name of the project that this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_name: Option<String>,
    /// The id of the document this document branched from
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branched_from_id: Option<String>,
    /// The id of the version this document branched from
    /// This could be either DocumentInstance or DocumentBom id depending on
    /// the file type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branched_from_version_id: Option<i64>,
    /// The id of the document family this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_family_id: Option<i64>,
    /// If the document is a DOCX document and unzipped, the document_bom will be present
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schema(value_type = Vec<BomPart>, nullable=true)]
    pub document_bom: Option<serde_json::Value>,
    /// The modification data for the document instance.
    /// This is only used for PDF documents.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modification_data: Option<serde_json::Value>,
    /// The time the document was created
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,
    /// The time the document instance / document BOM was updated
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,

    /// The time the document was deleted
    pub deleted_at: Option<chrono::DateTime<chrono::Utc>>,

    /// The sub type of the document if present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_type: Option<DocumentSubType>,
}

impl DocumentMetadata {
    #[expect(
        clippy::too_many_arguments,
        reason = "no good reason but too hard to fix right now"
    )]
    pub fn new_docx(
        document_id: &str,
        document_bom_id: i64,
        owner: Owner,
        document_name: &str,
        file_type: &str,
        document_family_id: Option<i64>,
        branched_from_id: Option<String>,
        branched_from_version_id: Option<i64>,
        project_id: Option<String>,
        project_name: Option<String>,
        created_at: Option<chrono::DateTime<chrono::Utc>>,
        updated_at: Option<chrono::DateTime<chrono::Utc>>,
    ) -> Self {
        Self {
            document_id: document_id.to_string(),
            owner,
            document_name: document_name.to_string(),
            file_type: Some(file_type.to_string()),
            sha: None,
            document_version_id: document_bom_id,
            document_bom: Some(serde_json::json!([])),
            modification_data: None,
            document_family_id,
            branched_from_id,
            branched_from_version_id,
            project_id,
            project_name,
            created_at,
            updated_at,
            sub_type: None,
            deleted_at: None, // New documents should never be deleted
        }
    }

    /// Creates a new document metadata
    #[expect(
        clippy::too_many_arguments,
        reason = "no good reason but too hard to fix right now"
    )]
    pub fn new_document(
        document_id: &str,
        document_instance_id: i64,
        owner: Owner,
        document_name: &str,
        file_type: Option<FileType>,
        sha: &str,
        document_family_id: Option<i64>,
        branched_from_id: Option<&str>,
        branched_from_version_id: Option<i64>,
        project_id: Option<&str>,
        project_name: Option<&str>,
        created_at: Option<chrono::DateTime<chrono::Utc>>,
        updated_at: Option<chrono::DateTime<chrono::Utc>>,
        sub_type: Option<DocumentSubType>,
    ) -> Self {
        Self {
            document_id: document_id.to_string(),
            owner,
            document_name: document_name.to_string(),
            file_type: file_type.map(|s| s.as_str().to_string()),
            sha: Some(sha.to_string()),
            document_version_id: document_instance_id,
            document_bom: None,
            modification_data: None,
            document_family_id,
            branched_from_id: branched_from_id.map(|s| s.to_string()),
            branched_from_version_id,
            project_id: project_id.map(|s| s.to_string()),
            project_name: project_name.map(|s| s.to_string()),
            created_at,
            updated_at,
            deleted_at: None, // New documents should never be deleted
            sub_type,
        }
    }
}

#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DocumentPreview {
    Access(DocumentPreviewData),
    NoAccess(WithDocumentId),
    DoesNotExist(WithDocumentId),
}

/// The sub type of a document preview with associated properties.
/// Task-related properties are encoded within the variant to ensure valid states.
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone, ToSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DocumentPreviewDataSubType {
    /// A task document with completion status
    Task {
        /// Whether the task is completed.
        /// True if the Status property is set to "Completed".
        is_completed: bool,
    },
    /// A snippet document — reusable markdown
    Snippet {},
    /// A skill document — markdown instructions for AI
    Skill {},
    /// The description document of an initiative
    InitiativeDescription {},
}

impl DocumentPreviewDataSubType {
    /// Converts from DB representation (separate sub_type and is_completed columns)
    /// to the domain enum.
    pub fn from_db(sub_type: Option<DocumentSubType>, is_completed: Option<bool>) -> Option<Self> {
        match sub_type? {
            DocumentSubType::Task => Some(Self::Task {
                is_completed: is_completed.unwrap_or_default(),
            }),
            DocumentSubType::Snippet => Some(Self::Snippet {}),
            DocumentSubType::Skill => Some(Self::Skill {}),
            DocumentSubType::InitiativeDescription => Some(Self::InitiativeDescription {}),
        }
    }
}

#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone, ToSchema)]
pub struct DocumentPreviewData {
    /// The document id
    pub document_id: String,
    /// The file type of the document (e.g. pdf, docx)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_type: Option<String>,
    /// The name of the document
    pub document_name: String,
    /// The id of the owner of the document
    pub owner: String,
    /// The time the document was last updated
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
    /// The sub type of the document if present.
    /// Task-related properties are encoded within the variant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_type: Option<DocumentPreviewDataSubType>,
}

#[derive(
    sqlx::FromRow, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone, ToSchema,
)]
pub struct WithDocumentId {
    pub document_id: String,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DocumentPreviewV2 {
    Found(DocumentPreviewData),
    DoesNotExist(WithDocumentId),
}