Files
silo-server/internal/jellycompat/userdata_direct.go
T
074d106402 feat(jellycompat): BoxSet collections, genre-by-name, and airtight ABS media exclusion (#115)
* feat(jellycompat): exclude audiobook libraries, add BoxSets and genre-by-name

- Audiobook libraries (type 'audiobooks'/'audiobook') no longer appear in
  Views/VirtualFolders, and all browse/search/genre/detail paths are clamped
  to movie/series/episode so audiobook items cannot leak or stream through
  the Jellyfin compat surface (they are served by the ABS-compat API).
- Library collections are now exposed as Jellyfin BoxSets:
  IncludeItemTypes=BoxSet listing (optionally scoped via ParentId library),
  /Items/{id} BoxSet detail, ParentId children with curated position order
  preserved (explicit SortBy delegates to catalog ordering), poster/backdrop
  presigning, and visibility + library-access filtering.
- /Items with only unexposable IncludeItemTypes (e.g. Playlist) returns an
  empty result instead of falling through to views/browse.
- New GET /Genres/{name} endpoint resolving canonical genre casing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(jellycompat): centralize ABS media-type exclusion, fix BoxSet edge cases from review

- Add catalog.AccessFilter.ExcludedMediaTypes, enforced by applyAccessFilter
  and threaded through Search/GetByIDsWithAccess/EnsureAccessible and
  BrowseFavorites. The compat layer stamps audiobook+podcast exclusions onto
  every resolved access filter (one wrap in withDefaults), closing the
  favorites, recommendations, and item-image leak paths that per-call-site
  guards missed.
- Treat podcast libraries like audiobook libraries: hidden from Views, items
  excluded everywhere (they're served by the ABS-compat API).
- HandleItems: BoxSet listing no longer hijacks user-state-filtered queries
  (IncludeItemTypes=BoxSet&Filters=IsFavorite returns empty again),
  IncludeItemTypes=CollectionFolder returns library views as before, and
  Ids=<boxsetId> re-hydrates the BoxSet DTO instead of falling through to
  the views response.
- BoxSet artwork is now durable: stable signed tags seeded from the artwork
  key (no churn on presign rotation) plus a collections fallback in the
  images handler, so posters survive restarts and cache expiry.
- BoxSet listing filters/sorts/pages the lightweight collection rows before
  building DTOs, so a Limit=24 page over 300 collections no longer presigns
  ~600 posters per request; collection children also page before hydrating
  user state.
- Dedupe: shared loadVisibleCollection guard, shared collection-page writer,
  single scoped-types implementation, emptyQueryResult helper.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(jellycompat): address PR review comments

- withCompatAccessExclusions merges compat exclusions with any the base
  resolver already supplies instead of conditionally skipping them.
- Explicit type filters clamp to a closed allowlist (movie/series/episode/
  season) rather than passing unknown types through to catalog queries.
- Collection artwork on the session path applies the same visibility rules
  as the BoxSet item endpoints (hidden or inaccessible-library collections
  404 instead of serving posters).
- loadVisibleCollection propagates infrastructure errors instead of masking
  transient DB failures as 404/empty; only ErrLibraryCollectionNotFound maps
  to not-found.
- ListFavorites filters ABS-surface favorites before applying the
  limit/offset window (over-fetching the raw rows) so pages don't shrink or
  shift, and presigns artwork only for the returned page.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-09 19:16:28 -04:00

325 lines
11 KiB
Go

package jellycompat
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/Silo-Server/silo-server/internal/catalog"
"github.com/Silo-Server/silo-server/internal/userstore"
"github.com/Silo-Server/silo-server/internal/watchstate"
)
// directUserDataService implements UserDataService using the user store directly.
type directUserDataService struct {
storeProvider userstore.UserStoreProvider
itemRepo *catalog.ItemRepository
detailSvc *catalog.DetailService
watchState *watchstate.Service
resumeFilter *catalog.ContinueWatchingProgressFilter
profileStaler profileStaler
profileRefreshRequester profileRefreshRequester
}
func newDirectUserDataService(
storeProvider userstore.UserStoreProvider,
itemRepo *catalog.ItemRepository,
episodeRepo *catalog.EpisodeRepository,
providerIDRepo *catalog.ProviderIDRepository,
detailSvc *catalog.DetailService,
resumeFilter *catalog.ContinueWatchingProgressFilter,
staler profileStaler,
requester profileRefreshRequester,
) *directUserDataService {
return &directUserDataService{
storeProvider: storeProvider,
itemRepo: itemRepo,
detailSvc: detailSvc,
watchState: watchstate.NewService(storeProvider).WithStableIdentityResolver(
watchstate.NewStableIdentityResolver(itemRepo, episodeRepo, providerIDRepo),
),
resumeFilter: resumeFilter,
profileStaler: staler,
profileRefreshRequester: requester,
}
}
func (s *directUserDataService) ListFavorites(ctx context.Context, session *Session, limit, offset int) ([]upstreamListItem, error) {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return nil, fmt.Errorf("open user store: %w", err)
}
// ABS-surface favorites (audiobooks/podcasts) are filtered out below, so
// the limit/offset window must apply to the *filtered* list — a raw
// store-level window would shift or shrink the visible page. Over-fetch
// the raw rows, filter, then window.
scanLimit := min(max((limit+offset)*2, 200), 10000)
favorites, err := store.ListFavorites(ctx, session.ProfileID, scanLimit, 0)
if err != nil {
return nil, fmt.Errorf("list favorites: %w", err)
}
contentIDs := make([]string, 0, len(favorites))
for _, fav := range favorites {
contentIDs = append(contentIDs, fav.MediaItemID)
}
if len(contentIDs) == 0 {
return []upstreamListItem{}, nil
}
items, err := s.itemRepo.GetByIDs(ctx, contentIDs)
if err != nil {
return nil, fmt.Errorf("get favorite items: %w", err)
}
// Build a map for ordering by the original favorites list order
itemMap := make(map[string]*upstreamListItem, len(items))
for _, mi := range items {
// Favorites are shared with the ABS surface; its media types are
// never exposed here (they would 404 on detail/PlaybackInfo).
if isCompatExcludedMediaType(mi.Type) {
continue
}
li := mediaItemToListItem(mi)
itemMap[mi.ContentID] = &li
}
ordered := make([]upstreamListItem, 0, len(contentIDs))
for _, id := range contentIDs {
if li, ok := itemMap[id]; ok {
ordered = append(ordered, *li)
}
}
// Presign artwork only for the page being returned.
result := slicePage(ordered, offset, limit)
for i := range result {
result[i].PosterURL = compatPresignImage(s.detailSvc, ctx, result[i].PosterURL, "poster", compatCardImageSize)
result[i].BackdropURL = compatPresignImage(s.detailSvc, ctx, result[i].BackdropURL, "backdrop", compatCardImageSize)
result[i].LogoURL = compatPresignImage(s.detailSvc, ctx, result[i].LogoURL, "logo", compatCardImageSize)
}
if result == nil {
result = []upstreamListItem{}
}
return result, nil
}
func (s *directUserDataService) ListFavoritesByMediaItems(ctx context.Context, session *Session, mediaItemIDs []string) (map[string]bool, error) {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return nil, fmt.Errorf("open user store: %w", err)
}
result, err := store.ListFavoritesByMediaItems(ctx, session.ProfileID, mediaItemIDs)
if err != nil {
return nil, fmt.Errorf("list favorites by media items: %w", err)
}
if result == nil {
return map[string]bool{}, nil
}
return result, nil
}
func (s *directUserDataService) IsFavorite(ctx context.Context, session *Session, contentID string) (bool, error) {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return false, fmt.Errorf("open user store: %w", err)
}
favorite, err := store.IsFavorite(ctx, session.ProfileID, contentID)
if err != nil {
return false, fmt.Errorf("check favorite: %w", err)
}
return favorite, nil
}
func (s *directUserDataService) AddFavorite(ctx context.Context, session *Session, contentID string) error {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return fmt.Errorf("open user store: %w", err)
}
if err := store.AddFavorite(ctx, session.ProfileID, contentID); err != nil {
return err
}
triggerProfileRefresh(ctx, s.profileStaler, s.profileRefreshRequester, session.StreamAppUserID, session.ProfileID)
return nil
}
func (s *directUserDataService) RemoveFavorite(ctx context.Context, session *Session, contentID string) error {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return fmt.Errorf("open user store: %w", err)
}
if err := store.RemoveFavorite(ctx, session.ProfileID, contentID); err != nil {
return err
}
triggerProfileRefresh(ctx, s.profileStaler, s.profileRefreshRequester, session.StreamAppUserID, session.ProfileID)
return nil
}
func (s *directUserDataService) ListProgress(ctx context.Context, session *Session, status string, limit, offset int) ([]upstreamProgress, error) {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return nil, fmt.Errorf("open user store: %w", err)
}
entries, err := store.ListProgress(ctx, session.ProfileID, status, limit, offset)
if err != nil {
return nil, fmt.Errorf("list progress: %w", err)
}
result := make([]upstreamProgress, 0, len(entries))
for _, entry := range entries {
result = append(result, toUpstreamProgress(entry))
}
return result, nil
}
// FilterResumeProgress applies the same hiding rules as the first-party
// Continue Watching fetcher: dismissed entries and episodes superseded by a
// later-completed episode in the same series.
func (s *directUserDataService) FilterResumeProgress(ctx context.Context, session *Session, entries []upstreamProgress) ([]upstreamProgress, error) {
if len(entries) == 0 {
return entries, nil
}
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return nil, fmt.Errorf("open user store: %w", err)
}
progress := make([]userstore.WatchProgress, 0, len(entries))
for _, entry := range entries {
progress = append(progress, fromUpstreamProgress(entry))
}
// Dismissal lookup failures degrade to showing the entries, matching the
// first-party fetcher.
if dismissals, err := store.ListHomeDismissals(ctx, session.ProfileID, userstore.HomeSurfaceContinueWatching); err != nil {
slog.Error("listing continue watching dismissals", "profile_id", session.ProfileID, "error", err)
} else {
progress = catalog.NewHomeDismissalIndex(dismissals).FilterProgress(progress)
}
superseded, err := s.resumeFilter.SupersededEpisodeProgressIDs(ctx, store, session.ProfileID, progress)
if err != nil {
return nil, fmt.Errorf("filter superseded progress: %w", err)
}
progress = catalog.FilterSupersededProgress(progress, superseded)
result := make([]upstreamProgress, 0, len(progress))
for _, entry := range progress {
result = append(result, toUpstreamProgress(entry))
}
return result, nil
}
func (s *directUserDataService) ListProgressByMediaItems(ctx context.Context, session *Session, mediaItemIDs []string) (map[string]*upstreamProgress, error) {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return nil, fmt.Errorf("open user store: %w", err)
}
progressMap, err := store.ListProgressByMediaItems(ctx, session.ProfileID, mediaItemIDs)
if err != nil {
return nil, fmt.Errorf("list progress by media items: %w", err)
}
result := make(map[string]*upstreamProgress, len(progressMap))
for contentID, progress := range progressMap {
entry := toUpstreamProgress(progress)
result[contentID] = &entry
}
return result, nil
}
func (s *directUserDataService) GetProgress(ctx context.Context, session *Session, contentID string) (*upstreamProgress, error) {
store, err := s.storeProvider.ForUser(ctx, session.StreamAppUserID)
if err != nil {
return nil, fmt.Errorf("open user store: %w", err)
}
progress, err := store.GetProgress(ctx, session.ProfileID, contentID)
if err != nil {
return nil, fmt.Errorf("get progress: %w", err)
}
if progress == nil {
return nil, nil
}
entry := toUpstreamProgress(*progress)
return &entry, nil
}
func (s *directUserDataService) MarkPlayed(ctx context.Context, session *Session, contentID string) error {
if s.watchState == nil {
return fmt.Errorf("watch state service is not configured")
}
if err := s.watchState.RecordJellycompatMarkPlayed(ctx, session.StreamAppUserID, session.ProfileID, contentID, time.Now().UTC()); err != nil {
return err
}
triggerProfileRefresh(ctx, s.profileStaler, s.profileRefreshRequester, session.StreamAppUserID, session.ProfileID)
return nil
}
func (s *directUserDataService) MarkPlayedBatch(ctx context.Context, session *Session, contentIDs []string) error {
if s.watchState == nil {
return fmt.Errorf("watch state service is not configured")
}
if len(contentIDs) == 0 {
return nil
}
if err := s.watchState.RecordJellycompatMarkPlayedBatch(ctx, session.StreamAppUserID, session.ProfileID, contentIDs, time.Now().UTC()); err != nil {
return err
}
triggerProfileRefresh(ctx, s.profileStaler, s.profileRefreshRequester, session.StreamAppUserID, session.ProfileID)
return nil
}
func (s *directUserDataService) MarkUnplayed(ctx context.Context, session *Session, contentID string) error {
if s.watchState == nil {
return fmt.Errorf("watch state service is not configured")
}
if err := s.watchState.RecordJellycompatMarkUnplayed(ctx, session.StreamAppUserID, session.ProfileID, contentID); err != nil {
return err
}
triggerProfileRefresh(ctx, s.profileStaler, s.profileRefreshRequester, session.StreamAppUserID, session.ProfileID)
return nil
}
func (s *directUserDataService) MarkUnplayedBatch(ctx context.Context, session *Session, contentIDs []string) error {
if s.watchState == nil {
return fmt.Errorf("watch state service is not configured")
}
if len(contentIDs) == 0 {
return nil
}
if err := s.watchState.RecordJellycompatMarkUnplayedBatch(ctx, session.StreamAppUserID, session.ProfileID, contentIDs); err != nil {
return err
}
triggerProfileRefresh(ctx, s.profileStaler, s.profileRefreshRequester, session.StreamAppUserID, session.ProfileID)
return nil
}
func toUpstreamProgress(entry userstore.WatchProgress) upstreamProgress {
return upstreamProgress{
MediaItemID: entry.MediaItemID,
PositionSeconds: entry.PositionSeconds,
DurationSeconds: entry.DurationSeconds,
Completed: entry.Completed,
UpdatedAt: entry.UpdatedAt,
}
}
func fromUpstreamProgress(entry upstreamProgress) userstore.WatchProgress {
return userstore.WatchProgress{
MediaItemID: entry.MediaItemID,
PositionSeconds: entry.PositionSeconds,
DurationSeconds: entry.DurationSeconds,
Completed: entry.Completed,
UpdatedAt: entry.UpdatedAt,
}
}