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.

fix(channels): exclude removed participants from channel recipient queries (#3670)

teobff9f85b2646
diff --git a/rust/cloud-storage/.sqlx/query-2ff80f0d8f9b480e0e9068fbb795afdfe2a80bdd764e843e9f07d767b1eea7df.json b/rust/cloud-storage/.sqlx/query-2ff80f0d8f9b480e0e9068fbb795afdfe2a80bdd764e843e9f07d767b1eea7df.json
deleted file mode 100644
index 96300aeeef..0000000000
--- a/rust/cloud-storage/.sqlx/query-2ff80f0d8f9b480e0e9068fbb795afdfe2a80bdd764e843e9f07d767b1eea7df.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT DISTINCT id AS \"user_id!\" FROM (\n SELECT m.sender_id AS id\n FROM comms_channel_participants cp\n JOIN comms_channels c ON c.id = cp.channel_id\n JOIN comms_messages m ON m.channel_id = c.id\n WHERE (m.id = $1 OR m.thread_id = $1) AND cp.left_at IS NULL\n UNION\n SELECT em.entity_id AS id\n FROM comms_entity_mentions em\n JOIN comms_messages m ON m.id::text = em.source_entity_id\n JOIN comms_channel_participants cp\n ON cp.channel_id = m.channel_id AND cp.user_id = em.entity_id\n WHERE (m.id = $1 OR m.thread_id = $1)\n AND em.source_entity_type = 'message'\n AND em.entity_type = 'user'\n AND cp.left_at IS NULL\n ) AS combined\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "user_id!",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "2ff80f0d8f9b480e0e9068fbb795afdfe2a80bdd764e843e9f07d767b1eea7df"
-}
diff --git a/rust/cloud-storage/.sqlx/query-e757b0ff97cdde3e3742a24ca0efe796c255daff9e95ca53b7dcc6f824d0c942.json b/rust/cloud-storage/.sqlx/query-6bb0523c1a1fec5d2d8600d1d18e7156eebd31e6d39dd4b9f6bbb397bd8a5f56.json
similarity index 86%
rename from rust/cloud-storage/.sqlx/query-e757b0ff97cdde3e3742a24ca0efe796c255daff9e95ca53b7dcc6f824d0c942.json
rename to rust/cloud-storage/.sqlx/query-6bb0523c1a1fec5d2d8600d1d18e7156eebd31e6d39dd4b9f6bbb397bd8a5f56.json
index d249a31a8d..261784e01a 100644
--- a/rust/cloud-storage/.sqlx/query-e757b0ff97cdde3e3742a24ca0efe796c255daff9e95ca53b7dcc6f824d0c942.json
+++ b/rust/cloud-storage/.sqlx/query-6bb0523c1a1fec5d2d8600d1d18e7156eebd31e6d39dd4b9f6bbb397bd8a5f56.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n user_id,\n channel_id,\n joined_at,\n left_at::timestamptz AS \"left_at?\",\n role AS \"role: ParticipantRole\"\n FROM comms_channel_participants\n WHERE channel_id = $1\n ORDER BY joined_at DESC\n ",
+ "query": "\n SELECT\n user_id,\n channel_id,\n joined_at,\n left_at::timestamptz AS \"left_at?\",\n role AS \"role: ParticipantRole\"\n FROM comms_channel_participants\n WHERE channel_id = $1 AND left_at IS NULL\n ORDER BY joined_at DESC\n ",
"describe": {
"columns": [
{
@@ -53,5 +53,5 @@
false
]
},
- "hash": "e757b0ff97cdde3e3742a24ca0efe796c255daff9e95ca53b7dcc6f824d0c942"
+ "hash": "6bb0523c1a1fec5d2d8600d1d18e7156eebd31e6d39dd4b9f6bbb397bd8a5f56"
}
diff --git a/rust/cloud-storage/.sqlx/query-eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956.json b/rust/cloud-storage/.sqlx/query-eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956.json
new file mode 100644
index 0000000000..07c61accc9
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT DISTINCT id AS \"user_id!\" FROM (\n SELECT m.sender_id AS id\n FROM comms_messages m\n JOIN comms_channel_participants cp\n ON cp.channel_id = m.channel_id AND cp.user_id = m.sender_id\n WHERE (m.id = $1 OR m.thread_id = $1) AND cp.left_at IS NULL\n UNION\n SELECT em.entity_id AS id\n FROM comms_entity_mentions em\n JOIN comms_messages m ON m.id::text = em.source_entity_id\n JOIN comms_channel_participants cp\n ON cp.channel_id = m.channel_id AND cp.user_id = em.entity_id\n WHERE (m.id = $1 OR m.thread_id = $1)\n AND em.source_entity_type = 'message'\n AND em.entity_type = 'user'\n AND cp.left_at IS NULL\n ) AS combined\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "user_id!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956"
+}
diff --git a/rust/cloud-storage/channels/fixtures/channels_repo.sql b/rust/cloud-storage/channels/fixtures/channels_repo.sql
index b93b05deb3..8f65c77385 100644
--- a/rust/cloud-storage/channels/fixtures/channels_repo.sql
+++ b/rust/cloud-storage/channels/fixtures/channels_repo.sql
@@ -98,6 +98,28 @@ INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at, le
('00000000-0000-0000-0000-000000000c01', 'macro|[email protected]', 'member', '2024-01-01 00:02:00+00', NULL),
('00000000-0000-0000-0000-000000000c01', 'macro|[email protected]', 'member', '2024-01-01 00:03:00+00', '2024-01-05 00:00:00+00');
+-- Channel ch3: isolated thread-recipient correctness fixture.
+-- A departed participant (left-user) authored a reply in a thread whose parent
+-- was authored by an active participant (user-a). The departed sender must be
+-- excluded from thread recipient/notification lookups, while the active sender
+-- is still included.
+INSERT INTO comms_channels (id, name, channel_type, org_id, owner_id, created_at, updated_at) VALUES
+ ('00000000-0000-0000-0000-000000000c03', 'thread-recipients', 'public', NULL, 'macro|[email protected]',
+ '2024-01-01 00:00:00+00', '2024-01-01 00:00:00+00');
+
+INSERT INTO comms_messages (id, channel_id, thread_id, sender_id, content, created_at, updated_at, edited_at, deleted_at) VALUES
+ -- thread parent authored by an active participant
+ ('00000000-0000-0000-0000-000000000031', '00000000-0000-0000-0000-000000000c03', NULL,
+ 'macro|[email protected]', 'thread parent', '2024-01-01 10:00:00+00', '2024-01-01 10:00:00+00', NULL, NULL),
+ -- reply authored by a participant who later left the channel
+ ('00000000-0000-0000-0000-00000000b031', '00000000-0000-0000-0000-000000000c03',
+ '00000000-0000-0000-0000-000000000031', 'macro|[email protected]', 'reply from departed user',
+ '2024-01-01 10:01:00+00', '2024-01-01 10:01:00+00', NULL, NULL);
+
+INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at, left_at) VALUES
+ ('00000000-0000-0000-0000-000000000c03', 'macro|[email protected]', 'owner', '2024-01-01 00:00:00+00', NULL),
+ ('00000000-0000-0000-0000-000000000c03', 'macro|[email protected]', 'member', '2024-01-01 00:03:00+00', '2024-01-05 00:00:00+00');
+
-- entity mentions (used by attachment-references tests)
-- doc-mention is mentioned inside msg3 (a message source → channel reference, gated by participation)
-- doc-generic is mentioned by a non-message source (doc → generic reference, not gated)
diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs
index 8fd3387e46..d81a58537c 100644
--- a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs
+++ b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs
@@ -438,9 +438,9 @@ async fn get_channel_participants_for_thread_id(
r#"
SELECT DISTINCT id AS "user_id!" FROM (
SELECT m.sender_id AS id
- FROM comms_channel_participants cp
- JOIN comms_channels c ON c.id = cp.channel_id
- JOIN comms_messages m ON m.channel_id = c.id
+ FROM comms_messages m
+ JOIN comms_channel_participants cp
+ ON cp.channel_id = m.channel_id AND cp.user_id = m.sender_id
WHERE (m.id = $1 OR m.thread_id = $1) AND cp.left_at IS NULL
UNION
SELECT em.entity_id AS id
@@ -2272,7 +2272,7 @@ impl ChannelRepo for PgChannelsRepo {
left_at::timestamptz AS "left_at?",
role AS "role: ParticipantRole"
FROM comms_channel_participants
- WHERE channel_id = $1
+ WHERE channel_id = $1 AND left_at IS NULL
ORDER BY joined_at DESC
"#,
channel_id,
@@ -2283,9 +2283,6 @@ impl ChannelRepo for PgChannelsRepo {
Ok(rows
.into_iter()
.filter_map(|row| {
- if row.left_at.is_some() {
- return None;
- }
let user_id = MacroUserIdStr::try_from(row.user_id).ok()?;
Some(ChannelParticipant {
channel_id: row.channel_id,
diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs
index 068a291bf1..dbae112bee 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
@@ -690,6 +690,29 @@ async fn participants_roles_parsed_correctly(pool: Pool<Postgres>) -> anyhow::Re
Ok(())
}
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn thread_participants_exclude_departed_senders(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let repo = repo(pool);
+ // ch3 thread parent (msg id 0x..31) was authored by an active participant,
+ // while its only reply was authored by a participant who has since left.
+ let parent = Uuid::from_u128(0x00000000_0000_0000_0000_000000000031);
+ let participants = repo.get_thread_participants(parent).await?;
+
+ let ids: Vec<&str> = participants.iter().map(|p| p.as_ref()).collect();
+ assert!(
+ ids.contains(&USER_A),
+ "active thread participant should be included"
+ );
+ assert!(
+ !ids.contains(&LEFT_USER),
+ "departed sender must not be treated as a thread participant"
+ );
+ Ok(())
+}
+
// -- resolve_top_level_parent -------------------------------------------------
#[sqlx::test(
diff --git a/rust/cloud-storage/channels/src/outbound/pg_side_effect_context.rs b/rust/cloud-storage/channels/src/outbound/pg_side_effect_context.rs
index d7f2428893..ab12f924a6 100644
--- a/rust/cloud-storage/channels/src/outbound/pg_side_effect_context.rs
+++ b/rust/cloud-storage/channels/src/outbound/pg_side_effect_context.rs
@@ -1,5 +1,8 @@
//! Postgres context adapter for channel side-effect policy.
+#[cfg(test)]
+mod tests;
+
use crate::domain::{
ports::ChannelSideEffectContext,
side_effects::{ChannelDocumentMention, ThreadNotificationContext},
@@ -197,9 +200,9 @@ async fn get_channel_participants_for_thread_id(
r#"
SELECT DISTINCT id AS "user_id!" FROM (
SELECT m.sender_id AS id
- FROM comms_channel_participants cp
- JOIN comms_channels c ON c.id = cp.channel_id
- JOIN comms_messages m ON m.channel_id = c.id
+ FROM comms_messages m
+ JOIN comms_channel_participants cp
+ ON cp.channel_id = m.channel_id AND cp.user_id = m.sender_id
WHERE (m.id = $1 OR m.thread_id = $1) AND cp.left_at IS NULL
UNION
SELECT em.entity_id AS id
diff --git a/rust/cloud-storage/channels/src/outbound/pg_side_effect_context/tests.rs b/rust/cloud-storage/channels/src/outbound/pg_side_effect_context/tests.rs
new file mode 100644
index 0000000000..92074ebe48
--- /dev/null
+++ b/rust/cloud-storage/channels/src/outbound/pg_side_effect_context/tests.rs
@@ -0,0 +1,33 @@
+use crate::domain::ports::ChannelSideEffectContext;
+use crate::outbound::pg_side_effect_context::PgChannelSideEffectContext;
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use sqlx::{Pool, Postgres};
+use uuid::Uuid;
+
+const USER_A: &str = "macro|[email protected]";
+const LEFT_USER: &str = "macro|[email protected]";
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn thread_notification_context_excludes_departed_senders(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let ctx = PgChannelSideEffectContext::new(pool);
+ // ch3 thread parent (msg id 0x..31) was authored by an active participant,
+ // while its only reply was authored by a participant who has since left.
+ let parent = Uuid::from_u128(0x00000000_0000_0000_0000_000000000031);
+ let context = ctx.get_thread_notification_context(parent).await?;
+
+ let ids: Vec<&str> = context.participants.iter().map(|p| p.as_ref()).collect();
+ assert!(
+ ids.contains(&USER_A),
+ "active thread participant should be a notification recipient"
+ );
+ assert!(
+ !ids.contains(&LEFT_USER),
+ "departed sender must not be a thread notification recipient"
+ );
+ Ok(())
+}