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
perf(entity_access): answer document owner checks before planning the share union (#7517)
teoe5300d64f81f
diff --git a/.sqlx/query-4d7420904e5228a6850b81a07dd0ed554427c96d43dcbfeb22db55b5517ccc04.json b/.sqlx/query-4d7420904e5228a6850b81a07dd0ed554427c96d43dcbfeb22db55b5517ccc04.jsonnew file mode 100644index 0000000000..57521dc198--- /dev/null+++ b/.sqlx/query-4d7420904e5228a6850b81a07dd0ed554427c96d43dcbfeb22db55b5517ccc04.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT EXISTS (\n SELECT 1\n FROM entity_access\n WHERE entity_id = $1\n AND entity_type = 'document'\n AND source_id = ANY($2)\n AND access_level = 'owner'\n ) AS \"is_direct_owner!\"\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "is_direct_owner!",+ "type_info": "Bool"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "TextArray"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "4d7420904e5228a6850b81a07dd0ed554427c96d43dcbfeb22db55b5517ccc04"+}diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/document_access.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/document_access.rsindex 7c4756c860..a57a081dda 100644--- a/crates/entity_access/src/outbound/pg_access_repo/queries/document_access.rs+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/document_access.rs@@ -43,6 +43,29 @@ pub async fn get_document_access(return Ok(access_level);}+ // A separate statement on purpose: an owner grant settles the result, and+ // planning the union below costs more than executing it.+ let is_direct_owner = sqlx::query_scalar!(+ r#"+ SELECT EXISTS (+ SELECT 1+ FROM entity_access+ WHERE entity_id = $1+ AND entity_type = 'document'+ AND source_id = ANY($2)+ AND access_level = 'owner'+ ) AS "is_direct_owner!"+ "#,+ document_id,+ &source_ids.0,+ )+ .fetch_one(pool)+ .await?;++ if is_direct_owner {+ return Ok(Some(AccessLevel::Owner));+ }+let user_id_str = user_id.map(AsRef::as_ref).unwrap_or("");let all_level_strings: Vec<Option<String>> = sqlx::query_scalar!(diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/document_access/test.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/document_access/test.rsindex 4444013efc..1b71ce2ee1 100644--- a/crates/entity_access/src/outbound/pg_access_repo/queries/document_access/test.rs+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/document_access/test.rs@@ -249,6 +249,57 @@ async fn returns_highest_link_or_explicit_access_level(pool: PgPool) -> anyhow::Ok(())}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn direct_owner_grant_returns_owner(pool: PgPool) -> anyhow::Result<()> {+ insert_user(&pool, OWNER_WITHOUT_TEAM).await?;+ let document_id =+ insert_link_shared_document(&pool, OWNER_WITHOUT_TEAM, Some("PUBLIC"), Some("edit"))+ .await?;+ insert_document_entity_access(&pool, document_id, OWNER_WITHOUT_TEAM, AccessLevel::Owner)+ .await?;+ let source_ids = SourceIds(vec![OWNER_WITHOUT_TEAM.to_string()]);++ let access = get_document_access(&pool, &document_id, &source_ids, None).await?;++ assert_eq!(access, Some(AccessLevel::Owner));+ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn link_share_above_direct_grant_wins(pool: PgPool) -> anyhow::Result<()> {+ const REQUESTER: &str = "macro|[email protected]";++ insert_user(&pool, OWNER_WITHOUT_TEAM).await?;+ let document_id =+ insert_link_shared_document(&pool, OWNER_WITHOUT_TEAM, Some("PUBLIC"), Some("edit"))+ .await?;+ insert_document_entity_access(&pool, document_id, REQUESTER, AccessLevel::View).await?;+ let source_ids = SourceIds(vec![REQUESTER.to_string()]);++ let access = get_document_access(&pool, &document_id, &source_ids, None).await?;++ assert_eq!(access, Some(AccessLevel::Edit));+ Ok(())+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn another_sources_owner_grant_is_ignored(pool: PgPool) -> anyhow::Result<()> {+ const REQUESTER: &str = "macro|[email protected]";++ insert_user(&pool, OWNER_WITHOUT_TEAM).await?;+ let document_id =+ insert_link_shared_document(&pool, OWNER_WITHOUT_TEAM, Some("PUBLIC"), Some("view"))+ .await?;+ insert_document_entity_access(&pool, document_id, OWNER_WITHOUT_TEAM, AccessLevel::Owner)+ .await?;+ let source_ids = SourceIds(vec![REQUESTER.to_string()]);++ let access = get_document_access(&pool, &document_id, &source_ids, None).await?;++ assert_eq!(access, Some(AccessLevel::View));+ Ok(())+}+// ---------------------------------------------------------------------------// Email-attachment documents: access inherited from the linked thread// (inbox owner, macro_user_links delegation, or a thread-level grant)