Adds project summaries and share counts on top of #32025. The sidebar's pinned projects read the summaries; the Projects page reads the summaries and the counts. - **`GET /api/experimental/chats/projects/summaries`** returns one `ChatProjectSummary` for every project in the existing project list (the same set): `project_id`, `pinned`, `pin_order`, `own_chat_count`, `own_unread_count`, `own_running`, `own_needs_attention` (`requires_action`, `error` or `paused`) and `last_activity_at` (newest `updated_at` among the caller's counted chats). Counts use only the caller's own top-level, non-archived chats; unread uses the same rule as `has_unread`. `pin_order` is the stored order from #32025: it sorts the pinned projects, lowest first, and can have gaps. A summary does not repeat the project: clients pair it with `GET /chats/projects` by ID. - **Share counts on `ChatProject`:** every project response now carries `member_count` (the owner plus the users the project is shared with directly), `group_count` (shared groups, including Everyone) and `shared_with_everyone`. They count exactly the users and groups `GET .../projects/{project}/acl` lists: users outside the project's organization and deleted groups are left out, through the same helper the ACL endpoint uses. Group members are not expanded, so a list costs two extra queries (memberships and groups) for any number of projects. While chat sharing is disabled or the `chat-project-sharing` experiment is off, every project reports 1 member, 0 groups. - **Same numbers on both pages:** the project page heading already showed "N members, M groups" from the ACL. It now uses a shared formatter, `formatProjectMembers`, which the Projects page card (#32083) also uses, and names the Everyone group first instead of counting it: "Everyone, 2 members, 1 group". - **Why summaries are separate:** the pin and the caller's chat counts change with every chat event, and clients refetch them often, so they do not come with the project, its templates and permissions. - **Authorization:** summaries return only projects the caller can read. They also carry the caller's pins, so they ask RBAC for `read_personal` on the caller: an API key scoped without `user:read_personal` gets a 403. The summaries authorize each project with a full evaluation while the caller has fewer than 200, because the partial evaluation `rbac.Filter` prepares by default costs an organization member about 6 ms per request and a full evaluation about 0.03 ms per project. <details> <summary>Verification</summary> `make gen/db` and the API docs and TypeScript types regenerate cleanly; Go lint on the changed packages, the summaries, share counts, pin, chat list, ACL and dbauthz tests pass; `pnpm lint` and the project page tests pass. `TestChatProjectShareCounts` fails if either ACL filter (organization membership, existing groups) is removed. The summaries timings (1.6 ms at 10 projects, 4.6 ms at 50) are medians of 40 runs against a local Postgres 13, measured before the share counts change. Dogfood UAT with a premium license ran on an earlier response shape, which carried the project and a people count inside each summary. It was not repeated for this shape; the Go and Vitest tests cover it. </details> > [!NOTE] > Most of this PR was written by AI agents (Xum, with UAT run through Coder Agents). The verification above is from those runs.
84 lines
3.9 KiB
SQL
84 lines
3.9 KiB
SQL
-- name: PinChatProject :exec
|
|
-- Pins a project at the end of the user's list. An existing pin keeps its
|
|
-- place. Callers must hold the per-user pin lock, or two new pins could
|
|
-- take the same position.
|
|
INSERT INTO chat_project_pins (user_id, project_id, pin_order)
|
|
SELECT @user_id::uuid, @project_id::uuid, COALESCE(MAX(pin_order), 0) + 1
|
|
FROM chat_project_pins
|
|
WHERE user_id = @user_id::uuid
|
|
ON CONFLICT (user_id, project_id) DO NOTHING;
|
|
|
|
-- name: ReorderChatProjectPins :exec
|
|
-- Puts the user's pins on project_ids in that order, followed by the user's
|
|
-- other pins in their current order. IDs the user has not pinned are
|
|
-- ignored. The other pins include those on projects the user can no longer
|
|
-- see, so no caller has to work out which pins are hidden. Callers must hold
|
|
-- the per-user pin lock, or overlapping reorders could deadlock.
|
|
WITH ranked AS (
|
|
SELECT p.project_id, ROW_NUMBER() OVER (
|
|
ORDER BY array_position(@project_ids::uuid[], p.project_id) ASC NULLS LAST, p.pin_order ASC, p.project_id ASC
|
|
) :: integer AS pin_order
|
|
FROM chat_project_pins p
|
|
WHERE p.user_id = @user_id::uuid
|
|
)
|
|
UPDATE chat_project_pins p
|
|
SET pin_order = ranked.pin_order
|
|
FROM ranked
|
|
WHERE p.user_id = @user_id::uuid AND p.project_id = ranked.project_id;
|
|
|
|
-- name: UnpinChatProject :exec
|
|
-- Deletes a pin. Unpinning a project that is not pinned changes nothing.
|
|
-- The remaining pins keep their order, with a gap in pin_order until the
|
|
-- next reorder.
|
|
DELETE FROM chat_project_pins
|
|
WHERE user_id = @user_id::uuid AND project_id = @project_id::uuid;
|
|
|
|
-- name: GetChatProjectSummariesByUserID :many
|
|
-- Returns the user's pin and aggregates over the user's own top-level,
|
|
-- non-archived chats for each project from
|
|
-- GetChatProjectsOwnedOrSharedWithUserID. The project columns only serve
|
|
-- authorization: entries match regardless of the actions they grant, so
|
|
-- callers must authorize each row. Pins on projects the user can no longer
|
|
-- see are never returned.
|
|
WITH own_chats AS (
|
|
-- Read through idx_chats_owner.
|
|
SELECT
|
|
c.project_id,
|
|
COUNT(*) AS chat_count,
|
|
-- Same rule as has_unread in GetChats.
|
|
COUNT(*) FILTER (WHERE EXISTS (
|
|
SELECT 1 FROM chat_messages cm
|
|
WHERE cm.chat_id = c.id AND cm.role = 'assistant' AND cm.deleted = false
|
|
AND cm.id > COALESCE(c.last_read_message_id, 0)
|
|
)) AS unread_count,
|
|
bool_or(c.status IN ('running', 'interrupting')) AS running,
|
|
bool_or(c.status IN ('requires_action', 'error', 'paused')) AS needs_attention,
|
|
MAX(c.updated_at)::timestamptz AS last_activity_at
|
|
FROM chats c
|
|
WHERE c.owner_id = @user_id::uuid AND c.project_id IS NOT NULL AND c.parent_chat_id IS NULL AND c.archived = false
|
|
GROUP BY c.project_id
|
|
)
|
|
SELECT
|
|
chat_projects.id AS project_id,
|
|
chat_projects.organization_id,
|
|
chat_projects.owner_id,
|
|
chat_projects.user_acl,
|
|
chat_projects.group_acl,
|
|
(pins.project_id IS NOT NULL)::boolean AS pinned,
|
|
-- The stored order: it sorts the pins, and its values can have gaps.
|
|
COALESCE(pins.pin_order, 0)::integer AS pin_order,
|
|
COALESCE(own_chats.chat_count, 0)::bigint AS own_chat_count,
|
|
COALESCE(own_chats.unread_count, 0)::bigint AS own_unread_count,
|
|
COALESCE(own_chats.running, false)::boolean AS own_running,
|
|
COALESCE(own_chats.needs_attention, false)::boolean AS own_needs_attention,
|
|
own_chats.last_activity_at
|
|
FROM chat_projects
|
|
LEFT JOIN chat_project_pins pins
|
|
ON pins.user_id = @user_id::uuid AND pins.project_id = chat_projects.id
|
|
LEFT JOIN own_chats ON own_chats.project_id = chat_projects.id
|
|
-- Grant keys select candidates even while chat sharing is disabled, as in
|
|
-- GetChatProjectsOwnedOrSharedWithUserID: a role can still let the caller
|
|
-- read the project.
|
|
WHERE chat_projects.id IN (SELECT project_id FROM chat_project_ids_for_user(@user_id::uuid, true))
|
|
ORDER BY lower(chat_projects.name), chat_projects.id;
|