First working POC
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// Favorites service - Handles toggling favorite status with optimistic updates
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
|
||||
/**
|
||||
* 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 invoke("storage_toggle_favorite", {
|
||||
userId,
|
||||
itemId,
|
||||
isFavorite: newIsFavorite,
|
||||
});
|
||||
|
||||
// 2. Sync to Jellyfin server
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
if (newIsFavorite) {
|
||||
await repo.markFavorite(itemId);
|
||||
} else {
|
||||
await repo.unmarkFavorite(itemId);
|
||||
}
|
||||
|
||||
// 3. Mark as synced
|
||||
await invoke("storage_mark_synced", { userId, itemId });
|
||||
} catch (error) {
|
||||
console.error("Failed to sync favorite to server:", error);
|
||||
// Favorite is stored locally and will be synced later
|
||||
// via sync queue (when implemented)
|
||||
}
|
||||
|
||||
return newIsFavorite;
|
||||
}
|
||||
Reference in New Issue
Block a user