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(email): scope inbox delegation edges to individual links (#4018)
gbirmanf097af8e8639
diff --git a/rust/cloud-storage/.sqlx/query-11ac63a8975bc8587b4c3b8f64201b8999a0cec4a928d253fb4fa49f78a8626e.json b/rust/cloud-storage/.sqlx/query-11ac63a8975bc8587b4c3b8f64201b8999a0cec4a928d253fb4fa49f78a8626e.jsondeleted file mode 100644index 9d4460d0dd..0000000000--- a/rust/cloud-storage/.sqlx/query-11ac63a8975bc8587b4c3b8f64201b8999a0cec4a928d253fb4fa49f78a8626e.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO macro_user_links (primary_macro_id, child_macro_id)\n VALUES ($1, $2)\n ON CONFLICT (primary_macro_id, child_macro_id) DO NOTHING\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Text"- ]- },- "nullable": []- },- "hash": "11ac63a8975bc8587b4c3b8f64201b8999a0cec4a928d253fb4fa49f78a8626e"-}diff --git a/rust/cloud-storage/.sqlx/query-e5389ae52536642246f7dd1b3a856d04bef89e61515707fe7dee51dcd5733c98.json b/rust/cloud-storage/.sqlx/query-2d75503f9dbc9a548bfd0737f7cf557b5bc3ca68a2cabd601fc6c91542ca3938.jsonsimilarity index 90%rename from rust/cloud-storage/.sqlx/query-e5389ae52536642246f7dd1b3a856d04bef89e61515707fe7dee51dcd5733c98.jsonrename to rust/cloud-storage/.sqlx/query-2d75503f9dbc9a548bfd0737f7cf557b5bc3ca68a2cabd601fc6c91542ca3938.jsonindex a952d45687..cc784b8cd2 100644--- a/rust/cloud-storage/.sqlx/query-e5389ae52536642246f7dd1b3a856d04bef89e61515707fe7dee51dcd5733c98.json+++ b/rust/cloud-storage/.sqlx/query-2d75503f9dbc9a548bfd0737f7cf557b5bc3ca68a2cabd601fc6c91542ca3938.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT l.id, l.macro_id, l.fusionauth_user_id, l.email_address, l.provider as \"provider: _\",\n l.is_sync_active, l.is_primary, l.created_at, l.updated_at\n FROM email_threads t\n JOIN email_links l ON l.id = t.link_id\n WHERE t.id = $1\n AND (\n l.macro_id = $2\n OR EXISTS (\n SELECT 1 FROM macro_user_links mul\n WHERE mul.child_macro_id = l.macro_id AND mul.primary_macro_id = $2\n )\n )\n ",+ "query": "\n SELECT l.id, l.macro_id, l.fusionauth_user_id, l.email_address, l.provider as \"provider: _\",\n l.is_sync_active, l.is_primary, l.created_at, l.updated_at\n FROM email_threads t\n JOIN email_links l ON l.id = t.link_id\n WHERE t.id = $1\n AND (\n l.macro_id = $2\n OR EXISTS (\n SELECT 1 FROM macro_user_links mul\n WHERE mul.link_id = l.id AND mul.primary_macro_id = $2\n )\n )\n ","describe": {"columns": [{@@ -76,5 +76,5 @@false]},- "hash": "e5389ae52536642246f7dd1b3a856d04bef89e61515707fe7dee51dcd5733c98"+ "hash": "2d75503f9dbc9a548bfd0737f7cf557b5bc3ca68a2cabd601fc6c91542ca3938"}diff --git a/rust/cloud-storage/.sqlx/query-392aaf2a2b9c1d3b0a30fdaed7107209446a4893f7f44b1d5c19cfa9fd387695.json b/rust/cloud-storage/.sqlx/query-355bb8b76dffeb7dbf8fef2270f49e6502607b652baf2bdca866d5f0ced08774.jsonsimilarity index 67%rename from rust/cloud-storage/.sqlx/query-392aaf2a2b9c1d3b0a30fdaed7107209446a4893f7f44b1d5c19cfa9fd387695.jsonrename to rust/cloud-storage/.sqlx/query-355bb8b76dffeb7dbf8fef2270f49e6502607b652baf2bdca866d5f0ced08774.jsonindex 5bf800b64e..7b4d5179f7 100644--- a/rust/cloud-storage/.sqlx/query-392aaf2a2b9c1d3b0a30fdaed7107209446a4893f7f44b1d5c19cfa9fd387695.json+++ b/rust/cloud-storage/.sqlx/query-355bb8b76dffeb7dbf8fef2270f49e6502607b652baf2bdca866d5f0ced08774.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT EXISTS (\n SELECT 1\n FROM public.email_threads t\n JOIN public.email_links l ON l.id = t.link_id\n WHERE t.id = $1::uuid\n AND (\n l.macro_id = $2\n OR EXISTS (\n SELECT 1\n FROM public.macro_user_links mul\n WHERE mul.child_macro_id = l.macro_id\n AND mul.primary_macro_id = $2\n )\n )\n ) AS \"exists!\"\n ",+ "query": "\n SELECT EXISTS (\n SELECT 1\n FROM public.email_threads t\n JOIN public.email_links l ON l.id = t.link_id\n WHERE t.id = $1::uuid\n AND (\n l.macro_id = $2\n OR EXISTS (\n SELECT 1\n FROM public.macro_user_links mul\n WHERE mul.link_id = l.id\n AND mul.primary_macro_id = $2\n )\n )\n ) AS \"exists!\"\n ","describe": {"columns": [{@@ -19,5 +19,5 @@null]},- "hash": "392aaf2a2b9c1d3b0a30fdaed7107209446a4893f7f44b1d5c19cfa9fd387695"+ "hash": "355bb8b76dffeb7dbf8fef2270f49e6502607b652baf2bdca866d5f0ced08774"}diff --git a/rust/cloud-storage/.sqlx/query-4d2bcb836116798691afde6c2acb7aa4f7feff671978130668fbd8fc49493246.json b/rust/cloud-storage/.sqlx/query-4d2bcb836116798691afde6c2acb7aa4f7feff671978130668fbd8fc49493246.jsonnew file mode 100644index 0000000000..ab0e8a28e9--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-4d2bcb836116798691afde6c2acb7aa4f7feff671978130668fbd8fc49493246.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT DISTINCT primary_macro_id\n FROM macro_user_links\n WHERE child_macro_id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "primary_macro_id",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "4d2bcb836116798691afde6c2acb7aa4f7feff671978130668fbd8fc49493246"+}diff --git a/rust/cloud-storage/.sqlx/query-6dde3a5ced431932d021e43180e45071efd36c3088dab4feada6254bc1aa41f2.json b/rust/cloud-storage/.sqlx/query-5c96439218818e8e913f53a8561b809af6a05dc56767f5df313f56c0adb0d07d.jsonsimilarity index 71%rename from rust/cloud-storage/.sqlx/query-6dde3a5ced431932d021e43180e45071efd36c3088dab4feada6254bc1aa41f2.jsonrename to rust/cloud-storage/.sqlx/query-5c96439218818e8e913f53a8561b809af6a05dc56767f5df313f56c0adb0d07d.jsonindex b996369a66..6af1eb0f62 100644--- a/rust/cloud-storage/.sqlx/query-6dde3a5ced431932d021e43180e45071efd36c3088dab4feada6254bc1aa41f2.json+++ b/rust/cloud-storage/.sqlx/query-5c96439218818e8e913f53a8561b809af6a05dc56767f5df313f56c0adb0d07d.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT EXISTS(\n SELECT 1\n FROM macro_user_links\n WHERE primary_macro_id = $1\n AND child_macro_id = $2\n ) AS \"exists!\"\n ",+ "query": "\n SELECT EXISTS(\n SELECT 1\n FROM macro_user_links\n WHERE primary_macro_id = $1\n AND child_macro_id = $2\n AND link_id = $3\n ) AS \"exists!\"\n ","describe": {"columns": [{@@ -12,12 +12,13 @@"parameters": {"Left": ["Text",- "Text"+ "Text",+ "Uuid"]},"nullable": [null]},- "hash": "6dde3a5ced431932d021e43180e45071efd36c3088dab4feada6254bc1aa41f2"+ "hash": "5c96439218818e8e913f53a8561b809af6a05dc56767f5df313f56c0adb0d07d"}diff --git a/rust/cloud-storage/.sqlx/query-a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64.json b/rust/cloud-storage/.sqlx/query-784ccf892cef52cc1a3d1d0c5b392c49cb431c1f4c3eef8648f2af230f2d39bc.jsonsimilarity index 72%rename from rust/cloud-storage/.sqlx/query-a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64.jsonrename to rust/cloud-storage/.sqlx/query-784ccf892cef52cc1a3d1d0c5b392c49cb431c1f4c3eef8648f2af230f2d39bc.jsonindex 6812c685dc..3a8e08c857 100644--- a/rust/cloud-storage/.sqlx/query-a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64.json+++ b/rust/cloud-storage/.sqlx/query-784ccf892cef52cc1a3d1d0c5b392c49cb431c1f4c3eef8648f2af230f2d39bc.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT primary_macro_id\n FROM macro_user_links\n WHERE child_macro_id = $1\n ",+ "query": "\n SELECT primary_macro_id\n FROM macro_user_links\n WHERE child_macro_id = $1\n AND link_id = $2\n ","describe": {"columns": [{@@ -11,12 +11,13 @@],"parameters": {"Left": [- "Text"+ "Text",+ "Uuid"]},"nullable": [false]},- "hash": "a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64"+ "hash": "784ccf892cef52cc1a3d1d0c5b392c49cb431c1f4c3eef8648f2af230f2d39bc"}diff --git a/rust/cloud-storage/.sqlx/query-9a360686b555a05569f9f679e0e774f6d1d2d0bb27eb3d7599037b0139870a53.json b/rust/cloud-storage/.sqlx/query-9a360686b555a05569f9f679e0e774f6d1d2d0bb27eb3d7599037b0139870a53.jsonnew file mode 100644index 0000000000..01b7967c34--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-9a360686b555a05569f9f679e0e774f6d1d2d0bb27eb3d7599037b0139870a53.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)\n VALUES ($1, $2, $3)\n ON CONFLICT (primary_macro_id, child_macro_id, link_id) DO NOTHING\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "9a360686b555a05569f9f679e0e774f6d1d2d0bb27eb3d7599037b0139870a53"+}diff --git a/rust/cloud-storage/.sqlx/query-70915b2ac501f2362bb6e7feeb3db3562eb6314f08ba61d5353ae13589db230b.json b/rust/cloud-storage/.sqlx/query-a159bcbeb3f71ebe7c42e184727f0f241ea8f7382e4793534daaf066567fc209.jsonsimilarity index 91%rename from rust/cloud-storage/.sqlx/query-70915b2ac501f2362bb6e7feeb3db3562eb6314f08ba61d5353ae13589db230b.jsonrename to rust/cloud-storage/.sqlx/query-a159bcbeb3f71ebe7c42e184727f0f241ea8f7382e4793534daaf066567fc209.jsonindex 11664ed99c..71a76686b2 100644--- a/rust/cloud-storage/.sqlx/query-70915b2ac501f2362bb6e7feeb3db3562eb6314f08ba61d5353ae13589db230b.json+++ b/rust/cloud-storage/.sqlx/query-a159bcbeb3f71ebe7c42e184727f0f241ea8f7382e4793534daaf066567fc209.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT id as \"id!\", macro_id as \"macro_id!\",\n fusionauth_user_id as \"fusionauth_user_id!\",\n email_address as \"email_address!\",\n provider as \"provider!: _\",\n is_sync_active as \"is_sync_active!\",\n is_primary as \"is_primary!\",\n created_at as \"created_at!\",\n updated_at as \"updated_at!\"\n FROM (\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.created_at, el.updated_at\n FROM email_links el\n WHERE el.macro_id = $1\n UNION\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.created_at, el.updated_at\n FROM email_links el\n JOIN macro_user_links mul ON el.macro_id = mul.child_macro_id\n WHERE mul.primary_macro_id = $1\n ) AS combined\n ORDER BY created_at DESC\n ",+ "query": "\n SELECT id as \"id!\", macro_id as \"macro_id!\",\n fusionauth_user_id as \"fusionauth_user_id!\",\n email_address as \"email_address!\",\n provider as \"provider!: _\",\n is_sync_active as \"is_sync_active!\",\n is_primary as \"is_primary!\",\n created_at as \"created_at!\",\n updated_at as \"updated_at!\"\n FROM (\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.created_at, el.updated_at\n FROM email_links el\n WHERE el.macro_id = $1\n UNION\n SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,\n el.provider, el.is_sync_active, el.is_primary, el.created_at, el.updated_at\n FROM email_links el\n JOIN macro_user_links mul ON el.id = mul.link_id\n WHERE mul.primary_macro_id = $1\n ) AS combined\n ORDER BY created_at DESC\n ","describe": {"columns": [{@@ -75,5 +75,5 @@null]},- "hash": "70915b2ac501f2362bb6e7feeb3db3562eb6314f08ba61d5353ae13589db230b"+ "hash": "a159bcbeb3f71ebe7c42e184727f0f241ea8f7382e4793534daaf066567fc209"}diff --git a/rust/cloud-storage/.sqlx/query-012063ce1220cd6f1b04eeefa088324d50ac472be760c0efe10b5060b0878c75.json b/rust/cloud-storage/.sqlx/query-b453c431475246f0c3e5951aa301bade0f476dd6139e43d0f2420ae9fdca3028.jsonsimilarity index 55%rename from rust/cloud-storage/.sqlx/query-012063ce1220cd6f1b04eeefa088324d50ac472be760c0efe10b5060b0878c75.jsonrename to rust/cloud-storage/.sqlx/query-b453c431475246f0c3e5951aa301bade0f476dd6139e43d0f2420ae9fdca3028.jsonindex 4a8de045bb..3bec8a2cfa 100644--- a/rust/cloud-storage/.sqlx/query-012063ce1220cd6f1b04eeefa088324d50ac472be760c0efe10b5060b0878c75.json+++ b/rust/cloud-storage/.sqlx/query-b453c431475246f0c3e5951aa301bade0f476dd6139e43d0f2420ae9fdca3028.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT child_macro_id\n FROM macro_user_links\n WHERE primary_macro_id = $1\n ",+ "query": "\n SELECT DISTINCT child_macro_id\n FROM macro_user_links\n WHERE primary_macro_id = $1\n ","describe": {"columns": [{@@ -18,5 +18,5 @@false]},- "hash": "012063ce1220cd6f1b04eeefa088324d50ac472be760c0efe10b5060b0878c75"+ "hash": "b453c431475246f0c3e5951aa301bade0f476dd6139e43d0f2420ae9fdca3028"}diff --git a/rust/cloud-storage/.sqlx/query-97c68b141af174883700c5184428b2e33f43a23f900df1a8a68acfa046af7dca.json b/rust/cloud-storage/.sqlx/query-d1eba683c01a615159db5a2aba7090ecb587e9c53c9875c1bc84b582be547ca1.jsonsimilarity index 90%rename from rust/cloud-storage/.sqlx/query-97c68b141af174883700c5184428b2e33f43a23f900df1a8a68acfa046af7dca.jsonrename to rust/cloud-storage/.sqlx/query-d1eba683c01a615159db5a2aba7090ecb587e9c53c9875c1bc84b582be547ca1.jsonindex d5d0150406..f9b7015405 100644--- a/rust/cloud-storage/.sqlx/query-97c68b141af174883700c5184428b2e33f43a23f900df1a8a68acfa046af7dca.json+++ b/rust/cloud-storage/.sqlx/query-d1eba683c01a615159db5a2aba7090ecb587e9c53c9875c1bc84b582be547ca1.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT l.id, l.macro_id, l.fusionauth_user_id, l.email_address, l.provider as \"provider: _\",\n l.is_sync_active, l.is_primary, l.created_at, l.updated_at\n FROM email_messages m\n JOIN email_links l ON l.id = m.link_id\n WHERE m.id = $1\n AND (\n l.macro_id = $2\n OR EXISTS (\n SELECT 1 FROM macro_user_links mul\n WHERE mul.child_macro_id = l.macro_id AND mul.primary_macro_id = $2\n )\n )\n ",+ "query": "\n SELECT l.id, l.macro_id, l.fusionauth_user_id, l.email_address, l.provider as \"provider: _\",\n l.is_sync_active, l.is_primary, l.created_at, l.updated_at\n FROM email_messages m\n JOIN email_links l ON l.id = m.link_id\n WHERE m.id = $1\n AND (\n l.macro_id = $2\n OR EXISTS (\n SELECT 1 FROM macro_user_links mul\n WHERE mul.link_id = l.id AND mul.primary_macro_id = $2\n )\n )\n ","describe": {"columns": [{@@ -76,5 +76,5 @@false]},- "hash": "97c68b141af174883700c5184428b2e33f43a23f900df1a8a68acfa046af7dca"+ "hash": "d1eba683c01a615159db5a2aba7090ecb587e9c53c9875c1bc84b582be547ca1"}diff --git a/rust/cloud-storage/.sqlx/query-547e3772bb1def3090b8647fcec5795696c6d02cbabc4619d6c384bfb5bff330.json b/rust/cloud-storage/.sqlx/query-d82e7e5bf7e54af1ffd3e03e084b05c4325e55a256367560513987dcf85d901b.jsonsimilarity index 64%rename from rust/cloud-storage/.sqlx/query-547e3772bb1def3090b8647fcec5795696c6d02cbabc4619d6c384bfb5bff330.jsonrename to rust/cloud-storage/.sqlx/query-d82e7e5bf7e54af1ffd3e03e084b05c4325e55a256367560513987dcf85d901b.jsonindex 9ddc4e92bd..3d2266e0f4 100644--- a/rust/cloud-storage/.sqlx/query-547e3772bb1def3090b8647fcec5795696c6d02cbabc4619d6c384bfb5bff330.json+++ b/rust/cloud-storage/.sqlx/query-d82e7e5bf7e54af1ffd3e03e084b05c4325e55a256367560513987dcf85d901b.json@@ -1,15 +1,16 @@{"db_name": "PostgreSQL",- "query": "\n DELETE FROM macro_user_links\n WHERE primary_macro_id = $1\n AND child_macro_id = $2\n ",+ "query": "\n DELETE FROM macro_user_links\n WHERE primary_macro_id = $1\n AND child_macro_id = $2\n AND link_id = $3\n ","describe": {"columns": [],"parameters": {"Left": ["Text",- "Text"+ "Text",+ "Uuid"]},"nullable": []},- "hash": "547e3772bb1def3090b8647fcec5795696c6d02cbabc4619d6c384bfb5bff330"+ "hash": "d82e7e5bf7e54af1ffd3e03e084b05c4325e55a256367560513987dcf85d901b"}diff --git a/rust/cloud-storage/.sqlx/query-e82b920450ded09debcd219dbe7a34dbde810b6b60c8d8a0854ee49ac57a7c02.json b/rust/cloud-storage/.sqlx/query-e82b920450ded09debcd219dbe7a34dbde810b6b60c8d8a0854ee49ac57a7c02.jsondeleted file mode 100644index 4913744755..0000000000--- a/rust/cloud-storage/.sqlx/query-e82b920450ded09debcd219dbe7a34dbde810b6b60c8d8a0854ee49ac57a7c02.json+++ /dev/null@@ -1,14 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n DELETE FROM macro_user_links\n WHERE child_macro_id = $1\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text"- ]- },- "nullable": []- },- "hash": "e82b920450ded09debcd219dbe7a34dbde810b6b60c8d8a0854ee49ac57a7c02"-}diff --git a/rust/cloud-storage/.sqlx/query-de8329eb43d809c9055c9530784594572d9da9387c41873beacfb4a615e964cb.json b/rust/cloud-storage/.sqlx/query-f5f17b4541173ce2963dbcce46cfc30fe9a30c6301dc466f8a31b4932f6c1c5c.jsonsimilarity index 78%rename from rust/cloud-storage/.sqlx/query-de8329eb43d809c9055c9530784594572d9da9387c41873beacfb4a615e964cb.jsonrename to rust/cloud-storage/.sqlx/query-f5f17b4541173ce2963dbcce46cfc30fe9a30c6301dc466f8a31b4932f6c1c5c.jsonindex fc174aa223..3211f52ecf 100644--- a/rust/cloud-storage/.sqlx/query-de8329eb43d809c9055c9530784594572d9da9387c41873beacfb4a615e964cb.json+++ b/rust/cloud-storage/.sqlx/query-f5f17b4541173ce2963dbcce46cfc30fe9a30c6301dc466f8a31b4932f6c1c5c.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT l.macro_id AS \"macro_id!\"\n FROM email_threads et\n JOIN email_links l ON et.link_id = l.id\n WHERE et.id = $1\n UNION\n SELECT mul.primary_macro_id\n FROM email_threads et\n JOIN email_links l ON et.link_id = l.id\n JOIN macro_user_links mul ON mul.child_macro_id = l.macro_id\n WHERE et.id = $1\n ",+ "query": "\n SELECT l.macro_id AS \"macro_id!\"\n FROM email_threads et\n JOIN email_links l ON et.link_id = l.id\n WHERE et.id = $1\n UNION\n SELECT mul.primary_macro_id\n FROM email_threads et\n JOIN email_links l ON et.link_id = l.id\n JOIN macro_user_links mul ON mul.link_id = l.id\n WHERE et.id = $1\n ","describe": {"columns": [{@@ -18,5 +18,5 @@null]},- "hash": "de8329eb43d809c9055c9530784594572d9da9387c41873beacfb4a615e964cb"+ "hash": "f5f17b4541173ce2963dbcce46cfc30fe9a30c6301dc466f8a31b4932f6c1c5c"}diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/link.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/link.rsindex 18c355d467..78a9f3a493 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/link.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/link.rs@@ -89,7 +89,7 @@ pub(super) async fn owned_link_for_thread(l.macro_id = $2OR EXISTS (SELECT 1 FROM macro_user_links mul- WHERE mul.child_macro_id = l.macro_id AND mul.primary_macro_id = $2+ WHERE mul.link_id = l.id AND mul.primary_macro_id = $2))"#,@@ -155,7 +155,7 @@ pub(super) async fn inboxes_for_macro_id(SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,el.provider, el.is_sync_active, el.is_primary, el.created_at, el.updated_atFROM email_links el- JOIN macro_user_links mul ON el.macro_id = mul.child_macro_id+ JOIN macro_user_links mul ON el.id = mul.link_idWHERE mul.primary_macro_id = $1) AS combinedORDER BY created_at DESCdiff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/link.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/link.rsindex 2d4247f024..815964697f 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/link.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/link.rs@@ -98,17 +98,19 @@ async fn test_inboxes_for_macro_id_includes_own_and_delegated(('a1000000-0000-0000-0000-000000000001'::uuid, 'macro|[email protected]', 'fa-alice', '[email protected]', 'GMAIL', true, NOW() - INTERVAL '2 hours', NOW()),('a2000000-0000-0000-0000-000000000002'::uuid, 'macro|[email protected]', 'fa-alice', '[email protected]', 'GMAIL', true, NOW() - INTERVAL '1 hour', NOW()),('5e000000-0000-0000-0000-000000000003'::uuid, 'macro|[email protected]', 'fa-shared', '[email protected]', 'GMAIL', true, NOW(), NOW()),+ ('5f000000-0000-0000-0000-000000000005'::uuid, 'macro|[email protected]', 'fa-shared', '[email protected]', 'GMAIL', true, NOW(), NOW()),('b0000000-0000-0000-0000-000000000004'::uuid, 'macro|[email protected]', 'fa-bob', '[email protected]', 'GMAIL', true, NOW(), NOW())"#,).execute(&pool).await?;- // alice is the primary; shared@ is delegated to her. bob is unrelated.+ // alice is the primary; only shared@'s first inbox is delegated to her.+ // bob is unrelated.sqlx::query(r#"- INSERT INTO macro_user_links (primary_macro_id, child_macro_id) VALUES- ('macro|[email protected]', 'macro|[email protected]')+ INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id) VALUES+ ('macro|[email protected]', 'macro|[email protected]', '5e000000-0000-0000-0000-000000000003'::uuid)"#,).execute(&pool)@@ -132,6 +134,10 @@ async fn test_inboxes_for_macro_id_includes_own_and_delegated(emails.contains("[email protected]"),"delegated inbox must be included");+ assert!(+ !emails.contains("[email protected]"),+ "child's inbox outside the link-scoped grant must be excluded"+ );assert!(!emails.contains("[email protected]"),"unrelated inbox must be excluded"@@ -247,8 +253,8 @@ async fn test_owned_link_for_thread_resolves_own_and_delegated(.await?;sqlx::query(- r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id)- VALUES ('macro|[email protected]', 'macro|[email protected]')"#,+ r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+ VALUES ('macro|[email protected]', 'macro|[email protected]', 'c0000000-0000-0000-0000-000000000001'::uuid)"#,).execute(&pool).await?;diff --git a/rust/cloud-storage/email_db_client/src/links/get.rs b/rust/cloud-storage/email_db_client/src/links/get.rsindex 10ac329d6f..a09d861dd4 100644--- a/rust/cloud-storage/email_db_client/src/links/get.rs+++ b/rust/cloud-storage/email_db_client/src/links/get.rs@@ -100,7 +100,7 @@ pub async fn fetch_inboxes_for_macro_id(SELECT el.id, el.macro_id, el.fusionauth_user_id, el.email_address,el.provider, el.is_sync_active, el.is_primary, el.created_at, el.updated_atFROM email_links el- JOIN macro_user_links mul ON el.macro_id = mul.child_macro_id+ JOIN macro_user_links mul ON el.id = mul.link_idWHERE mul.primary_macro_id = $1) AS combinedORDER BY created_at DESC@@ -175,7 +175,7 @@ pub async fn fetch_owned_link_for_thread(l.macro_id = $2OR EXISTS (SELECT 1 FROM macro_user_links mul- WHERE mul.child_macro_id = l.macro_id AND mul.primary_macro_id = $2+ WHERE mul.link_id = l.id AND mul.primary_macro_id = $2))"#,@@ -208,7 +208,7 @@ pub async fn fetch_owned_link_for_message(l.macro_id = $2OR EXISTS (SELECT 1 FROM macro_user_links mul- WHERE mul.child_macro_id = l.macro_id AND mul.primary_macro_id = $2+ WHERE mul.link_id = l.id AND mul.primary_macro_id = $2))"#,diff --git a/rust/cloud-storage/email_db_client/src/links/get/test.rs b/rust/cloud-storage/email_db_client/src/links/get/test.rsindex da762d04e7..3e468a07c4 100644--- a/rust/cloud-storage/email_db_client/src/links/get/test.rs+++ b/rust/cloud-storage/email_db_client/src/links/get/test.rs@@ -1,5 +1,6 @@use crate::links::get::{- fetch_link_by_email, fetch_owned_link_for_message, fetch_owned_link_for_thread,+ fetch_inboxes_for_macro_id, fetch_link_by_email, fetch_owned_link_for_message,+ fetch_owned_link_for_thread,};use macro_db_migrator::MACRO_DB_MIGRATIONS;use models_email::service::link::UserProvider;@@ -93,11 +94,13 @@ async fn insert_inbox_with_thread_and_message((link_id, thread_id, message_id)}-async fn insert_delegation(pool: &Pool<Postgres>, primary: &str, child: &str) {+async fn insert_delegation(pool: &Pool<Postgres>, primary: &str, child: &str, link_id: Uuid) {sqlx::query!(- r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id) VALUES ($1, $2)"#,+ r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+ VALUES ($1, $2, $3)"#,primary,child,+ link_id,).execute(pool).await@@ -110,7 +113,7 @@ async fn resolves_inbox_for_owner_and_delegate(pool: Pool<Postgres>) -> anyhow::insert_user(&pool, PRIMARY, "[email protected]").await;let (link_id, thread_id, message_id) =insert_inbox_with_thread_and_message(&pool, CHILD, "[email protected]").await;- insert_delegation(&pool, PRIMARY, CHILD).await;+ insert_delegation(&pool, PRIMARY, CHILD, link_id).await;// Delegate resolves the shared inbox from both thread and message.assert_eq!(@@ -142,9 +145,9 @@ async fn resolves_nothing_for_unrelated_caller(pool: Pool<Postgres>) -> anyhow::insert_user(&pool, CHILD, "[email protected]").await;insert_user(&pool, PRIMARY, "[email protected]").await;insert_user(&pool, STRANGER, "[email protected]").await;- let (_link_id, thread_id, message_id) =+ let (link_id, thread_id, message_id) =insert_inbox_with_thread_and_message(&pool, CHILD, "[email protected]").await;- insert_delegation(&pool, PRIMARY, CHILD).await;+ insert_delegation(&pool, PRIMARY, CHILD, link_id).await;// STRANGER is a real user who neither owns nor is delegated the inbox.assert!(@@ -161,6 +164,51 @@ async fn resolves_nothing_for_unrelated_caller(pool: Pool<Postgres>) -> anyhow::Ok(())}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn scoped_delegation_sees_only_its_link(pool: Pool<Postgres>) -> anyhow::Result<()> {+ insert_user(&pool, CHILD, "[email protected]").await;+ insert_user(&pool, PRIMARY, "[email protected]").await;+ let (link_a, _, _) =+ insert_inbox_with_thread_and_message(&pool, CHILD, "[email protected]").await;+ insert_delegation(&pool, PRIMARY, CHILD, link_a).await;++ // The child connects a second inbox after the grant; the scoped delegate+ // must not see it.+ let (link_b, thread_b, message_b) =+ insert_inbox_with_thread_and_message(&pool, CHILD, "[email protected]").await;++ let inbox_ids: Vec<Uuid> = fetch_inboxes_for_macro_id(&pool, PRIMARY)+ .await?+ .into_iter()+ .map(|l| l.id)+ .collect();+ assert_eq!(inbox_ids, vec![link_a]);++ assert!(+ fetch_owned_link_for_thread(&pool, PRIMARY, thread_b)+ .await?+ .is_none()+ );+ assert!(+ fetch_owned_link_for_message(&pool, PRIMARY, message_b)+ .await?+ .is_none()+ );++ // The child still owns both inboxes.+ let mut child_inboxes: Vec<Uuid> = fetch_inboxes_for_macro_id(&pool, CHILD)+ .await?+ .into_iter()+ .map(|l| l.id)+ .collect();+ child_inboxes.sort();+ let mut expected = vec![link_a, link_b];+ expected.sort();+ assert_eq!(child_inboxes, expected);++ Ok(())+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn fetch_link_by_email_finds_link_owned_by_another_macro_user(pool: Pool<Postgres>,diff --git a/rust/cloud-storage/email_service/src/api/email/init.rs b/rust/cloud-storage/email_service/src/api/email/init.rsindex da8bc8cfa8..516529d65a 100644--- a/rust/cloud-storage/email_service/src/api/email/init.rs+++ b/rust/cloud-storage/email_service/src/api/email/init.rs@@ -227,6 +227,7 @@ pub async fn handler(&mut *tx,&user_context.user_id,child_macro_id,+ child_link.id,).await.context("Failed to insert macro_user_links edge")?;@@ -294,6 +295,7 @@ pub async fn handler(&mut *tx,&user_context.user_id,child_macro_id,+ link.id,).await.context("Failed to insert macro_user_links edge")?;diff --git a/rust/cloud-storage/email_service/src/api/email/links/access.rs b/rust/cloud-storage/email_service/src/api/email/links/access.rsindex 4c8a42faa7..9b297f2dc4 100644--- a/rust/cloud-storage/email_service/src/api/email/links/access.rs+++ b/rust/cloud-storage/email_service/src/api/email/links/access.rs@@ -74,6 +74,7 @@ pub async fn authorize_inbox_access(&ctx.db,caller_macro_id,link.macro_id.as_ref(),+ link.id,).await?;diff --git a/rust/cloud-storage/email_service/src/api/email/links/delete.rs b/rust/cloud-storage/email_service/src/api/email/links/delete.rsindex 9f5aab727e..c6490eee34 100644--- a/rust/cloud-storage/email_service/src/api/email/links/delete.rs+++ b/rust/cloud-storage/email_service/src/api/email/links/delete.rs@@ -56,6 +56,7 @@ pub async fn delete_link_handler(&ctx.db,&user_context.user_id,link.macro_id.as_ref(),+ link.id,).await.context("failed to delete delegation edge")?;diff --git a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rsindex 29ab80336e..f3b619f097 100644--- a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs+++ b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs@@ -574,15 +574,18 @@ async fn send_notifications(snippet: message.snippet.unwrap_or_default(),};- let primaries =- macro_db_client::macro_user_links::get_primaries_for_child(&ctx.db, link.macro_id.as_ref())- .await- .map_err(|e| {- ProcessingError::Retryable(DetailedError {- reason: FailureReason::DatabaseQueryFailed,- source: e.context("Failed to fetch delegated primaries".to_string()),- })- })?;+ let primaries = macro_db_client::macro_user_links::get_primaries_for_link(+ &ctx.db,+ link.macro_id.as_ref(),+ link.id,+ )+ .await+ .map_err(|e| {+ ProcessingError::Retryable(DetailedError {+ reason: FailureReason::DatabaseQueryFailed,+ source: e.context("Failed to fetch delegated primaries".to_string()),+ })+ })?;let recipient_ids = build_notification_recipients(&link.macro_id, primaries);diff --git a/rust/cloud-storage/email_service/src/pubsub/link_manager/process.rs b/rust/cloud-storage/email_service/src/pubsub/link_manager/process.rsindex 47a689112c..63bfc9da93 100644--- a/rust/cloud-storage/email_service/src/pubsub/link_manager/process.rs+++ b/rust/cloud-storage/email_service/src/pubsub/link_manager/process.rs@@ -262,29 +262,8 @@ async fn handle_delete(tracing::error!(error=?e, link_id=?link.id, "Failed to set deleted_at on email link history");}- // If this was the owner's last inbox, prune any delegation edges pointing at- // them so grantees don't retain dangling references. A delegation edge grants- // access to all of the owner's inboxes, so only clean up once none remain.- // Best-effort: leftover edges are harmless (they resolve to nothing).- match email_db_client::links::get::fetch_link_by_macro_id(&ctx.db, link.macro_id.as_ref()).await- {- Ok(None) => {- if let Err(e) = macro_db_client::macro_user_links::delete_edges_for_child(- &ctx.db,- link.macro_id.as_ref(),- )- .await- {- tracing::error!(error=?e, "Failed to prune delegation edges after deleting owner's last inbox");- }- }- Ok(Some(_)) => {- tracing::debug!("Owner has remaining inboxes; keeping delegation edges");- }- Err(e) => {- tracing::error!(error=?e, "Failed to check remaining inboxes for delegation edge cleanup");- }- }+ // Delegation edges scoped to the deleted link were cascaded away by FK; no+ // manual pruning is needed.// If the deleted link was a promoted shared mailbox, remove its minted macro user too// (this also cascades its delegation edges and the promoted-mailbox marker). No-op fordiff --git a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/mod.rs b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/mod.rsindex a2297bd213..327c866c56 100644--- a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/mod.rs+++ b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/mod.rs@@ -107,7 +107,7 @@ pub(in crate::outbound::pg_access_repo) async fn get_entity_users(SELECT mul.primary_macro_idFROM email_threads etJOIN email_links l ON et.link_id = l.id- JOIN macro_user_links mul ON mul.child_macro_id = l.macro_id+ JOIN macro_user_links mul ON mul.link_id = l.idWHERE et.id = $1"#,entity_iddiff --git a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/test.rs b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/test.rsindex 9c85b96692..2adb59ff3c 100644--- a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/test.rs+++ b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/test.rs@@ -32,8 +32,8 @@ async fn insert_user(pool: &PgPool, user_id: &str, email: &str) {.unwrap();}-/// An empty link + thread owned by `owner_macro_id`. Returns the thread id.-async fn insert_thread(pool: &PgPool, owner_macro_id: &str) -> Uuid {+/// An empty link + thread owned by `owner_macro_id`. Returns `(link_id, thread_id)`.+async fn insert_thread(pool: &PgPool, owner_macro_id: &str, email: &str) -> (Uuid, Uuid) {let link_id = Uuid::new_v4();let thread_id = Uuid::new_v4();@@ -42,7 +42,7 @@ async fn insert_thread(pool: &PgPool, owner_macro_id: &str) -> Uuid {VALUES ($1, $2, $2, $3, 'GMAIL')"#,link_id,owner_macro_id,- format!("{owner_macro_id}@mail.test"),+ email,).execute(pool).await@@ -57,19 +57,21 @@ async fn insert_thread(pool: &PgPool, owner_macro_id: &str) -> Uuid {.await.unwrap();- thread_id+ (link_id, thread_id)}#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn get_entity_users_includes_inbox_delegate(pool: PgPool) -> anyhow::Result<()> {insert_user(&pool, OWNER, "[email protected]").await;insert_user(&pool, DELEGATE, "[email protected]").await;- let thread_id = insert_thread(&pool, OWNER).await;+ let (link_id, thread_id) = insert_thread(&pool, OWNER, "[email protected]").await;sqlx::query!(- r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id) VALUES ($1, $2)"#,+ r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+ VALUES ($1, $2, $3)"#,DELEGATE,OWNER,+ link_id,).execute(&pool).await@@ -82,3 +84,34 @@ async fn get_entity_users_includes_inbox_delegate(pool: PgPool) -> anyhow::Resulassert!(ids.contains(DELEGATE), "inbox delegate must be included");Ok(())}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn get_entity_users_excludes_delegate_scoped_to_other_link(+ pool: PgPool,+) -> anyhow::Result<()> {+ insert_user(&pool, OWNER, "[email protected]").await;+ insert_user(&pool, DELEGATE, "[email protected]").await;+ let (granted_link_id, _) = insert_thread(&pool, OWNER, "[email protected]").await;+ let (_, other_thread_id) = insert_thread(&pool, OWNER, "[email protected]").await;++ sqlx::query!(+ r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+ VALUES ($1, $2, $3)"#,+ DELEGATE,+ OWNER,+ granted_link_id,+ )+ .execute(&pool)+ .await+ .unwrap();++ let users = get_entity_users(&pool, &other_thread_id, EntityType::EmailThread).await?;+ let ids: std::collections::HashSet<String> = users.iter().map(|u| u.to_string()).collect();++ assert!(ids.contains(OWNER), "inbox owner must be included");+ assert!(+ !ids.contains(DELEGATE),+ "delegate scoped to a different link must be excluded"+ );+ Ok(())+}diff --git a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access.rsindex cb0825b6fd..4de3631f2d 100644--- a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs+++ b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs@@ -33,7 +33,7 @@ pub async fn get_thread_access(OR EXISTS (SELECT 1FROM public.macro_user_links mul- WHERE mul.child_macro_id = l.macro_id+ WHERE mul.link_id = l.idAND mul.primary_macro_id = $2))diff --git a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rsindex b640d72625..12ebe10063 100644--- a/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs+++ b/rust/cloud-storage/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs@@ -333,12 +333,20 @@ async fn returns_none_when_thread_does_not_exist(pool: PgPool) -> anyhow::Result// Delegated / shared inboxes (macro_user_links)// ----------------------------------------------------------------------------/// `primary_macro_id` is delegated `child_macro_id`'s inbox. Both must exist in "User".-async fn insert_delegation(pool: &PgPool, primary_macro_id: &str, child_macro_id: &str) {+/// `primary_macro_id` is delegated `child_macro_id`'s `link_id` inbox. Both users+/// must exist in "User".+async fn insert_delegation(+ pool: &PgPool,+ primary_macro_id: &str,+ child_macro_id: &str,+ link_id: Uuid,+) {sqlx::query!(- r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id) VALUES ($1, $2)"#,+ r#"INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+ VALUES ($1, $2, $3)"#,primary_macro_id,child_macro_id,+ link_id,).execute(pool).await@@ -351,8 +359,8 @@ async fn delegated_inbox_grants_access(pool: PgPool) -> anyhow::Result<()> {// owned by OWNER's inbox, so the delegate gets owner-equivalent access.insert_user(&pool, OWNER, "[email protected]").await;insert_user(&pool, REQUESTER, "[email protected]").await;- let (_link_id, thread_id) = create_link_and_thread(&pool, OWNER).await;- insert_delegation(&pool, REQUESTER, OWNER).await;+ let (link_id, thread_id) = create_link_and_thread(&pool, OWNER).await;+ insert_delegation(&pool, REQUESTER, OWNER, link_id).await;assert_eq!(access_as_requester(&pool, &thread_id).await,@@ -368,13 +376,47 @@ async fn delegation_scoped_to_the_primary(pool: PgPool) -> anyhow::Result<()> {insert_user(&pool, OWNER, "[email protected]").await;insert_user(&pool, REQUESTER, "[email protected]").await;insert_user(&pool, OTHER, "[email protected]").await;- let (_link_id, thread_id) = create_link_and_thread(&pool, OWNER).await;- insert_delegation(&pool, OTHER, OWNER).await;+ let (link_id, thread_id) = create_link_and_thread(&pool, OWNER).await;+ insert_delegation(&pool, OTHER, OWNER, link_id).await;assert_eq!(access_as_requester(&pool, &thread_id).await, None);Ok(())}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn delegation_scoped_to_another_link_denies_access(pool: PgPool) -> anyhow::Result<()> {+ // REQUESTER holds a grant on one of OWNER's inboxes, but the thread lives in a+ // different inbox of the same owner — the link-scoped grant must not reach it.+ insert_user(&pool, OWNER, "[email protected]").await;+ insert_user(&pool, REQUESTER, "[email protected]").await;+ let (granted_link_id, _) = create_link_and_thread(&pool, OWNER).await;++ let other_link_id = Uuid::new_v4();+ let other_thread_id = Uuid::new_v4();+ sqlx::query!(+ r#"INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)+ VALUES ($1, $2, $2, '[email protected]', 'GMAIL')"#,+ other_link_id,+ OWNER,+ )+ .execute(&pool)+ .await+ .unwrap();+ sqlx::query!(+ r#"INSERT INTO email_threads (id, link_id) VALUES ($1, $2)"#,+ other_thread_id,+ other_link_id,+ )+ .execute(&pool)+ .await+ .unwrap();++ insert_delegation(&pool, REQUESTER, OWNER, granted_link_id).await;++ assert_eq!(access_as_requester(&pool, &other_thread_id).await, None);+ Ok(())+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn unauthenticated_user_never_gets_crm_access(pool: PgPool) -> anyhow::Result<()> {// Even a fully-synced thread is invisible to an unauthenticated caller: withdiff --git a/rust/cloud-storage/macro_db_client/migrations/20260611183443_macro_user_links_link_granular.sql b/rust/cloud-storage/macro_db_client/migrations/20260611183443_macro_user_links_link_granular.sqlnew file mode 100644index 0000000000..753177f212--- /dev/null+++ b/rust/cloud-storage/macro_db_client/migrations/20260611183443_macro_user_links_link_granular.sql@@ -0,0 +1,27 @@+-- Scope delegation edges to a single inbox: link_id pins each edge to one email_links+-- row, replacing the account-wide grant over every link under child (including links+-- created after the edge).+ALTER TABLE macro_user_links+ ADD COLUMN link_id UUID REFERENCES email_links(id) ON DELETE CASCADE;++ALTER TABLE macro_user_links DROP CONSTRAINT macro_user_links_pkey;++-- Pin pre-existing account-wide edges to the links the child owns right now, so a+-- delegate keeps exactly what they can already see but gains nothing connected later.+-- An account-wide edge whose child has no links grants nothing and is simply dropped.+INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+SELECT mul.primary_macro_id, mul.child_macro_id, el.id+FROM macro_user_links mul+JOIN email_links el ON el.macro_id = mul.child_macro_id+WHERE mul.link_id IS NULL;++DELETE FROM macro_user_links WHERE link_id IS NULL;++ALTER TABLE macro_user_links+ ALTER COLUMN link_id SET NOT NULL;++ALTER TABLE macro_user_links+ ADD PRIMARY KEY (primary_macro_id, child_macro_id, link_id);++CREATE INDEX IF NOT EXISTS macro_user_links_link_idx+ ON macro_user_links (link_id);diff --git a/rust/cloud-storage/macro_db_client/src/macro_user_links.rs b/rust/cloud-storage/macro_db_client/src/macro_user_links.rsindex 3bdaad663b..e3b7930978 100644--- a/rust/cloud-storage/macro_db_client/src/macro_user_links.rs+++ b/rust/cloud-storage/macro_db_client/src/macro_user_links.rs@@ -1,31 +1,35 @@//! Directed links between macro_ids. Used by multi-inbox so a primary macro user can-//! read another macro user's inbox without merging identities. The "what" of the-//! delegation is implicit — primary may read child's email_links rows.+//! read another macro user's inbox without merging identities. Each edge is scoped to+//! a single `email_links` row via `link_id` — it grants exactly that inbox, never+//! links the child connects later.+use sqlx::types::Uuid;use sqlx::{Executor, Pool, Postgres};#[cfg(test)]mod test;-/// Insert an edge `(primary, child)`. Idempotent: if the edge already exists-/// the conflict is swallowed.+/// Insert an edge granting `primary` access to the child's `link_id` inbox.+/// Idempotent: if the edge already exists the conflict is swallowed.#[tracing::instrument(skip(db), err)]pub async fn insert_edge<'e, E>(db: E,primary_macro_id: &str,child_macro_id: &str,+ link_id: Uuid,) -> anyhow::Result<()>whereE: Executor<'e, Database = Postgres>,{sqlx::query!(r#"- INSERT INTO macro_user_links (primary_macro_id, child_macro_id)- VALUES ($1, $2)- ON CONFLICT (primary_macro_id, child_macro_id) DO NOTHING+ INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)+ VALUES ($1, $2, $3)+ ON CONFLICT (primary_macro_id, child_macro_id, link_id) DO NOTHING"#,primary_macro_id,child_macro_id,+ link_id,).execute(db).await?;@@ -33,21 +37,25 @@ whereOk(())}-/// Remove an edge. No-op if the edge does not exist.+/// Remove the edge granting `primary` access to the child's `link_id` inbox.+/// No-op if the edge does not exist.#[tracing::instrument(skip(db), err)]pub async fn delete_edge(db: &Pool<Postgres>,primary_macro_id: &str,child_macro_id: &str,+ link_id: Uuid,) -> anyhow::Result<()> {sqlx::query!(r#"DELETE FROM macro_user_linksWHERE primary_macro_id = $1AND child_macro_id = $2+ AND link_id = $3"#,primary_macro_id,child_macro_id,+ link_id,).execute(db).await?;@@ -55,34 +63,15 @@ pub async fn delete_edge(Ok(())}-/// Removes every edge granting access to `child_macro_id`'s inboxes. Used when a-/// macro user's last inbox is torn down so dangling delegation edges don't linger.-/// Returns the number of edges removed.-#[tracing::instrument(skip(db), err)]-pub async fn delete_edges_for_child(- db: &Pool<Postgres>,- child_macro_id: &str,-) -> anyhow::Result<u64> {- let result = sqlx::query!(- r#"- DELETE FROM macro_user_links- WHERE child_macro_id = $1- "#,- child_macro_id,- )- .execute(db)- .await?;-- Ok(result.rows_affected())-}--/// Returns whether the edge `(primary, child)` exists. Used to authorize a-/// primary macro user acting on an inbox delegated to them.+/// Returns whether the edge granting `primary` access to the child's `link_id`+/// inbox exists. Used to authorize a primary macro user acting on an inbox+/// delegated to them.#[tracing::instrument(skip(db), err)]pub async fn edge_exists(db: &Pool<Postgres>,primary_macro_id: &str,child_macro_id: &str,+ link_id: Uuid,) -> anyhow::Result<bool> {let exists = sqlx::query_scalar!(r#"@@ -91,10 +80,12 @@ pub async fn edge_exists(FROM macro_user_linksWHERE primary_macro_id = $1AND child_macro_id = $2+ AND link_id = $3) AS "exists!""#,primary_macro_id,child_macro_id,+ link_id,).fetch_one(db).await?;@@ -111,7 +102,7 @@ pub async fn children_for_primary() -> anyhow::Result<Vec<String>> {let rows = sqlx::query_scalar!(r#"- SELECT child_macro_id+ SELECT DISTINCT child_macro_idFROM macro_user_linksWHERE primary_macro_id = $1"#,@@ -123,9 +114,9 @@ pub async fn children_for_primary(Ok(rows)}-/// Returns the `primary_macro_id`s delegated to read the given child's inboxes.-/// Inverse of [`children_for_primary`]; used to fan a child inbox's notifications-/// out to every primary that can view it.+/// Returns the `primary_macro_id`s holding any delegation edge to the child,+/// regardless of which link it covers. Used to decide whether a child has+/// delegates left (e.g. the last-delegate teardown of a promoted shared mailbox).#[tracing::instrument(skip(db), err)]pub async fn get_primaries_for_child(db: &Pool<Postgres>,@@ -133,7 +124,7 @@ pub async fn get_primaries_for_child() -> anyhow::Result<Vec<String>> {let rows = sqlx::query_scalar!(r#"- SELECT primary_macro_id+ SELECT DISTINCT primary_macro_idFROM macro_user_linksWHERE child_macro_id = $1"#,@@ -144,3 +135,27 @@ pub async fn get_primaries_for_child(Ok(rows)}++/// Returns the `primary_macro_id`s delegated to read the child's `link_id` inbox.+/// Used to fan a child inbox's notifications out to every primary that can view it.+#[tracing::instrument(skip(db), err)]+pub async fn get_primaries_for_link(+ db: &Pool<Postgres>,+ child_macro_id: &str,+ link_id: Uuid,+) -> anyhow::Result<Vec<String>> {+ let rows = sqlx::query_scalar!(+ r#"+ SELECT primary_macro_id+ FROM macro_user_links+ WHERE child_macro_id = $1+ AND link_id = $2+ "#,+ child_macro_id,+ link_id,+ )+ .fetch_all(db)+ .await?;++ Ok(rows)+}diff --git a/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs b/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rsindex ccf3703aca..39dcd7ec47 100644--- a/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs+++ b/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs@@ -31,14 +31,33 @@ async fn insert_user(pool: &Pool<Postgres>, id: &str) -> anyhow::Result<()> {Ok(())}+async fn insert_link(pool: &Pool<Postgres>, macro_id: &str, email: &str) -> anyhow::Result<Uuid> {+ let link_id = macro_uuid::generate_uuid_v7();+ sqlx::query!(+ r#"+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)+ VALUES ($1, $2, $2, $3, 'GMAIL')+ "#,+ link_id,+ macro_id,+ email,+ )+ .execute(pool)+ .await?;++ Ok(link_id)+}+#[sqlx::test]async fn insert_then_list_children(pool: Pool<Postgres>) -> anyhow::Result<()> {insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;+ let link_a = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;+ let link_b = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link_a).await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link_b).await?;let mut children = children_for_primary(&pool, "macro|[email protected]").await?;children.sort();@@ -51,9 +70,10 @@ async fn insert_then_list_children(pool: Pool<Postgres>) -> anyhow::Result<()> {async fn insert_is_idempotent(pool: Pool<Postgres>) -> anyhow::Result<()> {insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;+ let link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;let children = children_for_primary(&pool, "macro|[email protected]").await?;assert_eq!(children, vec!["macro|[email protected]"]);@@ -65,9 +85,10 @@ async fn insert_is_idempotent(pool: Pool<Postgres>) -> anyhow::Result<()> {async fn delete_edge_removes_row(pool: Pool<Postgres>) -> anyhow::Result<()> {insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;+ let link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- delete_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;+ delete_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;let children = children_for_primary(&pool, "macro|[email protected]").await?;assert!(children.is_empty());@@ -79,40 +100,57 @@ async fn delete_edge_removes_row(pool: Pool<Postgres>) -> anyhow::Result<()> {async fn edge_exists_reflects_delegation(pool: Pool<Postgres>) -> anyhow::Result<()> {insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;+ let link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- assert!(edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;+ assert!(edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);// Direction matters: the reverse edge must not exist.- assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);- delete_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ delete_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;+ assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);Ok(())}#[sqlx::test]-async fn delete_edges_for_child_removes_all_grants(pool: Pool<Postgres>) -> anyhow::Result<()> {- insert_user(&pool, "macro|[email protected]").await?;- insert_user(&pool, "macro|[email protected]").await?;- insert_user(&pool, "macro|[email protected]").await?;- insert_user(&pool, "macro|[email protected]").await?;+async fn scoped_edge_does_not_grant_other_links(pool: Pool<Postgres>) -> anyhow::Result<()> {+ insert_user(&pool, "macro|[email protected]").await?;+ insert_user(&pool, "macro|[email protected]").await?;+ let link_a = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- // Two primaries delegate from the same owner, plus an unrelated edge.- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link_a).await?;- let removed = delete_edges_for_child(&pool, "macro|[email protected]").await?;- assert_eq!(removed, 2);+ // The child connects a second inbox after the edge was granted; the scoped+ // delegate must not gain access to it.+ let link_b = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;+ assert!(edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link_a).await?);+ assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link_b).await?);- assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);- assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);- // The unrelated edge is untouched.- assert!(edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ Ok(())+}++#[sqlx::test]+async fn deleting_link_cascades_scoped_edge(pool: Pool<Postgres>) -> anyhow::Result<()> {+ insert_user(&pool, "macro|[email protected]").await?;+ insert_user(&pool, "macro|[email protected]").await?;+ let link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;++ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await?;++ sqlx::query!(r#"DELETE FROM email_links WHERE id = $1"#, link)+ .execute(&pool)+ .await?;++ assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);+ assert!(+ children_for_primary(&pool, "macro|[email protected]")+ .await?+ .is_empty()+ );Ok(())}@@ -123,9 +161,22 @@ async fn get_primaries_for_child_returns_all_grantees(pool: Pool<Postgres>) -> ainsert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;+ let owner_link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;- insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;+ insert_edge(+ &pool,+ "macro|[email protected]",+ "macro|[email protected]",+ owner_link,+ )+ .await?;+ insert_edge(+ &pool,+ "macro|[email protected]",+ "macro|[email protected]",+ owner_link,+ )+ .await?;let mut primaries = get_primaries_for_child(&pool, "macro|[email protected]").await?;primaries.sort();@@ -151,18 +202,42 @@ async fn get_primaries_for_child_returns_all_grantees(pool: Pool<Postgres>) -> aOk(())}+#[sqlx::test]+async fn get_primaries_for_link_honors_scope(pool: Pool<Postgres>) -> anyhow::Result<()> {+ insert_user(&pool, "macro|[email protected]").await?;+ insert_user(&pool, "macro|[email protected]").await?;+ insert_user(&pool, "macro|[email protected]").await?;+ let link_a = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;+ let link_b = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;++ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link_a).await?;+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link_b).await?;++ assert_eq!(+ get_primaries_for_link(&pool, "macro|[email protected]", link_a).await?,+ vec!["macro|[email protected]"]+ );+ assert_eq!(+ get_primaries_for_link(&pool, "macro|[email protected]", link_b).await?,+ vec!["macro|[email protected]"]+ );++ Ok(())+}+#[sqlx::test]async fn insert_edge_within_transaction_commits(pool: Pool<Postgres>) -> anyhow::Result<()> {insert_user(&pool, "macro|[email protected]").await?;insert_user(&pool, "macro|[email protected]").await?;+ let link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;let mut tx = pool.begin().await?;- insert_edge(&mut *tx, "macro|[email protected]", "macro|[email protected]").await?;+ insert_edge(&mut *tx, "macro|[email protected]", "macro|[email protected]", link).await?;// Not visible outside the transaction until commit.- assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ assert!(!edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);tx.commit().await?;- assert!(edge_exists(&pool, "macro|[email protected]", "macro|[email protected]").await?);+ assert!(edge_exists(&pool, "macro|[email protected]", "macro|[email protected]", link).await?);Ok(())}@@ -170,8 +245,9 @@ async fn insert_edge_within_transaction_commits(pool: Pool<Postgres>) -> anyhow:#[sqlx::test]async fn self_referential_edge_rejected(pool: Pool<Postgres>) -> anyhow::Result<()> {insert_user(&pool, "macro|[email protected]").await?;+ let link = insert_link(&pool, "macro|[email protected]", "[email protected]").await?;- let result = insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await;+ let result = insert_edge(&pool, "macro|[email protected]", "macro|[email protected]", link).await;assert!(result.is_err(),"self-referential edge should be rejected by CHECK constraint"diff --git a/rust/cloud-storage/macro_db_client/src/shared_inbox.rs b/rust/cloud-storage/macro_db_client/src/shared_inbox.rsindex f9d9887b37..615851401a 100644--- a/rust/cloud-storage/macro_db_client/src/shared_inbox.rs+++ b/rust/cloud-storage/macro_db_client/src/shared_inbox.rs@@ -110,10 +110,20 @@ pub async fn promote_link_to_shared(anyhow::bail!("no email_links row {existing_link_id} to re-home; aborting promotion");}- crate::macro_user_links::insert_edge(&mut *conn, original_owner_macro_id, &mailbox_macro_id)- .await?;- crate::macro_user_links::insert_edge(&mut *conn, new_connector_macro_id, &mailbox_macro_id)- .await?;+ crate::macro_user_links::insert_edge(+ &mut *conn,+ original_owner_macro_id,+ &mailbox_macro_id,+ existing_link_id,+ )+ .await?;+ crate::macro_user_links::insert_edge(+ &mut *conn,+ new_connector_macro_id,+ &mailbox_macro_id,+ existing_link_id,+ )+ .await?;Ok(PromotedSharedInbox {mailbox_macro_id,diff --git a/rust/cloud-storage/macro_db_client/src/shared_inbox/test.rs b/rust/cloud-storage/macro_db_client/src/shared_inbox/test.rsindex dbc875b5a7..a00291b9c4 100644--- a/rust/cloud-storage/macro_db_client/src/shared_inbox/test.rs+++ b/rust/cloud-storage/macro_db_client/src/shared_inbox/test.rs@@ -91,6 +91,17 @@ async fn promote_dedups_to_single_link_with_two_edges(pool: Pool<Postgres>) -> aprimaries.sort();assert_eq!(primaries, vec![OWNER.to_string(), CONNECTOR.to_string()]);+ // The minted edges are scoped to the re-homed link, not account-wide.+ let scoped_count = sqlx::query_scalar!(+ r#"SELECT COUNT(*) AS "count!" FROM macro_user_links+ WHERE child_macro_id = $1 AND link_id = $2"#,+ mailbox_macro_id,+ link_id,+ )+ .fetch_one(&pool)+ .await?;+ assert_eq!(scoped_count, 2);+Ok(())}