Files
silo-server/internal/userstore/store.go
T
02e62767a1 feat(watchsync): sync watchlists with Trakt/Simkl/MDBList (#227)
* feat(watchsync): sync watchlists with Trakt/Simkl/MDBList

Extend the watch-providers feature to sync a user's watchlist, generalizing
the existing favorites pipeline rather than duplicating it.

What changed
- Generalize the favorites sync into one ListKind-parameterized pipeline
  (internal/watchsync/lists.go) driving both favorites and watchlist; the
  per-favorites service methods are replaced by kind-generic ones. The shadow
  table watch_provider_favorite_items becomes watch_provider_list_items with a
  list_kind discriminator.
- Providers: Trakt gains watchlist sync (/sync/watchlist, distinct from
  favorites); Simkl gains plan-to-watch sync; MDBList is re-mapped from
  favorites to watchlist (its only list is a watchlist) — its capabilities now
  report import_favorites=false / import_watchlist=true, and the migration
  re-binds existing MDBList connections.
- Auto-remove watched items from the watchlist: a standalone, default-on
  profile preference (user_profiles.remove_watched_from_watchlist) removes a
  movie when watched and a series once every episode is watched. Implemented as
  watchstate.CompletionObserver (internal/watchlist.Maintainer), wired into the
  manual mark-watched, playback-stop, and jellycompat mark-played paths.
- Optional MDBList sort-order mirroring: an opt-in, capability-gated toggle
  mirrors MDBList's watchlist order into Silo via user_watchlist.sort_index;
  ListWatchlist orders by sort_index then added_at, so both /api/v1/watchlist
  and the catalog watchlist view inherit it.
- Real-time + scheduled: local add/remove pushes to connected providers
  immediately (removals gated by the opt-in removals toggle); the hourly job is
  the inbound/import + retry/reconcile path.
- Web: watch-provider settings gain watchlist import/export/removals and
  "mirror watchlist order" toggles plus watchlist sync stats.

Why
- The favorites and watchlist pipelines are ~90% identical; generalizing keeps
  one code path (per CLAUDE.md's anti-duplication guidance) instead of cloning.

API/compat
- All new fields on ConnectionStatus/Capabilities/ConnectionUpdate/SyncRun and
  the web types are additive (Silo v1 additive-only rule). No existing field is
  renamed, removed, or retyped.

Risks / follow-up
- MDBList capability flip is intentional and client-visible: silo-android /
  silo-apple may need to surface MDBList under the watchlist (not favorites) UI.
- MDBList existing users: their MDBList list previously mirrored Silo favorites
  and now mirrors Silo watchlist; the first post-migration sync is a union
  (removals default off), so nothing is destructively purged.
- Order mirroring reflects the order MDBList returns from /watchlist/items
  (couldn't confirm against their docs — Cloudflare-blocked); if it ever
  diverges from the UI sort, a sort param is the small follow-up.

Tests: new maintainer (auto-remove) and watchlist-order unit tests; provider +
service tests updated. go build, go test (affected pkgs), migrate-validate,
verify-local-paths, web prettier/eslint/tsc all pass.

AI-use disclosure: implemented with Claude Code (Claude Opus 4.8).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(watchsync): update list shadow table references

* fix(watchsync): address review — retry/progress + error propagation

Addresses CodeRabbit review on #227:
- maintainer: propagate transient catalog lookup errors instead of silently
  treating every items.GetByID failure as "maybe an episode".
- exportList: mark every queued item not confirmed sent (not_found, failed, or
  omitted) so the pending loop always advances; the next run's upsert clears the
  error and re-attempts, so transient failures still retry.
- removePendingListItems + realtime removal: treat Sent and NotFound as
  reconciled; leave true failures pending (no last_error, which would strand
  them from the removal query) so the scheduled run retries, using in-memory
  dedupe to terminate the loop.
- exportLocalListItems: send the normalized items (with computed
  ProviderItemKey), not the original event slice.
- UpdateConnection: clear mirrored watchlist order before persisting the disable
  and propagate failures, so a failed clear can't report "disabled" while
  sort_index ordering is still active.
- web: include favorite + watchlist removal counts in the exported "sent" total.
- test: align serviceFakeRepo list-state with Postgres (clear last_error on
  successful transitions); add maintainer error-propagation test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 16:05:53 -04:00

126 lines
8.7 KiB
Go

package userstore
import (
"context"
"errors"
"time"
)
var ErrCollectionGroupNotFound = errors.New("collection group not found")
// UserStore defines the interface for per-user data storage.
// Both SQLite and Postgres backends implement this interface.
type UserStore interface {
// Profiles
CreateProfile(ctx context.Context, p Profile) error
GetProfile(ctx context.Context, id string) (*Profile, error)
ListProfiles(ctx context.Context) ([]Profile, error)
UpdateProfile(ctx context.Context, id string, u UpdateProfileInput) error
DeleteProfile(ctx context.Context, id string) error
VerifyPIN(ctx context.Context, profileID, pin string) (bool, error)
// Progress
UpdateProgress(ctx context.Context, profileID, mediaItemID string, position, duration float64, thresholds ProgressThresholds) error
SetProgress(ctx context.Context, profileID, mediaItemID string, position, duration float64, thresholds ProgressThresholds) error
SetProgressAt(ctx context.Context, profileID, mediaItemID string, position, duration float64, completed bool, updatedAt time.Time) error
SetProgressIfNewer(ctx context.Context, profileID, mediaItemID string, position, duration float64, completed bool, updatedAt time.Time) (bool, error)
UpdateProgressHints(ctx context.Context, profileID, mediaItemID string, hints VersionHints) error
MarkWatched(ctx context.Context, profileID, mediaItemID string, duration float64) error
MarkProgressBatch(ctx context.Context, profileID string, mediaItemIDs []string, updatedAt time.Time) error
ClearProgressBatch(ctx context.Context, profileID string, mediaItemIDs []string, updatedAt time.Time) error
ClearProgress(ctx context.Context, profileID, mediaItemID string) error
GetProgress(ctx context.Context, profileID, mediaItemID string) (*WatchProgress, error)
ListProgress(ctx context.Context, profileID, status string, limit, offset int) ([]WatchProgress, error)
ListProgressByMediaItems(ctx context.Context, profileID string, mediaItemIDs []string) (map[string]WatchProgress, error)
AddHistory(ctx context.Context, entry WatchHistoryEntry) error
AddHistoryIfMissing(ctx context.Context, entry WatchHistoryEntry) (bool, error)
ListHistory(ctx context.Context, profileID string, limit, offset int) ([]WatchHistoryEntry, error)
ListCompletedHistory(ctx context.Context, query CompletedHistoryQuery) ([]WatchHistoryEntry, error)
ListCompletedHistoryItems(ctx context.Context, query CompletedHistoryItemQuery) ([]CompletedHistoryItem, error)
RemoveHistoryItems(ctx context.Context, profileID string, mediaItemIDs []string, removedAt time.Time) error
DeleteHistoryBySource(ctx context.Context, profileID string, mediaItemIDs []string, source WatchHistorySource) error
ListHomeDismissals(ctx context.Context, profileID, surface string) ([]HomeItemDismissal, error)
UpsertHomeDismissal(ctx context.Context, dismissal HomeItemDismissal) error
DeleteHomeDismissal(ctx context.Context, profileID, surface, mediaItemID string) error
// Favorites & Watchlist
AddFavorite(ctx context.Context, profileID, mediaItemID string) error
AddFavoriteAt(ctx context.Context, profileID, mediaItemID string, addedAt time.Time) error
RemoveFavorite(ctx context.Context, profileID, mediaItemID string) error
ListFavorites(ctx context.Context, profileID string, limit, offset int) ([]Favorite, error)
ListFavoritesByMediaItems(ctx context.Context, profileID string, mediaItemIDs []string) (map[string]bool, error)
IsFavorite(ctx context.Context, profileID, mediaItemID string) (bool, error)
AddToWatchlist(ctx context.Context, profileID, mediaItemID string) error
AddToWatchlistAt(ctx context.Context, profileID, mediaItemID string, addedAt time.Time) error
RemoveFromWatchlist(ctx context.Context, profileID, mediaItemID string) error
// ReplaceWatchlistOrder mirrors a provider's watchlist order: the given ids
// get sort_index 0..N-1 in order; all other rows reset to added_at ordering.
ReplaceWatchlistOrder(ctx context.Context, profileID string, orderedMediaItemIDs []string) error
ListWatchlist(ctx context.Context, profileID string, limit, offset int) ([]WatchlistEntry, error)
ListWatchlistByMediaItems(ctx context.Context, profileID string, mediaItemIDs []string) (map[string]bool, error)
InWatchlist(ctx context.Context, profileID, mediaItemID string) (bool, error)
// RemoveWatchedFromWatchlist reports the profile's preference for auto-removing
// fully-watched items from the watchlist (defaults true).
RemoveWatchedFromWatchlist(ctx context.Context, profileID string) (bool, error)
// Collections
CreateCollection(ctx context.Context, input CreateCollectionInput) (*Collection, error)
GetCollection(ctx context.Context, id string) (*Collection, error)
ListCollections(ctx context.Context, profileID string) ([]Collection, error)
UpdateCollection(ctx context.Context, input UpdateCollectionInput) error
DeleteCollection(ctx context.Context, id string) error
AddCollectionItem(ctx context.Context, collectionID, mediaItemID string, position int) error
RemoveCollectionItem(ctx context.Context, collectionID, mediaItemID string) error
ListCollectionItems(ctx context.Context, collectionID string) ([]CollectionItem, error)
ReplaceCollectionItems(ctx context.Context, collectionID string, items []CollectionItemReplacement) error
ReorderCollectionItems(ctx context.Context, collectionID string, orderedMediaItemIDs []string) error
// ReorderCollections scopes to the supplied group_id. A nil groupID means
// the implicit Ungrouped bucket.
ReorderCollections(ctx context.Context, profileID string, groupID *string, orderedIDs []string) error
UpdateCollectionSyncState(ctx context.Context, input UpdateCollectionSyncStateInput) error
ListCollectionGroups(ctx context.Context) ([]CollectionGroup, error)
EnsureCollectionGroup(ctx context.Context, id string) error
CreateCollectionGroup(ctx context.Context, name, slug string, defaultSortMode GroupSortMode) (*CollectionGroup, error)
UpdateCollectionGroup(ctx context.Context, id string, name *string, slug *string, defaultSortMode *GroupSortMode) (*CollectionGroup, error)
DeleteCollectionGroup(ctx context.Context, id string) error
ReorderCollectionGroups(ctx context.Context, orderedIDs []string) error
// Section Overrides
ListSectionOverrides(ctx context.Context, profileID, scope, libraryID string) ([]SectionOverride, error)
SaveSectionOverrides(ctx context.Context, profileID, scope, libraryID string, overrides []SectionOverride) error
ResetSectionOverrides(ctx context.Context, profileID, scope, libraryID string) error
// Settings & Preferences
GetSetting(ctx context.Context, key string) (string, error)
SetSetting(ctx context.Context, key, value string) error
DeleteSetting(ctx context.Context, key string) error
ListSettings(ctx context.Context) ([]SettingEntry, error)
GetDeviceSetting(ctx context.Context, profileID, deviceID, key string) (*DeviceSettingEntry, error)
SetDeviceSetting(ctx context.Context, entry DeviceSettingEntry) error
DeleteDeviceSetting(ctx context.Context, profileID, deviceID, key string) error
DeleteAllDeviceSettings(ctx context.Context, profileID, deviceID string) error
DeleteDeviceSettingsByKey(ctx context.Context, key string) error
ListDeviceSettings(ctx context.Context, key string) ([]DeviceSettingEntry, error)
ListAllDeviceSettings(ctx context.Context) ([]DeviceSettingEntry, error)
SetSubtitlePreference(ctx context.Context, pref SubtitlePreference) error
GetSubtitlePreference(ctx context.Context, profileID, seriesID string) (*SubtitlePreference, error)
DeleteSubtitlePreference(ctx context.Context, profileID, seriesID string) error
SetAudioPreference(ctx context.Context, pref AudioPreference) error
GetAudioPreference(ctx context.Context, profileID, seriesID string) (*AudioPreference, error)
DeleteAudioPreference(ctx context.Context, profileID, seriesID string) error
SetSeriesPlaybackPreference(ctx context.Context, pref SeriesPlaybackPreference) error
GetSeriesPlaybackPreference(ctx context.Context, profileID, seriesID string) (*SeriesPlaybackPreference, error)
DeleteSeriesPlaybackPreference(ctx context.Context, profileID, seriesID string) error
GetLibraryPlaybackPreference(ctx context.Context, profileID string, libraryID int) (*LibraryPlaybackPreference, error)
ListLibraryPlaybackPreferences(ctx context.Context, profileID string) ([]LibraryPlaybackPreference, error)
UpsertLibraryPlaybackPreference(ctx context.Context, pref LibraryPlaybackPreference) error
DeleteLibraryPlaybackPreference(ctx context.Context, profileID string, libraryID int) error
}
// DeviceRegistry is implemented by stores that track observed devices even
// when they do not currently have any device-scoped overrides.
type DeviceRegistry interface {
RegisterDevice(ctx context.Context, entry DeviceEntry) error
ListDevices(ctx context.Context) ([]DeviceEntry, error)
}