Files
Oxicloud/src/application/services/share_browse_service.rs
T
Claude 9ccaeef0ab 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
2026-06-19 15:21:01 +00:00

196 lines
5.7 KiB
Rust

//! Share-scoped folder browsing for public folder shares.
use std::sync::Arc;
use uuid::Uuid;
use crate::application::dtos::folder_listing_dto::FolderListingDto;
use crate::application::ports::file_ports::FileRetrievalUseCase;
use crate::application::ports::folder_ports::FolderUseCase;
use crate::application::services::file_retrieval_service::FileRetrievalService;
use crate::application::services::folder_service::FolderService;
use crate::application::services::share_service::ShareService;
use crate::common::errors::DomainError;
use crate::domain::repositories::folder_repository::FolderRepository;
use crate::infrastructure::repositories::pg::folder_db_repository::FolderDbRepository;
struct ResolvedFolderShare {
root_folder_id: String,
owner_id: Uuid,
display_name: String,
}
pub struct ZipTarget {
pub folder_id: String,
pub display_name: String,
}
pub struct ShareBrowseService {
share_service: Arc<ShareService>,
folder_service: Arc<FolderService>,
file_retrieval: Arc<FileRetrievalService>,
folder_repo: Arc<FolderDbRepository>,
}
impl ShareBrowseService {
pub fn new(
share_service: Arc<ShareService>,
folder_service: Arc<FolderService>,
file_retrieval: Arc<FileRetrievalService>,
folder_repo: Arc<FolderDbRepository>,
) -> Self {
Self {
share_service,
folder_service,
file_retrieval,
folder_repo,
}
}
async fn resolve_folder_share(
&self,
token: &str,
unlock_jwt: Option<&str>,
) -> Result<ResolvedFolderShare, DomainError> {
let share = self
.share_service
.get_shared_link_with_unlock(token, unlock_jwt)
.await?;
if share.item_type != "folder" {
return Err(DomainError::validation_error(
"This endpoint is only valid for folder shares",
));
}
let owner_id = Uuid::parse_str(&share.created_by).map_err(|_| {
DomainError::internal_error(
"Share",
format!("Share has invalid created_by UUID: {}", share.created_by),
)
})?;
let display_name = match share.item_name {
Some(name) => name,
None => self
.folder_service
.get_folder(&share.item_id)
.await
.map(|f| f.name)
.unwrap_or_else(|_| "Shared folder".to_string()),
};
Ok(ResolvedFolderShare {
root_folder_id: share.item_id,
owner_id,
display_name,
})
}
pub async fn list_root(
&self,
token: &str,
unlock_jwt: Option<&str>,
) -> Result<FolderListingDto, DomainError> {
let resolved = self.resolve_folder_share(token, unlock_jwt).await?;
self.list_inner(&resolved.root_folder_id, resolved.owner_id)
.await
}
pub async fn list_subfolder(
&self,
token: &str,
folder_id: &str,
unlock_jwt: Option<&str>,
) -> Result<FolderListingDto, DomainError> {
let resolved = self.resolve_folder_share(token, unlock_jwt).await?;
if !self
.folder_repo
.is_folder_in_subtree(folder_id, &resolved.root_folder_id)
.await?
{
return Err(DomainError::not_found("Folder", folder_id));
}
self.list_inner(folder_id, resolved.owner_id).await
}
pub async fn assert_file_in_share(
&self,
token: &str,
file_id: &str,
unlock_jwt: Option<&str>,
) -> Result<(), DomainError> {
let resolved = self.resolve_folder_share(token, unlock_jwt).await?;
if !self
.folder_repo
.is_file_in_subtree(file_id, &resolved.root_folder_id)
.await?
{
return Err(DomainError::not_found("File", file_id));
}
Ok(())
}
pub async fn resolve_zip_target(
&self,
token: &str,
folder_id: Option<&str>,
unlock_jwt: Option<&str>,
) -> Result<ZipTarget, DomainError> {
let resolved = self.resolve_folder_share(token, unlock_jwt).await?;
let target_folder_id = match folder_id {
None => resolved.root_folder_id.clone(),
Some(id) => {
if !self
.folder_repo
.is_folder_in_subtree(id, &resolved.root_folder_id)
.await?
{
return Err(DomainError::not_found("Folder", id));
}
id.to_string()
}
};
let display_name = if folder_id.is_none() {
resolved.display_name
} else {
self.folder_service
.get_folder(&target_folder_id)
.await
.map(|f| f.name)
.unwrap_or_else(|_| "shared".to_string())
};
Ok(ZipTarget {
folder_id: target_folder_id,
display_name,
})
}
async fn list_inner(
&self,
parent_folder_id: &str,
owner_id: Uuid,
) -> Result<FolderListingDto, DomainError> {
let (folders_res, files_res) = tokio::join!(
self.folder_service
.list_folders_with_perms(Some(parent_folder_id), owner_id),
self.file_retrieval
.list_files_with_perms(Some(parent_folder_id), owner_id),
);
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(),
})
}
}