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.

7.1 KB
Raw Blame
use models_email::email::service::label::system_labels;
use sqlx::types::Uuid;

#[cfg(test)]
mod test;

/// Sets a message's read flag from its UNREAD label when `read` is set, and its
/// starred flag from its STARRED label when `starred` is set. A message whose
/// flags already match is not written, as a re-sync usually changes nothing.
#[tracing::instrument(skip(tx), err)]
pub async fn set_flags_from_labels(
    tx: &mut sqlx::PgConnection,
    message_id: Uuid,
    read: bool,
    starred: bool,
) -> anyhow::Result<()> {
    sqlx::query!(
        r#"
        WITH flags AS (
            SELECT
                CASE WHEN $2 THEN NOT EXISTS (
                    SELECT 1 FROM email_message_labels ml JOIN email_labels l ON l.id = ml.label_id
                    WHERE ml.message_id = m.id AND l.provider_label_id = $4
                ) ELSE m.is_read END AS is_read,
                CASE WHEN $3 THEN EXISTS (
                    SELECT 1 FROM email_message_labels ml JOIN email_labels l ON l.id = ml.label_id
                    WHERE ml.message_id = m.id AND l.provider_label_id = $5
                ) ELSE m.is_starred END AS is_starred
            FROM email_messages m
            WHERE m.id = $1
        )
        UPDATE email_messages m
        SET is_read = flags.is_read, is_starred = flags.is_starred
        FROM flags
        WHERE m.id = $1
            AND (m.is_read, m.is_starred) IS DISTINCT FROM (flags.is_read, flags.is_starred)
        "#,
        message_id,
        read,
        starred,
        system_labels::UNREAD,
        system_labels::STARRED,
    )
    .execute(tx)
    .await?;

    Ok(())
}

/// Update the read status of a single message, scoped to the inbox that owns it.
#[tracing::instrument(skip(tx), err)]
pub async fn update_message_read_status(
    tx: &mut sqlx::PgConnection,
    message_id: Uuid,
    link_id: Uuid,
    is_read: bool,
) -> anyhow::Result<Option<Uuid>> {
    let result = sqlx::query!(
        r#"
        UPDATE email_messages
        SET
            is_read = $1,
            updated_at = NOW()
        WHERE
            id = $2
            AND link_id = $3
        RETURNING id
            "#,
        is_read,
        message_id,
        link_id
    )
    .fetch_optional(tx)
    .await?;

    if result.is_none() {
        tracing::warn!(
            message_id = %message_id,
            link_id = %link_id,
            "No message was updated - message may not exist or doesn't belong to the inbox"
        );
    }

    // Return the ID of the updated message, or None if no message was updated
    Ok(result.map(|r| r.id))
}

/// Update the read status of multiple messages at once.
///
/// Scoped by `link_id` — the inbox that owns the messages — not by the calling user.
/// A delegated or shared inbox is owned by a different macro/fusion user than the
/// caller acting on it, so scoping by the caller would match no rows.
///
/// Returns the count of messages that were successfully updated.
#[tracing::instrument(skip(executor), err)]
pub async fn update_message_read_status_batch<'e, E>(
    executor: E,
    message_ids: Vec<Uuid>,
    link_id: Uuid,
    is_read: bool,
) -> anyhow::Result<usize>
where
    E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
    if message_ids.is_empty() {
        return Ok(0);
    }

    let result = sqlx::query!(
        r#"
        UPDATE email_messages
        SET
            is_read = $1,
            updated_at = NOW()
        WHERE
            id = ANY($2)
            AND link_id = $3
        RETURNING id
        "#,
        is_read,
        &message_ids,
        link_id
    )
    .fetch_all(executor)
    .await?;

    let updated_count = result.len();

    // Matching nothing means the ids and the link disagree, which is a scoping bug
    // rather than a race. Fail loudly instead of leaving is_read silently diverged.
    if updated_count == 0 {
        anyhow::bail!(
            "no messages updated for link {link_id}: none of the {} requested ids belong to it",
            message_ids.len()
        );
    }

    if updated_count < message_ids.len() {
        tracing::warn!(
            requested_count = message_ids.len(),
            updated_count = updated_count,
            link_id = %link_id,
            "Some messages were not updated - they may not exist or don't belong to the inbox"
        );
    }

    Ok(updated_count)
}

/// Update the starred status of multiple messages at once, scoped by owning inbox.
/// See [`update_message_read_status_batch`] for why this is not scoped by caller.
#[tracing::instrument(skip(executor), err)]
pub async fn update_message_starred_status_batch<'e, E>(
    executor: E,
    message_ids: Vec<Uuid>,
    link_id: Uuid,
    is_starred: bool,
) -> anyhow::Result<usize>
where
    E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
    if message_ids.is_empty() {
        return Ok(0);
    }

    let result = sqlx::query!(
        r#"
        UPDATE email_messages
        SET
            is_starred = $1,
            updated_at = NOW()
        WHERE
            id = ANY($2)
            AND link_id = $3
        RETURNING id
        "#,
        is_starred,
        &message_ids,
        link_id
    )
    .fetch_all(executor)
    .await?;

    let updated_count = result.len();

    if updated_count < message_ids.len() {
        tracing::warn!(
            requested_count = message_ids.len(),
            updated_count = updated_count,
            link_id = %link_id,
            "Some messages were not updated - they may not exist or don't belong to the inbox"
        );
    }

    Ok(updated_count)
}

/// Finalize a provider-accepted send and populate its provider IDs.
///
/// Macro messages may not have a provider timestamp until inbox sync runs.
/// Seed their delivery time now so thread metadata and the realtime Mail cache
/// can include them in Sent immediately. Preserve existing provider timestamps
/// and the timestamp from an earlier finalization attempt.
#[tracing::instrument(skip(tx), err)]
pub async fn mark_message_as_sent(
    tx: &mut sqlx::PgConnection,
    provider_id: &str,
    provider_thread_id: &str,
    link_id: Uuid,
    db_id: Uuid,
) -> anyhow::Result<()> {
    sqlx::query!(
        r#"
        UPDATE email_messages
        SET
            provider_id = $1,
            provider_thread_id = $2,
            is_draft = false,
            is_sent = true,
            internal_date_ts = COALESCE(internal_date_ts, NOW()),
            updated_at = NOW()
        WHERE
            id = $3
            AND link_id = $4
        "#,
        provider_id,
        provider_thread_id,
        db_id,
        link_id
    )
    .execute(tx)
    .await?;

    Ok(())
}

/// Updates the is_draft status of a message
#[tracing::instrument(skip(tx), err)]
pub async fn update_message_draft_status(
    tx: &mut sqlx::PgConnection,
    message_id: Uuid,
    link_id: Uuid,
    is_draft: bool,
) -> anyhow::Result<()> {
    sqlx::query!(
        r#"
        UPDATE email_messages
        SET
            is_draft = $1,
            updated_at = NOW()
        WHERE
            id = $2
            AND link_id = $3
        "#,
        is_draft,
        message_id,
        link_id
    )
    .execute(tx)
    .await?;

    Ok(())
}