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): notification participants should not include people from deleted messages (#6781)

Will Hutchinsonb38fc861334b
diff --git a/.sqlx/query-3480a69060f20430ed75c05cf9e40734b03ec692b637f99fb61b25f9b7c54583.json b/.sqlx/query-3480a69060f20430ed75c05cf9e40734b03ec692b637f99fb61b25f9b7c54583.json
deleted file mode 100644
index c2ea120327..0000000000
--- a/.sqlx/query-3480a69060f20430ed75c05cf9e40734b03ec692b637f99fb61b25f9b7c54583.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT DISTINCT id as \"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": "id!",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "3480a69060f20430ed75c05cf9e40734b03ec692b637f99fb61b25f9b7c54583"
-}
diff --git a/.sqlx/query-865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7.json b/.sqlx/query-865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7.json
new file mode 100644
index 0000000000..4c304fa37e
--- /dev/null
+++ b/.sqlx/query-865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7.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)\n AND m.deleted_at IS NULL\n 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 m.deleted_at IS NULL\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": "865ff79fbb79a924267e0a407a3506c09d8ac22046625a32300225aa7f4c6ec7"
+}
diff --git a/.sqlx/query-eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956.json b/.sqlx/query-eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956.json
deleted file mode 100644
index 07c61accc9..0000000000
--- a/.sqlx/query-eaeb36fcd2e9915d830eb2cdcefb6ab84c37acac7de5fdd06fefbec94abfe956.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_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/.sqlx/query-ebf8f0a46be65b6e758f0cb85763b9344434f1fa7d07b309412d49d32a66c2cf.json b/.sqlx/query-ebf8f0a46be65b6e758f0cb85763b9344434f1fa7d07b309412d49d32a66c2cf.json
new file mode 100644
index 0000000000..1f7cd4acec
--- /dev/null
+++ b/.sqlx/query-ebf8f0a46be65b6e758f0cb85763b9344434f1fa7d07b309412d49d32a66c2cf.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT DISTINCT id as \"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)\n AND m.deleted_at IS NULL\n 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 m.deleted_at IS NULL\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": "id!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "ebf8f0a46be65b6e758f0cb85763b9344434f1fa7d07b309412d49d32a66c2cf"
+}
diff --git a/crates/channels/fixtures/channels_repo.sql b/crates/channels/fixtures/channels_repo.sql
index 285b90ba43..1a31f17032 100644
--- a/crates/channels/fixtures/channels_repo.sql
+++ b/crates/channels/fixtures/channels_repo.sql
@@ -175,7 +175,9 @@ INSERT INTO comms_channel_participants (channel_id, user_id, role, joined_at, le
-- Participants: user-b (owner), user-c (member), user-e (member), left-user (left).
-- Threads (all rooted by user-b):
-- t41 (10:00): user-c replied (10:05); left-user replied (10:06); a soft-deleted
--- reply from user-e (10:07) that must NOT make user-e a participant
+-- reply from user-e (10:07) that must NOT make user-e a participant; a
+-- soft-deleted reply from user-b (10:09) that @-mentions user-e, which
+-- likewise must NOT make user-e a participant
-- t42 (11:00): user-c is @-mentioned in the root message
-- t43 (12:00): root message contains a group mention (@here), stored as the
-- client-side expansion: one user mention row per member at send time
@@ -204,6 +206,9 @@ INSERT INTO comms_messages (id, channel_id, thread_id, sender_id, content, creat
('00000000-0000-0000-0000-00000000b043', '00000000-0000-0000-0000-000000000c04',
'00000000-0000-0000-0000-000000000041', 'macro|[email protected]', 'deleted reply from user-e',
'2024-01-02 10:07:00+00', '2024-01-02 10:07:00+00', NULL, '2024-01-02 10:08:00+00'),
+ ('00000000-0000-0000-0000-00000000b045', '00000000-0000-0000-0000-000000000c04',
+ '00000000-0000-0000-0000-000000000041', 'macro|[email protected]', '',
+ '2024-01-02 10:09:00+00', '2024-01-02 10:10:00+00', NULL, '2024-01-02 10:10:00+00'),
-- t42: @-mention determines participants (mention row below)
('00000000-0000-0000-0000-000000000042', '00000000-0000-0000-0000-000000000c04', NULL,
'macro|[email protected]', 'mentioning <m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]"}</m-user-mention>',
@@ -225,6 +230,13 @@ INSERT INTO comms_entity_mentions (id, source_entity_type, source_entity_id, ent
('00000000-0000-0000-0000-00000000e042', 'message', '00000000-0000-0000-0000-000000000042',
'user', 'macro|[email protected]', NULL, '2024-01-02 11:00:00+00');
+-- user-e was @-mentioned in t41's soft-deleted reply from user-b. Deleting a
+-- message clears its content but leaves its mention rows behind, so the mention
+-- must be ignored through the message's deleted_at.
+INSERT INTO comms_entity_mentions (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id, created_at) VALUES
+ ('00000000-0000-0000-0000-00000000e045', 'message', '00000000-0000-0000-0000-00000000b045',
+ 'user', 'macro|[email protected]', NULL, '2024-01-02 10:09:00+00');
+
-- @here in t43's root message and t44's reply, as sent by the client: expanded into
-- one user mention row per channel member at send time (left-user had not left yet)
INSERT INTO comms_entity_mentions (id, source_entity_type, source_entity_id, entity_type, entity_id, user_id, created_at) VALUES
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rs
index 6847435643..049ecd89da 100644
--- a/crates/channels/src/outbound/pg_channels_repo.rs
+++ b/crates/channels/src/outbound/pg_channels_repo.rs
@@ -490,7 +490,9 @@ async fn get_channel_participants_for_thread_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
+ WHERE (m.id = $1 OR m.thread_id = $1)
+ AND m.deleted_at IS NULL
+ AND cp.left_at IS NULL
UNION
SELECT em.entity_id AS id
FROM comms_entity_mentions em
@@ -498,6 +500,7 @@ async fn get_channel_participants_for_thread_id(
JOIN comms_channel_participants cp
ON cp.channel_id = m.channel_id AND cp.user_id = em.entity_id
WHERE (m.id = $1 OR m.thread_id = $1)
+ AND m.deleted_at IS NULL
AND em.source_entity_type = 'message'
AND em.entity_type = 'user'
AND cp.left_at IS NULL
diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rs
index f1d7cfc753..1632980b3d 100644
--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs
+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs
@@ -2353,6 +2353,37 @@ async fn thread_participants_exclude_departed_senders(pool: Pool<Postgres>) -> a
Ok(())
}
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn thread_participants_exclude_deleted_messages(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let repo = repo(pool);
+ // t41: user-b rooted the thread and user-c replied. user-e only appears in
+ // soft-deleted replies: one they authored, and one from user-b that
+ // @-mentioned them (whose mention row still exists).
+ let participants = repo.get_thread_participants(T41).await?;
+
+ let ids: Vec<&str> = participants.iter().map(|p| p.as_ref()).collect();
+ assert!(
+ ids.contains(&USER_B),
+ "thread root sender should be included"
+ );
+ assert!(
+ ids.contains(&USER_C),
+ "active reply sender should be included"
+ );
+ assert!(
+ !ids.contains(&USER_E),
+ "a user who only sent, or was only mentioned in, deleted replies must not be a thread participant"
+ );
+ 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/crates/channels/src/outbound/pg_side_effect_context.rs b/crates/channels/src/outbound/pg_side_effect_context.rs
index d2a28011f9..115eb3b710 100644
--- a/crates/channels/src/outbound/pg_side_effect_context.rs
+++ b/crates/channels/src/outbound/pg_side_effect_context.rs
@@ -234,7 +234,9 @@ async fn get_channel_participants_for_thread_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
+ WHERE (m.id = $1 OR m.thread_id = $1)
+ AND m.deleted_at IS NULL
+ AND cp.left_at IS NULL
UNION
SELECT em.entity_id AS id
FROM comms_entity_mentions em
@@ -242,6 +244,7 @@ async fn get_channel_participants_for_thread_id(
JOIN comms_channel_participants cp
ON cp.channel_id = m.channel_id AND cp.user_id = em.entity_id
WHERE (m.id = $1 OR m.thread_id = $1)
+ AND m.deleted_at IS NULL
AND em.source_entity_type = 'message'
AND em.entity_type = 'user'
AND cp.left_at IS NULL
diff --git a/crates/channels/src/outbound/pg_side_effect_context/tests.rs b/crates/channels/src/outbound/pg_side_effect_context/tests.rs
index 92074ebe48..166347df22 100644
--- a/crates/channels/src/outbound/pg_side_effect_context/tests.rs
+++ b/crates/channels/src/outbound/pg_side_effect_context/tests.rs
@@ -5,6 +5,9 @@ use sqlx::{Pool, Postgres};
use uuid::Uuid;
const USER_A: &str = "macro|[email protected]";
+const USER_B: &str = "macro|[email protected]";
+const USER_C: &str = "macro|[email protected]";
+const USER_E: &str = "macro|[email protected]";
const LEFT_USER: &str = "macro|[email protected]";
#[sqlx::test(
@@ -31,3 +34,34 @@ async fn thread_notification_context_excludes_departed_senders(
);
Ok(())
}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn thread_notification_context_excludes_deleted_messages(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let ctx = PgChannelSideEffectContext::new(pool);
+ // ch4 thread t41 (msg id 0x..41): user-b rooted it and user-c replied. user-e
+ // only appears in soft-deleted replies: one they authored, and one from
+ // user-b that @-mentioned them (whose mention row still exists). Neither may
+ // keep notifying user-e about later replies.
+ let parent = Uuid::from_u128(0x00000000_0000_0000_0000_000000000041);
+ 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_B) && ids.contains(&USER_C),
+ "senders of live thread messages should be notification recipients"
+ );
+ assert!(
+ !ids.contains(&USER_E),
+ "a user who only sent, or was only mentioned in, deleted replies must not be a recipient"
+ );
+ assert!(
+ !ids.contains(&LEFT_USER),
+ "departed sender must not be a thread notification recipient"
+ );
+ Ok(())
+}
diff --git a/crates/comms_db_client/fixtures/threads.sql b/crates/comms_db_client/fixtures/threads.sql
index a218f767bc..ea39776b86 100644
--- a/crates/comms_db_client/fixtures/threads.sql
+++ b/crates/comms_db_client/fixtures/threads.sql
@@ -11,6 +11,10 @@ VALUES ('11111111-1111-1111-1111-111111111111', 'owner', 'macro|[email protected]')
('22222222-2222-2222-2222-222222222222', 'owner', 'macro|[email protected]'),
('22222222-2222-2222-2222-222222222222', 'member', 'macro|[email protected]');
+-- departed replied in thread 1 and then left channel 1
+INSERT INTO comms_channel_participants (channel_id, role, user_id, left_at)
+VALUES ('11111111-1111-1111-1111-111111111111', 'member', 'macro|[email protected]', NOW());
+
INSERT INTO comms_messages (id, channel_id, thread_id, sender_id, content)
VALUES ('11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111',
'11111111-1111-1111-1111-111111111111', 'macro|[email protected]', 'Test message 1'),
@@ -21,7 +25,9 @@ VALUES ('11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-1111111
('44444444-4444-4444-4444-444444444444', '11111111-1111-1111-1111-111111111111',
'11111111-1111-1111-1111-111111111111', 'macro|[email protected]', 'Test message 1'),
('55555555-5555-5555-5555-555555555555', '11111111-1111-1111-1111-111111111111',
- '22222222-2222-2222-2222-222222222222', 'macro|[email protected]', 'Test message 1');
+ '22222222-2222-2222-2222-222222222222', 'macro|[email protected]', 'Test message 1'),
+ ('66666666-6666-6666-6666-666666666666', '11111111-1111-1111-1111-111111111111',
+ '11111111-1111-1111-1111-111111111111', 'macro|[email protected]', 'reply from departed user');
INSERT INTO comms_entity_mentions (id, source_entity_type, source_entity_id, entity_type, entity_id)
VALUES ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'message', '22222222-2222-2222-2222-222222222222',
diff --git a/crates/comms_db_client/src/participants/get_participants.rs b/crates/comms_db_client/src/participants/get_participants.rs
index 5398d44c65..f8c6902b3e 100644
--- a/crates/comms_db_client/src/participants/get_participants.rs
+++ b/crates/comms_db_client/src/participants/get_participants.rs
@@ -132,10 +132,12 @@ pub async fn get_channel_participants_for_thread_id(
r#"
SELECT DISTINCT id as "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
- WHERE (m.id = $1 OR m.thread_id = $1) AND cp.left_at IS NULL
+ 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 m.deleted_at IS NULL
+ AND cp.left_at IS NULL
UNION
SELECT em.entity_id AS id
FROM comms_entity_mentions em
@@ -143,6 +145,7 @@ pub async fn get_channel_participants_for_thread_id(
JOIN comms_channel_participants cp
ON cp.channel_id = m.channel_id AND cp.user_id = em.entity_id
WHERE (m.id = $1 OR m.thread_id = $1)
+ AND m.deleted_at IS NULL
AND em.source_entity_type = 'message'
AND em.entity_type = 'user'
AND cp.left_at IS NULL
@@ -198,6 +201,12 @@ mod tests {
participants.contains(&MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap())
);
+ // departed authored a thread-1 reply but has since left the channel, so the
+ // sender path must exclude them even though other participants are active.
+ assert!(
+ !participants
+ .contains(&MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap())
+ );
// user6 is mentioned in a message that belongs to a different thread and
// must not leak into this thread's participant set.
assert!(