Routes (additiv unter /api/v1/marketplace/*):
Discovery (optional-auth, anonymer Read erlaubt):
- GET /explore — featured + trending side-by-side
- GET /decks — browse mit q/tag/language/author/sort/limit/offset
(sort: recent | popular | trending; trending = star-velocity 7d)
- GET /tags — flacher Tag-Tree
Engagement (auth pro Schreib-Route, optional-auth für GET state):
- POST/DELETE/GET /decks/:slug/star
- POST/DELETE/GET /authors/:slug/follow (cannot-follow-self → 409)
Subscribe + Version-Read + Smart-Merge-Diff:
- POST/DELETE/GET /decks/:slug/subscribe
- GET /me/subscriptions (mit update_available-Indicator)
- GET /decks/:slug/versions/:semver — voller Cards-Payload in ord-
Reihenfolge
- GET /decks/:slug/diff?from=:semver — computeDiff (added/changed/
removed/unchanged) basierend auf content_hash + ord-Heuristik für
"changed an gleicher Position"
Fork + Smart-Merge-Pull (auth):
- POST /decks/:slug/fork — kopiert latest version in privaten
cards.decks (forked_from_marketplace_* gesetzt) + cards.cards mit
übernommenem content_hash + frische FSRS-Reviews
- POST /private/:deckId/pull-update — Smart-Merge: existing private
hashes deduplizieren, nur added/changed cards einfügen (mit fresh
reviews), unveränderte Karten BEHALTEN inkl. FSRS-State, removed
cards bleiben lokal (server-authoritative User-Choice). Update der
forked_from_marketplace_version_id auf latest.
Schema (R3a):
- cards.decks: 2 neue Columns forked_from_marketplace_deck_id +
forked_from_marketplace_version_id (text, nullable). Drizzle-push
grün.
Architektur-Highlights:
- @cards/domain.cardContentHash ist die single source of truth für
Karten-Hashing; marketplace.deck_cards und cards.cards berechnen
identisch → Smart-Merge ist hash-equality + INSERT-IGNORE statt
Diff-Replay
- pgSchema-Trennung (marketplace.* vs. cards.*) zahlt sich aus:
Marketplace-Read-Path (Public + Engagement) und privater Lern-Pfad
haben separate FK-Welten und können unabhängig versioniert werden
- Hono-Middleware-Pattern: per-route authMiddleware/optionalAuth statt
Sub-Router-Mount, weil ein Wildcard '*' auf einem Sub-Router via
r.route('/', sub) sonst die Public-GET-Routes des Parents fängt
(Hono-Routing-Subtilität, kostete eine Smoke-Iteration)
Verifikation:
- type-check 0 errors
- 6 neue Diff-Heuristik-Tests, 78 gesamt grün
- End-to-End-Smoke gegen lokale cards-api:
· Cardecky-Author + Deck `r3-stoische-grundbegriffe` v1.0.0 (3 Karten)
· Till browst (anon → 200), starred, folgt Cardecky, subscribed
· Till forkt → privates Deck mit 3 Karten + 3 fresh FSRS-Reviews
· SQL-Manipulation: Apatheia-Review auf state='review',
stability=10, reps=3 (simuliert "schon gelernt")
· Cardecky publisht v1.1.0: Apatheia + Eudaimonia unverändert,
Logos präzisiert (changed), Tugendlehre neu (added)
· Diff-Endpoint zeigt: unchanged=2, changed=1, added=1, removed=0
· Till pull-update → cards_inserted=2 (changed.next + added)
· Verifikation: card_count=5 (war 3), Apatheia-Review **identisch
erhalten** (state=review, stability=10, reps=3, last_review IS
NOT NULL), neue Karten state=new — FSRS-State der unveränderten
Karte überlebt Smart-Merge unverletzt
Verbleibend: R4 ε (PRs + Card-Discussions), R5 Frontend-Routes
(/explore, /d/[slug], /u/[slug], /me/subscribed, /me/forks), R6
voller UI-E2E.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
/**
|
|
* Smart-Merge-Diff zwischen zwei Versionen oder zwischen einer Version
|
|
* und der Hash-Liste eines privaten geforkten Decks.
|
|
*
|
|
* Klassifikation:
|
|
* - **unchanged**: content_hash identisch zwischen alt und neu →
|
|
* unveränderte Karte, FSRS-State bleibt.
|
|
* - **added**: Hash nur in neu → komplett neue Karte.
|
|
* - **removed**: Hash nur in alt → Karte aus dem Deck entfernt.
|
|
* - **changed** (Heuristik): Karte am selben `ord` ist „added" und
|
|
* gleichzeitig an demselben `ord` eine „removed". Pull-Requests
|
|
* bringen später eine echte Karten-Lineage; bis dahin reicht die
|
|
* Position-Heuristik.
|
|
*
|
|
* Geport aus
|
|
* `cards-decommission-base:services/cards-server/src/services/subscriptions.ts`
|
|
* (`diffSince`-Funktion, ord-Pairing-Heuristik 1:1).
|
|
*/
|
|
|
|
export interface DiffCardForHash {
|
|
contentHash: string;
|
|
ord: number;
|
|
}
|
|
|
|
export interface DiffCardFull {
|
|
contentHash: string;
|
|
type: string;
|
|
fields: Record<string, string>;
|
|
ord: number;
|
|
}
|
|
|
|
export interface DiffPayload {
|
|
from: { semver?: string; versionId?: string };
|
|
to: { semver: string; versionId: string };
|
|
added: DiffCardFull[];
|
|
changed: { previous: { contentHash: string }; next: DiffCardFull }[];
|
|
unchanged: { contentHash: string; ord: number }[];
|
|
removed: { contentHash: string }[];
|
|
}
|
|
|
|
export interface ComputeDiffInput {
|
|
from: DiffCardForHash[];
|
|
to: DiffCardFull[];
|
|
fromInfo: { semver?: string; versionId?: string };
|
|
toInfo: { semver: string; versionId: string };
|
|
}
|
|
|
|
export function computeDiff(input: ComputeDiffInput): DiffPayload {
|
|
const { from, to, fromInfo, toInfo } = input;
|
|
const fromHashes = new Set(from.map((c) => c.contentHash));
|
|
const toHashes = new Set(to.map((c) => c.contentHash));
|
|
|
|
const unchanged: { contentHash: string; ord: number }[] = [];
|
|
|
|
// Build ord → hash map of removed cards (in `from` but not in `to`).
|
|
const removedByOrd = new Map<number, string>();
|
|
for (const c of from) {
|
|
if (!toHashes.has(c.contentHash)) removedByOrd.set(c.ord, c.contentHash);
|
|
}
|
|
|
|
const added: DiffCardFull[] = [];
|
|
const changed: { previous: { contentHash: string }; next: DiffCardFull }[] = [];
|
|
|
|
for (const c of to) {
|
|
if (fromHashes.has(c.contentHash)) {
|
|
unchanged.push({ contentHash: c.contentHash, ord: c.ord });
|
|
} else if (removedByOrd.has(c.ord)) {
|
|
const previousHash = removedByOrd.get(c.ord)!;
|
|
removedByOrd.delete(c.ord);
|
|
changed.push({ previous: { contentHash: previousHash }, next: c });
|
|
} else {
|
|
added.push(c);
|
|
}
|
|
}
|
|
|
|
// Was in removedByOrd übrig bleibt = echte Removals (nicht ord-paired).
|
|
const removed: { contentHash: string }[] = [...removedByOrd.values()].map((h) => ({
|
|
contentHash: h,
|
|
}));
|
|
|
|
return {
|
|
from: fromInfo,
|
|
to: toInfo,
|
|
added,
|
|
changed,
|
|
unchanged,
|
|
removed,
|
|
};
|
|
}
|