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.

13.4 KB
Raw Blame
//! One capability-checked message boundary for every caller and parent.

use super::{models::*, ports::*, service::*};
use entity_access::domain::models::EntityAccessReceipt;
use uuid::Uuid;

/// Conversation reads under a verified parent capability.
#[cfg_attr(feature = "test-utils", mockall::automock)]
#[async_trait::async_trait]
pub trait MessageReader: Send + Sync + 'static {
    /// Read an individual message under its parent's view permission.
    async fn get(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: Uuid,
    ) -> Result<Message, MessageError>;
    /// Read the root, state, and ordered replies of a discussion.
    async fn get_thread(
        &self,
        access: EntityAccessReceipt<MessageView>,
        root: Uuid,
    ) -> Result<MessageThread, MessageError>;
    /// Read a page of discussions on an authorized parent.
    async fn timeline(
        &self,
        access: EntityAccessReceipt<MessageView>,
        query: MessageTimelineQuery,
    ) -> Result<MessagePage, MessageError>;
    /// Read a parent's discussions and timeline activity as one ordered page.
    async fn timeline_entries(
        &self,
        access: EntityAccessReceipt<MessageView>,
        query: MessageTimelineQuery,
    ) -> Result<MessageTimelinePage, MessageError>;

    /// Read live history preceding a prompt, scoped by its parent.
    async fn preceding(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: Uuid,
        limit: u16,
    ) -> Result<Vec<Message>, MessageError>;
    /// Read an old link under current parent access.
    async fn resolve_legacy(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: i64,
        is_thread: bool,
    ) -> Result<Message, MessageError>;
    /// The parent a live message belongs to, for adapters addressed only by
    /// message id. Grants nothing: callers mint the parent's receipt before any read.
    async fn parent_of(&self, id: Uuid) -> Result<Option<MessageParent>, MessageError>;
}

/// Conversation mutations under a verified actor and parent capability.
#[cfg_attr(feature = "test-utils", mockall::automock)]
#[async_trait::async_trait]
pub trait MessageCommands: Send + Sync + 'static {
    /// Post as the principal carried by the verified capability.
    async fn post(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        input: PostMessage,
    ) -> Result<Message, MessageError>;
    /// Idempotently post a trusted server event, using its UUIDv7 as the message
    /// id without a client clock-skew limit. Replays return the original message,
    /// including tombstones, after checking its parent, author, and thread.
    async fn post_from_event(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        event_id: Uuid,
        input: PostMessage,
    ) -> Result<Message, MessageError>;
    /// Apply partial body, mention, and attachment changes under the common policy.
    async fn patch(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        input: MessagePatch,
    ) -> Result<Message, MessageError>;
    /// Tombstone a message, or delete the whole discussion when it is a root.
    async fn delete(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        nonce: Option<String>,
    ) -> Result<Message, MessageError>;
    /// Change the authenticated actor's reaction.
    async fn react(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        emoji: String,
        add: bool,
        nonce: Option<String>,
    ) -> Result<Message, MessageError>;
    /// Publish ephemeral typing to the authorized conversation.
    async fn typing(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root: Option<Uuid>,
        active: bool,
        nonce: Option<String>,
    ) -> Result<(), MessageError>;
    /// Publish an agent session typing through the bot that speaks for it.
    ///
    /// Defaults to plain typing, which drops the session: an adapter that
    /// cannot carry it still shows the bot typing.
    async fn agent_typing(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root: Option<Uuid>,
        active: bool,
        agent: AgentTyping,
    ) -> Result<(), MessageError> {
        let _ = agent;
        self.typing(access, root, active, None).await
    }
    /// Update entity discussion state or detach removed document Markdown text.
    async fn patch_thread(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root_id: Uuid,
        patch: ThreadPatch,
    ) -> Result<ThreadState, MessageError>;
    /// Delete a discussion under the common moderation policy.
    async fn delete_thread(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root_id: Uuid,
        nonce: Option<String>,
    ) -> Result<ThreadState, MessageError>;
}

/// Combined application boundary for adapters that need both reads and commands.
pub trait MessageServiceApi: MessageReader + MessageCommands {}

impl<T: MessageReader + MessageCommands> MessageServiceApi for T {}

#[async_trait::async_trait]
impl<R: MessageRepository, E: MessageEventPublisher> MessageReader for MessageService<R, E> {
    async fn get(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: Uuid,
    ) -> Result<Message, MessageError> {
        MessageService::get(self, access, id).await
    }
    async fn get_thread(
        &self,
        access: EntityAccessReceipt<MessageView>,
        root: Uuid,
    ) -> Result<MessageThread, MessageError> {
        MessageService::get_thread(self, access, root).await
    }
    async fn timeline(
        &self,
        access: EntityAccessReceipt<MessageView>,
        query: MessageTimelineQuery,
    ) -> Result<MessagePage, MessageError> {
        MessageService::timeline(self, access, query).await
    }
    async fn timeline_entries(
        &self,
        access: EntityAccessReceipt<MessageView>,
        query: MessageTimelineQuery,
    ) -> Result<MessageTimelinePage, MessageError> {
        MessageService::timeline_entries(self, access, query).await
    }

    async fn preceding(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: Uuid,
        limit: u16,
    ) -> Result<Vec<Message>, MessageError> {
        MessageService::preceding(self, access, id, limit).await
    }
    async fn resolve_legacy(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: i64,
        is_thread: bool,
    ) -> Result<Message, MessageError> {
        MessageService::resolve_legacy(self, access, id, is_thread).await
    }
    async fn parent_of(&self, id: Uuid) -> Result<Option<MessageParent>, MessageError> {
        MessageService::parent_of(self, id).await
    }
}

/// Creates a channel call's thread root, for the call domain.
#[async_trait::async_trait]
pub trait CallRootPoster: Send + Sync + 'static {
    /// Create the root once, however often it is asked for.
    async fn ensure_call_root(&self, call_id: Uuid) -> Result<(), MessageError>;
}

#[async_trait::async_trait]
impl<R: MessageRepository, E: MessageEventPublisher> CallRootPoster for MessageService<R, E> {
    async fn ensure_call_root(&self, call_id: Uuid) -> Result<(), MessageError> {
        MessageService::ensure_call_root(self, call_id)
            .await
            .map(drop)
    }
}

#[async_trait::async_trait]
impl<R: MessageRepository, E: MessageEventPublisher> MessageCommands for MessageService<R, E> {
    async fn post(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        input: PostMessage,
    ) -> Result<Message, MessageError> {
        MessageService::post(self, access, input).await
    }
    async fn post_from_event(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        event_id: Uuid,
        input: PostMessage,
    ) -> Result<Message, MessageError> {
        MessageService::post_from_event(self, access, event_id, input).await
    }
    async fn patch(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        input: MessagePatch,
    ) -> Result<Message, MessageError> {
        MessageService::patch(self, access, id, input).await
    }
    async fn delete(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        nonce: Option<String>,
    ) -> Result<Message, MessageError> {
        MessageService::delete(self, access, id, nonce).await
    }
    async fn react(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        emoji: String,
        add: bool,
        nonce: Option<String>,
    ) -> Result<Message, MessageError> {
        MessageService::react(self, access, id, emoji, add, nonce).await
    }
    async fn typing(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root: Option<Uuid>,
        active: bool,
        nonce: Option<String>,
    ) -> Result<(), MessageError> {
        MessageService::typing(self, access, root, active, nonce).await
    }
    async fn agent_typing(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root: Option<Uuid>,
        active: bool,
        agent: AgentTyping,
    ) -> Result<(), MessageError> {
        MessageService::agent_typing(self, access, root, active, agent).await
    }
    async fn patch_thread(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root_id: Uuid,
        patch: ThreadPatch,
    ) -> Result<ThreadState, MessageError> {
        MessageService::patch_thread(self, access, root_id, patch).await
    }
    async fn delete_thread(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root_id: Uuid,
        nonce: Option<String>,
    ) -> Result<ThreadState, MessageError> {
        MessageService::delete_thread(self, access, root_id, nonce).await
    }
}

#[cfg(feature = "test-utils")]
mockall::mock! {
    /// Test double for an adapter that uses both application capabilities.
    pub MessageServiceApi {}
    #[async_trait::async_trait]
    impl MessageReader for MessageServiceApi {
    /// Read an individual message under its parent's view permission.
    async fn get(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: Uuid,
    ) -> Result<Message, MessageError>;
    /// Read the root, state, and ordered replies of a discussion.
    async fn get_thread(
        &self,
        access: EntityAccessReceipt<MessageView>,
        root: Uuid,
    ) -> Result<MessageThread, MessageError>;
    /// Read a page of discussions on an authorized parent.
    async fn timeline(
        &self,
        access: EntityAccessReceipt<MessageView>,
        query: MessageTimelineQuery,
    ) -> Result<MessagePage, MessageError>;
    /// Read a parent's discussions and timeline activity as one ordered page.
    async fn timeline_entries(
        &self,
        access: EntityAccessReceipt<MessageView>,
        query: MessageTimelineQuery,
    ) -> Result<MessageTimelinePage, MessageError>;

    /// Read live history preceding a prompt, scoped by its parent.
    async fn preceding(
        &self,
        access: EntityAccessReceipt<MessageView>,
        id: Uuid,
        limit: u16,
    ) -> Result<Vec<Message>, MessageError>;
    /// Read an old link under current parent access.
    async fn resolve_legacy(&self, access: EntityAccessReceipt<MessageView>, id: i64, is_thread: bool) -> Result<Message, MessageError>;
    /// The parent a message belongs to, for id-addressed adapters.
    async fn parent_of(&self, id: Uuid) -> Result<Option<MessageParent>, MessageError>;
    }
    #[async_trait::async_trait]
    impl MessageCommands for MessageServiceApi {
    /// Post as the principal carried by the verified capability.
    async fn post(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        input: PostMessage,
    ) -> Result<Message, MessageError>;
    /// Post or recover the message belonging to a trusted server event.
    async fn post_from_event(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        event_id: Uuid,
        input: PostMessage,
    ) -> Result<Message, MessageError>;
    /// Apply partial body, mention, and attachment changes under the common policy.
    async fn patch(&self, access: EntityAccessReceipt<MessageWrite>, id: Uuid, input: MessagePatch) -> Result<Message, MessageError>;
    /// Tombstone a message, or delete the whole discussion when it is a root.
    async fn delete(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        nonce: Option<String>,
    ) -> Result<Message, MessageError>;
    /// Change the authenticated actor's reaction.
    async fn react(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        id: Uuid,
        emoji: String,
        add: bool,
        nonce: Option<String>,
    ) -> Result<Message, MessageError>;
    /// Publish ephemeral typing to the authorized conversation.
    async fn typing(
        &self,
        access: EntityAccessReceipt<MessageWrite>,
        root: Option<Uuid>,
        active: bool,
        nonce: Option<String>,
    ) -> Result<(), MessageError>;    /// Update document discussion state or detach removed Markdown text.
    async fn patch_thread(&self, access: EntityAccessReceipt<MessageWrite>, root_id: Uuid, patch: ThreadPatch) -> Result<ThreadState, MessageError>;
    /// Delete a discussion under the common moderation policy.
    async fn delete_thread(&self, access: EntityAccessReceipt<MessageWrite>, root_id: Uuid, nonce: Option<String>) -> Result<ThreadState, MessageError>;
    }
}