perf(listing): return per-item is_favorite/is_shared, drop client badge fetches

The folder listing now carries the favorite/share badge state for exactly the
items it returns, so the files browser stops fetching favorites and outgoing
shares separately. This removes the last per-navigation badge round-trips AND
fixes the correctness hole of the previous approaches: badges were derived from
only the first 200 global favorites / shares, so a favorited or shared item
outside that window showed no badge. Now every listed item is correct, and the
work is scoped to the items on screen.

Backend (`GET /api/folders/{id}/listing`):
- `FolderListingDto` gains `favorite_ids` and `shared_ids` (sorted) — listing-
  level metadata, so no churn to the many FileDto/FolderDto constructors.
- The handler computes both with two batched, index-backed queries run
  concurrently: `FavoritesService::favorited_ids` (auth.user_favorites, ANY) and
  `PgAclEngine::shared_resource_ids` (storage.role_grants by granted_by + ANY,
  which already covers public links as 'token' grants — same membership the
  /grants/outgoing/resources endpoint exposes). Both fold into the ETag.
- Public-share browsing passes empty sets (anonymous, read-only context).

Frontend:
- `listFolder` reads `favorite_ids` / `shared_ids`; the files view seeds local
  badge sets straight from the listing and updates them optimistically on
  favorite toggle / batch / share creation (via ShareDialog's `onshared`).
- Removes the session `badges` store + its fetches entirely — the listing is now
  the single, authoritative, fetch-free source.

Net: favorite/share badges cost zero extra client requests per navigation and
are correct regardless of how many favorites/shares the user has. Validated:
cargo check + clippy -D warnings (backend; integration tests need Postgres,
unavailable here), frontend npm run check + unit tests, and a headless render of
the real files route (list + grid) with the new flags present — no errors.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M8Vb9QHmLZnEMzHz7MrFy6
This commit is contained in:
Claude
2026-06-19 15:21:01 +00:00
parent 546dcef305
commit 9ccaeef0ab
9 changed files with 156 additions and 174 deletions
@@ -12,4 +12,11 @@ pub struct FolderListingDto {
pub folders: Vec<FolderDto>,
/// Files inside the requested folder
pub files: Vec<FileDto>,
/// Ids (folders + files in this listing) the caller has favorited. Lets the
/// client render star badges without a separate per-navigation favorites
/// fetch. Sorted for a stable response / ETag.
pub favorite_ids: Vec<String>,
/// Ids in this listing the caller has an outgoing share/grant on (incl.
/// public links). Sorted for a stable response / ETag.
pub shared_ids: Vec<String>,
}
@@ -27,6 +27,17 @@ impl FavoritesService {
pub fn new(repo: Arc<FavoritesPgRepository>) -> Self {
Self { repo }
}
/// Subset of `(item_id, item_type)` pairs the user has favorited — used to
/// stamp star badges onto a folder listing in one batched query (no N+1, no
/// global page fetch).
pub async fn favorited_ids(
&self,
user_id: Uuid,
items: &[(&str, &str)],
) -> Result<HashSet<String>> {
self.repo.batch_check_favorites(user_id, items).await
}
}
impl FavoritesUseCase for FavoritesService {
@@ -186,6 +186,10 @@ impl ShareBrowseService {
Ok(FolderListingDto {
folders: folders_res?,
files: files_res?,
// Public-share browsing is an anonymous, read-only context — no
// per-caller favorite/share badges apply.
favorite_ids: Vec::new(),
shared_ids: Vec::new(),
})
}
}