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.

feat(channels): channel kafka events (#4585)

Will Hutchinson3dd8a30b86c2
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.json
index fa885291eb..06053689cd 100644
--- a/.github/kafka-cluster-topics.json
+++ b/.github/kafka-cluster-topics.json
@@ -1,4 +1,5 @@
[
+ "macro.channels",
"macro.documents",
"macro.example"
]
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index f442cb10d7..8fd1a9779a 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -472,6 +472,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -564,6 +566,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -748,6 +752,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -844,6 +850,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -2390,6 +2398,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -2564,6 +2574,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -3255,6 +3267,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -3338,6 +3352,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -3953,6 +3969,8 @@
"macro_entrypoint",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_queues",
"macro_service_urls",
@@ -4034,6 +4052,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -4114,6 +4134,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -4244,6 +4266,8 @@
"macro_entrypoint",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -4349,6 +4373,8 @@
"macro_db_client",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
@@ -4576,6 +4602,8 @@
"macro_entrypoint",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_middleware",
"macro_service_urls",
"macro_user_id",
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 56ba83afd2..4527ffc26b 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -2552,6 +2552,10 @@ dependencies = [
"http-body-util",
"item_filters",
"macro_db_migrator",
+ "macro_env",
+ "macro_env_var",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_user_id",
"macro_uuid",
"mockall",
@@ -2563,6 +2567,7 @@ dependencies = [
"models_permissions",
"non_empty",
"notification",
+ "rdkafka",
"recursion",
"rootcause",
"schemars 1.2.1",
@@ -2575,6 +2580,7 @@ dependencies = [
"tokio",
"tower 0.5.3",
"tracing",
+ "tracing-subscriber",
"utoipa",
"uuid",
"workspace-hack",
diff --git a/rust/cloud-storage/channels/Cargo.toml b/rust/cloud-storage/channels/Cargo.toml
index cb1e0cf38c..36756f20fb 100644
--- a/rust/cloud-storage/channels/Cargo.toml
+++ b/rust/cloud-storage/channels/Cargo.toml
@@ -62,7 +62,7 @@ outbound = [
"macro_user_id/sqlx",
"ports",
]
-ports = ["dep:tokio"]
+ports = ["dep:tokio", "macro_event_broker/ports"]
[dependencies]
anyhow = { workspace = true }
@@ -81,6 +81,8 @@ filter_ast = { path = "../filter_ast", optional = true }
frecency = { path = "../frecency", optional = true }
futures = { workspace = true, optional = true }
item_filters = { path = "../item_filters", optional = true }
+macro_event_broker = { path = "../macro_event_broker", default-features = false }
+macro_event_topics = { path = "../macro_event_topics" }
macro_user_id = { path = "../macro_user_id" }
macro_uuid = { path = "../macro_uuid", optional = true }
model-entity = { path = "../model-entity", optional = true }
@@ -115,7 +117,13 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }
[dev-dependencies]
http-body-util = { workspace = true }
macro_db_migrator = { path = "../macro_db_migrator" }
+macro_env = { path = "../macro_env" }
+macro_env_var = { path = "../macro_env_var" }
+# Examples need the Kafka adapter (outbound); the library itself only uses ports.
+macro_event_broker = { path = "../macro_event_broker" }
mockall = { workspace = true }
+rdkafka = { workspace = true }
serde_json = { workspace = true }
-tokio = { workspace = true }
+tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] }
tower = { workspace = true }
+tracing-subscriber = { workspace = true }
diff --git a/rust/cloud-storage/channels/examples/channels_consumer.rs b/rust/cloud-storage/channels/examples/channels_consumer.rs
new file mode 100644
index 0000000000..0360eb2cea
--- /dev/null
+++ b/rust/cloud-storage/channels/examples/channels_consumer.rs
@@ -0,0 +1,272 @@
+//! Kafka consumer example for the `macro.channels` topic.
+//!
+//! Connects with the same `KAFKA_BROKERS` env var and environment-driven
+//! transport as [`macro_event_broker::KafkaEventPublisher`] (plaintext for
+//! `ENVIRONMENT=local`, TLS + SASL/OAUTHBEARER with MSK IAM otherwise),
+//! subscribes to [`MacroChannelsTopic`], and prints every channel event it
+//! receives.
+//!
+//! The consumer follows the standard poll-loop / worker split: a Kafka poll
+//! loop decodes messages and hands them to a processing task over a bounded
+//! tokio mpsc channel (backpressure applies when the processor falls behind),
+//! offsets are committed only after a successful handoff (at-least-once), and
+//! ctrl-c drains the channel before exiting.
+//!
+//! Run against the local docker broker:
+//! ```sh
+//! ENVIRONMENT=local KAFKA_BROKERS=localhost:9092 \
+//! cargo run -p channels --example channels_consumer
+//! ```
+//! or against the dev MSK cluster (needs AWS credentials and network access):
+//! ```sh
+//! ENVIRONMENT=develop AWS_REGION=us-east-1 KAFKA_BROKERS=<bootstrapBrokersSaslIam> \
+//! cargo run -p channels --example channels_consumer
+//! ```
+
+use anyhow::Context as _;
+use channels::domain::broker_events::{ChannelMacroEvent, ChannelTopicEvent};
+use macro_env::Environment;
+use macro_event_broker::outbound::msk_iam::configure_sasl_iam;
+use macro_event_broker::{EventBrokerError, MacroEvent, MskIamClientContext, Topic as _};
+use macro_event_topics::MacroChannelsTopic;
+use rdkafka::ClientConfig;
+use rdkafka::consumer::{CommitMode, Consumer, StreamConsumer};
+use rdkafka::error::KafkaResult;
+use rdkafka::message::{BorrowedMessage, Message};
+use tokio::sync::mpsc;
+
+macro_env_var::env_var! {
+ struct ConsumerEnvVars {
+ KafkaBrokers,
+ }
+}
+
+/// Consumer group id for this example. Offsets are committed under this group,
+/// so re-runs resume where the previous run left off.
+const GROUP_ID: &str = "channels-consumer-example";
+
+/// Bounded capacity of the channel between the poll loop and the processor.
+const CHANNEL_CAPACITY: usize = 128;
+
+/// Consumer-specific event enum for a consumer subscribed to `macro.channels`,
+/// following the pattern in `macro_event_broker/examples/example_event.rs`.
+enum ChannelsConsumerEvent {
+ /// Event received on [`MacroChannelsTopic`].
+ Channels(ChannelMacroEvent),
+}
+
+impl ChannelsConsumerEvent {
+ /// Decode one Kafka message into this consumer's event enum.
+ fn decode(topic: &str, key: &str, payload: &[u8]) -> Result<Self, EventBrokerError> {
+ match topic {
+ topic if topic == MacroChannelsTopic.as_str() => {
+ Ok(Self::Channels(ChannelMacroEvent::decode(key, payload)?))
+ }
+ unknown => Err(EventBrokerError::UnknownTopic(unknown.to_string())),
+ }
+ }
+}
+
+/// A decoded event plus the Kafka coordinates it came from, as handed from the
+/// poll loop to the processor over the channel.
+struct ReceivedEvent {
+ partition: i32,
+ offset: i64,
+ event: ChannelsConsumerEvent,
+}
+
+/// The underlying consumer, split by transport (mirrors `KafkaEventPublisher`).
+enum ChannelsConsumer {
+ /// Unauthenticated plaintext connection (local docker broker).
+ Plaintext(StreamConsumer),
+ /// TLS + SASL/OAUTHBEARER with AWS MSK IAM auth (deployed clusters).
+ MskIam(StreamConsumer<MskIamClientContext>),
+}
+
+impl ChannelsConsumer {
+ /// Build a consumer for the given brokers, choosing the transport from the
+ /// `ENVIRONMENT` variable exactly like the publisher does.
+ fn from_env(brokers: &str) -> anyhow::Result<Self> {
+ let mut config = ClientConfig::new();
+ config
+ .set("bootstrap.servers", brokers)
+ .set("group.id", GROUP_ID)
+ // Offsets are committed manually after a successful handoff.
+ .set("enable.auto.commit", "false")
+ // Start from the beginning of the topic on the first ever run.
+ .set("auto.offset.reset", "earliest");
+
+ let consumer = match Environment::new_or_prod() {
+ Environment::Local => Self::Plaintext(
+ config
+ .create()
+ .context("failed to create plaintext kafka consumer")?,
+ ),
+ Environment::Develop | Environment::Production => {
+ configure_sasl_iam(&mut config);
+ Self::MskIam(
+ config
+ .create_with_context(MskIamClientContext::from_env())
+ .context("failed to create MSK IAM kafka consumer")?,
+ )
+ }
+ };
+
+ Ok(consumer)
+ }
+
+ fn subscribe(&self) -> KafkaResult<()> {
+ let topics = [MacroChannelsTopic.as_str()];
+ match self {
+ Self::Plaintext(consumer) => consumer.subscribe(&topics),
+ Self::MskIam(consumer) => consumer.subscribe(&topics),
+ }
+ }
+
+ /// Receive the next message. `StreamConsumer::recv` is cancel-safe, so it
+ /// can sit in a `select!` without losing messages.
+ async fn recv(&self) -> KafkaResult<BorrowedMessage<'_>> {
+ match self {
+ Self::Plaintext(consumer) => consumer.recv().await,
+ Self::MskIam(consumer) => consumer.recv().await,
+ }
+ }
+
+ fn commit(&self, message: &BorrowedMessage<'_>) -> KafkaResult<()> {
+ match self {
+ Self::Plaintext(consumer) => consumer.commit_message(message, CommitMode::Async),
+ Self::MskIam(consumer) => consumer.commit_message(message, CommitMode::Async),
+ }
+ }
+}
+
+fn event_type(event: &ChannelTopicEvent) -> &'static str {
+ match event {
+ ChannelTopicEvent::Created(_) => "channel.created",
+ ChannelTopicEvent::Updated(_) => "channel.updated",
+ ChannelTopicEvent::Deleted(_) => "channel.deleted",
+ ChannelTopicEvent::MessagePosted(_) => "channel.message_posted",
+ ChannelTopicEvent::MessagePatched(_) => "channel.message_patched",
+ ChannelTopicEvent::MessageDeleted(_) => "channel.message_deleted",
+ ChannelTopicEvent::MessageAttachmentCreated(_) => "channel.message_attachment_created",
+ ChannelTopicEvent::MessageAttachmentRemoved(_) => "channel.message_attachment_removed",
+ ChannelTopicEvent::ParticipantAdded(_) => "channel.participant_added",
+ ChannelTopicEvent::ParticipantRemoved(_) => "channel.participant_removed",
+ }
+}
+
+/// Print each received event. This stands in for real event handling — swap
+/// the body for indexing, notifications, etc.
+async fn process_events(mut events: mpsc::Receiver<ReceivedEvent>) {
+ while let Some(received) = events.recv().await {
+ let ChannelsConsumerEvent::Channels(event) = received.event;
+ let envelope = event.event();
+
+ println!(
+ "[partition {} offset {}] {} key={} event_id={} schema_version={}",
+ received.partition,
+ received.offset,
+ event_type(&envelope.event),
+ event.key(),
+ envelope.event_id,
+ envelope.schema_version,
+ );
+ match serde_json::to_string_pretty(envelope) {
+ Ok(json) => println!("{json}"),
+ Err(e) => eprintln!("failed to re-serialize event: {e}"),
+ }
+ }
+}
+
+#[tokio::main]
+async fn main() -> anyhow::Result<()> {
+ // Surface tracing from macro_event_broker (e.g. MSK IAM token refreshes)
+ // and librdkafka's internal connection/auth logs (bridged from the `log`
+ // crate). Tune with RUST_LOG, e.g. RUST_LOG=debug for librdkafka detail.
+ tracing_subscriber::fmt()
+ .with_env_filter(
+ tracing_subscriber::EnvFilter::try_from_default_env()
+ .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
+ )
+ .init();
+
+ let env = ConsumerEnvVars::new().context("KAFKA_BROKERS must be set")?;
+
+ let consumer = ChannelsConsumer::from_env(env.kafka_brokers.as_ref())?;
+ consumer
+ .subscribe()
+ .context("failed to subscribe to channels topic")?;
+ println!(
+ "listening on topic={} group={} brokers={} (ctrl-c to stop)",
+ MacroChannelsTopic.as_str(),
+ GROUP_ID,
+ env.kafka_brokers.as_ref(),
+ );
+
+ let (events_tx, events_rx) = mpsc::channel::<ReceivedEvent>(CHANNEL_CAPACITY);
+ let processor = tokio::spawn(process_events(events_rx));
+
+ let mut shutdown = std::pin::pin!(tokio::signal::ctrl_c());
+ loop {
+ tokio::select! {
+ _ = &mut shutdown => {
+ println!("received ctrl-c, shutting down");
+ break;
+ }
+ result = consumer.recv() => {
+ let message = match result {
+ Ok(message) => message,
+ Err(e) => {
+ eprintln!("kafka receive error: {e}");
+ continue;
+ }
+ };
+
+ let Some(payload) = message.payload() else {
+ eprintln!(
+ "skipping message with empty payload at partition {} offset {}",
+ message.partition(),
+ message.offset(),
+ );
+ let _ = consumer.commit(&message).inspect_err(|e| {
+ eprintln!("failed to commit offset {}: {e}", message.offset());
+ });
+ continue;
+ };
+ let key = String::from_utf8_lossy(message.key().unwrap_or_default());
+
+ match ChannelsConsumerEvent::decode(message.topic(), key.as_ref(), payload) {
+ Ok(event) => {
+ events_tx
+ .send(ReceivedEvent {
+ partition: message.partition(),
+ offset: message.offset(),
+ event,
+ })
+ .await
+ .context("processor task closed unexpectedly")?;
+ }
+ // Undecodable messages are logged and skipped rather than
+ // wedging the partition on a poison message.
+ Err(e) => eprintln!(
+ "failed to decode message at partition {} offset {}: {e}",
+ message.partition(),
+ message.offset(),
+ ),
+ }
+
+ // Commit only after the event was handed to the processor:
+ // at-least-once delivery (up to the channel buffer on a crash).
+ let _ = consumer.commit(&message).inspect_err(|e| {
+ eprintln!("failed to commit offset {}: {e}", message.offset());
+ });
+ }
+ }
+ }
+
+ // Close the channel and let the processor drain what it already received.
+ drop(events_tx);
+ processor.await.context("processor task panicked")?;
+
+ Ok(())
+}
diff --git a/rust/cloud-storage/channels/src/domain/broker_events.rs b/rust/cloud-storage/channels/src/domain/broker_events.rs
new file mode 100644
index 0000000000..0d8da32c9c
--- /dev/null
+++ b/rust/cloud-storage/channels/src/domain/broker_events.rs
@@ -0,0 +1,344 @@
+//! Kafka event models for the `macro.channels` topic.
+//!
+//! Follows the canonical pattern in `documents/src/domain/events.rs`:
+//! per-variant metadata structs, a [`TopicEvent`] enum tagged by `event_type`,
+//! and a [`MacroEvent`] wrapper keyed by channel id.
+
+#[cfg(test)]
+mod test;
+
+use channel_sender::ChannelSender;
+use chrono::{DateTime, Utc};
+use macro_event_broker::{Event, MacroEvent, TopicEvent};
+use macro_event_topics::MacroChannelsTopic;
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use crate::domain::models::{ChannelType, MutatedAttachment, SimpleMention};
+
+/// Attachment payload carried by channel wire events.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelEventAttachment {
+ /// Attachment id.
+ pub attachment_id: Uuid,
+ /// Attached entity type (e.g. `document`).
+ pub entity_type: String,
+ /// Attached entity id.
+ pub entity_id: String,
+ /// Creation timestamp of the attachment row.
+ pub created_at: DateTime<Utc>,
+}
+
+impl From<&MutatedAttachment> for ChannelEventAttachment {
+ fn from(attachment: &MutatedAttachment) -> Self {
+ Self {
+ attachment_id: attachment.id,
+ entity_type: attachment.entity_type.clone(),
+ entity_id: attachment.entity_id.clone(),
+ created_at: attachment.created_at,
+ }
+ }
+}
+
+/// Metadata for [`ChannelTopicEvent::Created`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelCreatedMetadata {
+ /// The id of the created channel.
+ pub channel_id: Uuid,
+ /// Actor that created the channel (channel owner).
+ pub actor: ChannelSender<'static>,
+ /// Type of channel that was created.
+ pub channel_type: ChannelType,
+ /// Stored channel name; `None` for direct message / unnamed channels.
+ pub channel_name: Option<String>,
+ /// Active participants after creation (including the owner).
+ pub participant_user_ids: Vec<MacroUserIdStr<'static>>,
+}
+
+/// Metadata for [`ChannelTopicEvent::Updated`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelUpdatedMetadata {
+ /// The id of the updated channel.
+ pub channel_id: Uuid,
+ /// The user who updated the channel.
+ pub actor: MacroUserIdStr<'static>,
+ /// Stored channel name before the update.
+ pub previous_name: Option<String>,
+ /// New channel name.
+ pub channel_name: Option<String>,
+}
+
+/// Metadata for [`ChannelTopicEvent::Deleted`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelDeletedMetadata {
+ /// The id of the deleted channel.
+ pub channel_id: Uuid,
+ /// Actor that deleted the channel.
+ pub actor: ChannelSender<'static>,
+}
+
+/// Metadata for [`ChannelTopicEvent::MessagePosted`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelMessagePostedMetadata {
+ /// Channel containing the message.
+ pub channel_id: Uuid,
+ /// The id of the posted message.
+ pub message_id: Uuid,
+ /// Thread parent id when the message is a thread reply.
+ pub thread_id: Option<Uuid>,
+ /// Message author; may be a bot.
+ pub sender: ChannelSender<'static>,
+ /// For an agent (bot) message, the id of the user who triggered it.
+ pub triggered_by: Option<String>,
+ /// Type of channel containing the message.
+ pub channel_type: ChannelType,
+ /// Message body.
+ pub content: String,
+ /// Mentions attached to the message.
+ pub mentions: Vec<SimpleMention>,
+ /// Attachments persisted with the message.
+ pub attachments: Vec<ChannelEventAttachment>,
+ /// Creation timestamp reported by the repository.
+ pub created_at: DateTime<Utc>,
+}
+
+/// Metadata for [`ChannelTopicEvent::MessagePatched`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelMessagePatchedMetadata {
+ /// Channel containing the message.
+ pub channel_id: Uuid,
+ /// The id of the patched message.
+ pub message_id: Uuid,
+ /// Thread parent id when the message is a thread reply.
+ pub thread_id: Option<Uuid>,
+ /// Actor that patched the message.
+ pub actor: ChannelSender<'static>,
+ /// Message body after the patch.
+ pub content: String,
+ /// Edit timestamp, when the patch marked the message edited.
+ pub edited_at: Option<DateTime<Utc>>,
+ /// Update timestamp reported by the repository.
+ pub updated_at: DateTime<Utc>,
+}
+
+/// Metadata for [`ChannelTopicEvent::MessageDeleted`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelMessageDeletedMetadata {
+ /// Channel containing the message.
+ pub channel_id: Uuid,
+ /// The id of the deleted message.
+ pub message_id: Uuid,
+ /// Thread parent id when the message is a thread reply.
+ pub thread_id: Option<Uuid>,
+ /// Actor that deleted the message; not necessarily the author.
+ pub actor: ChannelSender<'static>,
+ /// Deletion (tombstone) timestamp reported by the repository.
+ pub deleted_at: Option<DateTime<Utc>>,
+}
+
+/// Metadata for [`ChannelTopicEvent::MessageAttachmentCreated`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelMessageAttachmentCreatedMetadata {
+ /// Channel containing the message.
+ pub channel_id: Uuid,
+ /// Message the attachments were added to.
+ pub message_id: Uuid,
+ /// Actor that added the attachments.
+ pub actor: ChannelSender<'static>,
+ /// Attachments created by this mutation.
+ pub attachments: Vec<ChannelEventAttachment>,
+}
+
+/// Metadata for [`ChannelTopicEvent::MessageAttachmentRemoved`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelMessageAttachmentRemovedMetadata {
+ /// Channel containing the message.
+ pub channel_id: Uuid,
+ /// Message the attachments were removed from.
+ pub message_id: Uuid,
+ /// Actor that removed the attachments.
+ pub actor: ChannelSender<'static>,
+ /// Attachments removed by this mutation.
+ pub attachments: Vec<ChannelEventAttachment>,
+}
+
+/// Metadata for [`ChannelTopicEvent::ParticipantAdded`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelParticipantAddedMetadata {
+ /// Channel receiving new participants.
+ pub channel_id: Uuid,
+ /// Type of channel that received participants.
+ pub channel_type: ChannelType,
+ /// Actor who added the participants; equals the sole added user for a
+ /// self-service join.
+ pub added_by: ChannelSender<'static>,
+ /// Users added by this mutation.
+ pub added_user_ids: Vec<MacroUserIdStr<'static>>,
+}
+
+/// Metadata for [`ChannelTopicEvent::ParticipantRemoved`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChannelParticipantRemovedMetadata {
+ /// Channel the participants were removed from.
+ pub channel_id: Uuid,
+ /// Type of channel the participants were removed from.
+ pub channel_type: ChannelType,
+ /// Actor who removed the participants; equals the sole removed user for a
+ /// self-service leave.
+ pub removed_by: MacroUserIdStr<'static>,
+ /// Users removed by this mutation.
+ pub removed_user_ids: Vec<MacroUserIdStr<'static>>,
+}
+
+/// Events that can be published to [`MacroChannelsTopic`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "event_type", content = "metadata")]
+pub enum ChannelTopicEvent {
+ /// A channel was created.
+ #[serde(rename = "channel.created")]
+ Created(ChannelCreatedMetadata),
+ /// A channel's metadata was updated (rename).
+ #[serde(rename = "channel.updated")]
+ Updated(ChannelUpdatedMetadata),
+ /// A channel was deleted.
+ #[serde(rename = "channel.deleted")]
+ Deleted(ChannelDeletedMetadata),
+ /// A message was posted.
+ #[serde(rename = "channel.message_posted")]
+ MessagePosted(ChannelMessagePostedMetadata),
+ /// A message's content was patched.
+ #[serde(rename = "channel.message_patched")]
+ MessagePatched(ChannelMessagePatchedMetadata),
+ /// A message was soft-deleted.
+ #[serde(rename = "channel.message_deleted")]
+ MessageDeleted(ChannelMessageDeletedMetadata),
+ /// Attachments were added to a message.
+ #[serde(rename = "channel.message_attachment_created")]
+ MessageAttachmentCreated(ChannelMessageAttachmentCreatedMetadata),
+ /// Attachments were removed from a message.
+ #[serde(rename = "channel.message_attachment_removed")]
+ MessageAttachmentRemoved(ChannelMessageAttachmentRemovedMetadata),
+ /// Participants were added to a channel (admin invite or self-join).
+ #[serde(rename = "channel.participant_added")]
+ ParticipantAdded(ChannelParticipantAddedMetadata),
+ /// Participants were removed from a channel (admin removal or self-leave).
+ #[serde(rename = "channel.participant_removed")]
+ ParticipantRemoved(ChannelParticipantRemovedMetadata),
+}
+
+impl TopicEvent for ChannelTopicEvent {
+ type Topic = MacroChannelsTopic;
+
+ fn schema_version(&self) -> u8 {
+ 1
+ }
+}
+
+/// Publishable event for [`MacroChannelsTopic`], keyed by channel id.
+pub struct ChannelMacroEvent {
+ key: String,
+ event: Event<ChannelTopicEvent>,
+}
+
+impl ChannelMacroEvent {
+ /// Build a created event keyed by the created channel id.
+ pub fn created(metadata: ChannelCreatedMetadata) -> Self {
+ Self::new(metadata.channel_id, ChannelTopicEvent::Created(metadata))
+ }
+
+ /// Build an updated event keyed by the updated channel id.
+ pub fn updated(metadata: ChannelUpdatedMetadata) -> Self {
+ Self::new(metadata.channel_id, ChannelTopicEvent::Updated(metadata))
+ }
+
+ /// Build a deleted event keyed by the deleted channel id.
+ pub fn deleted(metadata: ChannelDeletedMetadata) -> Self {
+ Self::new(metadata.channel_id, ChannelTopicEvent::Deleted(metadata))
+ }
+
+ /// Build a message posted event keyed by the containing channel id.
+ pub fn message_posted(metadata: ChannelMessagePostedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::MessagePosted(metadata),
+ )
+ }
+
+ /// Build a message patched event keyed by the containing channel id.
+ pub fn message_patched(metadata: ChannelMessagePatchedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::MessagePatched(metadata),
+ )
+ }
+
+ /// Build a message deleted event keyed by the containing channel id.
+ pub fn message_deleted(metadata: ChannelMessageDeletedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::MessageDeleted(metadata),
+ )
+ }
+
+ /// Build an attachment created event keyed by the containing channel id.
+ pub fn message_attachment_created(metadata: ChannelMessageAttachmentCreatedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::MessageAttachmentCreated(metadata),
+ )
+ }
+
+ /// Build an attachment removed event keyed by the containing channel id.
+ pub fn message_attachment_removed(metadata: ChannelMessageAttachmentRemovedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::MessageAttachmentRemoved(metadata),
+ )
+ }
+
+ /// Build a participant added event keyed by the channel id.
+ pub fn participant_added(metadata: ChannelParticipantAddedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::ParticipantAdded(metadata),
+ )
+ }
+
+ /// Build a participant removed event keyed by the channel id.
+ pub fn participant_removed(metadata: ChannelParticipantRemovedMetadata) -> Self {
+ Self::new(
+ metadata.channel_id,
+ ChannelTopicEvent::ParticipantRemoved(metadata),
+ )
+ }
+
+ /// Build an event from a topic-specific event variant, keyed by channel id.
+ pub fn new(channel_id: Uuid, event: ChannelTopicEvent) -> Self {
+ Self::with_event(channel_id.to_string(), Event::new(event))
+ }
+
+ /// Build an event from a pre-built envelope.
+ pub fn with_event(key: impl Into<String>, event: Event<ChannelTopicEvent>) -> Self {
+ Self {
+ key: key.into(),
+ event,
+ }
+ }
+}
+
+impl MacroEvent for ChannelMacroEvent {
+ type EventPayload = ChannelTopicEvent;
+
+ fn key(&self) -> &str {
+ &self.key
+ }
+
+ fn event(&self) -> &Event<Self::EventPayload> {
+ &self.event
+ }
+
+ fn from_event(key: String, event: Event<Self::EventPayload>) -> Self {
+ Self::with_event(key, event)
+ }
+}
diff --git a/rust/cloud-storage/channels/src/domain/broker_events/test.rs b/rust/cloud-storage/channels/src/domain/broker_events/test.rs
new file mode 100644
index 0000000000..c741b7a52f
--- /dev/null
+++ b/rust/cloud-storage/channels/src/domain/broker_events/test.rs
@@ -0,0 +1,221 @@
+use chrono::{DateTime, Utc};
+use macro_event_broker::Event;
+use serde_json::json;
+
+use super::*;
+
+fn user(id: &str) -> ChannelSender<'static> {
+ ChannelSender::try_from(id.to_string()).expect("valid user principal")
+}
+
+fn user_id(id: &str) -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from(id.to_string()).expect("valid user id")
+}
+
+fn timestamp() -> DateTime<Utc> {
+ DateTime::parse_from_rfc3339("2026-01-02T03:04:05Z")
+ .expect("valid timestamp")
+ .with_timezone(&Utc)
+}
+
+#[test]
+fn created_event_wire_shape() {
+ let channel_id = Uuid::nil();
+ let event_id = Uuid::nil();
+ let event = Event::with_event_id(
+ event_id,
+ ChannelTopicEvent::Created(ChannelCreatedMetadata {
+ channel_id,
+ actor: user("macro|[email protected]"),
+ channel_type: ChannelType::Private,
+ channel_name: Some("general".to_string()),
+ participant_user_ids: vec![
+ user_id("macro|[email protected]"),
+ user_id("macro|[email protected]"),
+ ],
+ }),
+ );
+
+ let value = serde_json::to_value(&event).expect("serializable");
+ assert_eq!(
+ value,
+ json!({
+ "event_id": "00000000-0000-0000-0000-000000000000",
+ "schema_version": 1,
+ "event_type": "channel.created",
+ "metadata": {
+ "channel_id": "00000000-0000-0000-0000-000000000000",
+ "actor": "macro|[email protected]",
+ "channel_type": "private",
+ "channel_name": "general",
+ "participant_user_ids": [
+ ],
+ },
+ })
+ );
+}
+
+#[test]
+fn message_posted_wire_shape_with_bot_sender() {
+ let channel_id = Uuid::nil();
+ let message_id = Uuid::nil();
+ let bot_uuid = Uuid::nil();
+ let event = Event::with_event_id(
+ Uuid::nil(),
+ ChannelTopicEvent::MessagePosted(ChannelMessagePostedMetadata {
+ channel_id,
+ message_id,
+ thread_id: None,
+ sender: user(&format!("bot|{bot_uuid}")),
+ triggered_by: Some("macro|[email protected]".to_string()),
+ channel_type: ChannelType::Team,
+ content: "hello world".to_string(),
+ mentions: vec![SimpleMention {
+ entity_type: "user".to_string(),
+ entity_id: "macro|[email protected]".to_string(),
+ }],
+ attachments: vec![ChannelEventAttachment {
+ attachment_id: Uuid::nil(),
+ entity_type: "document".to_string(),
+ entity_id: "doc-1".to_string(),
+ created_at: timestamp(),
+ }],
+ created_at: timestamp(),
+ }),
+ );
+
+ let value = serde_json::to_value(&event).expect("serializable");
+ assert_eq!(
+ value,
+ json!({
+ "event_id": "00000000-0000-0000-0000-000000000000",
+ "schema_version": 1,
+ "event_type": "channel.message_posted",
+ "metadata": {
+ "channel_id": "00000000-0000-0000-0000-000000000000",
+ "message_id": "00000000-0000-0000-0000-000000000000",
+ "thread_id": null,
+ "sender": "bot|00000000-0000-0000-0000-000000000000",
+ "triggered_by": "macro|[email protected]",
+ "channel_type": "team",
+ "content": "hello world",
+ "mentions": [
+ { "entity_type": "user", "entity_id": "macro|[email protected]" },
+ ],
+ "attachments": [
+ {
+ "attachment_id": "00000000-0000-0000-0000-000000000000",
+ "entity_type": "document",
+ "entity_id": "doc-1",
+ "created_at": "2026-01-02T03:04:05Z",
+ },
+ ],
+ "created_at": "2026-01-02T03:04:05Z",
+ },
+ })
+ );
+}
+
+#[test]
+fn decode_round_trips() {
+ let channel_id = Uuid::new_v4();
+ let original = ChannelMacroEvent::participant_removed(ChannelParticipantRemovedMetadata {
+ channel_id,
+ channel_type: ChannelType::Private,
+ removed_by: user_id("macro|[email protected]"),
+ removed_user_ids: vec![user_id("macro|[email protected]")],
+ });
+
+ let payload = serde_json::to_vec(original.event()).expect("serializable");
+ let decoded = ChannelMacroEvent::decode(original.key(), &payload).expect("decodable payload");
+
+ assert_eq!(decoded.key(), channel_id.to_string());
+ assert_eq!(decoded.event(), original.event());
+}
+
+#[test]
+fn events_are_keyed_by_channel_id() {
+ let channel_id = Uuid::new_v4();
+ let event = ChannelMacroEvent::deleted(ChannelDeletedMetadata {
+ channel_id,
+ actor: user("macro|[email protected]"),
+ });
+ assert_eq!(event.key(), channel_id.to_string());
+}
+
+#[test]
+fn event_type_strings_follow_dot_convention() {
+ let channel_id = Uuid::nil();
+ let actor = user("macro|[email protected]");
+ let attachment_metadata = ChannelMessageAttachmentCreatedMetadata {
+ channel_id,
+ message_id: Uuid::nil(),
+ actor: actor.clone(),
+ attachments: vec![],
+ };
+
+ let cases = vec![
+ (
+ ChannelMacroEvent::updated(ChannelUpdatedMetadata {
+ channel_id,
+ actor: user_id("macro|[email protected]"),
+ previous_name: None,
+ channel_name: Some("renamed".to_string()),
+ }),
+ "channel.updated",
+ ),
+ (
+ ChannelMacroEvent::message_patched(ChannelMessagePatchedMetadata {
+ channel_id,
+ message_id: Uuid::nil(),
+ thread_id: None,
+ actor: actor.clone(),
+ content: "edited".to_string(),
+ edited_at: Some(timestamp()),
+ updated_at: timestamp(),
+ }),
+ "channel.message_patched",
+ ),
+ (
+ ChannelMacroEvent::message_deleted(ChannelMessageDeletedMetadata {
+ channel_id,
+ message_id: Uuid::nil(),
+ thread_id: None,
+ actor: actor.clone(),
+ deleted_at: Some(timestamp()),
+ }),
+ "channel.message_deleted",
+ ),
+ (
+ ChannelMacroEvent::message_attachment_created(attachment_metadata.clone()),
+ "channel.message_attachment_created",
+ ),
+ (
+ ChannelMacroEvent::message_attachment_removed(
+ ChannelMessageAttachmentRemovedMetadata {
+ channel_id,
+ message_id: Uuid::nil(),
+ actor,
+ attachments: attachment_metadata.attachments,
+ },
+ ),
+ "channel.message_attachment_removed",
+ ),
+ (
+ ChannelMacroEvent::participant_added(ChannelParticipantAddedMetadata {
+ channel_id,
+ channel_type: ChannelType::Team,
+ added_by: user("macro|[email protected]"),
+ added_user_ids: vec![user_id("macro|[email protected]")],
+ }),
+ "channel.participant_added",
+ ),
+ ];
+
+ for (event, expected_type) in cases {
+ let value = serde_json::to_value(event.event()).expect("serializable");
+ assert_eq!(value["event_type"], *expected_type);
+ }
+}
diff --git a/rust/cloud-storage/channels/src/domain/events.rs b/rust/cloud-storage/channels/src/domain/events.rs
index 3162015915..e5506c3fb3 100644
--- a/rust/cloud-storage/channels/src/domain/events.rs
+++ b/rust/cloud-storage/channels/src/domain/events.rs
@@ -32,6 +32,8 @@ pub enum ChannelEvent {
actor: ChannelSender<'static>,
/// Type of channel that was created.
channel_type: ChannelType,
+ /// Stored channel name; `None` for direct message / unnamed channels.
+ channel_name: Option<String>,
/// Active participants after creation.
participant_user_ids: Vec<MacroUserIdStr<'static>>,
},
@@ -73,6 +75,10 @@ pub enum ChannelEvent {
message_id: Uuid,
/// Current attachment set for the message.
attachments: Vec<MutatedAttachment>,
+ /// Attachments added by this mutation.
+ added: Vec<MutatedAttachment>,
+ /// Attachments removed by this mutation.
+ removed: Vec<MutatedAttachment>,
/// Realtime recipients at mutation time.
recipients: Vec<MacroUserIdStr<'static>>,
/// Client mutation nonce echoed to realtime listeners.
@@ -164,4 +170,27 @@ pub enum ChannelEvent {
/// Active participants after the join.
active_participant_user_ids: Vec<MacroUserIdStr<'static>>,
},
+ /// Channel metadata was updated (rename).
+ ChannelUpdated {
+ /// Updated channel id.
+ channel_id: Uuid,
+ /// The user who updated the channel.
+ actor: MacroUserIdStr<'static>,
+ /// Stored channel name before the update.
+ previous_name: Option<String>,
+ /// New channel name.
+ channel_name: Option<String>,
+ },
+ /// Participants were removed from a channel (admin removal or self-leave).
+ ParticipantsRemoved {
+ /// Channel the participants were removed from.
+ channel_id: Uuid,
+ /// Type of channel the participants were removed from.
+ channel_type: ChannelType,
+ /// The user who removed the participants; equals the sole removed
+ /// user for a self-service leave.
+ actor: MacroUserIdStr<'static>,
+ /// Users removed by this mutation.
+ removed_user_ids: Vec<MacroUserIdStr<'static>>,
+ },
}
diff --git a/rust/cloud-storage/channels/src/domain/mod.rs b/rust/cloud-storage/channels/src/domain/mod.rs
index 1e8be8d4d6..c9063e424e 100644
--- a/rust/cloud-storage/channels/src/domain/mod.rs
+++ b/rust/cloud-storage/channels/src/domain/mod.rs
@@ -1,3 +1,5 @@
+/// Kafka event models for the `macro.channels` topic.
+pub mod broker_events;
/// Domain events emitted by channel workflows.
pub mod events;
/// Legacy channel list service implementation.
diff --git a/rust/cloud-storage/channels/src/domain/service.rs b/rust/cloud-storage/channels/src/domain/service.rs
index b62aca1e08..c9e937cbc0 100644
--- a/rust/cloud-storage/channels/src/domain/service.rs
+++ b/rust/cloud-storage/channels/src/domain/service.rs
@@ -339,6 +339,7 @@ where
let participants_clone = req.participants.clone();
let channel_type = req.channel_type;
+ let channel_name = req.name.clone();
let channel_id = self
.create_channel_record(actor.copied(), org_id, req)
@@ -348,6 +349,7 @@ where
channel_id,
actor: Sender::new_from_user(actor.clone()),
channel_type,
+ channel_name,
participant_user_ids: created_channel_participant_ids(actor, participants_clone)
.into_iter()
.collect(),
@@ -445,10 +447,20 @@ where
"cannot change channel_name for direct message channels".to_string(),
));
}
+ let channel_name = req.channel_name.clone();
self.repo
.patch_channel(channel_id, actor.as_ref().to_string(), req)
.await
- .map_err(|e| ChannelMutationErr::Repo(e.into()))
+ .map_err(|e| ChannelMutationErr::Repo(e.into()))?;
+ if channel_name.is_some() {
+ self.events.dispatch(ChannelEvent::ChannelUpdated {
+ channel_id,
+ actor,
+ previous_name: info.name,
+ channel_name,
+ });
+ }
+ Ok(())
}
#[tracing::instrument(err, skip(self))]
@@ -915,10 +927,11 @@ where
#[tracing::instrument(err, skip(self, req))]
async fn remove_participants(
&self,
- _actor: Sender,
+ actor: Sender,
channel_id: Uuid,
req: RemoveParticipantsRequest,
) -> Result<(), ChannelMutationErr> {
+ let actor_user = require_user_actor(&actor)?;
let info = self
.repo
.get_channel_info(channel_id)
@@ -942,12 +955,22 @@ where
"cannot remove the channel owner".to_string(),
));
}
- for participant in req.participants {
+ for participant in &req.participants {
self.repo
- .remove_participant(channel_id, participant)
+ .remove_participant(channel_id, participant.clone())
.await
.map_err(|e| ChannelMutationErr::Repo(e.into()))?;
}
+ self.events.dispatch(ChannelEvent::ParticipantsRemoved {
+ channel_id,
+ channel_type: info.channel_type,
+ actor: actor_user,
+ removed_user_ids: req
+ .participants
+ .into_iter()
+ .filter_map(|id| MacroUserIdStr::try_from(id).ok())
+ .collect(),
+ });
Ok(())
}
@@ -1022,7 +1045,14 @@ where
self.repo
.remove_participant(channel_id, actor_user.as_ref().to_string())
.await
- .map_err(|e| ChannelMutationErr::Repo(e.into()))
+ .map_err(|e| ChannelMutationErr::Repo(e.into()))?;
+ self.events.dispatch(ChannelEvent::ParticipantsRemoved {
+ channel_id,
+ channel_type: info.channel_type,
+ actor: actor_user.clone(),
+ removed_user_ids: vec![actor_user],
+ });
+ Ok(())
}
}
@@ -1059,6 +1089,7 @@ where
}
let channel_type = create_req.channel_type;
+ let channel_name = create_req.name.clone();
let owner_sender = ChannelSender::new_from_user(owner_id.clone());
let participant_user_ids =
created_channel_participant_ids(owner_id.clone(), create_req.participants.clone())
@@ -1071,6 +1102,7 @@ where
channel_id,
actor: owner_sender,
channel_type,
+ channel_name,
participant_user_ids,
});
Ok(GetOrCreateChannelResponse {
@@ -1128,12 +1160,14 @@ where
.map_err(|e| ChannelMutationErr::Repo(e.into()))?;
}
- if !attachments_to_add.is_empty() {
+ let added_attachments = if attachments_to_add.is_empty() {
+ Vec::new()
+ } else {
self.repo
.add_attachments(message_id, channel_id, attachments_to_add.clone())
.await
- .map_err(|e| ChannelMutationErr::Repo(e.into()))?;
- }
+ .map_err(|e| ChannelMutationErr::Repo(e.into()))?
+ };
let items = extract_share_items(&attachments_to_add, &[]);
if !items.is_empty()
@@ -1171,6 +1205,8 @@ where
actor,
message_id,
attachments: all_attachments,
+ added: added_attachments,
+ removed: attachments_to_delete,
recipients: participant_ids(&participants),
nonce,
});
diff --git a/rust/cloud-storage/channels/src/domain/service/test.rs b/rust/cloud-storage/channels/src/domain/service/test.rs
index 5cd578998f..eb4ef81511 100644
--- a/rust/cloud-storage/channels/src/domain/service/test.rs
+++ b/rust/cloud-storage/channels/src/domain/service/test.rs
@@ -1605,3 +1605,178 @@ async fn remove_participants_allows_removing_non_owner() {
vec!["macro|[email protected]".to_string()]
);
}
+
+#[tokio::test]
+async fn create_channel_event_carries_channel_name() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let events = FakeEvents::default();
+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());
+
+ svc.create_channel(
+ sender("macro|[email protected]"),
+ None,
+ crate::domain::models::CreateChannelRequest {
+ name: Some("general".to_string()),
+ channel_type: ChannelType::Private,
+ team_id: None,
+ participants: HashSet::from([macro_id("macro|[email protected]")]),
+ },
+ )
+ .await
+ .unwrap();
+
+ let events = events.events.lock().unwrap();
+ assert!(matches!(
+ events.as_slice(),
+ [ChannelEvent::ChannelCreated { channel_name: Some(name), .. }] if name == "general"
+ ));
+}
+
+#[tokio::test]
+async fn patch_channel_dispatches_channel_updated() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let events = FakeEvents::default();
+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());
+
+ svc.patch_channel(
+ sender("macro|[email protected]"),
+ channel_id,
+ PatchChannelRequest {
+ channel_name: Some("Renamed".to_string()),
+ },
+ )
+ .await
+ .unwrap();
+
+ let events = events.events.lock().unwrap();
+ assert!(matches!(
+ events.as_slice(),
+ [ChannelEvent::ChannelUpdated { previous_name: Some(previous), channel_name: Some(new), actor, .. }]
+ if previous == "Project" && new == "Renamed" && actor == &macro_id("macro|[email protected]")
+ ));
+}
+
+#[tokio::test]
+async fn noop_patch_channel_dispatches_nothing() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let events = FakeEvents::default();
+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());
+
+ svc.patch_channel(
+ sender("macro|[email protected]"),
+ channel_id,
+ PatchChannelRequest { channel_name: None },
+ )
+ .await
+ .unwrap();
+
+ assert!(events.events.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn remove_participants_dispatches_participants_removed() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let events = FakeEvents::default();
+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());
+
+ svc.remove_participants(
+ sender("macro|[email protected]"),
+ channel_id,
+ RemoveParticipantsRequest {
+ participants: vec!["macro|[email protected]".to_string()],
+ },
+ )
+ .await
+ .unwrap();
+
+ let events = events.events.lock().unwrap();
+ assert!(matches!(
+ events.as_slice(),
+ [ChannelEvent::ParticipantsRemoved { actor, removed_user_ids, .. }]
+ if actor == &macro_id("macro|[email protected]")
+ && removed_user_ids == &vec![macro_id("macro|[email protected]")]
+ ));
+}
+
+#[tokio::test]
+async fn leave_channel_dispatches_participants_removed_for_self() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ // A private channel needs more than 2 participants to allow leaving.
+ repo.state
+ .lock()
+ .unwrap()
+ .participants
+ .push(ChannelParticipant {
+ channel_id,
+ user_id: "macro|[email protected]".to_string(),
+ role: ParticipantRole::Member,
+ joined_at: Utc::now(),
+ left_at: None,
+ });
+ let events = FakeEvents::default();
+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());
+
+ svc.leave_channel(sender("macro|[email protected]"), channel_id)
+ .await
+ .unwrap();
+
+ let events = events.events.lock().unwrap();
+ assert!(matches!(
+ events.as_slice(),
+ [ChannelEvent::ParticipantsRemoved { actor, removed_user_ids, .. }]
+ if actor == &macro_id("macro|[email protected]")
+ && removed_user_ids == &vec![macro_id("macro|[email protected]")]
+ ));
+}
+
+#[tokio::test]
+async fn patch_message_attachments_event_carries_deltas() {
+ let channel_id = Uuid::new_v4();
+ let repo = FakeMutationRepo::new(channel_id, "macro|[email protected]");
+ let message_id = repo.state.lock().unwrap().message.id;
+ let existing = MutatedAttachment {
+ id: Uuid::new_v4(),
+ channel_id,
+ message_id,
+ entity_type: "document".to_string(),
+ entity_id: "doc-old".to_string(),
+ width: None,
+ height: None,
+ created_at: Utc::now(),
+ };
+ repo.state.lock().unwrap().attachments = vec![existing.clone()];
+ let events = FakeEvents::default();
+ let svc = mutation_service(repo, events.clone(), FakeReferenceSharing::default());
+
+ svc.patch_message_attachments(
+ sender("macro|[email protected]"),
+ channel_id,
+ message_id,
+ vec![existing.id.to_string()],
+ vec![NewChannelAttachment {
+ entity_type: "document".to_string(),
+ entity_id: "doc-new".to_string(),
+ width: None,
+ height: None,
+ }],
+ None,
+ )
+ .await
+ .unwrap();
+
+ let events = events.events.lock().unwrap();
+ match events.as_slice() {
+ [ChannelEvent::AttachmentsChanged { added, removed, .. }] => {
+ assert_eq!(added.len(), 1);
+ assert_eq!(added[0].entity_id, "doc-new");
+ assert_eq!(removed.len(), 1);
+ assert_eq!(removed[0].id, existing.id);
+ }
+ other => panic!("expected one AttachmentsChanged event, got {other:?}"),
+ }
+}
diff --git a/rust/cloud-storage/channels/src/domain/side_effects.rs b/rust/cloud-storage/channels/src/domain/side_effects.rs
index 4f5f56992b..724030e253 100644
--- a/rust/cloud-storage/channels/src/domain/side_effects.rs
+++ b/rust/cloud-storage/channels/src/domain/side_effects.rs
@@ -1,6 +1,12 @@
//! Domain policies for channel side effects.
use crate::domain::{
+ broker_events::{
+ ChannelCreatedMetadata, ChannelDeletedMetadata, ChannelEventAttachment, ChannelMacroEvent,
+ ChannelMessageAttachmentCreatedMetadata, ChannelMessageAttachmentRemovedMetadata,
+ ChannelMessageDeletedMetadata, ChannelMessagePatchedMetadata, ChannelMessagePostedMetadata,
+ ChannelParticipantAddedMetadata, ChannelParticipantRemovedMetadata, ChannelUpdatedMetadata,
+ },
events::ChannelEvent,
models::{
BotId, BotSenderProfile, ChannelMetadata, ChannelParticipant, ChannelType, CountedReaction,
@@ -14,6 +20,7 @@ use crate::domain::{
},
};
use bot_id::BotIdStr;
+use macro_event_broker::{MacroEventBroker, NoopMacroEventBroker};
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use std::collections::HashSet;
use tokio::sync::mpsc::UnboundedSender;
@@ -287,13 +294,14 @@ pub enum ChannelNotificationEffect {
/// Domain service that derives and dispatches side effects for channel events.
#[derive(Clone)]
-pub struct ChannelSideEffectService<C, R, N, S, K> {
+pub struct ChannelSideEffectService<C, R, N, S, K, B = NoopMacroEventBroker> {
context: C,
realtime: R,
notifications: N,
search: S,
contacts: K,
bot_triggers: Option<ChannelBotTriggerSender>,
+ macro_event_broker: B,
}
struct MessagePostedSideEffects {
@@ -319,7 +327,10 @@ struct InviteNotificationRequest {
}
impl<C, R, N, S, K> ChannelSideEffectService<C, R, N, S, K> {
- /// Create a channel side-effect service.
+ /// Create a channel side-effect service that drops broker events.
+ ///
+ /// Use [`Self::with_macro_event_broker`] to publish channel events to the
+ /// macro event broker.
pub fn new(context: C, realtime: R, notifications: N, search: S, contacts: K) -> Self {
Self {
context,
@@ -328,15 +339,34 @@ impl<C, R, N, S, K> ChannelSideEffectService<C, R, N, S, K> {
search,
contacts,
bot_triggers: None,
+ macro_event_broker: NoopMacroEventBroker,
}
}
+}
+impl<C, R, N, S, K, B> ChannelSideEffectService<C, R, N, S, K, B> {
/// Configure a sender for bot triggers derived from channel messages.
pub fn with_bot_trigger_sender(mut self, bot_triggers: ChannelBotTriggerSender) -> Self {
self.bot_triggers = Some(bot_triggers);
self
}
+ /// Configure a macro event broker to publish channel events to.
+ pub fn with_macro_event_broker<B2: MacroEventBroker>(
+ self,
+ macro_event_broker: B2,
+ ) -> ChannelSideEffectService<C, R, N, S, K, B2> {
+ ChannelSideEffectService {
+ context: self.context,
+ realtime: self.realtime,
+ notifications: self.notifications,
+ search: self.search,
+ contacts: self.contacts,
+ bot_triggers: self.bot_triggers,
+ macro_event_broker,
+ }
+ }
+
/// Dispatch bot triggers for any bots mentioned in a user-authored message.
fn dispatch_bot_triggers(
&self,
@@ -397,19 +427,23 @@ where
}
}
-impl<C, R, N, S, K> ChannelEventHandler for ChannelSideEffectService<C, R, N, S, K>
+impl<C, R, N, S, K, B> ChannelEventHandler for ChannelSideEffectService<C, R, N, S, K, B>
where
C: ChannelSideEffectContext + Clone,
R: ChannelRealtimePublisher + Clone,
N: ChannelNotificationSender + Clone,
S: ChannelSearchIndexer + Clone,
K: ChannelContactsDispatcher + Clone,
+ B: MacroEventBroker + Clone,
{
async fn handle(&self, event: ChannelEvent) {
let contact_sync_users = contact_sync_users_for_event(&event);
+ let broker_events = broker_events_for_event(&event);
match event {
ChannelEvent::ChannelCreated { .. } => {}
+ ChannelEvent::ChannelUpdated { .. } => {}
+ ChannelEvent::ParticipantsRemoved { .. } => {}
ChannelEvent::ChannelDeleted { channel_id, .. } => {
self.search.remove_message(channel_id, None).await;
}
@@ -570,10 +604,23 @@ where
let err: anyhow::Error = err.into();
tracing::error!(error=?err, "unable to enqueue channel contact sync");
}
+
+ // Published after the other side effects so broker latency never
+ // delays realtime updates or notifications; failures are logged and
+ // dropped.
+ for broker_event in broker_events {
+ let _ = self
+ .macro_event_broker
+ .send_event(&broker_event)
+ .await
+ .inspect_err(|e| {
+ tracing::error!(error=?e, "failed to publish channel event");
+ });
+ }
}
}
-impl<C, R, N, S, K> ChannelSideEffectService<C, R, N, S, K>
+impl<C, R, N, S, K, B> ChannelSideEffectService<C, R, N, S, K, B>
where
C: ChannelSideEffectContext + Clone,
R: ChannelRealtimePublisher + Clone,
@@ -1149,5 +1196,191 @@ fn contact_sync_users_for_event(event: &ChannelEvent) -> Option<HashSet<MacroUse
}
}
+/// Map an internal channel event to the wire events published to the
+/// `macro.channels` topic.
+///
+/// Ephemeral events (typing) and reaction changes publish nothing.
+fn broker_events_for_event(event: &ChannelEvent) -> Vec<ChannelMacroEvent> {
+ match event {
+ ChannelEvent::ChannelCreated {
+ channel_id,
+ actor,
+ channel_type,
+ channel_name,
+ participant_user_ids,
+ } => vec![ChannelMacroEvent::created(ChannelCreatedMetadata {
+ channel_id: *channel_id,
+ actor: actor.clone(),
+ channel_type: *channel_type,
+ channel_name: channel_name.clone(),
+ participant_user_ids: participant_user_ids.clone(),
+ })],
+ ChannelEvent::ChannelUpdated {
+ channel_id,
+ actor,
+ previous_name,
+ channel_name,
+ } => vec![ChannelMacroEvent::updated(ChannelUpdatedMetadata {
+ channel_id: *channel_id,
+ actor: actor.clone(),
+ previous_name: previous_name.clone(),
+ channel_name: channel_name.clone(),
+ })],
+ ChannelEvent::ChannelDeleted { channel_id, actor } => {
+ vec![ChannelMacroEvent::deleted(ChannelDeletedMetadata {
+ channel_id: *channel_id,
+ actor: actor.clone(),
+ })]
+ }
+ ChannelEvent::MessagePosted {
+ channel_id,
+ metadata,
+ message,
+ mentions,
+ attachments,
+ ..
+ } => {
+ let mut events = vec![ChannelMacroEvent::message_posted(
+ ChannelMessagePostedMetadata {
+ channel_id: *channel_id,
+ message_id: message.id,
+ thread_id: message.thread_id,
+ sender: message.sender_id.clone(),
+ triggered_by: message.triggered_by.clone(),
+ channel_type: metadata.channel_type,
+ content: message.content.clone(),
+ mentions: mentions.clone(),
+ attachments: attachments
+ .iter()
+ .map(ChannelEventAttachment::from)
+ .collect(),
+ created_at: message.created_at,
+ },
+ )];
+ if !attachments.is_empty() {
+ events.push(ChannelMacroEvent::message_attachment_created(
+ ChannelMessageAttachmentCreatedMetadata {
+ channel_id: *channel_id,
+ message_id: message.id,
+ actor: message.sender_id.clone(),
+ attachments: attachments
+ .iter()
+ .map(ChannelEventAttachment::from)
+ .collect(),
+ },
+ ));
+ }
+ events
+ }
+ ChannelEvent::MessageChanged {
+ channel_id,
+ actor,
+ message,
+ ..
+ } => vec![ChannelMacroEvent::message_patched(
+ ChannelMessagePatchedMetadata {
+ channel_id: *channel_id,
+ message_id: message.id,
+ thread_id: message.thread_id,
+ actor: actor.clone(),
+ content: message.content.clone(),
+ edited_at: message.edited_at,
+ updated_at: message.updated_at,
+ },
+ )],
+ ChannelEvent::MessageDeleted {
+ channel_id,
+ actor,
+ message,
+ ..
+ } => vec![ChannelMacroEvent::message_deleted(
+ ChannelMessageDeletedMetadata {
+ channel_id: *channel_id,
+ message_id: message.id,
+ thread_id: message.thread_id,
+ actor: actor.clone(),
+ deleted_at: message.deleted_at,
+ },
+ )],
+ ChannelEvent::AttachmentsChanged {
+ channel_id,
+ actor,
+ message_id,
+ added,
+ removed,
+ ..
+ } => {
+ let mut events = Vec::new();
+ if !added.is_empty() {
+ events.push(ChannelMacroEvent::message_attachment_created(
+ ChannelMessageAttachmentCreatedMetadata {
+ channel_id: *channel_id,
+ message_id: *message_id,
+ actor: actor.clone(),
+ attachments: added.iter().map(ChannelEventAttachment::from).collect(),
+ },
+ ));
+ }
+ if !removed.is_empty() {
+ events.push(ChannelMacroEvent::message_attachment_removed(
+ ChannelMessageAttachmentRemovedMetadata {
+ channel_id: *channel_id,
+ message_id: *message_id,
+ actor: actor.clone(),
+ attachments: removed.iter().map(ChannelEventAttachment::from).collect(),
+ },
+ ));
+ }
+ events
+ }
+ ChannelEvent::ParticipantsAdded {
+ channel_id,
+ channel_type,
+ invited_by,
+ recipient_user_ids,
+ ..
+ } => vec![ChannelMacroEvent::participant_added(
+ ChannelParticipantAddedMetadata {
+ channel_id: *channel_id,
+ channel_type: *channel_type,
+ added_by: invited_by.clone(),
+ added_user_ids: recipient_user_ids.clone(),
+ },
+ )],
+ ChannelEvent::ParticipantJoined {
+ channel_id,
+ channel_type,
+ user_id,
+ ..
+ } => user_id
+ .as_user()
+ .map(|joined| {
+ vec![ChannelMacroEvent::participant_added(
+ ChannelParticipantAddedMetadata {
+ channel_id: *channel_id,
+ channel_type: *channel_type,
+ added_by: user_id.clone(),
+ added_user_ids: vec![joined.clone()],
+ },
+ )]
+ })
+ .unwrap_or_default(),
+ ChannelEvent::ParticipantsRemoved {
+ channel_id,
+ channel_type,
+ actor,
+ removed_user_ids,
+ } => vec![ChannelMacroEvent::participant_removed(
+ ChannelParticipantRemovedMetadata {
+ channel_id: *channel_id,
+ channel_type: *channel_type,
+ removed_by: actor.clone(),
+ removed_user_ids: removed_user_ids.clone(),
+ },
+ )],
+ ChannelEvent::ReactionChanged { .. } | ChannelEvent::TypingChanged { .. } => Vec::new(),
+ }
+}
+
#[cfg(test)]
mod test;
diff --git a/rust/cloud-storage/channels/src/domain/side_effects/test.rs b/rust/cloud-storage/channels/src/domain/side_effects/test.rs
index 048df3deaa..eb38e5cdd1 100644
--- a/rust/cloud-storage/channels/src/domain/side_effects/test.rs
+++ b/rust/cloud-storage/channels/src/domain/side_effects/test.rs
@@ -900,6 +900,7 @@ fn contact_sync_is_derived_from_private_channel_created() {
channel_id: Uuid::nil(),
actor: Sender::new_from_user(user("[email protected]")),
channel_type: ChannelType::Private,
+ channel_name: None,
participant_user_ids: users(&["[email protected]", "[email protected]"]),
};
@@ -916,6 +917,7 @@ fn contact_sync_ignores_public_channel_created() {
channel_id: Uuid::nil(),
actor: Sender::new_from_user(user("[email protected]")),
channel_type: ChannelType::Public,
+ channel_name: None,
participant_user_ids: users(&["[email protected]", "[email protected]"]),
};
@@ -1030,3 +1032,336 @@ fn macro_ai_user_mention_is_not_a_user_recipient() {
));
assert!(!is_bot_principal("macro|[email protected]"));
}
+
+#[derive(Clone, Default)]
+struct TestEventBroker {
+ published: Arc<Mutex<Vec<PublishedEvent>>>,
+}
+
+#[derive(Debug, Clone)]
+struct PublishedEvent {
+ topic: String,
+ key: String,
+ envelope: serde_json::Value,
+}
+
+impl MacroEventBroker for TestEventBroker {
+ async fn send_event<E: macro_event_broker::MacroEvent + ?Sized>(
+ &self,
+ event: &E,
+ ) -> Result<(), macro_event_broker::EventBrokerError> {
+ use macro_event_topics::Topic as _;
+ self.published.lock().unwrap().push(PublishedEvent {
+ topic: event.topic().as_str().to_string(),
+ key: event.key().to_string(),
+ envelope: serde_json::to_value(event.event())?,
+ });
+ Ok(())
+ }
+}
+
+#[derive(Clone, Default)]
+struct FailingEventBroker;
+
+impl MacroEventBroker for FailingEventBroker {
+ async fn send_event<E: macro_event_broker::MacroEvent + ?Sized>(
+ &self,
+ _event: &E,
+ ) -> Result<(), macro_event_broker::EventBrokerError> {
+ Err(macro_event_broker::EventBrokerError::Publish(
+ "broker unavailable".to_string(),
+ ))
+ }
+}
+
+fn broker_service(
+ broker: TestEventBroker,
+) -> ChannelSideEffectService<
+ FakeContext,
+ FakeRealtime,
+ FakeNotifications,
+ FakeSearch,
+ FakeContacts,
+ TestEventBroker,
+> {
+ ChannelSideEffectService::new(
+ FakeContext::default(),
+ FakeRealtime::default(),
+ FakeNotifications::default(),
+ FakeSearch::default(),
+ FakeContacts::default(),
+ )
+ .with_macro_event_broker(broker)
+}
+
+fn attachment(channel_id: Uuid, message_id: Uuid) -> MutatedAttachment {
+ MutatedAttachment {
+ id: Uuid::new_v4(),
+ channel_id,
+ message_id,
+ entity_type: "document".to_string(),
+ entity_id: "doc-1".to_string(),
+ width: None,
+ height: None,
+ created_at: Utc::now(),
+ }
+}
+
+#[tokio::test]
+async fn handle_publishes_channel_created_event() {
+ let broker = TestEventBroker::default();
+ let service = broker_service(broker.clone());
+ let channel_id = Uuid::new_v4();
+
+ service
+ .handle(ChannelEvent::ChannelCreated {
+ channel_id,
+ actor: Sender::new_from_user(user("[email protected]")),
+ channel_type: ChannelType::Private,
+ channel_name: Some("general".to_string()),
+ participant_user_ids: users(&["[email protected]", "[email protected]"]),
+ })
+ .await;
+
+ let published = broker.published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ assert_eq!(published[0].topic, "macro.channels");
+ assert_eq!(published[0].key, channel_id.to_string());
+ assert_eq!(published[0].envelope["event_type"], "channel.created");
+ assert_eq!(published[0].envelope["metadata"]["channel_name"], "general");
+ assert_eq!(
+ published[0].envelope["metadata"]["actor"],
+ );
+}
+
+#[tokio::test]
+async fn handle_publishes_message_posted_and_attachment_created_events() {
+ let broker = TestEventBroker::default();
+ let service = broker_service(broker.clone());
+ let channel_id = Uuid::new_v4();
+ let message_id = Uuid::new_v4();
+ let now = Utc::now();
+
+ service
+ .handle(ChannelEvent::MessagePosted {
+ channel_id,
+ metadata: ChannelMetadata {
+ channel_type: ChannelType::Team,
+ channel_name: "Project".to_string(),
+ },
+ participants: Vec::new(),
+ message: MutatedMessage {
+ id: message_id,
+ channel_id,
+ thread_id: None,
+ sender_id: Sender::new_from_user(user("[email protected]")),
+ triggered_by: None,
+ content: "hello world".to_string(),
+ created_at: now,
+ updated_at: now,
+ edited_at: None,
+ deleted_at: None,
+ },
+ mentions: Vec::new(),
+ has_attachments: true,
+ attachments: vec![attachment(channel_id, message_id)],
+ nonce: None,
+ notification_policy: PostMessageNotificationPolicy::Default,
+ })
+ .await;
+
+ let published = broker.published.lock().unwrap();
+ assert_eq!(published.len(), 2);
+ assert_eq!(
+ published[0].envelope["event_type"],
+ "channel.message_posted"
+ );
+ assert_eq!(published[0].envelope["metadata"]["content"], "hello world");
+ assert_eq!(published[0].envelope["metadata"]["channel_type"], "team");
+ assert_eq!(
+ published[1].envelope["event_type"],
+ "channel.message_attachment_created"
+ );
+ assert_eq!(
+ published[1].envelope["metadata"]["attachments"]
+ .as_array()
+ .unwrap()
+ .len(),
+ 1
+ );
+ for event in published.iter() {
+ assert_eq!(event.key, channel_id.to_string());
+ }
+}
+
+#[tokio::test]
+async fn handle_publishes_attachment_deltas() {
+ let broker = TestEventBroker::default();
+ let service = broker_service(broker.clone());
+ let channel_id = Uuid::new_v4();
+ let message_id = Uuid::new_v4();
+ let added = attachment(channel_id, message_id);
+ let removed = attachment(channel_id, message_id);
+
+ service
+ .handle(ChannelEvent::AttachmentsChanged {
+ channel_id,
+ actor: Sender::new_from_user(user("[email protected]")),
+ message_id,
+ attachments: vec![added.clone()],
+ added: vec![added.clone()],
+ removed: vec![removed.clone()],
+ recipients: Vec::new(),
+ nonce: None,
+ })
+ .await;
+
+ let published = broker.published.lock().unwrap();
+ assert_eq!(published.len(), 2);
+ assert_eq!(
+ published[0].envelope["event_type"],
+ "channel.message_attachment_created"
+ );
+ assert_eq!(
+ published[0].envelope["metadata"]["attachments"][0]["attachment_id"],
+ added.id.to_string()
+ );
+ assert_eq!(
+ published[1].envelope["event_type"],
+ "channel.message_attachment_removed"
+ );
+ assert_eq!(
+ published[1].envelope["metadata"]["attachments"][0]["attachment_id"],
+ removed.id.to_string()
+ );
+}
+
+#[tokio::test]
+async fn handle_publishes_participant_events() {
+ let broker = TestEventBroker::default();
+ let service = broker_service(broker.clone());
+ let channel_id = Uuid::new_v4();
+
+ service
+ .handle(ChannelEvent::ParticipantsRemoved {
+ channel_id,
+ channel_type: ChannelType::Team,
+ actor: user("[email protected]"),
+ removed_user_ids: users(&["[email protected]"]),
+ })
+ .await;
+
+ let published = broker.published.lock().unwrap();
+ assert_eq!(published.len(), 1);
+ assert_eq!(
+ published[0].envelope["event_type"],
+ "channel.participant_removed"
+ );
+ assert_eq!(
+ published[0].envelope["metadata"]["removed_by"],
+ );
+ assert_eq!(
+ published[0].envelope["metadata"]["removed_user_ids"][0],
+ );
+}
+
+#[tokio::test]
+async fn handle_publishes_nothing_for_typing() {
+ let broker = TestEventBroker::default();
+ let service = broker_service(broker.clone());
+
+ service
+ .handle(ChannelEvent::TypingChanged {
+ channel_id: Uuid::new_v4(),
+ actor: Sender::new_from_user(user("[email protected]")),
+ action: TypingAction::Start,
+ thread_id: None,
+ recipients: Vec::new(),
+ nonce: None,
+ })
+ .await;
+
+ assert!(broker.published.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn publish_failure_does_not_break_other_side_effects() {
+ let search = FakeSearch::default();
+ let service = ChannelSideEffectService::new(
+ FakeContext::default(),
+ FakeRealtime::default(),
+ FakeNotifications::default(),
+ search.clone(),
+ FakeContacts::default(),
+ )
+ .with_macro_event_broker(FailingEventBroker);
+ let channel_id = Uuid::new_v4();
+
+ service
+ .handle(ChannelEvent::ChannelDeleted {
+ channel_id,
+ actor: Sender::new_from_user(user("[email protected]")),
+ })
+ .await;
+
+ assert_eq!(
+ search.removed.lock().unwrap().as_slice(),
+ &[(channel_id, None)]
+ );
+}
+
+#[test]
+fn broker_events_map_participant_joined_to_participant_added() {
+ use macro_event_broker::MacroEvent as _;
+ let channel_id = Uuid::new_v4();
+ let events = broker_events_for_event(&ChannelEvent::ParticipantJoined {
+ channel_id,
+ channel_type: ChannelType::Public,
+ user_id: Sender::new_from_user(user("[email protected]")),
+ active_participant_user_ids: users(&["[email protected]"]),
+ });
+
+ assert_eq!(events.len(), 1);
+ let envelope = serde_json::to_value(events[0].event()).unwrap();
+ assert_eq!(envelope["event_type"], "channel.participant_added");
+ assert_eq!(envelope["metadata"]["added_by"], "macro|[email protected]");
+ assert_eq!(
+ envelope["metadata"]["added_user_ids"][0],
+ );
+}
+
+#[test]
+fn broker_events_map_channel_updated() {
+ use macro_event_broker::MacroEvent as _;
+ let channel_id = Uuid::new_v4();
+ let events = broker_events_for_event(&ChannelEvent::ChannelUpdated {
+ channel_id,
+ actor: user("[email protected]"),
+ previous_name: Some("old".to_string()),
+ channel_name: Some("new".to_string()),
+ });
+
+ assert_eq!(events.len(), 1);
+ let envelope = serde_json::to_value(events[0].event()).unwrap();
+ assert_eq!(envelope["event_type"], "channel.updated");
+ assert_eq!(envelope["metadata"]["previous_name"], "old");
+ assert_eq!(envelope["metadata"]["channel_name"], "new");
+}
+
+#[test]
+fn broker_events_skip_reaction_changes() {
+ let events = broker_events_for_event(&ChannelEvent::ReactionChanged {
+ channel_id: Uuid::new_v4(),
+ actor: Sender::new_from_user(user("[email protected]")),
+ message_id: Uuid::new_v4(),
+ reactions: Vec::new(),
+ recipients: Vec::new(),
+ nonce: None,
+ });
+
+ assert!(events.is_empty());
+}
diff --git a/rust/cloud-storage/deny.toml b/rust/cloud-storage/deny.toml
index 09b227ccaa..661e774348 100644
--- a/rust/cloud-storage/deny.toml
+++ b/rust/cloud-storage/deny.toml
@@ -195,6 +195,7 @@ deny = [
"workspace-hack", # cargo-hakari generated crate
], reason = "Use macro_aws_config for AWS configuration in workspace crates" },
{ crate = "tracing-subscriber", wrappers = [
+ "channels", # Dev logging only
"documents", # Dev logging only
"email", # Dev logging only
"lambda_runtime_api_client", # External crate allowed to use tracing-subscriber
diff --git a/rust/cloud-storage/document_storage_service/src/api/context.rs b/rust/cloud-storage/document_storage_service/src/api/context.rs
index e05025d64b..5287f80389 100644
--- a/rust/cloud-storage/document_storage_service/src/api/context.rs
+++ b/rust/cloud-storage/document_storage_service/src/api/context.rs
@@ -235,6 +235,7 @@ pub(crate) type DssChannelService = ChannelServiceImpl<
NotificationChannelSender<NotificationIngressType>,
SqsChannelSearchIndexer,
ContactsChannelDispatcher<SqsContactsIngress<SqsContactsQueue>>,
+ MacroEventBrokerService<KafkaEventPublisher>,
>,
>,
PgChannelReferenceSharePermissions<EntityAccessService>,
diff --git a/rust/cloud-storage/document_storage_service/src/main.rs b/rust/cloud-storage/document_storage_service/src/main.rs
index 35b531f30d..712019da8e 100644
--- a/rust/cloud-storage/document_storage_service/src/main.rs
+++ b/rust/cloud-storage/document_storage_service/src/main.rs
@@ -399,7 +399,7 @@ async fn main() -> anyhow::Result<()> {
connection_service,
entity_access_management_service.clone(),
ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(db.clone())),
- macro_event_broker,
+ macro_event_broker.clone(),
));
let foreign_entity_service = Arc::new(ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(
@@ -639,7 +639,8 @@ async fn main() -> anyhow::Result<()> {
SqsChannelSearchIndexer::new(sqs_client.clone()),
ContactsChannelDispatcher::new(contacts_ingress.clone()),
)
- .with_bot_trigger_sender(bot_trigger_sender);
+ .with_bot_trigger_sender(bot_trigger_sender)
+ .with_macro_event_broker(macro_event_broker.clone());
let channels_service = Arc::new(ChannelServiceImpl::with_dependencies(
channels_repo,
diff --git a/rust/cloud-storage/macro_event_broker/src/domain/service.rs b/rust/cloud-storage/macro_event_broker/src/domain/service.rs
index f0de7f4018..0f0ad727c2 100644
--- a/rust/cloud-storage/macro_event_broker/src/domain/service.rs
+++ b/rust/cloud-storage/macro_event_broker/src/domain/service.rs
@@ -32,3 +32,16 @@ impl<P: EventPublisher> MacroEventBroker for MacroEventBrokerService<P> {
self.publisher.publish(topic, key, &payload).await
}
}
+
+/// A [`MacroEventBroker`] that drops every event.
+///
+/// Useful as a default type parameter for services that publish events
+/// optionally, and in tests that don't care about published events.
+#[derive(Debug, Default, Clone, Copy)]
+pub struct NoopMacroEventBroker;
+
+impl MacroEventBroker for NoopMacroEventBroker {
+ async fn send_event<E: MacroEvent + ?Sized>(&self, _event: &E) -> Result<(), EventBrokerError> {
+ Ok(())
+ }
+}
diff --git a/rust/cloud-storage/macro_event_broker/src/lib.rs b/rust/cloud-storage/macro_event_broker/src/lib.rs
index 6299331701..bf72397cb3 100644
--- a/rust/cloud-storage/macro_event_broker/src/lib.rs
+++ b/rust/cloud-storage/macro_event_broker/src/lib.rs
@@ -14,12 +14,12 @@
pub mod domain;
pub use domain::models::{Event, EventBrokerError, MacroEvent, TopicEvent};
-pub use macro_event_topics::{MacroDocumentsTopic, MacroExampleTopic, Topic};
+pub use macro_event_topics::{MacroChannelsTopic, MacroDocumentsTopic, MacroExampleTopic, Topic};
#[cfg(feature = "ports")]
pub use domain::ports::{EventPublisher, MacroEventBroker};
#[cfg(feature = "ports")]
-pub use domain::service::MacroEventBrokerService;
+pub use domain::service::{MacroEventBrokerService, NoopMacroEventBroker};
#[cfg(feature = "outbound")]
pub use outbound::kafka_event_publisher::KafkaEventPublisher;
#[cfg(feature = "outbound")]
diff --git a/rust/cloud-storage/macro_event_topics/src/lib.rs b/rust/cloud-storage/macro_event_topics/src/lib.rs
index e0af821a29..678055ced6 100644
--- a/rust/cloud-storage/macro_event_topics/src/lib.rs
+++ b/rust/cloud-storage/macro_event_topics/src/lib.rs
@@ -51,4 +51,6 @@ topics! {
MacroExampleTopic => "macro.example",
/// Document lifecycle events (created / updated / deleted / copied).
MacroDocumentsTopic => "macro.documents",
+ /// Channel lifecycle, message, participant, and attachment events.
+ MacroChannelsTopic => "macro.channels",
}
diff --git a/rust/cloud-storage/macro_event_topics/src/test.rs b/rust/cloud-storage/macro_event_topics/src/test.rs
index 0e91ab30e3..5b86f9203d 100644
--- a/rust/cloud-storage/macro_event_topics/src/test.rs
+++ b/rust/cloud-storage/macro_event_topics/src/test.rs
@@ -19,4 +19,5 @@ fn all_topic_names_is_non_empty_and_unique() {
fn all_topic_names_includes_declared_topics() {
assert!(all_topic_names().contains(&MacroExampleTopic.as_str()));
assert!(all_topic_names().contains(&MacroDocumentsTopic.as_str()));
+ assert!(all_topic_names().contains(&MacroChannelsTopic.as_str()));
}