feat: ratings, shelves, sleep timer, EPUB export + fix TS errors
All checks were successful
Release / Docker / caddy (push) Successful in 38s
Release / Check ui (push) Successful in 39s
Release / Test backend (push) Successful in 57s
Release / Docker / ui (push) Successful in 1m49s
Release / Docker / runner (push) Successful in 3m12s
Release / Docker / backend (push) Successful in 3m46s
Release / Gitea Release (push) Successful in 13s

**Ratings (1–5 stars)**
- New `book_ratings` PB collection (session_id, user_id, slug, rating)
- `getBookRating`, `getBookAvgRating`, `setBookRating` in pocketbase.ts
- GET/POST /api/ratings/[slug] API route
- StarRating.svelte component with hover, animated stars, avg display
- Star rating shown on book detail page (desktop + mobile)

**Plan-to-Read shelf**
- `shelf` field added to `user_library` (reading/plan_to_read/completed/dropped)
- `updateBookShelf`, `getShelfMap` in pocketbase.ts
- PATCH /api/library/[slug] for shelf updates
- Shelf selector dropdown on book detail page (only when saved)
- Shelf tabs on library page to filter by category

**Sleep timer**
- `sleepUntil` state added to AudioStore
- Layout handles timer lifecycle (survives chapter navigation)
- Cycles Off → 15m → 30m → 45m → 60m → Off
- Shows live countdown in AudioPlayer when active

**EPUB export**
- Go backend: GET /api/export/{slug}?from=N&to=N
- Generates valid EPUB2 zip (mimetype uncompressed, OPF, NCX, XHTML chapters)
- Markdown → HTML via goldmark
- SvelteKit proxy at /api/export/[slug]
- Download button on book detail page (only when in library)

**Fix TS errors**
- discover/+page.svelte: currentBook possibly undefined (use {@const book})
- cardEl now $state for reactive binding

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Admin
2026-04-02 21:50:04 +05:00
parent 809dc8d898
commit 08361172c6
17 changed files with 752 additions and 32 deletions

View File

@@ -0,0 +1,29 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { backendFetch } from '$lib/server/scraper';
export const GET: RequestHandler = async ({ params, url }) => {
const { slug } = params;
const from = url.searchParams.get('from');
const to = url.searchParams.get('to');
const qs = new URLSearchParams();
if (from) qs.set('from', from);
if (to) qs.set('to', to);
const query = qs.size ? `?${qs}` : '';
const res = await backendFetch(`/api/export/${encodeURIComponent(slug)}${query}`);
if (!res.ok) {
const text = await res.text().catch(() => '');
error(res.status as Parameters<typeof error>[0], text || 'Export failed');
}
const bytes = await res.arrayBuffer();
return new Response(bytes, {
headers: {
'Content-Type': 'application/epub+zip',
'Content-Disposition': `attachment; filename="${slug}.epub"`
}
});
};

View File

@@ -1,6 +1,7 @@
import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { saveBook, unsaveBook } from '$lib/server/pocketbase';
import { saveBook, unsaveBook, updateBookShelf } from '$lib/server/pocketbase';
import type { ShelfName } from '$lib/server/pocketbase';
import { log } from '$lib/server/logger';
/**
@@ -32,3 +33,17 @@ export const DELETE: RequestHandler = async ({ params, locals }) => {
}
return json({ ok: true });
};
/**
* PATCH /api/library/[slug]
* Update the shelf category for a saved book.
*/
export const PATCH: RequestHandler = async ({ params, request, locals }) => {
const { slug } = params;
const body = await request.json().catch(() => null);
const shelf = body?.shelf ?? '';
const VALID = ['', 'plan_to_read', 'completed', 'dropped'];
if (!VALID.includes(shelf)) error(400, 'invalid shelf');
await updateBookShelf(locals.sessionId, slug, shelf as ShelfName, locals.user?.id);
return json({ ok: true });
};

View File

@@ -0,0 +1,24 @@
import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getBookRating, getBookAvgRating, setBookRating } from '$lib/server/pocketbase';
export const GET: RequestHandler = async ({ params, locals }) => {
const { slug } = params;
const [userRating, avg] = await Promise.all([
getBookRating(locals.sessionId, slug, locals.user?.id),
getBookAvgRating(slug)
]);
return json({ userRating, avg: avg.avg, count: avg.count });
};
export const POST: RequestHandler = async ({ params, request, locals }) => {
const { slug } = params;
const body = await request.json().catch(() => null);
const rating = body?.rating;
if (typeof rating !== 'number' || rating < 1 || rating > 5) {
error(400, 'rating must be 15');
}
await setBookRating(locals.sessionId, slug, rating, locals.user?.id);
const avg = await getBookAvgRating(slug);
return json({ ok: true, avg: avg.avg, count: avg.count });
};