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.2 KB
Raw Blame
#[cfg(test)]
mod test;

use crate::messages;
use crate::messages::get;

use models_email::service::message;
use sqlx::types::Uuid;
use std::collections::HashMap;

/// Updates a single message's replying_to_id field. The row is only rewritten when the value
/// changes. Returns whether the message exists for the link.
#[tracing::instrument(skip(executor), err)]
pub async fn update_db_message_replying_to_id<'e, E>(
    executor: E,
    link_id: Uuid,
    message_id: Uuid,
    replying_to_id: Option<Uuid>,
) -> anyhow::Result<bool>
where
    E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
    let found = sqlx::query_scalar!(
        r#"
        WITH updated AS (
            UPDATE email_messages
            SET
                replying_to_id = $1,
                updated_at = NOW()
            WHERE
                id = $2
                AND link_id = $3
                AND replying_to_id IS DISTINCT FROM $1
        )
        SELECT EXISTS (
            SELECT 1 FROM email_messages WHERE id = $2 AND link_id = $3
        ) AS "found!"
        "#,
        replying_to_id,
        message_id,
        link_id
    )
    .fetch_one(executor)
    .await?;

    if !found {
        tracing::warn!(
            message_id = %message_id,
            link_id = %link_id,
            "Message not found - it may not exist or doesn't belong to the link"
        );
    }

    Ok(found)
}

/// Updates the replying_to_id for multiple messages belonging to a single link (typically part
/// of the same thread)
#[tracing::instrument(skip(executor), err)]
pub async fn update_db_messages_replying_to_ids<'e, E>(
    executor: E,
    link_id: Uuid,
    updates: &[(Uuid, Uuid)],
) -> anyhow::Result<()>
where
    E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
    if updates.is_empty() {
        return Ok(());
    }

    let (message_ids, replying_to_ids): (Vec<Uuid>, Vec<Uuid>) = updates.iter().cloned().unzip();

    // Rows that already hold the value are left alone; the count covers every matching message
    // so the warning below only fires for messages that are missing.
    let matched = sqlx::query_scalar!(
        r#"
        WITH updated AS (
            UPDATE email_messages
            SET
                replying_to_id = update_data.new_replying_to_id,
                updated_at = NOW()
            FROM
                UNNEST($1::uuid[], $2::uuid[])
                    AS update_data(message_id, new_replying_to_id)
            WHERE
                email_messages.id = update_data.message_id
                AND email_messages.link_id = $3
                AND email_messages.replying_to_id IS DISTINCT FROM update_data.new_replying_to_id
        )
        SELECT COUNT(*) AS "matched!"
        FROM email_messages
        WHERE id = ANY($1) AND link_id = $3
        "#,
        &message_ids,
        &replying_to_ids,
        link_id
    )
    .fetch_one(executor)
    .await?;

    if matched < updates.len() as i64 {
        tracing::warn!(
            link_id = %link_id,
            expected_messages = updates.len(),
            found_messages = matched,
            "Fewer messages found than expected. Some messages may not exist or do not belong to the link."
        );
    }

    Ok(())
}

/// Update the replying_to_id field of a message based on its In-Reply-To header. This can only be done
/// after all existing messages in the message's thread have been inserted, else the message the passed
/// message is replying to may not exist in the database yet.
#[tracing::instrument(skip(tx, message), err)]
pub async fn update_message_replying_to_from_headers(
    tx: &mut sqlx::PgConnection,
    message: &message::Message,
    message_db_id: Uuid,
    link_id: Uuid,
) -> anyhow::Result<()> {
    if let Some(headers) = &message.headers_json
        && let Some(in_reply_to) = extract_in_reply_to(headers)
    {
        let db_id_replying_to =
            get::get_message_id_by_global_id(&mut *tx, link_id, &in_reply_to).await?;

        // doesn't always exist, because forwarded emails can have In-Reply-To referring to
        // the email that was forwarded, which may or may not exist in db
        if let Some(db_id_replying_to) = db_id_replying_to {
            // Update the message's replying_to_id field
            update_db_message_replying_to_id(tx, link_id, message_db_id, Some(db_id_replying_to))
                .await?;

            tracing::debug!(
                message_id = %message_db_id,
                replying_to_id = %db_id_replying_to,
                "Updated message replying_to_id"
            );
        }
    }

    Ok(())
}

/// Update the replying_to_id field of a message in a thread based on its In-Reply-To header. This can only be done
/// after all existing messages in the message's thread have been inserted, else the message a message
/// is replying to may not exist in the database yet.
#[tracing::instrument(skip(tx), err)]
pub async fn update_thread_messages_replying_to(
    tx: &mut sqlx::PgConnection,
    thread_db_id: Uuid,
    link_id: Uuid,
) -> anyhow::Result<()> {
    let simple_messages = messages::get_simple_messages::get_simple_messages_for_thread(
        &mut *tx,
        thread_db_id,
        link_id,
    )
    .await?;

    // if there is only one message in the thread, we know it isn't replying to anything
    if simple_messages.len() == 1 {
        return Ok(());
    }

    // create map of key global_id, value db_id for quick retrieval of db_ids
    let mut global_db_map: HashMap<String, Uuid> = HashMap::new();
    for message in &simple_messages {
        global_db_map.insert(message.global_id.clone(), message.db_id);
    }

    let mut replying_to_id_tuples: Vec<(Uuid, Uuid)> = Vec::new();

    // if message has In-Reply-To header, get db_id of message with that global_id and add to list for insertion
    for message in simple_messages {
        if let Some(headers) = &message.headers_json
            && let Some(in_reply_to) = extract_in_reply_to(headers)
        {
            // doesn't always exist, because forwarded emails can have In-Reply-To referring to
            // the email that was forwarded,y which may or may not exist in db
            if let Some(db_id_replying_to) = global_db_map.get(&in_reply_to) {
                replying_to_id_tuples.push((message.db_id, *db_id_replying_to));
            }
        }
    }

    // set replying_to_ids in database
    update_db_messages_replying_to_ids(tx, link_id, &replying_to_id_tuples).await?;

    Ok(())
}

/// Extracts the In-Reply-To value from the headers_json
fn extract_in_reply_to(headers_json: &serde_json::Value) -> Option<String> {
    // Check if headers_json is an array
    if let Some(headers) = headers_json.as_array() {
        // Iterate through each header object
        for header in headers {
            // Get the name field
            if let Some(name) = header.get("name").and_then(|n| n.as_str()) {
                // RFC header names are case-insensitive across providers.
                if name.eq_ignore_ascii_case("In-Reply-To") {
                    // Return the value
                    return header
                        .get("value")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string());
                }
            }
        }
    }
    None
}