Files
jellytau/src/lib/services/favorites.ts
T
dtourolle d54d8cc7c4 refactor(logging): route frontend console calls through the logger
TRACES: | DR-204

484 ungated `console.*` calls across 63 non-test frontend files shipped to
end users with no way to turn them off. Mechanical substitution, no control
flow, error handling or message semantics changed:

  console.log / console.debug -> log.debug
  console.info                -> log.info
  console.warn                -> log.warn
  console.error               -> log.error

Hand-written `"[Scope] …"` prefixes are dropped where the logger's scope
now carries them; scope names that already existed are preserved verbatim
(`[Auth]`, `[VideoPlayer]`, `[PiP]`, …) and inferred from the filename
where a file had none. `src/routes/player/[id]/+page.svelte` keeps its
`NextEpisode` and `AutoPlay` sub-scopes as separate loggers rather than
flattening them into the page scope.

`grep -rn 'console\.' src/` now matches nothing outside the tests and the
facade itself.
2026-08-20 19:29:59 +02:00

73 lines
2.4 KiB
TypeScript

// Favorites service - Handles toggling favorite status with optimistic updates
// TRACES: UR-017, UR-068 | DR-021, DR-119
import { get } from "svelte/store";
import { commands } from "$lib/api/bindings";
import { auth } from "$lib/stores/auth";
import { isConnected } from "$lib/stores/connectivity";
import { setFavorite } from "$lib/stores/favorites";
import { createLogger } from "$lib/utils/logger";
const log = createLogger("Favorites");
/**
* Toggle the favorite status of an item.
*
* Flow:
* 1. Update local database immediately (optimistic update)
* 2. Sync to Jellyfin server
* 3. Mark as synced on success, or leave pending_sync flag on failure
*
* @param itemId - The Jellyfin item ID
* @param currentIsFavorite - The current favorite status
* @returns The new favorite status
* @throws Error if not authenticated or database update fails
*/
export async function toggleFavorite(
itemId: string,
currentIsFavorite: boolean
): Promise<boolean> {
const userId = auth.getUserId();
if (!userId) {
throw new Error("Not authenticated");
}
const newIsFavorite = !currentIsFavorite;
// 1. Update local database first (optimistic update)
await commands.storageToggleFavorite(userId, itemId, newIsFavorite);
// Publish to every mounted view at once, so the heart on a card, the detail
// page and the Favourites grid never disagree. TRACES: UR-068 | DR-119
setFavorite(itemId, newIsFavorite);
// 2. Sync to Jellyfin server.
//
// Only attempt this when we're actually connected. When offline, the server
// call can hang on a long network timeout rather than failing fast — which
// blocks the caller (and leaves the favorite button greyed out with a wait
// cursor) until the request finally gives up, effectively only recovering
// once we're back online. The local DB write above keeps the pending_sync
// flag set, so the change still syncs later; we just don't block the UI on
// an unreachable server here.
if (get(isConnected)) {
try {
const repo = auth.getRepository();
if (newIsFavorite) {
await repo.markFavorite(itemId);
} else {
await repo.unmarkFavorite(itemId);
}
// 3. Mark as synced
await commands.storageMarkSynced(userId, itemId);
} catch (error) {
log.error("Failed to sync favorite to server:", error);
// Favorite is stored locally and will be synced later
// via sync queue (when implemented)
}
}
return newIsFavorite;
}