fix(reader): read position from the API at open; never write a restored position
The reader page embedded a snapshot of reading state (position, bookmarks) server-side at render time. Browsers may reuse that HTML (heuristic caching, bfcache), so opening a book could restore a stale position — and worse, the restore's relocate auto-saved it back, overwriting a newer device push minutes later. A KOReader sync followed by opening the web reader would silently revert the row to the old web position; the row's source and the rendered page disagreed. The web reader is intrinsically tied to the server, so it has no business preserving reading state client-side: - The rendered page now carries only immutable book metadata. The reader fetches progress fresh (cache: no-store) from the existing progress API at open and restores with the same priority as before (page for fixed-layout, CFI, percentage, fresh start); a failed fetch opens at the start and writes nothing. Initial bookmarks likewise come from their endpoint instead of the embed; annotations already did. - Progress saves are gated on deliberate navigation only (page turns, keys, slider, search/TOC/bookmark/back-stack jumps, tap zones — each marks the session as user-moved). Restores and section-load relocations never write, so displaying a position can no longer clobber a newer one. A bfcache-resurrected page resets the flag and cannot write its frozen position either. This replaces the old five-second post-init suppression, which a stale page bypassed. - The server-rendered initial progress badges render a neutral placeholder until the first relocate fills them (sub-second). No API, schema, or sync-engine changes. Normal reading saves exactly as before — the first save now simply waits for the first real page turn.
This commit is contained in:
@@ -1,18 +1,15 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"bookhoard/internal/database"
|
||||
"bookhoard/internal/handlers"
|
||||
"bookhoard/internal/services"
|
||||
"bookhoard/internal/sync"
|
||||
"bookhoard/internal/utils"
|
||||
"bookhoard/templates"
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
@@ -69,21 +66,10 @@ func registerReaderRoutes(cfg *Config) {
|
||||
if !visible {
|
||||
return renderErrorPage(c, "Access denied", "access_denied")
|
||||
}
|
||||
// Get reading progress
|
||||
var progress database.ReadingProgress
|
||||
progress, err = cfg.Queries.GetReadingProgress(c.Request().Context(), database.GetReadingProgressParams{
|
||||
MediaItemID: pgtype.UUID{Bytes: parsedUUID, Valid: true},
|
||||
UserID: uuidToPGType(userUUID),
|
||||
})
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
progress = database.ReadingProgress{}
|
||||
}
|
||||
// Get bookmarks
|
||||
bookmarks, _ := cfg.Queries.GetMediaBookmarks(c.Request().Context(), database.GetMediaBookmarksParams{
|
||||
MediaItemID: pgtype.UUID{Bytes: parsedUUID, Valid: true},
|
||||
UserID: uuidToPGType(userUUID),
|
||||
})
|
||||
// Convert to template types
|
||||
// Convert to template types. Reading state is deliberately NOT
|
||||
// fetched or embedded: the reader pulls position, bookmarks, and
|
||||
// annotations from the APIs at open time so the page can never
|
||||
// carry (nor write back) a stale snapshot.
|
||||
mediaUUID, _ := uuid.FromBytes(mediaItem.ID.Bytes[0:16])
|
||||
libUUID, _ := uuid.FromBytes(mediaItem.LibraryID.Bytes[0:16])
|
||||
metadata := templates.ReaderMetadata{
|
||||
@@ -104,58 +90,9 @@ func registerReaderRoutes(cfg *Config) {
|
||||
TotalCharacters: mediaItem.TotalCharacters.Int64,
|
||||
EstimatedPages: sync.EstimatedPages(mediaItem.TotalCharacters.Int64),
|
||||
}
|
||||
// Progress conversion (inline)
|
||||
progressUUID, _ := uuid.FromBytes(progress.ID.Bytes[0:16])
|
||||
progressMediaUUID, _ := uuid.FromBytes(progress.MediaItemID.Bytes[0:16])
|
||||
progressUserUUID, _ := uuid.FromBytes(progress.UserID.Bytes[0:16])
|
||||
templateProgress := templates.ReadingProgress{
|
||||
ID: progressUUID.String(),
|
||||
MediaItemID: progressMediaUUID.String(),
|
||||
UserID: progressUserUUID.String(),
|
||||
CurrentPage: int(progress.CurrentPage.Int32),
|
||||
TotalPages: int(progress.TotalPages.Int32),
|
||||
Percentage: progress.Percentage.Float64 * 100,
|
||||
EpubCfi: textToString(progress.Epubcfi),
|
||||
LastReadAt: progress.LastReadAt.Time,
|
||||
Chapter: int(progress.Chapter.Int32),
|
||||
ChapterProgress: progress.ChapterProgress.Float64 * 100,
|
||||
FormatGroup: mediaItem.FormatGroup,
|
||||
}
|
||||
// Bookmarks conversion (inline, with loop)
|
||||
templateBookmarks := make([]templates.Bookmark, len(bookmarks))
|
||||
for i, b := range bookmarks {
|
||||
bookmarkUUID, _ := uuid.FromBytes(b.ID.Bytes[0:16])
|
||||
bookmarkMediaUUID, _ := uuid.FromBytes(b.MediaItemID.Bytes[0:16])
|
||||
bookmarkUserUUID, _ := uuid.FromBytes(b.UserID.Bytes[0:16])
|
||||
|
||||
var pageNumber *int
|
||||
if b.PageNumber.Valid {
|
||||
val := int(b.PageNumber.Int32)
|
||||
pageNumber = &val
|
||||
}
|
||||
|
||||
var chapterNumber *int
|
||||
if b.ChapterNumber.Valid {
|
||||
val := int(b.ChapterNumber.Int32)
|
||||
chapterNumber = &val
|
||||
}
|
||||
|
||||
templateBookmarks[i] = templates.Bookmark{
|
||||
ID: bookmarkUUID.String(),
|
||||
MediaItemID: bookmarkMediaUUID.String(),
|
||||
UserID: bookmarkUserUUID.String(),
|
||||
PageNumber: pageNumber,
|
||||
ChapterNumber: chapterNumber,
|
||||
CfiPosition: textToString(b.CfiPosition),
|
||||
Title: b.Title,
|
||||
Position: textToString(b.Position),
|
||||
Notes: textToString(b.Notes),
|
||||
CreatedAt: b.CreatedAt.Time,
|
||||
}
|
||||
}
|
||||
// 8. Render template
|
||||
// Render template
|
||||
var buf bytes.Buffer
|
||||
err = templates.Reader(user, metadata, templateProgress, templateBookmarks).Render(c.Request().Context(), &buf)
|
||||
err = templates.Reader(user, metadata).Render(c.Request().Context(), &buf)
|
||||
if err != nil {
|
||||
return renderErrorPage(c, "Error rendering reader", "render_error")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user