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.

perf(soup): route unfiltered item pages through the dynamic builder (#6333)

Will Hutchinsond8fdfbb2f29a
diff --git a/.sqlx/query-58dcd8c3231ed3681efcc9e6acbdd0641d02b34f48e35b2aac077b2fe16a00db.json b/.sqlx/query-58dcd8c3231ed3681efcc9e6acbdd0641d02b34f48e35b2aac077b2fe16a00db.json
deleted file mode 100644
index 997fa0ea6e..0000000000
--- a/.sqlx/query-58dcd8c3231ed3681efcc9e6acbdd0641d02b34f48e35b2aac077b2fe16a00db.json
+++ /dev/null
@@ -1,166 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": " \n WITH user_source_ids AS (\n SELECT cp.channel_id::text as source_id FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL\n SELECT t.team_id::text FROM team_user t\n WHERE t.user_id = $1\n UNION ALL\n SELECT $1\n ),\n UserAccessibleItems AS (\n SELECT DISTINCT\n ea.entity_id::text as item_id,\n ea.entity_type as item_type\n FROM entity_access ea\n WHERE ea.source_id = ANY(SELECT source_id FROM user_source_ids)\n ),\n Combined AS (\n SELECT\n 'document' as \"item_type!\",\n d.id as \"id!\",\n CAST(COALESCE(di.id, db.id) as TEXT) as \"document_version_id\",\n d.owner as \"user_id!\",\n d.name as \"name!\",\n d.\"branchedFromId\" as \"branched_from_id\",\n d.\"branchedFromVersionId\" as \"branched_from_version_id\",\n d.\"documentFamilyId\" as \"document_family_id\",\n d.\"fileType\" as \"file_type\",\n d.\"createdAt\"::timestamptz as \"created_at!\",\n d.\"updatedAt\"::timestamptz as \"updated_at!\",\n d.\"projectId\" as \"project_id\",\n NULL as \"is_persistent\",\n di.sha as \"sha\",\n dt.sub_type as \"sub_type?: DocumentSubType\",\n EXISTS (\n SELECT 1\n FROM document_email de\n WHERE de.document_id = d.id\n ) as \"is_email_attachment!\",\n (\n dt.sub_type IS DISTINCT FROM 'task'\n OR EXISTS (\n SELECT 1\n FROM entity_properties ep_assignees\n WHERE ep_assignees.entity_id = d.id\n AND ep_assignees.entity_type = 'TASK'\n AND ep_assignees.property_definition_id = $8\n AND ep_assignees.values->'value' @> jsonb_build_array(\n jsonb_build_object('entity_id', $1)\n )\n )\n ) as \"is_important!\",\n ARRAY(\n SELECT status_option_id::uuid\n FROM jsonb_array_elements_text(\n CASE\n WHEN jsonb_typeof(ep_status.values->'value') = 'array'\n THEN ep_status.values->'value'\n ELSE '[]'::jsonb\n END\n ) AS status_option_id\n ) as \"status_option_ids!: Vec<Uuid>\",\n uh.\"updatedAt\"::timestamptz as \"viewed_at\",\n CASE $2\n WHEN 'viewed_updated' THEN COALESCE(uh.\"updatedAt\", d.\"updatedAt\")\n WHEN 'viewed_at' THEN COALESCE(uh.\"updatedAt\", '1970-01-01 00:00:00+00')\n WHEN 'created_at' THEN d.\"createdAt\"\n ELSE d.\"updatedAt\"\n END::timestamptz as \"sort_ts!\",\n CASE\n WHEN dt.sub_type = 'task'\n AND ep_status.values->'value' ? $6\n THEN true\n WHEN dt.sub_type = 'task'\n THEN false\n ELSE NULL\n END as \"is_completed\",\n d.\"deletedAt\"::timestamptz as \"deleted_at\"\n FROM \"Document\" d\n LEFT JOIN document_sub_type dt ON dt.document_id = d.id\n LEFT JOIN entity_properties ep_status\n ON dt.sub_type = 'task'\n AND ep_status.entity_id = d.id\n AND ep_status.entity_type = 'TASK'\n AND ep_status.property_definition_id = $7\n INNER JOIN UserAccessibleItems uai ON uai.item_id = d.id AND uai.item_type = 'document'\n -- This MUST be a LEFT JOIN to support all three sort methods\n LEFT JOIN \"UserHistory\" uh ON uh.\"itemId\" = d.id AND uh.\"itemType\" = 'document' AND uh.\"userId\" = $1\n LEFT JOIN LATERAL (\n SELECT b.id\n FROM \"DocumentBom\" b\n WHERE b.\"documentId\" = d.id\n ORDER BY b.\"createdAt\" DESC\n LIMIT 1\n ) db ON true\n LEFT JOIN LATERAL (\n SELECT i.id, i.sha\n FROM \"DocumentInstance\" i\n WHERE i.\"documentId\" = d.id\n ORDER BY i.\"updatedAt\" DESC\n LIMIT 1\n ) di ON true\n WHERE d.\"deletedAt\" IS NULL\n\n UNION ALL\n\n SELECT\n 'chat' as \"item_type!\",\n c.id as \"id!\",\n NULL as \"document_version_id\",\n c.\"userId\" as \"user_id!\",\n c.name as \"name!\",\n NULL as \"branched_from_id\",\n NULL as \"branched_from_version_id\",\n NULL as \"document_family_id\",\n NULL as \"file_type\",\n c.\"createdAt\"::timestamptz as \"created_at!\",\n c.\"updatedAt\"::timestamptz as \"updated_at!\",\n c.\"projectId\" as \"project_id\",\n c.\"isPersistent\" as \"is_persistent\",\n NULL as \"sha\",\n NULL as \"sub_type\",\n false as \"is_email_attachment!\",\n true as \"is_important!\",\n ARRAY[]::uuid[] as \"status_option_ids!: Vec<Uuid>\",\n uh.\"updatedAt\"::timestamptz as \"viewed_at\",\n CASE $2\n WHEN 'viewed_updated' THEN COALESCE(uh.\"updatedAt\", c.\"updatedAt\")\n WHEN 'viewed_at' THEN COALESCE(uh.\"updatedAt\", '1970-01-01 00:00:00+00')\n WHEN 'created_at' THEN c.\"createdAt\"\n ELSE c.\"updatedAt\"\n END::timestamptz as \"sort_ts!\",\n NULL as \"is_completed\",\n c.\"deletedAt\"::timestamptz as \"deleted_at\"\n FROM \"Chat\" c\n INNER JOIN UserAccessibleItems uai ON uai.item_id = c.id AND uai.item_type = 'chat'\n LEFT JOIN \"UserHistory\" uh ON uh.\"itemId\" = c.id AND uh.\"itemType\" = 'chat' AND uh.\"userId\" = $1\n WHERE c.\"deletedAt\" IS NULL\n\n UNION ALL\n\n SELECT\n 'project' as \"item_type!\",\n p.id as \"id!\",\n NULL as \"document_version_id\",\n p.\"userId\" as \"user_id!\",\n p.name as \"name!\",\n NULL as \"branched_from_id\",\n NULL as \"branched_from_version_id\",\n NULL as \"document_family_id\",\n NULL as \"file_type\",\n p.\"createdAt\"::timestamptz as \"created_at!\",\n p.\"updatedAt\"::timestamptz as \"updated_at!\",\n p.\"parentId\" as \"project_id\",\n NULL as \"is_persistent\",\n NULL as \"sha\",\n NULL as \"sub_type\",\n false as \"is_email_attachment!\",\n true as \"is_important!\",\n ARRAY[]::uuid[] as \"status_option_ids!: Vec<Uuid>\",\n uh.\"updatedAt\"::timestamptz as \"viewed_at\",\n CASE $2\n WHEN 'viewed_updated' THEN COALESCE(uh.\"updatedAt\", p.\"updatedAt\")\n WHEN 'viewed_at' THEN COALESCE(uh.\"updatedAt\", '1970-01-01 00:00:00+00')\n WHEN 'created_at' THEN p.\"createdAt\"\n ELSE p.\"updatedAt\"\n END::timestamptz as \"sort_ts!\",\n NULL as \"is_completed\",\n p.\"deletedAt\"::timestamptz as \"deleted_at\"\n FROM \"Project\" p\n INNER JOIN UserAccessibleItems uai\n ON uai.item_id = p.id\n AND uai.item_type = 'project'\n LEFT JOIN \"UserHistory\" uh\n ON uh.\"itemId\" = p.id\n AND uh.\"itemType\" = 'project'\n AND uh.\"userId\" = $1\n WHERE p.\"deletedAt\" IS NULL\n )\n SELECT Combined.* FROM Combined\n LEFT JOIN frecency_aggregates fa\n ON fa.entity_id = Combined.\"id!\"\n AND fa.entity_type = Combined.\"item_type!\"\n AND fa.user_id = $1\n WHERE fa.id IS NULL\n AND (\n ($4::timestamptz IS NULL)\n OR\n (Combined.\"sort_ts!\", Combined.\"id!\"::text) < ($4, $5)\n )\n ORDER BY Combined.\"sort_ts!\" DESC, Combined.\"id!\" DESC\n LIMIT $3\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "item_type!",
- "type_info": "Text"
- },
- {
- "ordinal": 1,
- "name": "id!",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "document_version_id",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "user_id!",
- "type_info": "Text"
- },
- {
- "ordinal": 4,
- "name": "name!",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "branched_from_id",
- "type_info": "Text"
- },
- {
- "ordinal": 6,
- "name": "branched_from_version_id",
- "type_info": "Int8"
- },
- {
- "ordinal": 7,
- "name": "document_family_id",
- "type_info": "Int8"
- },
- {
- "ordinal": 8,
- "name": "file_type",
- "type_info": "Text"
- },
- {
- "ordinal": 9,
- "name": "created_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 10,
- "name": "updated_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 11,
- "name": "project_id",
- "type_info": "Text"
- },
- {
- "ordinal": 12,
- "name": "is_persistent",
- "type_info": "Bool"
- },
- {
- "ordinal": 13,
- "name": "sha",
- "type_info": "Text"
- },
- {
- "ordinal": 14,
- "name": "sub_type?: DocumentSubType",
- "type_info": {
- "Custom": {
- "name": "document_sub_type_value",
- "kind": {
- "Enum": [
- "task",
- "snippet",
- "skill"
- ]
- }
- }
- }
- },
- {
- "ordinal": 15,
- "name": "is_email_attachment!",
- "type_info": "Bool"
- },
- {
- "ordinal": 16,
- "name": "is_important!",
- "type_info": "Bool"
- },
- {
- "ordinal": 17,
- "name": "status_option_ids!: Vec<Uuid>",
- "type_info": "UuidArray"
- },
- {
- "ordinal": 18,
- "name": "viewed_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 19,
- "name": "sort_ts!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 20,
- "name": "is_completed",
- "type_info": "Bool"
- },
- {
- "ordinal": 21,
- "name": "deleted_at",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Text",
- "Int8",
- "Timestamptz",
- "Text",
- "Text",
- "Uuid",
- "Uuid"
- ]
- },
- "nullable": [
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null
- ]
- },
- "hash": "58dcd8c3231ed3681efcc9e6acbdd0641d02b34f48e35b2aac077b2fe16a00db"
-}
diff --git a/.sqlx/query-c22f782cbd620dc831763981afc9fc9e83c8dbf3629271b5f678eefcce9d9ada.json b/.sqlx/query-c22f782cbd620dc831763981afc9fc9e83c8dbf3629271b5f678eefcce9d9ada.json
deleted file mode 100644
index 2d57d08760..0000000000
--- a/.sqlx/query-c22f782cbd620dc831763981afc9fc9e83c8dbf3629271b5f678eefcce9d9ada.json
+++ /dev/null
@@ -1,166 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n -- =============================================================================\n -- EXPANDED GENERIC CURSOR SOUP QUERY\n -- =============================================================================\n -- Retrieves all items (documents, chats, projects) that a user has access to,\n -- including both explicit permissions and inherited permissions through the\n -- project hierarchy.\n -- =============================================================================\n\n WITH user_source_ids AS (\n SELECT cp.channel_id::text as source_id FROM comms_channel_participants cp\n WHERE cp.user_id = $1 AND cp.left_at IS NULL\n UNION ALL\n SELECT t.team_id::text FROM team_user t\n WHERE t.user_id = $1\n UNION ALL\n SELECT $1\n ),\n\n UserAccessibleItems AS (\n SELECT DISTINCT\n ea.entity_id::text as item_id,\n ea.entity_type as item_type\n FROM entity_access ea\n WHERE ea.source_id = ANY(SELECT source_id FROM user_source_ids)\n ),\n\n -- Identify the top N items with minimal columns before joining full details.\n -- Performs early filtering and sorting to reduce the data processed in\n -- subsequent joins.\n TopItems AS (\n SELECT item_type, id, sort_ts, updated_at FROM (\n SELECT\n 'document'::text as item_type,\n d.id,\n -- Dynamic sort column based on user's selected sort method\n CASE $2\n WHEN 'viewed_updated' THEN COALESCE(uh.\"updatedAt\", d.\"updatedAt\")\n WHEN 'viewed_at' THEN COALESCE(uh.\"updatedAt\", '1970-01-01 00:00:00+00')\n WHEN 'created_at' THEN d.\"createdAt\"\n ELSE d.\"updatedAt\"\n END::timestamptz as sort_ts,\n d.\"updatedAt\"::timestamptz as updated_at\n FROM \"Document\" d\n INNER JOIN UserAccessibleItems uai ON uai.item_id = d.id AND uai.item_type = 'document'\n LEFT JOIN \"UserHistory\" uh ON uh.\"itemId\" = d.id AND uh.\"itemType\" = 'document' AND uh.\"userId\" = $1\n WHERE d.\"deletedAt\" IS NULL\n\n UNION ALL\n\n SELECT\n 'chat'::text,\n c.id,\n CASE $2\n WHEN 'viewed_updated' THEN COALESCE(uh.\"updatedAt\", c.\"updatedAt\")\n WHEN 'viewed_at' THEN COALESCE(uh.\"updatedAt\", '1970-01-01 00:00:00+00')\n WHEN 'created_at' THEN c.\"createdAt\"\n ELSE c.\"updatedAt\"\n END::timestamptz,\n c.\"updatedAt\"::timestamptz\n FROM \"Chat\" c\n INNER JOIN UserAccessibleItems uai ON uai.item_id = c.id AND uai.item_type = 'chat'\n LEFT JOIN \"UserHistory\" uh ON uh.\"itemId\" = c.id AND uh.\"itemType\" = 'chat' AND uh.\"userId\" = $1\n WHERE c.\"deletedAt\" IS NULL\n\n UNION ALL\n\n SELECT\n 'project'::text,\n p.id,\n CASE $2\n WHEN 'viewed_updated' THEN COALESCE(uh.\"updatedAt\", p.\"updatedAt\")\n WHEN 'viewed_at' THEN COALESCE(uh.\"updatedAt\", '1970-01-01 00:00:00+00')\n WHEN 'created_at' THEN p.\"createdAt\"\n ELSE p.\"updatedAt\"\n END::timestamptz,\n p.\"updatedAt\"::timestamptz\n FROM \"Project\" p\n INNER JOIN UserAccessibleItems uai ON uai.item_id = p.id AND uai.item_type = 'project'\n LEFT JOIN \"UserHistory\" uh ON uh.\"itemId\" = p.id AND uh.\"itemType\" = 'project' AND uh.\"userId\" = $1\n WHERE p.\"deletedAt\" IS NULL\n ) all_items\n WHERE\n -- Cursor-based pagination: skip items we've already seen.\n -- NULL cursor means first page (no items to skip).\n ($4::timestamptz IS NULL)\n OR\n -- Seek method: find items \"before\" the cursor position\n -- using (sort_ts, id) tuple comparison for deterministic ordering\n (sort_ts, id::text) < ($4, $5)\n ORDER BY sort_ts DESC, id DESC\n LIMIT $3\n )\n\n -- Join full item details only for the filtered top items.\n SELECT * FROM (\n SELECT\n 'document' as \"item_type!\",\n d.id as \"id!\",\n CAST(COALESCE(di.id, db.id) as TEXT) as \"document_version_id\",\n d.owner as \"user_id!\",\n d.name as \"name!\",\n d.\"branchedFromId\" as \"branched_from_id\",\n d.\"branchedFromVersionId\" as \"branched_from_version_id\",\n d.\"documentFamilyId\" as \"document_family_id\",\n d.\"fileType\" as \"file_type\",\n d.\"createdAt\"::timestamptz as \"created_at!\",\n d.\"updatedAt\"::timestamptz as \"updated_at!\",\n d.\"projectId\" as \"project_id\",\n NULL as \"is_persistent\",\n di.sha as \"sha\",\n dt.sub_type as \"sub_type?: DocumentSubType\",\n EXISTS (\n SELECT 1\n FROM document_email de\n WHERE de.document_id = d.id\n ) as \"is_email_attachment!\",\n (\n dt.sub_type IS DISTINCT FROM 'task'\n OR EXISTS (\n SELECT 1\n FROM entity_properties ep_assignees\n WHERE ep_assignees.entity_id = d.id\n AND ep_assignees.entity_type = 'TASK'\n AND ep_assignees.property_definition_id = $8\n AND ep_assignees.values->'value' @> jsonb_build_array(\n jsonb_build_object('entity_id', $1)\n )\n )\n ) as \"is_important!\",\n ARRAY(\n SELECT status_option_id::uuid\n FROM jsonb_array_elements_text(\n CASE\n WHEN jsonb_typeof(ep_status.values->'value') = 'array'\n THEN ep_status.values->'value'\n ELSE '[]'::jsonb\n END\n ) AS status_option_id\n ) as \"status_option_ids!: Vec<Uuid>\",\n uh.\"updatedAt\"::timestamptz as \"viewed_at\",\n t.sort_ts as \"sort_ts!\",\n -- Task completion status: check if status property matches \"completed\"\n CASE\n WHEN dt.sub_type = 'task'\n AND ep_status.values->'value' ? $6\n THEN true\n WHEN dt.sub_type = 'task'\n THEN false\n ELSE NULL\n END as \"is_completed\",\n d.\"deletedAt\"::timestamptz as \"deleted_at\"\n FROM TopItems t\n INNER JOIN \"Document\" d ON d.id = t.id\n LEFT JOIN document_sub_type dt ON dt.document_id = d.id\n LEFT JOIN entity_properties ep_status\n ON dt.sub_type = 'task'\n AND ep_status.entity_id = d.id\n AND ep_status.entity_type = 'TASK'\n AND ep_status.property_definition_id = $7\n LEFT JOIN \"UserHistory\" uh\n ON uh.\"itemId\" = d.id AND uh.\"itemType\" = 'document' AND uh.\"userId\" = $1\n -- LATERAL joins to get the latest version info\n LEFT JOIN LATERAL (\n SELECT b.id\n FROM \"DocumentBom\" b\n WHERE b.\"documentId\" = d.id\n ORDER BY b.\"createdAt\" DESC\n LIMIT 1\n ) db ON true\n LEFT JOIN LATERAL (\n SELECT i.id, i.sha\n FROM \"DocumentInstance\" i\n WHERE i.\"documentId\" = d.id\n ORDER BY i.\"updatedAt\" DESC\n LIMIT 1\n ) di ON true\n WHERE t.item_type = 'document'\n\n UNION ALL\n\n SELECT\n 'chat' as \"item_type!\",\n c.id as \"id!\",\n NULL as \"document_version_id\",\n c.\"userId\" as \"user_id!\",\n c.name as \"name!\",\n NULL as \"branched_from_id\",\n NULL as \"branched_from_version_id\",\n NULL as \"document_family_id\",\n NULL as \"file_type\",\n c.\"createdAt\"::timestamptz as \"created_at!\",\n c.\"updatedAt\"::timestamptz as \"updated_at!\",\n c.\"projectId\" as \"project_id\",\n c.\"isPersistent\" as \"is_persistent\",\n NULL as \"sha\",\n NULL as \"sub_type\",\n false as \"is_email_attachment!\",\n true as \"is_important!\",\n ARRAY[]::uuid[] as \"status_option_ids!: Vec<Uuid>\",\n uh.\"updatedAt\"::timestamptz as \"viewed_at\",\n t.sort_ts as \"sort_ts!\",\n NULL as \"is_completed\",\n c.\"deletedAt\"::timestamptz as \"deleted_at\"\n FROM TopItems t\n INNER JOIN \"Chat\" c ON c.id = t.id\n LEFT JOIN \"UserHistory\" uh\n ON uh.\"itemId\" = c.id AND uh.\"itemType\" = 'chat' AND uh.\"userId\" = $1\n WHERE t.item_type = 'chat'\n\n UNION ALL\n\n SELECT\n 'project' as \"item_type!\",\n p.id as \"id!\",\n NULL as \"document_version_id\",\n p.\"userId\" as \"user_id!\",\n p.name as \"name!\",\n NULL as \"branched_from_id\",\n NULL as \"branched_from_version_id\",\n NULL as \"document_family_id\",\n NULL as \"file_type\",\n p.\"createdAt\"::timestamptz as \"created_at!\",\n p.\"updatedAt\"::timestamptz as \"updated_at!\",\n p.\"parentId\" as \"project_id\",\n NULL as \"is_persistent\",\n NULL as \"sha\",\n NULL as \"sub_type\",\n false as \"is_email_attachment!\",\n true as \"is_important!\",\n ARRAY[]::uuid[] as \"status_option_ids!: Vec<Uuid>\",\n uh.\"updatedAt\"::timestamptz as \"viewed_at\",\n t.sort_ts as \"sort_ts!\",\n NULL as \"is_completed\",\n p.\"deletedAt\"::timestamptz as \"deleted_at\"\n FROM TopItems t\n INNER JOIN \"Project\" p ON p.id = t.id\n LEFT JOIN \"UserHistory\" uh\n ON uh.\"itemId\" = p.id AND uh.\"itemType\" = 'project' AND uh.\"userId\" = $1\n WHERE t.item_type = 'project'\n ) Combined\n -- Sort by timestamp descending, with ID as tiebreaker for deterministic pagination.\n -- This ensures consistent ordering when multiple items share the same timestamp,\n -- preventing items from being skipped or duplicated across pages.\n ORDER BY \"sort_ts!\" DESC, \"id!\" DESC\n LIMIT $3\n",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "item_type!",
- "type_info": "Text"
- },
- {
- "ordinal": 1,
- "name": "id!",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "document_version_id",
- "type_info": "Text"
- },
- {
- "ordinal": 3,
- "name": "user_id!",
- "type_info": "Text"
- },
- {
- "ordinal": 4,
- "name": "name!",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "branched_from_id",
- "type_info": "Text"
- },
- {
- "ordinal": 6,
- "name": "branched_from_version_id",
- "type_info": "Int8"
- },
- {
- "ordinal": 7,
- "name": "document_family_id",
- "type_info": "Int8"
- },
- {
- "ordinal": 8,
- "name": "file_type",
- "type_info": "Text"
- },
- {
- "ordinal": 9,
- "name": "created_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 10,
- "name": "updated_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 11,
- "name": "project_id",
- "type_info": "Text"
- },
- {
- "ordinal": 12,
- "name": "is_persistent",
- "type_info": "Bool"
- },
- {
- "ordinal": 13,
- "name": "sha",
- "type_info": "Text"
- },
- {
- "ordinal": 14,
- "name": "sub_type?: DocumentSubType",
- "type_info": {
- "Custom": {
- "name": "document_sub_type_value",
- "kind": {
- "Enum": [
- "task",
- "snippet",
- "skill"
- ]
- }
- }
- }
- },
- {
- "ordinal": 15,
- "name": "is_email_attachment!",
- "type_info": "Bool"
- },
- {
- "ordinal": 16,
- "name": "is_important!",
- "type_info": "Bool"
- },
- {
- "ordinal": 17,
- "name": "status_option_ids!: Vec<Uuid>",
- "type_info": "UuidArray"
- },
- {
- "ordinal": 18,
- "name": "viewed_at",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 19,
- "name": "sort_ts!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 20,
- "name": "is_completed",
- "type_info": "Bool"
- },
- {
- "ordinal": 21,
- "name": "deleted_at",
- "type_info": "Timestamptz"
- }
- ],
- "parameters": {
- "Left": [
- "Text",
- "Text",
- "Int8",
- "Timestamptz",
- "Text",
- "Text",
- "Uuid",
- "Uuid"
- ]
- },
- "nullable": [
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null
- ]
- },
- "hash": "c22f782cbd620dc831763981afc9fc9e83c8dbf3629271b5f678eefcce9d9ada"
-}
diff --git a/crates/macro_db_client/migrations/20260910144339_document_instance_document_id_updated_at_index.sql b/crates/macro_db_client/migrations/20260910144339_document_instance_document_id_updated_at_index.sql
new file mode 100644
index 0000000000..6435507569
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260910144339_document_instance_document_id_updated_at_index.sql
@@ -0,0 +1,16 @@
+-- no-transaction
+-- The soup detail stage resolves each row's current instance with
+-- SELECT i.id, i.sha FROM "DocumentInstance" i
+-- WHERE i."documentId" = d.id ORDER BY i."updatedAt" DESC LIMIT 1
+-- (crates/soup, crates/projects). The only ordered index on this table is
+-- idx_document_instance_document_id_created_at, which orders by "createdAt" --
+-- a different column -- so every lateral reads all of a document's instances
+-- and sorts them to return one row.
+-- Must stay a single statement: sqlx sends no-transaction migrations as one
+-- simple-query batch, and a multi-statement batch gets an implicit
+-- transaction, which CONCURRENTLY forbids. If an interrupted deploy leaves an
+-- INVALID index behind, IF NOT EXISTS will not rebuild it -- drop it by hand
+-- and re-run. Check with:
+-- SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid;
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_document_instance_document_id_updated_at
+ ON "DocumentInstance" ("documentId", "updatedAt" DESC);
diff --git a/crates/macro_db_client/migrations/20260910144340_document_bom_document_id_created_at_index.sql b/crates/macro_db_client/migrations/20260910144340_document_bom_document_id_created_at_index.sql
new file mode 100644
index 0000000000..fe82cbad91
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260910144340_document_bom_document_id_created_at_index.sql
@@ -0,0 +1,9 @@
+-- no-transaction
+-- Same defect as idx_document_instance_document_id_updated_at, one table over:
+-- SELECT b.id FROM "DocumentBom" b
+-- WHERE b."documentId" = d.id ORDER BY b."createdAt" DESC LIMIT 1
+-- runs once per soup detail row against a bare ("documentId") index, so the
+-- sort is unavoidable without the timestamp in the index.
+-- Single statement: see 20260910144339 for the CONCURRENTLY constraint.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_document_bom_document_id_created_at
+ ON "DocumentBom" ("documentId", "createdAt" DESC);
diff --git a/crates/soup/src/outbound/pg_soup_repo.rs b/crates/soup/src/outbound/pg_soup_repo.rs
index b1e2899993..1c96d24119 100644
--- a/crates/soup/src/outbound/pg_soup_repo.rs
+++ b/crates/soup/src/outbound/pg_soup_repo.rs
@@ -11,6 +11,7 @@ use crate::{
ExpandedDynamicCursorArgs, GroupedDynamicCursorArgs,
},
};
+use item_filters::ast::EntityFilterAst;
use macro_user_id::user_id::MacroUserIdStr;
use models_pagination::{Identify, SortOn};
use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;
@@ -87,21 +88,36 @@ impl SoupRepo for PgSoupRepo {
)
.await?
}
+ // The unfiltered arms route through the same dynamic builder as
+ // the filtered ones. An empty `EntityFilterAst` folds to no
+ // predicates and keeps all three entity arms, so the SQL is
+ // semantically identical to the hand-written queries this
+ // replaced — but it gets their shape: a lightweight `TopItems`
+ // stage that applies the cursor and LIMIT before any detail join,
+ // a flattenable per-arm `entity_access` semi-join instead of a
+ // materialised whole-corpus CTE, and a sort expression fixed at
+ // build time so the ORDER BY can be served by an index.
SimpleSortQuery::FilterFrecency(f) => {
- expanded::by_cursor::no_frecency_expanded_generic_soup_with_projection(
+ expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
&self.pool.0,
- req.user_id,
- req.limit,
- f,
+ ExpandedDynamicCursorArgs {
+ user_id: req.user_id,
+ limit: req.limit,
+ cursor: f.map_filter(|_| EntityFilterAst::default()),
+ exclude_frecency: true,
+ },
)
.await?
}
SimpleSortQuery::NoFilter(f) => {
- expanded::by_cursor::expanded_generic_cursor_soup_with_projection(
+ expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
&self.pool.0,
- req.user_id,
- req.limit,
- f,
+ ExpandedDynamicCursorArgs {
+ user_id: req.user_id,
+ limit: req.limit,
+ cursor: f.map_filter(|_| EntityFilterAst::default()),
+ exclude_frecency: false,
+ },
)
.await?
}
@@ -129,14 +145,22 @@ impl SoupRepo for PgSoupRepo {
let mut items = match req.cursor {
SimpleSortQuery::ItemsFilter(_) => not_implemented(req).await?,
SimpleSortQuery::ItemsAndFrecencyFilter(_) => not_implemented(req).await?,
+ // Same substitution as the expanded path above; this arm has
+ // always answered with the expanded query.
SimpleSortQuery::FilterFrecency(f) => {
- expanded::by_cursor::no_frecency_expanded_generic_soup(
+ expanded::dynamic::expanded_dynamic_cursor_soup_with_projection(
&self.pool.0,
- req.user_id,
- req.limit,
- f,
+ ExpandedDynamicCursorArgs {
+ user_id: req.user_id,
+ limit: req.limit,
+ cursor: f.map_filter(|_| EntityFilterAst::default()),
+ exclude_frecency: true,
+ },
)
.await?
+ .into_iter()
+ .map(|hydration| hydration.item)
+ .collect()
}
SimpleSortQuery::NoFilter(f) => {
unexpanded::by_cursor::unexpanded_generic_cursor_soup(
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded.rs b/crates/soup/src/outbound/pg_soup_repo/expanded.rs
index 7c92f5d6d5..20ef2cb8d8 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded.rs
@@ -1,6 +1,5 @@
#[cfg(test)]
mod tests;
-pub(super) mod by_cursor;
pub(super) mod by_ids;
pub(super) mod dynamic;
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/by_cursor.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/by_cursor.rs
deleted file mode 100644
index 490d44a92a..0000000000
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/by_cursor.rs
+++ /dev/null
@@ -1,585 +0,0 @@
-use crate::domain::models::SoupProjectionHydration;
-use crate::map_soup_projection_hydration;
-use crate::outbound::pg_soup_repo::type_err;
-use document_sub_type::DocumentSubType;
-use macro_user_id::cowlike::CowLike;
-use macro_user_id::user_id::MacroUserIdStr;
-use models_pagination::{Frecency, Query, SimpleSortMethod};
-use models_soup::item::SoupItem;
-use sqlx::PgPool;
-use std::str::FromStr;
-use system_properties::{StatusOption, SystemPropertyKey};
-use uuid::Uuid;
-
-/// Returns objects that a user has EXPLICIT and IMPLICIT access to.
-///
-/// This function returns all items the user can access, including those with inherited
-/// permissions through project hierarchy. If a user has access to a project, all items
-/// within that project (and its sub-projects) WILL be included in the results, even if
-/// the user doesn't have explicit permissions on those items.
-/// Sorting is dynamically controlled by the sort_method parameter.
-#[tracing::instrument(skip(db, limit))]
-async fn expanded_generic_cursor_soup_hydrated(
- db: &PgPool,
- user_id: MacroUserIdStr<'_>,
- limit: u16,
- cursor: Query<Uuid, SimpleSortMethod, ()>,
-) -> Result<Vec<SoupProjectionHydration>, sqlx::Error> {
- let query_limit = limit as i64;
- let sort_method_str = cursor.sort_method().to_string();
- let (cursor_id, cursor_timestamp) = cursor.vals();
- let cursor_id = cursor_id.as_ref().map(|u| u.to_string());
-
- let status_property_id = SystemPropertyKey::STATUS_UUID;
- let assignees_property_id = SystemPropertyKey::ASSIGNEES_UUID;
- let completed_option_id = StatusOption::COMPLETED_UUID.to_string();
-
- let items: Vec<SoupProjectionHydration> = sqlx::query!(
-r#"
- -- =============================================================================
- -- EXPANDED GENERIC CURSOR SOUP QUERY
- -- =============================================================================
- -- Retrieves all items (documents, chats, projects) that a user has access to,
- -- including both explicit permissions and inherited permissions through the
- -- project hierarchy.
- -- =============================================================================
-
- WITH user_source_ids AS (
- SELECT cp.channel_id::text as source_id FROM comms_channel_participants cp
- WHERE cp.user_id = $1 AND cp.left_at IS NULL
- UNION ALL
- SELECT t.team_id::text FROM team_user t
- WHERE t.user_id = $1
- UNION ALL
- SELECT $1
- ),
-
- UserAccessibleItems AS (
- SELECT DISTINCT
- ea.entity_id::text as item_id,
- ea.entity_type as item_type
- FROM entity_access ea
- WHERE ea.source_id = ANY(SELECT source_id FROM user_source_ids)
- ),
-
- -- Identify the top N items with minimal columns before joining full details.
- -- Performs early filtering and sorting to reduce the data processed in
- -- subsequent joins.
- TopItems AS (
- SELECT item_type, id, sort_ts, updated_at FROM (
- SELECT
- 'document'::text as item_type,
- d.id,
- -- Dynamic sort column based on user's selected sort method
- CASE $2
- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", d."updatedAt")
- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')
- WHEN 'created_at' THEN d."createdAt"
- ELSE d."updatedAt"
- END::timestamptz as sort_ts,
- d."updatedAt"::timestamptz as updated_at
- FROM "Document" d
- INNER JOIN UserAccessibleItems uai ON uai.item_id = d.id AND uai.item_type = 'document'
- LEFT JOIN "UserHistory" uh ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1
- WHERE d."deletedAt" IS NULL
-
- UNION ALL
-
- SELECT
- 'chat'::text,
- c.id,
- CASE $2
- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", c."updatedAt")
- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')
- WHEN 'created_at' THEN c."createdAt"
- ELSE c."updatedAt"
- END::timestamptz,
- c."updatedAt"::timestamptz
- FROM "Chat" c
- INNER JOIN UserAccessibleItems uai ON uai.item_id = c.id AND uai.item_type = 'chat'
- LEFT JOIN "UserHistory" uh ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1
- WHERE c."deletedAt" IS NULL
-
- UNION ALL
-
- SELECT
- 'project'::text,
- p.id,
- CASE $2
- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", p."updatedAt")
- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')
- WHEN 'created_at' THEN p."createdAt"
- ELSE p."updatedAt"
- END::timestamptz,
- p."updatedAt"::timestamptz
- FROM "Project" p
- INNER JOIN UserAccessibleItems uai ON uai.item_id = p.id AND uai.item_type = 'project'
- LEFT JOIN "UserHistory" uh ON uh."itemId" = p.id AND uh."itemType" = 'project' AND uh."userId" = $1
- WHERE p."deletedAt" IS NULL
- ) all_items
- WHERE
- -- Cursor-based pagination: skip items we've already seen.
- -- NULL cursor means first page (no items to skip).
- ($4::timestamptz IS NULL)
- OR
- -- Seek method: find items "before" the cursor position
- -- using (sort_ts, id) tuple comparison for deterministic ordering
- (sort_ts, id::text) < ($4, $5)
- ORDER BY sort_ts DESC, id DESC
- LIMIT $3
- )
-
- -- Join full item details only for the filtered top items.
- SELECT * FROM (
- SELECT
- 'document' as "item_type!",
- d.id as "id!",
- CAST(COALESCE(di.id, db.id) as TEXT) as "document_version_id",
- d.owner as "user_id!",
- d.name as "name!",
- d."branchedFromId" as "branched_from_id",
- d."branchedFromVersionId" as "branched_from_version_id",
- d."documentFamilyId" as "document_family_id",
- d."fileType" as "file_type",
- d."createdAt"::timestamptz as "created_at!",
- d."updatedAt"::timestamptz as "updated_at!",
- d."projectId" as "project_id",
- NULL as "is_persistent",
- di.sha as "sha",
- dt.sub_type as "sub_type?: DocumentSubType",
- EXISTS (
- SELECT 1
- FROM document_email de
- WHERE de.document_id = d.id
- ) as "is_email_attachment!",
- (
- dt.sub_type IS DISTINCT FROM 'task'
- OR EXISTS (
- SELECT 1
- FROM entity_properties ep_assignees
- WHERE ep_assignees.entity_id = d.id
- AND ep_assignees.entity_type = 'TASK'
- AND ep_assignees.property_definition_id = $8
- AND ep_assignees.values->'value' @> jsonb_build_array(
- jsonb_build_object('entity_id', $1)
- )
- )
- ) as "is_important!",
- ARRAY(
- SELECT status_option_id::uuid
- FROM jsonb_array_elements_text(
- CASE
- WHEN jsonb_typeof(ep_status.values->'value') = 'array'
- THEN ep_status.values->'value'
- ELSE '[]'::jsonb
- END
- ) AS status_option_id
- ) as "status_option_ids!: Vec<Uuid>",
- uh."updatedAt"::timestamptz as "viewed_at",
- t.sort_ts as "sort_ts!",
- -- Task completion status: check if status property matches "completed"
- CASE
- WHEN dt.sub_type = 'task'
- AND ep_status.values->'value' ? $6
- THEN true
- WHEN dt.sub_type = 'task'
- THEN false
- ELSE NULL
- END as "is_completed",
- d."deletedAt"::timestamptz as "deleted_at"
- FROM TopItems t
- INNER JOIN "Document" d ON d.id = t.id
- LEFT JOIN document_sub_type dt ON dt.document_id = d.id
- LEFT JOIN entity_properties ep_status
- ON dt.sub_type = 'task'
- AND ep_status.entity_id = d.id
- AND ep_status.entity_type = 'TASK'
- AND ep_status.property_definition_id = $7
- LEFT JOIN "UserHistory" uh
- ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1
- -- LATERAL joins to get the latest version info
- LEFT JOIN LATERAL (
- SELECT b.id
- FROM "DocumentBom" b
- WHERE b."documentId" = d.id
- ORDER BY b."createdAt" DESC
- LIMIT 1
- ) db ON true
- LEFT JOIN LATERAL (
- SELECT i.id, i.sha
- FROM "DocumentInstance" i
- WHERE i."documentId" = d.id
- ORDER BY i."updatedAt" DESC
- LIMIT 1
- ) di ON true
- WHERE t.item_type = 'document'
-
- UNION ALL
-
- SELECT
- 'chat' as "item_type!",
- c.id as "id!",
- NULL as "document_version_id",
- c."userId" as "user_id!",
- c.name as "name!",
- NULL as "branched_from_id",
- NULL as "branched_from_version_id",
- NULL as "document_family_id",
- NULL as "file_type",
- c."createdAt"::timestamptz as "created_at!",
- c."updatedAt"::timestamptz as "updated_at!",
- c."projectId" as "project_id",
- c."isPersistent" as "is_persistent",
- NULL as "sha",
- NULL as "sub_type",
- false as "is_email_attachment!",
- true as "is_important!",
- ARRAY[]::uuid[] as "status_option_ids!: Vec<Uuid>",
- uh."updatedAt"::timestamptz as "viewed_at",
- t.sort_ts as "sort_ts!",
- NULL as "is_completed",
- c."deletedAt"::timestamptz as "deleted_at"
- FROM TopItems t
- INNER JOIN "Chat" c ON c.id = t.id
- LEFT JOIN "UserHistory" uh
- ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1
- WHERE t.item_type = 'chat'
-
- UNION ALL
-
- SELECT
- 'project' as "item_type!",
- p.id as "id!",
- NULL as "document_version_id",
- p."userId" as "user_id!",
- p.name as "name!",
- NULL as "branched_from_id",
- NULL as "branched_from_version_id",
- NULL as "document_family_id",
- NULL as "file_type",
- p."createdAt"::timestamptz as "created_at!",
- p."updatedAt"::timestamptz as "updated_at!",
- p."parentId" as "project_id",
- NULL as "is_persistent",
- NULL as "sha",
- NULL as "sub_type",
- false as "is_email_attachment!",
- true as "is_important!",
- ARRAY[]::uuid[] as "status_option_ids!: Vec<Uuid>",
- uh."updatedAt"::timestamptz as "viewed_at",
- t.sort_ts as "sort_ts!",
- NULL as "is_completed",
- p."deletedAt"::timestamptz as "deleted_at"
- FROM TopItems t
- INNER JOIN "Project" p ON p.id = t.id
- LEFT JOIN "UserHistory" uh
- ON uh."itemId" = p.id AND uh."itemType" = 'project' AND uh."userId" = $1
- WHERE t.item_type = 'project'
- ) Combined
- -- Sort by timestamp descending, with ID as tiebreaker for deterministic pagination.
- -- This ensures consistent ordering when multiple items share the same timestamp,
- -- preventing items from being skipped or duplicated across pages.
- ORDER BY "sort_ts!" DESC, "id!" DESC
- LIMIT $3
-"#,
- user_id.as_ref(), // $1
- sort_method_str, // $2
- query_limit, // $3
- cursor_timestamp, // $4
- cursor_id, // $5
- completed_option_id, // $6
- status_property_id, // $7
- assignees_property_id, // $8
- )
- .try_map(map_soup_projection_hydration!())
- .fetch_all(db)
- .await?;
-
- Ok(items)
-}
-
-/// Returns expanded objects with projection metadata loaded from the same
-/// authorized detail rows.
-#[tracing::instrument(skip(db, limit))]
-pub async fn expanded_generic_cursor_soup_with_projection(
- db: &PgPool,
- user_id: MacroUserIdStr<'_>,
- limit: u16,
- cursor: Query<Uuid, SimpleSortMethod, ()>,
-) -> Result<Vec<SoupProjectionHydration>, sqlx::Error> {
- expanded_generic_cursor_soup_hydrated(db, user_id, limit, cursor).await
-}
-
-/// Returns expanded objects while discarding internal projection metadata.
-#[cfg(test)]
-#[tracing::instrument(skip(db, limit))]
-pub async fn expanded_generic_cursor_soup(
- db: &PgPool,
- user_id: MacroUserIdStr<'_>,
- limit: u16,
- cursor: Query<Uuid, SimpleSortMethod, ()>,
-) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
- Ok(
- expanded_generic_cursor_soup_hydrated(db, user_id, limit, cursor)
- .await?
- .into_iter()
- .map(|hydration| hydration.item)
- .collect(),
- )
-}
-
-/// This is the same query as the expanded generic cursor soup except it will
-/// never return items that have frecency scores.
-#[tracing::instrument(skip(db, limit))]
-async fn no_frecency_expanded_generic_soup_hydrated(
- db: &PgPool,
- user_id: MacroUserIdStr<'_>,
- limit: u16,
- cursor: Query<Uuid, SimpleSortMethod, Frecency>,
-) -> Result<Vec<SoupProjectionHydration>, sqlx::Error> {
- let query_limit = limit as i64;
- let sort_method_str = cursor.sort_method().to_string();
- let (cursor_id, cursor_timestamp) = cursor.vals();
- let cursor_id = cursor_id.as_ref().map(|u| u.to_string());
-
- let status_property_id = SystemPropertyKey::STATUS_UUID;
- let assignees_property_id = SystemPropertyKey::ASSIGNEES_UUID;
- let completed_option_id = StatusOption::COMPLETED_UUID.to_string();
-
- let items: Vec<SoupProjectionHydration> = sqlx::query!(
-r#"
- WITH user_source_ids AS (
- SELECT cp.channel_id::text as source_id FROM comms_channel_participants cp
- WHERE cp.user_id = $1 AND cp.left_at IS NULL
- UNION ALL
- SELECT t.team_id::text FROM team_user t
- WHERE t.user_id = $1
- UNION ALL
- SELECT $1
- ),
- UserAccessibleItems AS (
- SELECT DISTINCT
- ea.entity_id::text as item_id,
- ea.entity_type as item_type
- FROM entity_access ea
- WHERE ea.source_id = ANY(SELECT source_id FROM user_source_ids)
- ),
- Combined AS (
- SELECT
- 'document' as "item_type!",
- d.id as "id!",
- CAST(COALESCE(di.id, db.id) as TEXT) as "document_version_id",
- d.owner as "user_id!",
- d.name as "name!",
- d."branchedFromId" as "branched_from_id",
- d."branchedFromVersionId" as "branched_from_version_id",
- d."documentFamilyId" as "document_family_id",
- d."fileType" as "file_type",
- d."createdAt"::timestamptz as "created_at!",
- d."updatedAt"::timestamptz as "updated_at!",
- d."projectId" as "project_id",
- NULL as "is_persistent",
- di.sha as "sha",
- dt.sub_type as "sub_type?: DocumentSubType",
- EXISTS (
- SELECT 1
- FROM document_email de
- WHERE de.document_id = d.id
- ) as "is_email_attachment!",
- (
- dt.sub_type IS DISTINCT FROM 'task'
- OR EXISTS (
- SELECT 1
- FROM entity_properties ep_assignees
- WHERE ep_assignees.entity_id = d.id
- AND ep_assignees.entity_type = 'TASK'
- AND ep_assignees.property_definition_id = $8
- AND ep_assignees.values->'value' @> jsonb_build_array(
- jsonb_build_object('entity_id', $1)
- )
- )
- ) as "is_important!",
- ARRAY(
- SELECT status_option_id::uuid
- FROM jsonb_array_elements_text(
- CASE
- WHEN jsonb_typeof(ep_status.values->'value') = 'array'
- THEN ep_status.values->'value'
- ELSE '[]'::jsonb
- END
- ) AS status_option_id
- ) as "status_option_ids!: Vec<Uuid>",
- uh."updatedAt"::timestamptz as "viewed_at",
- CASE $2
- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", d."updatedAt")
- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')
- WHEN 'created_at' THEN d."createdAt"
- ELSE d."updatedAt"
- END::timestamptz as "sort_ts!",
- CASE
- WHEN dt.sub_type = 'task'
- AND ep_status.values->'value' ? $6
- THEN true
- WHEN dt.sub_type = 'task'
- THEN false
- ELSE NULL
- END as "is_completed",
- d."deletedAt"::timestamptz as "deleted_at"
- FROM "Document" d
- LEFT JOIN document_sub_type dt ON dt.document_id = d.id
- LEFT JOIN entity_properties ep_status
- ON dt.sub_type = 'task'
- AND ep_status.entity_id = d.id
- AND ep_status.entity_type = 'TASK'
- AND ep_status.property_definition_id = $7
- INNER JOIN UserAccessibleItems uai ON uai.item_id = d.id AND uai.item_type = 'document'
- -- This MUST be a LEFT JOIN to support all three sort methods
- LEFT JOIN "UserHistory" uh ON uh."itemId" = d.id AND uh."itemType" = 'document' AND uh."userId" = $1
- LEFT JOIN LATERAL (
- SELECT b.id
- FROM "DocumentBom" b
- WHERE b."documentId" = d.id
- ORDER BY b."createdAt" DESC
- LIMIT 1
- ) db ON true
- LEFT JOIN LATERAL (
- SELECT i.id, i.sha
- FROM "DocumentInstance" i
- WHERE i."documentId" = d.id
- ORDER BY i."updatedAt" DESC
- LIMIT 1
- ) di ON true
- WHERE d."deletedAt" IS NULL
-
- UNION ALL
-
- SELECT
- 'chat' as "item_type!",
- c.id as "id!",
- NULL as "document_version_id",
- c."userId" as "user_id!",
- c.name as "name!",
- NULL as "branched_from_id",
- NULL as "branched_from_version_id",
- NULL as "document_family_id",
- NULL as "file_type",
- c."createdAt"::timestamptz as "created_at!",
- c."updatedAt"::timestamptz as "updated_at!",
- c."projectId" as "project_id",
- c."isPersistent" as "is_persistent",
- NULL as "sha",
- NULL as "sub_type",
- false as "is_email_attachment!",
- true as "is_important!",
- ARRAY[]::uuid[] as "status_option_ids!: Vec<Uuid>",
- uh."updatedAt"::timestamptz as "viewed_at",
- CASE $2
- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", c."updatedAt")
- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')
- WHEN 'created_at' THEN c."createdAt"
- ELSE c."updatedAt"
- END::timestamptz as "sort_ts!",
- NULL as "is_completed",
- c."deletedAt"::timestamptz as "deleted_at"
- FROM "Chat" c
- INNER JOIN UserAccessibleItems uai ON uai.item_id = c.id AND uai.item_type = 'chat'
- LEFT JOIN "UserHistory" uh ON uh."itemId" = c.id AND uh."itemType" = 'chat' AND uh."userId" = $1
- WHERE c."deletedAt" IS NULL
-
- UNION ALL
-
- SELECT
- 'project' as "item_type!",
- p.id as "id!",
- NULL as "document_version_id",
- p."userId" as "user_id!",
- p.name as "name!",
- NULL as "branched_from_id",
- NULL as "branched_from_version_id",
- NULL as "document_family_id",
- NULL as "file_type",
- p."createdAt"::timestamptz as "created_at!",
- p."updatedAt"::timestamptz as "updated_at!",
- p."parentId" as "project_id",
- NULL as "is_persistent",
- NULL as "sha",
- NULL as "sub_type",
- false as "is_email_attachment!",
- true as "is_important!",
- ARRAY[]::uuid[] as "status_option_ids!: Vec<Uuid>",
- uh."updatedAt"::timestamptz as "viewed_at",
- CASE $2
- WHEN 'viewed_updated' THEN COALESCE(uh."updatedAt", p."updatedAt")
- WHEN 'viewed_at' THEN COALESCE(uh."updatedAt", '1970-01-01 00:00:00+00')
- WHEN 'created_at' THEN p."createdAt"
- ELSE p."updatedAt"
- END::timestamptz as "sort_ts!",
- NULL as "is_completed",
- p."deletedAt"::timestamptz as "deleted_at"
- FROM "Project" p
- INNER JOIN UserAccessibleItems uai
- ON uai.item_id = p.id
- AND uai.item_type = 'project'
- LEFT JOIN "UserHistory" uh
- ON uh."itemId" = p.id
- AND uh."itemType" = 'project'
- AND uh."userId" = $1
- WHERE p."deletedAt" IS NULL
- )
- SELECT Combined.* FROM Combined
- LEFT JOIN frecency_aggregates fa
- ON fa.entity_id = Combined."id!"
- AND fa.entity_type = Combined."item_type!"
- AND fa.user_id = $1
- WHERE fa.id IS NULL
- AND (
- ($4::timestamptz IS NULL)
- OR
- (Combined."sort_ts!", Combined."id!"::text) < ($4, $5)
- )
- ORDER BY Combined."sort_ts!" DESC, Combined."id!" DESC
- LIMIT $3
- "#,
- user_id.as_ref(), // $1
- sort_method_str, // $2
- query_limit, // $3
- cursor_timestamp, // $4
- cursor_id, // $5
- completed_option_id, // $6
- status_property_id, // $7
- assignees_property_id, // $8
- )
- .try_map(map_soup_projection_hydration!())
- .fetch_all(db)
- .await?;
-
- Ok(items)
-}
-
-/// Returns the non-frecency expanded fallback with projection metadata.
-#[tracing::instrument(skip(db, limit))]
-pub async fn no_frecency_expanded_generic_soup_with_projection(
- db: &PgPool,
- user_id: MacroUserIdStr<'_>,
- limit: u16,
- cursor: Query<Uuid, SimpleSortMethod, Frecency>,
-) -> Result<Vec<SoupProjectionHydration>, sqlx::Error> {
- no_frecency_expanded_generic_soup_hydrated(db, user_id, limit, cursor).await
-}
-
-/// Returns the non-frecency expanded fallback without projection metadata.
-#[tracing::instrument(skip(db, limit))]
-pub async fn no_frecency_expanded_generic_soup(
- db: &PgPool,
- user_id: MacroUserIdStr<'_>,
- limit: u16,
- cursor: Query<Uuid, SimpleSortMethod, Frecency>,
-) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
- Ok(
- no_frecency_expanded_generic_soup_hydrated(db, user_id, limit, cursor)
- .await?
- .into_iter()
- .map(|hydration| hydration.item)
- .collect(),
- )
-}
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs
index 606db3e713..acd0a8065a 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/tests.rs
@@ -2,7 +2,6 @@ use crate::{
domain::models::SoupDocumentServerFacts,
outbound::pg_soup_repo::{
expanded::{
- by_cursor::{expanded_generic_cursor_soup, no_frecency_expanded_generic_soup},
by_ids::{expanded_soup_by_ids, expanded_soup_by_ids_with_projection},
dynamic::{
ExpandedDynamicCursorArgs, expanded_dynamic_cursor_soup,
@@ -32,6 +31,47 @@ use std::sync::Arc;
use system_properties::{StatusOption, SystemPropertyKey};
use uuid::Uuid;
+/// The unfiltered soup pages used to be served by two hand-written queries in
+/// `expanded::by_cursor`. They are now answered by the dynamic builder with an
+/// empty filter, which folds to the same predicates. These helpers keep the
+/// old call shape so the suite below goes on asserting the same behaviour
+/// against the replacement.
+async fn expanded_generic_cursor_soup(
+ db: &PgPool,
+ user_id: MacroUserIdStr<'_>,
+ limit: u16,
+ cursor: Query<Uuid, SimpleSortMethod, ()>,
+) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
+ expanded_dynamic_cursor_soup(
+ db,
+ ExpandedDynamicCursorArgs {
+ user_id,
+ limit,
+ cursor: cursor.map_filter(|_| EntityFilterAst::default()),
+ exclude_frecency: false,
+ },
+ )
+ .await
+}
+
+async fn no_frecency_expanded_generic_soup(
+ db: &PgPool,
+ user_id: MacroUserIdStr<'_>,
+ limit: u16,
+ cursor: Query<Uuid, SimpleSortMethod, Frecency>,
+) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
+ expanded_dynamic_cursor_soup(
+ db,
+ ExpandedDynamicCursorArgs {
+ user_id,
+ limit,
+ cursor: cursor.map_filter(|_| EntityFilterAst::default()),
+ exclude_frecency: true,
+ },
+ )
+ .await
+}
+
macro_rules! unwrap_enum {
// Base case: single variant
($value:expr, $variant:path) => {