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.

3.4 KB
Raw Blame
use models_opensearch::SearchIndex;

use crate::{
    Result,
    error::{OpensearchClientError, OpensearchOperation},
};

/// Deletes all channel messages with the specified channel_id
#[tracing::instrument(skip(client))]
pub async fn delete_channel_by_id(
    client: &opensearch::OpenSearch,
    channel_id: &str,
    index_override: Option<&str>,
) -> Result<()> {
    let query = serde_json::json!({
        "query": {
            "term": {
                "entity_id": channel_id
            }
        },
    });

    let index = index_override.unwrap_or(SearchIndex::Channels.as_ref());
    let response = client
        .delete_by_query(opensearch::DeleteByQueryParts::Index(&[index]))
        .body(query)
        .refresh(true) // Ensure the index reflects changes immediately
        .send()
        .await
        .map_err(|err| {
            OpensearchClientError::request_failed(OpensearchOperation::DeleteChannelById, &err)
        })?;

    let status_code = response.status_code();

    if !status_code.is_success() {
        let body = response.text().await.map_err(|err| {
            OpensearchClientError::deserialization_failed(
                OpensearchOperation::DeleteChannelById,
                &err,
            )
        })?;

        tracing::error!(
            status_code = ?status_code,
            body = ?body,
            "error deleting channels by id"
        );

        return Err(OpensearchClientError::Unknown {
            details: body,
            operation: OpensearchOperation::DeleteChannelById,
        });
    }

    Ok(())
}

/// Deletes a particular channel message with the specified channel_id and channel_message_id
#[tracing::instrument(skip(client))]
pub async fn delete_channel_message_by_id(
    client: &opensearch::OpenSearch,
    channel_id: &str,
    channel_message_id: &str,
    index_override: Option<&str>,
) -> Result<()> {
    let query = serde_json::json!({
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "entity_id": channel_id
                        }
                    },
                    {
                        "term": {
                            "message_id": channel_message_id
                        }
                    }
                ]
            }
        }
    });

    let index = index_override.unwrap_or(SearchIndex::Channels.as_ref());
    let response = client
        .delete_by_query(opensearch::DeleteByQueryParts::Index(&[index]))
        .body(query)
        .refresh(true) // Ensure the index reflects changes immediately
        .send()
        .await
        .map_err(|err| {
            OpensearchClientError::request_failed(
                OpensearchOperation::DeleteChannelMessageById,
                &err,
            )
        })?;

    let status_code = response.status_code();

    if !status_code.is_success() {
        let body = response.text().await.map_err(|err| {
            OpensearchClientError::deserialization_failed(
                OpensearchOperation::DeleteChannelMessageById,
                &err,
            )
        })?;

        tracing::error!(
            status_code = ?status_code,
            body = ?body,
            "error deleting channels by id"
        );

        return Err(OpensearchClientError::Unknown {
            details: body,
            operation: OpensearchOperation::DeleteChannelMessageById,
        });
    }

    Ok(())
}