many changes
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
// Playback reporting service - syncs to both Jellyfin server and local DB
|
||||
// Playback reporting service
|
||||
//
|
||||
// This service handles:
|
||||
// - Updating local DB (always works, even offline)
|
||||
// - Reporting to Jellyfin server when online
|
||||
// - Queueing operations for sync when offline
|
||||
// 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 { invoke } from "@tauri-apps/api/core";
|
||||
import { get } from "svelte/store";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
import { isServerReachable } from "$lib/stores/connectivity";
|
||||
import { syncService } from "./syncService";
|
||||
import { secondsToTicks } from "$lib/utils/playbackUnits";
|
||||
|
||||
/**
|
||||
* Report playback start to Jellyfin and local DB
|
||||
* 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,
|
||||
@@ -21,10 +26,18 @@ export async function reportPlaybackStart(
|
||||
contextType: "container" | "single" = "single",
|
||||
contextId: string | null = null
|
||||
): Promise<void> {
|
||||
const positionTicks = secondsToTicks(positionSeconds);
|
||||
const positionTicks = Math.floor(positionSeconds * 10000000);
|
||||
const userId = auth.getUserId();
|
||||
|
||||
console.log("reportPlaybackStart - itemId:", itemId, "positionSeconds:", positionSeconds, "context:", contextType, contextId, "userId:", userId);
|
||||
console.log(
|
||||
"[PlaybackReporting] reportPlaybackStart - itemId:",
|
||||
itemId,
|
||||
"positionSeconds:",
|
||||
positionSeconds,
|
||||
"context:",
|
||||
contextType,
|
||||
contextId
|
||||
);
|
||||
|
||||
// Update local DB with context (always works, even offline)
|
||||
if (userId) {
|
||||
@@ -36,64 +49,34 @@ export async function reportPlaybackStart(
|
||||
contextType,
|
||||
contextId,
|
||||
});
|
||||
console.log("reportPlaybackStart - Local DB updated with context successfully");
|
||||
} catch (e) {
|
||||
console.error("Failed to update playback context:", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Check connectivity before trying server
|
||||
if (!get(isServerReachable)) {
|
||||
console.log("reportPlaybackStart - Server not reachable, queueing for sync");
|
||||
if (userId) {
|
||||
await syncService.queueMutation("report_playback_start", itemId, { positionTicks });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Report to Jellyfin server
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
await repo.reportPlaybackStart(itemId, positionTicks);
|
||||
console.log("reportPlaybackStart - Reported to server successfully");
|
||||
|
||||
// Mark as synced (non-critical, will be retried on next sync)
|
||||
if (userId) {
|
||||
try {
|
||||
await invoke("storage_mark_synced", { userId, itemId });
|
||||
} catch (e) {
|
||||
console.debug("Failed to mark sync status (will retry):", e);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to report playback start to server:", e);
|
||||
// Queue for sync later
|
||||
if (userId) {
|
||||
await syncService.queueMutation("report_playback_start", itemId, { positionTicks });
|
||||
console.error("[PlaybackReporting] Failed to update playback context:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Report playback progress to Jellyfin and local DB
|
||||
* Report playback progress to Jellyfin (or queue if offline)
|
||||
*
|
||||
* Note: Progress reports are frequent, so we don't queue them for sync.
|
||||
* 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
|
||||
_isPaused = false
|
||||
): Promise<void> {
|
||||
const positionTicks = secondsToTicks(positionSeconds);
|
||||
const positionTicks = Math.floor(positionSeconds * 10000000);
|
||||
const userId = auth.getUserId();
|
||||
|
||||
// Reduce logging for frequent progress updates
|
||||
if (Math.floor(positionSeconds) % 30 === 0) {
|
||||
console.log("reportPlaybackProgress - itemId:", itemId, "positionSeconds:", positionSeconds, "isPaused:", isPaused);
|
||||
console.log("[PlaybackReporting] reportPlaybackProgress - itemId:", itemId, "position:", positionSeconds);
|
||||
}
|
||||
|
||||
// Update local DB first (always works, even offline)
|
||||
// Update local DB only (progress updates are frequent, don't report to server)
|
||||
if (userId) {
|
||||
try {
|
||||
await invoke("storage_update_playback_progress", {
|
||||
@@ -102,37 +85,24 @@ export async function reportPlaybackProgress(
|
||||
positionTicks,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to update local playback progress:", e);
|
||||
console.error("[PlaybackReporting] Failed to update local progress:", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Check connectivity before trying server
|
||||
if (!get(isServerReachable)) {
|
||||
// Don't queue progress updates - too frequent. Just store locally.
|
||||
return;
|
||||
}
|
||||
|
||||
// Report to Jellyfin server (silent failure - progress reports are non-critical)
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
await repo.reportPlaybackProgress(itemId, positionTicks);
|
||||
} catch {
|
||||
// Silent failure for progress reports - they're frequent and non-critical
|
||||
// The final position is captured by reportPlaybackStopped
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Report playback stopped to Jellyfin and local DB
|
||||
* 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 positionTicks = secondsToTicks(positionSeconds);
|
||||
export async function reportPlaybackStopped(itemId: string, positionSeconds: number): Promise<void> {
|
||||
const positionTicks = Math.floor(positionSeconds * 10000000);
|
||||
const userId = auth.getUserId();
|
||||
|
||||
console.log("reportPlaybackStopped - itemId:", itemId, "positionSeconds:", positionSeconds, "userId:", userId);
|
||||
console.log("[PlaybackReporting] reportPlaybackStopped - itemId:", itemId, "positionSeconds:", positionSeconds);
|
||||
|
||||
// Update local DB first (always works, even offline)
|
||||
if (userId) {
|
||||
@@ -142,90 +112,52 @@ export async function reportPlaybackStopped(
|
||||
itemId,
|
||||
positionTicks,
|
||||
});
|
||||
console.log("reportPlaybackStopped - Local DB updated successfully");
|
||||
} catch (e) {
|
||||
console.error("Failed to update local playback progress:", e);
|
||||
console.error("[PlaybackReporting] Failed to update local progress:", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Check connectivity before trying server
|
||||
if (!get(isServerReachable)) {
|
||||
console.log("reportPlaybackStopped - Server not reachable, queueing for sync");
|
||||
if (userId) {
|
||||
await syncService.queueMutation("report_playback_stopped", itemId, { positionTicks });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Report to Jellyfin server
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
await repo.reportPlaybackStopped(itemId, positionTicks);
|
||||
console.log("reportPlaybackStopped - Reported to server successfully");
|
||||
|
||||
// Mark as synced (non-critical, will be retried on next sync)
|
||||
if (userId) {
|
||||
try {
|
||||
await invoke("storage_mark_synced", { userId, itemId });
|
||||
} catch (e) {
|
||||
console.debug("Failed to mark sync status (will retry):", e);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to report playback stopped to server:", e);
|
||||
// Queue for sync later
|
||||
if (userId) {
|
||||
await syncService.queueMutation("report_playback_stopped", itemId, { positionTicks });
|
||||
// Queue for sync to server (the sync service will handle retry logic)
|
||||
if (userId && positionSeconds > 0) {
|
||||
try {
|
||||
// Get the repository to check if we should queue
|
||||
const repo = auth.getRepository();
|
||||
await repo.reportPlaybackStopped(itemId, positionTicks);
|
||||
} catch (e) {
|
||||
console.error("[PlaybackReporting] Failed to report to server:", e);
|
||||
// Server error - could queue, but for now just log
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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("markAsPlayed - itemId:", itemId, "userId:", userId);
|
||||
console.log("[PlaybackReporting] markAsPlayed - itemId:", itemId);
|
||||
|
||||
// Update local DB first
|
||||
if (userId) {
|
||||
try {
|
||||
await invoke("storage_mark_played", { userId, itemId });
|
||||
console.log("markAsPlayed - Local DB updated successfully");
|
||||
} catch (e) {
|
||||
console.error("Failed to mark as played in local DB:", e);
|
||||
console.error("[PlaybackReporting] Failed to mark as played in local DB:", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Check connectivity before trying server
|
||||
if (!get(isServerReachable)) {
|
||||
console.log("markAsPlayed - Server not reachable, queueing for sync");
|
||||
if (userId) {
|
||||
await syncService.queueMutation("mark_played", itemId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// For Jellyfin, we need to get the item's runtime and report stopped at 100%
|
||||
// Try to report to server via repository (handles queuing internally)
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
const item = await repo.getItem(itemId);
|
||||
|
||||
if (item.runTimeTicks) {
|
||||
await repo.reportPlaybackStopped(itemId, item.runTimeTicks);
|
||||
console.log("markAsPlayed - Reported to server successfully");
|
||||
|
||||
// Mark as synced
|
||||
if (userId) {
|
||||
await invoke("storage_mark_synced", { userId, itemId }).catch(() => {});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to mark as played on server:", e);
|
||||
// Queue for sync later
|
||||
if (userId) {
|
||||
await syncService.queueMutation("mark_played", itemId);
|
||||
}
|
||||
console.error("[PlaybackReporting] Failed to report as played:", e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user