Phase 9j: Anki-Re-Import-Dedupe via content_hash

Neuer Domain-Helper cardContentHash({ type, fields }) — SHA-256 über
canonisierten JSON ({type, sorted-fields}), pure Web-Crypto. Field-
Reihenfolge ist invariant; Whitespace + Cloze-Markup zählen mit
(zwei Karten mit identischem Text aber unterschiedlichem
{{c1::…}}-Markup sind verschiedene Karten).

cards-API POST schreibt content_hash automatisch in den schon
existierenden Schema-Slot. Neuer Endpoint GET /api/v1/cards/hashes
liefert die kompakte Hash-Liste des Users (ohne Card-Body) — eine
Anfrage pro Anki-Import statt pro Karte.

apps/web/src/lib/anki/import.ts holt die Hashes vor dem Loop und
prüft pro Karte clientseitig. Duplikate werden gezählt
(cardsSkippedDuplicate) und übersprungen, der Counter erscheint
in der AnkiImport-Done-View. Same-File-Drift (Anki-interne
Doppel-Notes) wird auch erkannt — nach erfolgreichem Insert
landet der Hash sofort im Set.

Fallback: wenn /hashes fehlschlägt (älterer Server), bleibt das
Dedupe-Set leer und Karten werden eingefügt wie zuvor — kein
Hard-Bruch.

Pre-Phase-9j-Karten haben null content_hash (Hashes-Endpoint
filtert sie weg) — sie können also irrtümlich erneut eingespielt
werden, falls noch im Anki-File. Pragmatisch akzeptiert: ein
Backfill-Script wäre Phase-10-Polish, sobald Live-User da sind.

5 neue Domain-Tests, 1 neuer API-Auth-Gate-Test (105 grün ges.:
51 + 49 + 5). svelte-check 380 files 0 errors. E2E gegen lokale
Postgres bestätigt: neue Karte hat content_hash (64-char-hex),
/hashes listet sie.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-05-08 18:29:56 +02:00
parent 4b451f1b8d
commit 593d4475df
10 changed files with 176 additions and 8 deletions

View file

@ -4,6 +4,7 @@ import { Hono } from 'hono';
import {
CardCreateSchema,
CardUpdateSchema,
cardContentHash,
newReview,
subIndexCount,
subIndexCountForCloze,
@ -66,6 +67,10 @@ export function cardsRouter(deps: CardsDeps = {}): Hono<{ Variables: AuthVars }>
const cardId = ulid();
const now = new Date();
const subIndices = Array.from({ length: count }, (_, i) => i);
const contentHash = await cardContentHash({
type: parsed.data.type,
fields: parsed.data.fields,
});
const [cardRow] = await dbOf().transaction(async (tx) => {
const [card] = await tx
@ -77,6 +82,7 @@ export function cardsRouter(deps: CardsDeps = {}): Hono<{ Variables: AuthVars }>
type: parsed.data.type,
fields: parsed.data.fields,
mediaRefs: parsed.data.media_refs ?? [],
contentHash,
createdAt: now,
updatedAt: now,
})
@ -120,6 +126,24 @@ export function cardsRouter(deps: CardsDeps = {}): Hono<{ Variables: AuthVars }>
return c.json({ cards: rows.map(toCardDto), total: rows.length });
});
/**
* Liefert nur die content_hash-Liste des Users kompakter Pfad für
* den Anki-Re-Import-Dedupe. Frontend lädt das einmal und prüft pro
* Karte clientseitig, statt für jeden Insert einen Round-Trip zu
* machen. Karten ohne content_hash (Pre-Phase-9j) werden weggefiltert.
*/
r.get('/hashes', async (c) => {
const userId = c.get('userId');
const rows = await dbOf()
.select({ contentHash: cards.contentHash })
.from(cards)
.where(eq(cards.userId, userId));
const hashes = rows
.map((r) => r.contentHash)
.filter((h): h is string => typeof h === 'string' && h.length > 0);
return c.json({ hashes, total: hashes.length });
});
r.get('/:id', async (c) => {
const userId = c.get('userId');
const id = c.req.param('id');

View file

@ -28,6 +28,12 @@ describe('cardsRouter — auth-gate', () => {
const res = await app.request('/api/v1/cards');
expect(res.status).toBe(401);
});
it('GET /hashes ohne X-User-Id ist 401', async () => {
const { app } = buildApp();
const res = await app.request('/api/v1/cards/hashes');
expect(res.status).toBe(401);
});
});
describe('cardsRouter — Input-Validation', () => {