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.

2.1 KB
Raw Blame
use models_opensearch::SearchIndex;

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

/// The arguments for upserting a channel message into the opensearch index.
/// Threadless messages are indexed with `thread_id == message_id`.
#[derive(Debug, serde::Serialize)]
pub struct UpsertChannelMessageArgs {
    #[serde(rename = "entity_id")]
    pub channel_id: String,
    pub channel_type: String,
    pub org_id: Option<i64>,
    pub message_id: String,
    pub thread_id: String,
    pub sender_id: String,
    /// Searchable fallback display name, never an authenticated sender identity.
    pub imported_author: Option<String>,
    pub mentions: Vec<String>,
    pub content: String,
    pub created_at_millis: EpochMillis,
    pub updated_at_millis: EpochMillis,
}

#[cfg(test)]
mod test;

#[tracing::instrument(skip(client))]
pub(crate) async fn upsert_channel_message(
    client: &opensearch::OpenSearch,
    args: &UpsertChannelMessageArgs,
    index_override: Option<&str>,
) -> Result<()> {
    let id = format!("{}:{}", args.channel_id, args.message_id);
    let index = index_override.unwrap_or(SearchIndex::Channels.as_ref());

    let response = client
        .index(opensearch::IndexParts::IndexId(index, &id))
        .body(args)
        .send()
        .await
        .map_err(|err| {
            OpensearchClientError::request_failed(OpensearchOperation::UpsertChannelMessage, &err)
        })?;

    let status_code = response.status_code();
    if status_code.is_success() {
        tracing::trace!(id=%id, "channel message upserted successfully");
    } else {
        let body = response.text().await.map_err(|err| {
            OpensearchClientError::deserialization_failed(
                OpensearchOperation::UpsertChannelMessage,
                &err,
            )
        })?;

        tracing::error!(
            status_code=%status_code,
            body=%body,
            "error upserting channel message",
        );

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