Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(channels): exclude attachments from deleted messages (#2322)
teoab931150094b
diff --git a/rust/cloud-storage/channels/.sqlx/query-24699ee15071155de425d4228c9a5ae5feeab0bd3eed5a659795ea3a07c96ba9.json b/rust/cloud-storage/channels/.sqlx/query-6958dcc3ad28334546d21a169c3724111cd93d5304bf2d9da4dac7a1cc5ad77d.jsonsimilarity index 83%rename from rust/cloud-storage/channels/.sqlx/query-24699ee15071155de425d4228c9a5ae5feeab0bd3eed5a659795ea3a07c96ba9.jsonrename to rust/cloud-storage/channels/.sqlx/query-6958dcc3ad28334546d21a169c3724111cd93d5304bf2d9da4dac7a1cc5ad77d.jsonindex ed5d43a20c..acac0f944a 100644--- a/rust/cloud-storage/channels/.sqlx/query-24699ee15071155de425d4228c9a5ae5feeab0bd3eed5a659795ea3a07c96ba9.json+++ b/rust/cloud-storage/channels/.sqlx/query-6958dcc3ad28334546d21a169c3724111cd93d5304bf2d9da4dac7a1cc5ad77d.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT a.id, a.channel_id, a.message_id, m.sender_id,\n a.entity_type, a.entity_id,\n a.width AS \"width?\", a.height AS \"height?\", a.created_at\n FROM comms_attachments a\n JOIN comms_messages m ON m.id = a.message_id\n WHERE a.channel_id = $1\n AND ($2::timestamptz IS NULL OR (a.created_at, a.id) < ($2, $3))\n ORDER BY a.created_at DESC, a.id DESC\n LIMIT $4\n ",+ "query": "\n SELECT a.id, a.channel_id, a.message_id, m.sender_id,\n a.entity_type, a.entity_id,\n a.width AS \"width?\", a.height AS \"height?\", a.created_at\n FROM comms_attachments a\n JOIN comms_messages m ON m.id = a.message_id\n WHERE a.channel_id = $1\n AND m.deleted_at IS NULL\n AND ($2::timestamptz IS NULL OR (a.created_at, a.id) < ($2, $3))\n ORDER BY a.created_at DESC, a.id DESC\n LIMIT $4\n ","describe": {"columns": [{@@ -69,5 +69,5 @@false]},- "hash": "24699ee15071155de425d4228c9a5ae5feeab0bd3eed5a659795ea3a07c96ba9"+ "hash": "6958dcc3ad28334546d21a169c3724111cd93d5304bf2d9da4dac7a1cc5ad77d"}diff --git a/rust/cloud-storage/channels/fixtures/channels_repo.sql b/rust/cloud-storage/channels/fixtures/channels_repo.sqlindex 5b9a7ddf78..c0465e5721 100644--- a/rust/cloud-storage/channels/fixtures/channels_repo.sql+++ b/rust/cloud-storage/channels/fixtures/channels_repo.sql@@ -3,7 +3,8 @@-- Channel: ch1 (public, in org 1)-- Top-level messages: msg1 (oldest), msg2, msg3 (newest)-- msg1 has a thread with 4 replies (r1..r4), reactions, and an attachment--- msg2 is soft-deleted but has an active reply → should still appear+-- msg2 is soft-deleted but has an active reply → should still appear in message listings+-- but its attachments should not appear in the channel attachments endpoint-- msg3 is a normal message with no thread---- Also:@@ -85,6 +86,11 @@ INSERT INTO comms_attachments (id, message_id, channel_id, entity_type, entity_i('00000000-0000-0000-0000-00000000a003', '00000000-0000-0000-0000-000000000003','00000000-0000-0000-0000-000000000c01', 'document', 'doc-2', NULL, NULL, '2024-01-01 12:00:00+00');+-- attachment on deleted msg2: should be excluded from channel-level attachments+INSERT INTO comms_attachments (id, message_id, channel_id, entity_type, entity_id, width, height, created_at) VALUES+ ('00000000-0000-0000-0000-00000000a004', '00000000-0000-0000-0000-000000000002',+ '00000000-0000-0000-0000-000000000c01', 'image', 'img-deleted', 640, 480, '2024-01-01 11:00:30+00');+-- participants in ch1INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at, left_at) VALUES('00000000-0000-0000-0000-000000000c01', 'macro|[email protected]', 'owner', '2024-01-01 00:00:00+00', NULL),diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rsindex 165f7f958a..90f0e4e4ba 100644--- a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs+++ b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs@@ -435,6 +435,7 @@ impl ChannelMessagesRepo for PgChannelMessagesRepo {FROM comms_attachments aJOIN comms_messages m ON m.id = a.message_idWHERE a.channel_id = $1+ AND m.deleted_at IS NULLAND ($2::timestamptz IS NULL OR (a.created_at, a.id) < ($2, $3))ORDER BY a.created_at DESC, a.id DESCLIMIT $4diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rsindex 78c96bb2e0..0b1b6e7c3d 100644--- a/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs+++ b/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs@@ -13,6 +13,7 @@ const MSG2: Uuid = Uuid::from_u128(0x00000000_0000_0000_0000_000000000002);const MSG3: Uuid = Uuid::from_u128(0x00000000_0000_0000_0000_000000000003);const REPLY1: Uuid = Uuid::from_u128(0x00000000_0000_0000_0000_00000000b001);const REPLY5: Uuid = Uuid::from_u128(0x00000000_0000_0000_0000_00000000b005);+const DELETED_MSG_ATTACHMENT: Uuid = Uuid::from_u128(0x00000000_0000_0000_0000_00000000a004);fn repo(pool: Pool<Postgres>) -> PgChannelMessagesRepo {PgChannelMessagesRepo::new(pool)@@ -390,8 +391,9 @@ async fn attachments_batch_grouped_by_message(pool: Pool<Postgres>) -> anyhow::Rlet map = repo.get_attachments_batch(&[MSG1, MSG2, MSG3]).await?;assert_eq!(map[&MSG1].len(), 2);+ assert_eq!(map[&MSG2].len(), 1);assert_eq!(map[&MSG3].len(), 1);- assert!(map.get(&MSG2).is_none(), "msg2 has no attachments");+ assert_eq!(map[&MSG2][0].id, DELETED_MSG_ATTACHMENT);Ok(())}@@ -450,6 +452,27 @@ async fn channel_attachments_include_dimensions(pool: Pool<Postgres>) -> anyhow:Ok(())}+#[sqlx::test(+ fixtures(path = "../../../fixtures", scripts("channels_repo")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn channel_attachments_exclude_deleted_messages(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let repo = repo(pool);+ let all = repo+ .get_channel_attachments(CH1, &Query::Sort(CreatedAt, ()), 50)+ .await?;++ let ids: Vec<Uuid> = all.iter().map(|a| a.id).collect();+ assert_eq!(+ ids.len(),+ 3,+ "only attachments from non-deleted messages are returned"+ );+ assert!(!ids.contains(&DELETED_MSG_ATTACHMENT));+ assert!(all.iter().all(|a| a.message_id != MSG2));+ Ok(())+}+// -- get_channel_participants ----------------------------------------------#[sqlx::test(