readest/docker/volumes/db/init/schema.sql
Huang Xin 045beb4d1a
feat(crosspoint): sync CrossPoint 2.0 clippings with Readest highlights (#7317)
* feat(crosspoint): sync CrossPoint 2.0 clippings with Readest highlights

CrossPoint 2.0's Sync Clippings first probes
{KOSync server}/api/v1/clippings/{document} for sync_version 2, and stops
the whole sync, progress included, when the probe fails. Readest's KOSync
endpoint had no such route, so a reader signed in with the Readest plugin
showed "Could Not Sync Clippings" and stopped syncing progress once the
setting was on.

- /api/crosspoint/api/v1/clippings/{document} serves the clippings API
  (sync_version 2, cursor paging, tombstones). A clipping is a book_notes
  highlight whose new `crosspoint` column holds the reader's anchor (spine
  plus a code-point range), the way xpointer0/xpointer1 hold KOReader's.
  Only items the firmware can decode are sent, up to the 256 an X3/X4
  holds per book. A rewritten copy (Optimize EPUB) syncs as the library
  book it is linked to, and its first clippings move to that book when
  its progress upload links it.
- Readest apps turn a clipping into a CFI when they open the book, and
  give each EPUB highlight a CrossPoint anchor when they push it (tried
  once a session). Moving a highlight's range drops the old anchors.
- Migration 033 adds book_notes.crosspoint (nullable jsonb). Apply it
  before the web deploy.
- Plugin README: CrossPoint 2.0 names and requirements, and highlights.

Fails without the change: useNotesSync-crosspoint (4 of 5, e.g. "turns a
clipping a reader synced into a highlight at its CFI"), transform "carries
the anchor in the crosspoint column, both ways", useAnnotationEditor "a
moved range drops the KOReader and CrossPoint anchors of the old one"
(expected the old xpointer to be undefined), and the route test "moves a
rewritten copy's clippings to the library book it gets linked to". The
clippings route tests can't run without the route.

Verified on a real X4 (CrossPoint 2.0.0) with Sync Clippings on: the
probe and uploads answer 200, a reader clipping shows in Readest on its
words, a Readest highlight shows on the X4, and deletions go both ways.

Reported by itsthisjustin (CrossPoint).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(crosspoint): sync a document a reader names by its file name

A reader set to match documents by file name (CrossPoint's default, and
the alternate lookup of its Smart Sync) sends md5 of the book's file
name, as KOReader does. Since #7237 Readest keeps the name a book was
imported under (books.original_filename), and #7253 digests it the same
way in Readest's own KOSync client.

Progress, clippings and reading sessions now resolve such a document to
the library book imported under that name, link it on first sight like a
rewritten copy, and move clippings saved under the name's id to the book.
When two books were imported under one name, the most recently read one
wins. A document that is a book's own id is never looked up by name, and
an NFD spelling of the name does not match.

Fails without the change: 5 of the 6 tests in "a document named by its
file name", e.g. "syncs it as the library book imported under that name,
linked on first sight" (the link write and the library book's queries
never happen). The sixth, a name no book has, passed before too.

Not run on a device: the books in the test library were imported before
#7237 and have no original_filename.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(crosspoint): page the file-name lookup, and retry a failed clipping move

From CodeRabbit's review of this PR:

- The lookup of the book imported under a file name read the user's books
  in one request, and PostgREST answers at most 1000 rows (db-max-rows),
  so a match past them was never found. It now pages through them by
  book_hash.
- A copy was linked before its clippings moved to the library book, and a
  failed move only logged a warning. Every later request followed the
  link, so those clippings stayed under the copy's id for good. They now
  move first, and a failed move fails that sync, leaving the copy
  unlinked so the next sync tries again.

Fails without the change: "finds the book past the first 1000 imported
under a name" (no link written: expected [ …(20) ] to deeply equal
ArrayContaining{…}), and "links a copy only once its clippings have
moved, so a failed move is tried again" (expected 200 to be 500).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:22:58 +01:00

144 lines
No EOL
6.1 KiB
PL/PgSQL

ALTER FUNCTION auth.uid() OWNER TO supabase_auth_admin;
ALTER FUNCTION auth.role() OWNER TO supabase_auth_admin;
CREATE TABLE public.books (
user_id uuid NOT NULL,
book_hash text NOT NULL,
meta_hash text NULL,
format text NULL,
title text NULL,
source_title text NULL,
original_filename text NULL,
author text NULL,
"group" text NULL,
tags text[] NULL,
created_at timestamp with time zone NULL DEFAULT now(),
updated_at timestamp with time zone NULL DEFAULT now(),
deleted_at timestamp with time zone NULL,
uploaded_at timestamp with time zone NULL,
synced_at timestamp with time zone NOT NULL DEFAULT now(),
progress integer[] NULL,
reading_status text NULL,
reading_status_updated_at timestamp with time zone NULL,
cover_hash text NULL,
cover_updated_at timestamp with time zone NULL,
metadata_updated_at timestamp with time zone NULL,
group_id text NULL,
group_name text NULL,
group_updated_at timestamp with time zone NULL,
page_count integer NULL,
metadata json NULL,
CONSTRAINT books_pkey PRIMARY KEY (user_id, book_hash),
CONSTRAINT books_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
);
-- Server-assigned incremental-pull cursor, decoupled from updated_at (the
-- client event time / sort key). A trigger stamps it on every write so a
-- server-resolved merge propagates without reordering the date-read library.
-- See migration 016_add_books_synced_at.sql (issue #4678).
CREATE INDEX idx_books_user_synced ON public.books (user_id, synced_at);
CREATE OR REPLACE FUNCTION public.set_books_synced_at()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.synced_at := now();
RETURN NEW;
END;
$$;
CREATE TRIGGER books_set_synced_at
BEFORE INSERT OR UPDATE ON public.books
FOR EACH ROW
EXECUTE FUNCTION public.set_books_synced_at();
ALTER TABLE public.books ENABLE ROW LEVEL SECURITY;
CREATE POLICY select_books ON public.books FOR SELECT TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE POLICY insert_books ON public.books FOR INSERT TO authenticated WITH CHECK ((SELECT auth.uid()) = user_id);
CREATE POLICY update_books ON public.books FOR UPDATE TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE POLICY delete_books ON public.books FOR DELETE TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE TABLE public.book_configs (
user_id uuid NOT NULL,
book_hash text NOT NULL,
meta_hash text NULL,
location text NULL,
xpointer text NULL,
progress jsonb NULL,
rsvp_position text NULL,
search_config jsonb NULL,
view_settings jsonb NULL,
tts_packs jsonb NULL,
created_at timestamp with time zone NULL DEFAULT now(),
updated_at timestamp with time zone NULL DEFAULT now(),
deleted_at timestamp with time zone NULL,
CONSTRAINT book_configs_pkey PRIMARY KEY (user_id, book_hash),
CONSTRAINT book_configs_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
);
ALTER TABLE public.book_configs ENABLE ROW LEVEL SECURITY;
CREATE POLICY select_book_configs ON public.book_configs FOR SELECT TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE POLICY insert_book_configs ON public.book_configs FOR INSERT TO authenticated WITH CHECK ((SELECT auth.uid()) = user_id);
CREATE POLICY update_book_configs ON public.book_configs FOR UPDATE TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE POLICY delete_book_configs ON public.book_configs FOR DELETE TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE TABLE public.book_notes (
user_id uuid NOT NULL,
book_hash text NOT NULL,
meta_hash text NULL,
id text NOT NULL,
type text NULL,
cfi text NULL,
xpointer0 text NULL,
xpointer1 text NULL,
text text NULL,
style text NULL,
color text NULL,
note text NULL,
page integer NULL,
global boolean NULL,
ink jsonb NULL,
crosspoint jsonb NULL,
created_at timestamp with time zone NULL DEFAULT now(),
updated_at timestamp with time zone NULL DEFAULT now(),
deleted_at timestamp with time zone NULL,
CONSTRAINT book_notes_pkey PRIMARY KEY (user_id, book_hash, id),
CONSTRAINT book_notes_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
);
ALTER TABLE public.book_notes ENABLE ROW LEVEL SECURITY;
CREATE POLICY select_book_notes ON public.book_notes FOR SELECT TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE POLICY insert_book_notes ON public.book_notes FOR INSERT TO authenticated WITH CHECK ((SELECT auth.uid()) = user_id);
CREATE POLICY update_book_notes ON public.book_notes FOR UPDATE TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE POLICY delete_book_notes ON public.book_notes FOR DELETE TO authenticated USING ((SELECT auth.uid()) = user_id);
CREATE TABLE public.files (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
book_hash text NULL,
file_key text NOT NULL,
file_size bigint NOT NULL,
created_at timestamp with time zone NULL DEFAULT now(),
updated_at timestamp with time zone NULL DEFAULT now(),
deleted_at timestamp with time zone NULL,
CONSTRAINT files_pkey PRIMARY KEY (id),
CONSTRAINT files_file_key_key UNIQUE (file_key),
CONSTRAINT files_file_size_nonnegative CHECK (file_size >= 0),
CONSTRAINT files_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
);
CREATE INDEX idx_files_user_id_deleted_at ON public.files (user_id, deleted_at);
CREATE INDEX idx_files_file_key ON public.files (file_key);
CREATE INDEX idx_files_file_key_deleted_at ON public.files (file_key, deleted_at);
ALTER TABLE public.files ENABLE ROW LEVEL SECURITY;
CREATE POLICY files_insert ON public.files FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY files_select ON public.files FOR SELECT USING (auth.uid() = user_id AND deleted_at IS NULL);
CREATE POLICY files_update ON public.files FOR UPDATE USING (auth.uid() = user_id) WITH CHECK (deleted_at IS NULL OR deleted_at > now());
CREATE POLICY files_delete ON public.files FOR DELETE USING (auth.uid() = user_id);
GRANT ALL ON public.books TO authenticated;
GRANT ALL ON public.book_configs TO authenticated;
GRANT ALL ON public.book_notes TO authenticated;
GRANT SELECT ON public.files TO authenticated;