Files
jellytau/src/lib/services/playbackReporting.ts
T
dtourolleandClaude Opus 5 e4632bb2b2 fix(sync): queue a watch position the server could not be told about (DR-154)
sync_queue and its drain (DR-131) were built, tested and running, but the
stop-report path never fed them, so closing a video while the server was
unreachable lost the resume point outright.

HybridRepository::report_playback_stopped is a bare pass-through to the
online repository ("Playback reporting goes directly to server"), and on
failure the error surfaced to a frontend catch whose own comment read
"Server error - could queue, but for now just log". Both producers that
would have queued it -- PlaybackReporter::queue_for_sync in Rust and
syncService.queuePlaybackProgress on the frontend -- have no callers on
the playback path. user_data.pending_sync was dutifully set to 1, but
nothing drains that flag for positions the way favourites do (DR-120).

The command layer now enqueues a report_playback_stopped row whenever the
push fails; the existing drain already parses and replays that operation.

The pending row for an item is superseded in place rather than appended
to: progress is reported every 10s, so a server that stays down would
otherwise add a row per tick, all obsoleted by the newest -- the
unbounded queue DR-131 exists to prevent. Only pending/failed rows are
superseded, since reviving an abandoned row restores that same growing
counter. Queueing is best-effort and never fails the command: the local
position is already saved, so a failed queue write must not be reported
as a lost position.

Verified red->green in the jellytau-builder image: the four new tests
failed to compile (enqueue_playback_stopped not found) before the fix.
Full Rust suite passes (627 tests), cargo fmt clean, clippy adds no new
warnings; frontend suite (933) and svelte-check also clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-12 19:18:11 +02:00

150 lines
4.4 KiB
TypeScript

// Playback reporting service
//
// Simplified service that delegates all logic to the Rust backend.
// The backend handles:
// - Local DB updates
// - Jellyfin server reporting
// - Offline queueing (via sync queue)
// - Connectivity checks
//
// TRACES: UR-005, UR-019, UR-025 | DR-028, DR-047
import { commands } from "$lib/api/bindings";
import { auth } from "$lib/stores/auth";
/**
* Report playback start to Jellyfin (or queue if offline)
*
* The Rust backend handles both local DB updates and server reporting,
* automatically queueing for sync if the server is unreachable.
*
* TRACES: UR-005, UR-025 | DR-028
*/
export async function reportPlaybackStart(
itemId: string,
positionSeconds: number,
contextType: "container" | "single" = "single",
contextId: string | null = null
): Promise<void> {
const positionMs = Math.floor(positionSeconds * 1000);
const userId = auth.getUserId();
console.log(
"[PlaybackReporting] reportPlaybackStart - itemId:",
itemId,
"positionSeconds:",
positionSeconds,
"context:",
contextType,
contextId
);
// Update local DB with context (always works, even offline)
if (userId) {
try {
await commands.storageUpdatePlaybackContext(userId, itemId, positionMs, contextType, contextId);
} catch (e) {
console.error("[PlaybackReporting] Failed to update playback context:", e);
}
}
}
/**
* Report playback progress to Jellyfin (or queue if offline)
*
* Note: Progress reports are frequent and are not queued for sync.
* The final position is captured by reportPlaybackStopped.
*
* TRACES: UR-005 | DR-028
*/
export async function reportPlaybackProgress(
itemId: string,
positionSeconds: number,
_isPaused = false
): Promise<void> {
const positionMs = Math.floor(positionSeconds * 1000);
const userId = auth.getUserId();
// Reduce logging for frequent progress updates
if (Math.floor(positionSeconds) % 30 === 0) {
console.log("[PlaybackReporting] reportPlaybackProgress - itemId:", itemId, "position:", positionSeconds);
}
// Update local DB only (progress updates are frequent, don't report to server)
if (userId) {
try {
await commands.storageUpdatePlaybackProgress(userId, itemId, positionMs);
} catch (e) {
console.error("[PlaybackReporting] Failed to update local progress:", e);
}
}
}
/**
* Report playback stopped to Jellyfin (or queue if offline)
*
* The Rust backend handles both local DB updates and server reporting,
* automatically queuing for sync if the server is unreachable.
*
* TRACES: UR-005, UR-025 | DR-028
*/
export async function reportPlaybackStopped(itemId: string, positionSeconds: number): Promise<void> {
const positionMs = Math.floor(positionSeconds * 1000);
const userId = auth.getUserId();
console.log("[PlaybackReporting] reportPlaybackStopped - itemId:", itemId, "positionSeconds:", positionSeconds);
// Update local DB first (always works, even offline)
if (userId) {
try {
await commands.storageUpdatePlaybackProgress(userId, itemId, positionMs);
} catch (e) {
console.error("[PlaybackReporting] Failed to update local progress:", e);
}
}
// Report to the server. Rust queues the position for the next reconnect if
// the server cannot be reached (DR-154), so a throw here means the report
// did not land *this time* — not that the position was lost.
if (userId && positionSeconds > 0) {
try {
const repo = auth.getRepository();
await repo.reportPlaybackStopped(itemId, positionMs);
} catch (e) {
console.warn("[PlaybackReporting] Stop-report did not reach the server; queued for sync:", e);
}
}
}
/**
* Mark an item as played (100% progress)
*
* TRACES: UR-025 | DR-028
*/
export async function markAsPlayed(itemId: string): Promise<void> {
const userId = auth.getUserId();
console.log("[PlaybackReporting] markAsPlayed - itemId:", itemId);
// Update local DB first
if (userId) {
try {
await commands.storageMarkPlayed(userId, itemId);
} catch (e) {
console.error("[PlaybackReporting] Failed to mark as played in local DB:", e);
}
}
// Try to report to server via repository (handles queuing internally)
try {
const repo = auth.getRepository();
const item = await repo.getItem(itemId);
if (item.durationMs) {
await repo.reportPlaybackStopped(itemId, item.durationMs);
}
} catch (e) {
console.error("[PlaybackReporting] Failed to report as played:", e);
}
}