V3 S5 : graphe documentaire + doublons + collections
Backend : resource_links + scan_session_id (000021), réconciliation déterministe (same_scan outbox, doublons SHA-256 à l'upload + backfill, same_issuer/référence via faits effectifs, verdicts préservés, élagage auto), GET /files/:id/relations + verdict (owner-only), GET /duplicates (groupes + gain récupérable). Tests service + handlers + migrations. Mobile (Room v22) : links + collections, scanSessionId dans les creates de scan, LinksRepository (sync + verdicts REST), « Voir aussi » dans la fiche (verdict infirmer/re-confirmer), écran Doublons (garder le plus récent), collections locales (sauver/rejouer/supprimer). FR/EN. Contrat docs/api-v1.md (/relations, pas /links — pris par le partage), V3.md (S5 livré, version_of non matérialisé).
This commit is contained in:
@@ -13,6 +13,7 @@ Le format suit [Keep a Changelog](https://keepachangelog.com/fr/1.1.0/) et le ve
|
||||
- Extract générique V3 (S2 backend) : package `extract` (règles FR stdlib — dates/échéances/périodes/montants/émetteurs/références masquées/`doc_type` suggéré, confiances, corpus testé), table `resource_facts` (migration `000019`, `origin` auto/user), hook worker OCR (`OnText` + backfill boot borné, best-effort, anti-panic), `GET /files/:id/facts` (viewer+, `[]` si vide, 404 si trashé/étranger).
|
||||
- Corrections V3 (S3) : `PATCH /files/:id/facts` + op `correct_facts` (full-replace du jeu `user`, editor+, validation Go → 400 jamais 500), Room v20 (`resource_facts` + `FactsDao`, migration 19→20), fiche « Ce qu'on a compris » sur le viewer (chips, confiance grisée + suggéré, édition par type, reset auto), `FactsRepository` (sync remplace-en-bloc, correction optimiste + outbox atomique), `GET /files/:id/facts` consommé côté client.
|
||||
- Recherche V3 (S4) : colonne `search_tsv` (migration `000020`, `french`, index GIN) + `SearchDocumentsVisible` (nom LIKE + TS + faits, rang nom > TS > fait sûr > fait douteux, facettes émetteur/type/montant/période), `GET /files/search` étendu (400 si facette malformée), Room v21 (FTS4 `files_fts` + triggers partagés fresh-install/migration, entité déclarée pour KSP), `searchDocuments` offline (même rang, accents préservés) + UI filtres (émetteur, types chips, montants, période ISO, compteur, effacer).
|
||||
- Relations V3 (S5) : table `resource_links` + `scan_session_id` (migration `000021`), réconciliation déterministe (same_scan via outbox, doublons via SHA-256 posé à l'upload + backfill boot, same_issuer/référence via faits effectifs, verdicts préservés, élagage auto), `GET /files/:id/relations` + verdict (owner-only), `GET /duplicates` (groupes + gain récupérable), Room v22 (links + collections), « Voir aussi » dans la fiche (verdicts en ligne), écran Doublons (garder le plus récent), collections locales (sauver/rejouer/supprimer depuis la recherche), `scanSessionId` dans les creates de scan.
|
||||
- Corbeille V3 (S1 mobile, Room v19) : colonne `trashed_at` (files + folders, migration 18→19), tombstone `exists = 0` (requêtes `exists = 1` inchangées), `FileDeleter`/`FolderDeleter`/`SafFileDeleter` → ops `trash_resource` (plus de `delete_resource` émis), garde walk anti-résurrection + reconcile, `TrashRepository` (restore/purge/convergence `GET /trash`/expiration locale 31j), écran Corbeille (Réglages → restore/purge avec confirmation), expiration tombstones dans `RetentionWorker`, dialogue de suppression rewordé (corbeille 30j, plus « irréversible »).
|
||||
|
||||
## [1.1.0] - 2026-09-25
|
||||
|
||||
@@ -52,7 +52,7 @@ Sans ces 4 items, les piliers grincent. Ils sont petits et intégrés aux slices
|
||||
- `000018_versions` : table `resource_versions (id 32-hex PK, resource_id FK CASCADE, version_no INT, size_bytes BIGINT, content_hash TEXT, stored_path TEXT, created_at)` + `UNIQUE(resource_id, version_no)`. À chaque overwrite d'octets (upload ciblé), la copie précédente est poussée en version (garder N=3, purger la plus vieille).
|
||||
- `000019_facts` : table `resource_facts (id 32-hex PK, resource_id FK CASCADE, fact_type TEXT CHECK IN ('date','due_date','period','amount','issuer','reference','doc_type'), value_text TEXT, value_num NUMERIC NULL, value_date DATE NULL, confidence REAL CHECK 0..1, origin TEXT CHECK IN ('auto','user'), created_at, updated_at)` + index `(resource_id)`, `(fact_type, value_text)`. `doc_type` n'est qu'un fait comme un autre (suggestion, pas statut).
|
||||
- `000020_search_fts` (livré S4) : colonne `search_tsv TSVECTOR` générée (`name + ocr_text`, `french`) + index GIN ; faits joints à la requête (rang, jamais d'exclusion).
|
||||
- `000021_links` (S5) : table `resource_links (id 32-hex PK, from_id FK CASCADE, to_id FK CASCADE, link_type TEXT CHECK IN ('same_scan','duplicate_of','version_of','same_issuer','same_reference'), confidence REAL, created_by TEXT CHECK IN ('auto','user'), CHECK(from_id != to_id), UNIQUE(from_id,to_id,link_type))`.
|
||||
- `000021_links` (S5, livré) : table `resource_links (id 32-hex PK, from_id/to_id canoniques, link_type IN (same_scan, duplicate_of, same_issuer, same_reference) — pas de `version_of`, CHECK from != to, UNIQUE trio)` + `resources.scan_session_id` (session opaque du create outbox). `version_of` non matérialisé (versions DANS la ressource).
|
||||
- `000022_token_version` (S6) : `ALTER TABLE users ADD COLUMN token_version INT NOT NULL DEFAULT 0` — `PATCH /users/me/password` bump ; claim paseto `tv`, middleware `RequireAuth` rejette `tv != users.token_version` (lève la limite V1 « tokens non révoqués »).
|
||||
- `000023_quota` (A2, S6) : `ALTER TABLE users ADD COLUMN storage_quota_bytes BIGINT NOT NULL DEFAULT 10737418240` (10 Go, V2.md §3.1) + vue/fonction `user_used_bytes(user_id)` (somme `size_bytes`hors trash). Check à l'upload et à l'overwrite versionnée → `413 QUOTA_EXCEEDED` (nouveau code §7).
|
||||
|
||||
@@ -66,7 +66,7 @@ Sans ces 4 items, les piliers grincent. Ils sont petits et intégrés aux slices
|
||||
- `backend/repository/links.go` : `EnsureAuto` (idempotent, upsert par `(from,to,type)`), `SetUserVerdict` (confirme/invalide → `created_by='user'`, confidence 0/1), `ListForResource`. Règles auto V3 : `same_scan` (déterministe depuis `scan_sessions` — en fait côté mobile, voir §4, serveur : `duplicate_of` via `content_hash` égal, `version_of` via `resource_versions`, `same_issuer` via facts `issuer` normalisé identique + confidence = min des deux facts).
|
||||
- `backend/service/resources.go` : trash/restore/purge (`Trash` = set `trashed_at`, `Restore` = NULL, `Purge` = hard delete + versions + faits + liens + fichier physique) ; overwrite d'octets → pousser l'ancienne copie en `resource_versions` avant écriture.
|
||||
- `backend/service/sync.go` : nouvelles ops outbox **`trash_resource` / `restore_resource` / `purge_resource` / `correct_facts`** (même enveloppe §6.1, `operation_id` 32-hex, idempotence `(device_id, operation_id)`, arrêt au premier échec non-idempotent). `delete_resource` legacy → mappé vers `trash_resource` (pas de break client existant). `correct_facts` porte `[{fact_type, value, confidence}]`, applique `Correct` (writer = user, écrase l'auto).
|
||||
- `backend/handlers/` : `GET /trash` (liste `trashed`, paginé, même enveloppe `{data,meta}`), `POST /files/:id/restore`, `DELETE /files/:id?mode=purge` (défaut trash ; `purge` = définitif), `GET /files/:id/versions` + `POST /files/:id/versions/:n/restore`, `GET /files/:id/facts` + `PATCH /files/:id/facts` (correction directe, même sémantique que `correct_facts`), `GET /files/:id/links`, `GET /users/me/usage` (quota + copies 3-2-1 : `used_bytes`, `quota_bytes`, `copies`, `offsite_ok`), `GET /devices` + `DELETE /devices/:id` (A1, lecture `storage_locations` + révocation), `GET /export` (C3, zip + manifest, streamé). Tous scopés `viewer+` lecture / `owner` mutation sauf `correct_facts` (`editor+`, comme `update_metadata`).
|
||||
- `backend/handlers/` : `GET /trash` (liste `trashed`, paginé, même enveloppe `{data,meta}`), `POST /files/:id/restore`, `DELETE /files/:id?mode=purge` (défaut trash ; `purge` = définitif), `GET /files/:id/versions` + `POST /files/:id/versions/:n/restore`, `GET /files/:id/facts` + `PATCH /files/:id/facts` (correction directe, même sémantique que `correct_facts`), `GET /files/:id/relations` + `POST /files/:id/relations/verdict` (livrés S5 — `/links` étant pris par les liens de partage), `GET /duplicates` (livré S5 B2), `GET /users/me/usage` (quota + copies 3-2-1 : `used_bytes`, `quota_bytes`, `copies`, `offsite_ok`), `GET /devices` + `DELETE /devices/:id` (A1, lecture `storage_locations` + révocation), `GET /export` (C3, zip + manifest, streamé). Tous scopés `viewer+` lecture / `owner` mutation sauf `correct_facts` (`editor+`, comme `update_metadata`).
|
||||
- `backend/handlers/router.go` + `cmd/server/router_test.go` : enregistrer les routes (le test de contrat miroir du client doit rester vert).
|
||||
- `docs/api-v1.md` §6.1/§7 : documenter les 4 ops + codes (`TRASHED` vs `NOT_FOUND` — une ressource trashée reste `NOT_FOUND` en lecture normale, visible seulement via `/trash`).
|
||||
|
||||
@@ -80,7 +80,8 @@ Sans ces 4 items, les piliers grincent. Ils sont petits et intégrés aux slices
|
||||
### 4.1 Room (v19 → v20 faits, v21 FTS — voir S3/S4, `app/schemas/` exporté, tests `MigrationXToYTest`)
|
||||
|
||||
- v19 (S1, livré) : `trashed_at` sur files + folders.
|
||||
- v20 (S3, livré) : `resource_facts (id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin, updated_at)` + `FactsDao` (remplacement en bloc au sync, corrections `user` poussées en outbox). `resource_links`, `is_favorite` et collections → S5.
|
||||
- v20 (S3, livré) : `resource_facts (id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin, updated_at)` + `FactsDao` (remplacement en bloc au sync, corrections `user` poussées en outbox).
|
||||
- v22 (S5, livré) : `resource_links` (miroir `GET /relations`, remplacement en bloc, verdicts REST directs) + `LinksDao` + `collections` (requêtes sauvegardées locales, jamais syncées) + `CollectionDao`. `is_favorite` reporté (non bloquant, S6+ si besoin).
|
||||
- v21 (S4) : FTS4 `files_fts (name, ocr_text, facts_text)` + triggers sync + requête `searchDocuments(query, facets)` remplaçant `searchWithFilters` pour les docs (garder l'ancien pour dossiers). `facts_text` = concat des `value_text` — reconstruit à chaque sync facts.
|
||||
- `OutboxRepository.kt` : `enqueueTrash/restore/purge`, `enqueueCorrectFacts` (payload `[{fact_type, value}]`, `GenerateId` 32-hex, même transaction Room que la mutation parente — contrat outbox atomique inchangé). `delete_resource` existant → renommé en trash côté appelants (`FileDeleter`, `FolderDeleter`).
|
||||
|
||||
@@ -111,7 +112,7 @@ Sans ces 4 items, les piliers grincent. Ils sont petits et intégrés aux slices
|
||||
2. **S2 — Extract générique** (livré : `extract/rules.go` + `service/extract.go` + `000019` + `UpsertAuto` + `GET /files/:id/facts`) — done quand : un scan EDF propose émetteur/montant/échéance.
|
||||
3. **S3 — Fiche + correction** (livré : `PATCH /files/:id/facts` + op `correct_facts` editor+ + Room v20 facts + fiche viewer + `FactsRepository` + B4 a11y fiche) — done quand : corriger en un tap converge serveur ↔ 2e device.
|
||||
4. **S4 — Recherche** (livré : `000020` FTS + facettes serveur + Room FTS v21 + UI filtres + B4 a11y) — done quand : « facture edf mars » trouve `scan_042.pdf` offline ET online.
|
||||
5. **S5 — Relations + B2** (`000021` links + `duplicate/same_issuer` auto + `same_scan` via `scanSessionId` + fiche « Voir aussi » + collections locales + écran Doublons « garder un seul ») — done quand : fiche CNI → verso + doublon listés, invalidables ; doublons chiffrés en Mo récupérables.
|
||||
5. **S5 — Relations + B2** (livré : `000021` links + doublons SHA-256 + same_scan outbox + `GET /relations` + verdict + `GET /duplicates` + Room v22 + « Voir aussi » + Doublons + collections locales) — done quand : fiche CNI → verso + doublon listés, invalidables ; doublons chiffrés en Mo récupérables.
|
||||
6. **S6 — Partage sobre + A1/A2/A4 + B1 (+B5 si temps)** (expiration défaut 7j + `000022` token_version + `000023` quota + `413 QUOTA_EXCEEDED` + `GET /devices` + écran Appareils + `update_metadata{baseUpdatedAt}` + flag `conflict` + import « classeur » unifié B1 + onboarding cercle B5) — done quand : lien RIB expiré → `NOT_FOUND`, password changé → autres devices re-logués, device perdu révoqué, quota plein → 413 propre, rename concurrent → badge « renommé ailleurs ».
|
||||
7. **S7 — Preview 1ère page** (`pdf_preview` V2.md §3.3, une seule taille) + **C3 export chiffré** (`GET /export` zip + manifest) + B3 impression si temps + polish + release 1.2.0. Arbitrage stretch : **export > embeddings** (vrai 3-2-1 perçu, zéro GPU).
|
||||
|
||||
|
||||
@@ -92,11 +92,23 @@ func main() {
|
||||
// échec d'extract ne fait jamais échouer le job OCR.
|
||||
extractSvc := service.NewExtract(repo)
|
||||
handlers.Ocr.OnText = func(userID, resourceID, text string) {
|
||||
if err := extractSvc.ProcessText(resourceID, text); err != nil {
|
||||
if err := extractSvc.ProcessText(userID, resourceID, text); err != nil {
|
||||
log.Printf("extract: %s: %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// V3 S5 : backfill des hashes (uploads d'avant la V3) — doublons.
|
||||
go func() {
|
||||
n, err := handlers.Store.BackfillHashes(200)
|
||||
if err != nil {
|
||||
log.Printf("content backfill hashes: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("content backfill hashes: %d fichiers", n)
|
||||
}
|
||||
}()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ var expectedRoutes = []string{
|
||||
"GET /api/v1/files/:id/links",
|
||||
"GET /api/v1/files/:id/placements",
|
||||
"GET /api/v1/files/:id/versions",
|
||||
"GET /api/v1/files/:id/relations",
|
||||
"POST /api/v1/files/:id/relations/verdict",
|
||||
"GET /api/v1/duplicates",
|
||||
"POST /api/v1/files/:id/restore",
|
||||
"POST /api/v1/files/:id/versions/:n/restore",
|
||||
"GET /api/v1/trash",
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE resources DROP COLUMN IF EXISTS scan_session_id;
|
||||
DROP TABLE IF EXISTS resource_links;
|
||||
@@ -0,0 +1,32 @@
|
||||
-- V3 relations documentaires (S5) : graphe auto + verdicts utilisateur.
|
||||
-- `resource_links` : arêtes orientées (from_id → to_id) typées, avec
|
||||
-- confiance et origine. `duplicate_of` (même content_hash), `same_scan`
|
||||
-- (même session de scan, déterministe), `same_issuer` / `same_reference`
|
||||
-- (faits normalisés égaux). `version_of` non matérialisé : les versions
|
||||
-- vivent DANS la ressource (resource_versions, 000018), pas entre ressources.
|
||||
-- `created_by` : 'auto' (dérivé serveur, écrasable) vs 'user' (verdict
|
||||
-- explicite — la réconciliation ne touche jamais aux verdicts). Les
|
||||
-- invalidations (confidence 0) sont conservées (pas de re-création auto).
|
||||
-- `resources.scan_session_id` : session de scan opaque fournie par le client
|
||||
-- dans le payload `create_resource` (zéro nouvelle op) — le serveur en
|
||||
-- déduit les arêtes `same_scan`.
|
||||
CREATE TABLE resource_links (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
from_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
to_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
link_type TEXT NOT NULL CHECK (link_type IN (
|
||||
'same_scan', 'duplicate_of', 'same_issuer', 'same_reference'
|
||||
)),
|
||||
confidence REAL NOT NULL CHECK (confidence >= 0 AND confidence <= 1),
|
||||
created_by TEXT NOT NULL DEFAULT 'auto' CHECK (created_by IN ('auto', 'user')),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
CHECK (from_id != to_id),
|
||||
UNIQUE (from_id, to_id, link_type)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_links_from ON resource_links(from_id);
|
||||
CREATE INDEX idx_links_to ON resource_links(to_id);
|
||||
|
||||
ALTER TABLE resources ADD COLUMN scan_session_id TEXT;
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_scan_session ON resources(user_id, scan_session_id)
|
||||
WHERE scan_session_id IS NOT NULL;
|
||||
@@ -447,6 +447,50 @@ func TestMigrationsUpDown(t *testing.T) {
|
||||
t.Error("index idx_resources_search_tsv manquant après 000020")
|
||||
}
|
||||
|
||||
// --- 000021 : graphe documentaire ----------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "resource_links", "schema_migrations")
|
||||
|
||||
// resource_links.id : CHECK 32-hex (requête ciblée — le helper
|
||||
// générique matcherait `from_id <> to_id` qui contient aussi "id").
|
||||
var linksIDCheck string
|
||||
err = conn.QueryRow(`
|
||||
SELECT pg_get_constraintdef(c.oid)
|
||||
FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
WHERE t.relname = 'resource_links' AND c.conname = 'resource_links_id_check'`).Scan(&linksIDCheck)
|
||||
if err != nil {
|
||||
t.Fatalf("contrainte CHECK(resource_links.id) manquante: %v", err)
|
||||
}
|
||||
if !strings.Contains(linksIDCheck, "^[0-9a-f]{32}$") {
|
||||
t.Errorf("CHECK resource_links.id attendu 32-hex, obtenu : %s", linksIDCheck)
|
||||
}
|
||||
|
||||
var linksUniq int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resource_links' AND i.indisunique
|
||||
AND ARRAY(SELECT a.attname FROM unnest(i.indkey) WITH ORDINALITY k(attnum, ord)
|
||||
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
||||
ORDER BY k.ord)::text[] = ARRAY['from_id','to_id','link_type']`).Scan(&linksUniq)
|
||||
if err != nil {
|
||||
t.Fatalf("links unique: %v", err)
|
||||
}
|
||||
if linksUniq == 0 {
|
||||
t.Error("contrainte UNIQUE(from_id, to_id, link_type) manquante sur resource_links après 000021")
|
||||
}
|
||||
|
||||
var scanSessionCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'scan_session_id'`).Scan(&scanSessionCol)
|
||||
if err != nil {
|
||||
t.Fatalf("scan_session_id column: %v", err)
|
||||
}
|
||||
if scanSessionCol == 0 {
|
||||
t.Error("colonne resources.scan_session_id manquante après 000021")
|
||||
}
|
||||
|
||||
if err := MigrateDownDatabase(url); err != nil {
|
||||
t.Fatalf("migrate down: %v", err)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestFactsEndpoint(t *testing.T) {
|
||||
t.Fatalf("ocr text: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
if err := extractSvc.ProcessText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
@@ -95,7 +95,7 @@ func TestCorrectFactsEndpoint(t *testing.T) {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
if err := extractSvc.ProcessText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
|
||||
@@ -183,6 +183,96 @@ func FilesCorrectFacts(c *gin.Context) {
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// RelationsList expose le graphe documentaire d'une ressource possédée
|
||||
// (V3 S5) : `GET /files/:id/relations`. Réconcilie d'abord (les arêtes
|
||||
// déterministes convergent à la lecture), puis renvoie les arêtes vivantes
|
||||
// (l'autre bout non trashé), confiance décroissante. Owner-only : le graphe
|
||||
// d'autrui ne s'énumère pas (404, comme `/shares`).
|
||||
func RelationsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
relations, err := Store.ListRelations(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
if relations == nil {
|
||||
relations = []service.RelationDTO{}
|
||||
}
|
||||
api.OK(c, relations)
|
||||
}
|
||||
|
||||
// RelationVerdict enregistre un verdict explicite sur une arête (V3 S5) :
|
||||
// `POST /files/:id/relations/verdict { otherId, linkType, confirmed }`.
|
||||
// Confirmé → 1.0, infirmé → 0.0 (conservé, bloque la re-création auto).
|
||||
// Owner-only, les deux bouts possédés et vifs, sinon 404 ; type/paire
|
||||
// invalide → 400.
|
||||
func RelationVerdict(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
OtherID string `json:"otherId"`
|
||||
LinkType string `json:"linkType"`
|
||||
Confirmed bool `json:"confirmed"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { otherId, linkType, confirmed })")
|
||||
return
|
||||
}
|
||||
if !deviceIDPattern.MatchString(req.OtherID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `otherId` (32 lowercase hex)")
|
||||
return
|
||||
}
|
||||
if err := Store.LinkVerdict(userID, id, req.OtherID, req.LinkType, req.Confirmed); err != nil {
|
||||
if errors.Is(err, service.ErrInvalidLinkType) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
relations, err := Store.ListRelations(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, relations)
|
||||
}
|
||||
|
||||
// DuplicatesList expose les doublons d'octets du compte (V3 S5 B2) :
|
||||
// `GET /duplicates`. Groupes par hash, plus gros d'abord, avec le gain
|
||||
// récupérable (« garder un seul »). Scope owner — `[]` quand propre.
|
||||
func DuplicatesList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
groups, err := Store.ListDuplicates(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, groups)
|
||||
}
|
||||
|
||||
// FilesContent streame les octets physiques d'un fichier visible (viewer+,
|
||||
// own ou shared) : GET /files/:id/content. Réponse binaire (Content-Type du
|
||||
// fichier, Content-Disposition inline) — jamais d'enveloppe {data}. Supporte
|
||||
// Range via http.ServeContent (Gin c.File). Sans bytes serveur (ressource
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestRelationsEndpoints : GET /files/:id/relations (réconciliation à la
|
||||
// lecture) + POST verdict (confirmé/infirmé, 400/404).
|
||||
func TestRelationsEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "rl"), "relations-test-password", device)
|
||||
|
||||
session := repository.NewID()
|
||||
page1, page2 := repository.NewID(), repository.NewID()
|
||||
for _, id := range []string{page1, page2} {
|
||||
if err := repo.Resources.InsertFile(userID, id, id+".png", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.SetScanSession(userID, id, session); err != nil {
|
||||
t.Fatalf("session: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
||||
env := expectOK(t, rec, "relations-get")
|
||||
var rels []struct {
|
||||
ID string `json:"id"`
|
||||
OtherID string `json:"otherId"`
|
||||
Type string `json:"type"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
OtherName string `json:"otherName"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(rels) != 1 || rels[0].Type != "same_scan" || rels[0].OtherID != page2 {
|
||||
t.Fatalf("relations inattendues: %+v", rels)
|
||||
}
|
||||
|
||||
// Verdict infirmé : arête conservée à 0.0, origine user.
|
||||
body := fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":false}`, page2)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(body), "application/json")
|
||||
env = expectOK(t, rec, "relations-verdict")
|
||||
rels = nil
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal verdict: %v", err)
|
||||
}
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("verdict inattendu: %+v", rels)
|
||||
}
|
||||
|
||||
// Type inconnu → 400 ; paire non-hex → 400 ; étranger → 404.
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
status int
|
||||
code string
|
||||
}{
|
||||
{"type", fmt.Sprintf(`{"otherId":%q,"linkType":"copains","confirmed":true}`, page2), http.StatusBadRequest, "INVALID_REQUEST"},
|
||||
{"hex", `{"otherId":"nope","linkType":"same_scan","confirmed":true}`, http.StatusBadRequest, "INVALID_REQUEST"},
|
||||
} {
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(tc.body), "application/json")
|
||||
expectError(t, rec, tc.status, tc.code, "relations-verdict-400:"+tc.name)
|
||||
}
|
||||
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "rb"), "relations-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-foreign")
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", tokenB,
|
||||
[]byte(fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":true}`, page2)), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-verdict-foreign")
|
||||
|
||||
// Trashé → 404 (l'autre bout trashé disparaît aussi de la liste).
|
||||
if err := repo.Resources.TrashResource(userID, page2); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
||||
env = expectOK(t, rec, "relations-trashed-other")
|
||||
rels = nil
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(rels) != 0 {
|
||||
t.Fatalf("bout trashé encore listé: %+v", rels)
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
protected.GET("/files/:id/links", LinksList)
|
||||
protected.GET("/files/:id/placements", PlacementsList)
|
||||
protected.GET("/files/:id/versions", VersionsList)
|
||||
protected.GET("/files/:id/relations", RelationsList)
|
||||
protected.POST("/files/:id/relations/verdict", RelationVerdict)
|
||||
protected.GET("/duplicates", DuplicatesList)
|
||||
protected.POST("/files/:id/restore", FilesRestore)
|
||||
protected.POST("/files/:id/versions/:n/restore", VersionRestore)
|
||||
protected.GET("/trash", TrashList)
|
||||
|
||||
@@ -127,6 +127,7 @@ func (r *Resources) CorrectFacts(resourceID string, params []FactParam) error {
|
||||
}
|
||||
|
||||
type factNeedingExtract struct {
|
||||
UserID string
|
||||
ResourceID string
|
||||
Text string
|
||||
}
|
||||
@@ -135,8 +136,8 @@ type factNeedingExtract struct {
|
||||
// origin='auto' facts (boot backfill, S2). Live rows only.
|
||||
func (r *Resources) ListNeedingExtract(limit int) ([]factNeedingExtract, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id, ocr_text FROM resources
|
||||
WHERE type = 'file' AND deleted_at IS NULL
|
||||
`SELECT user_id, resource_id, ocr_text FROM resources
|
||||
WHERE type = 'file' AND deleted_at IS NULL AND user_id IS NOT NULL
|
||||
AND ocr_text IS NOT NULL AND ocr_text <> ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts
|
||||
@@ -152,7 +153,7 @@ func (r *Resources) ListNeedingExtract(limit int) ([]factNeedingExtract, error)
|
||||
var out []factNeedingExtract
|
||||
for rows.Next() {
|
||||
var row factNeedingExtract
|
||||
if err := rows.Scan(&row.ResourceID, &row.Text); err != nil {
|
||||
if err := rows.Scan(&row.UserID, &row.ResourceID, &row.Text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
|
||||
@@ -0,0 +1,355 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// Link types — mirror of the resource_links CHECK (migration 000021).
|
||||
// All types are symmetric : stored once with from_id < to_id (canonical
|
||||
// order), read from either end. version_of is intentionally absent :
|
||||
// versions live INSIDE a resource (resource_versions, 000018).
|
||||
const (
|
||||
LinkSameScan = "same_scan"
|
||||
LinkDuplicateOf = "duplicate_of"
|
||||
LinkSameIssuer = "same_issuer"
|
||||
LinkSameReference = "same_reference"
|
||||
)
|
||||
|
||||
// LinkRow is one edge viewed from the queried resource.
|
||||
type LinkRow struct {
|
||||
ID string
|
||||
OtherID string
|
||||
Type string
|
||||
Confidence float64
|
||||
CreatedBy string
|
||||
OtherName string
|
||||
OtherType string
|
||||
OtherSize int64
|
||||
}
|
||||
|
||||
// canonicalPair orders an unordered pair for symmetric storage.
|
||||
func canonicalPair(a, b string) (string, string) {
|
||||
if a < b {
|
||||
return a, b
|
||||
}
|
||||
return b, a
|
||||
}
|
||||
|
||||
// ListLinks returns the live edges touching resourceID (either direction),
|
||||
// joined with the other end's identity. Trashed others are excluded.
|
||||
// No ownership check — the caller scopes (owner-only in S5).
|
||||
func (r *Resources) ListLinks(resourceID string) ([]LinkRow, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT l.id,
|
||||
CASE WHEN l.from_id = $1 THEN l.to_id ELSE l.from_id END AS other_id,
|
||||
l.link_type, l.confidence, l.created_by,
|
||||
o.name, o.type, COALESCE(o.size_bytes, 0)
|
||||
FROM resource_links l
|
||||
JOIN resources o ON o.resource_id = CASE WHEN l.from_id = $1 THEN l.to_id ELSE l.from_id END
|
||||
WHERE (l.from_id = $1 OR l.to_id = $1)
|
||||
AND o.deleted_at IS NULL
|
||||
ORDER BY l.confidence DESC, o.name ASC`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]LinkRow, 0)
|
||||
for rows.Next() {
|
||||
var row LinkRow
|
||||
if err := rows.Scan(&row.ID, &row.OtherID, &row.Type, &row.Confidence, &row.CreatedBy, &row.OtherName, &row.OtherType, &row.OtherSize); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ensureAutoLink inserts a symmetric auto edge (canonical order). Any
|
||||
// existing row — auto or user verdict — blocks recreation (an invalidated
|
||||
// edge never comes back by itself).
|
||||
func (r *Resources) ensureAutoLink(from, to, linkType string, confidence float64) error {
|
||||
a, b := canonicalPair(from, to)
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.DB.Exec(
|
||||
`INSERT INTO resource_links (id, from_id, to_id, link_type, confidence, created_by)
|
||||
VALUES ($1, $2, $3, $4, $5, 'auto')
|
||||
ON CONFLICT (from_id, to_id, link_type) DO NOTHING`,
|
||||
id, a, b, linkType, clamp01(confidence),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetUserVerdict records an explicit verdict (created_by='user') : confirm →
|
||||
// confidence 1.0, invalidate → 0.0. Upserts over any previous row (auto or
|
||||
// older verdict) — the latest human word wins.
|
||||
func (r *Resources) SetUserVerdict(one, other, linkType string, confirmed bool) error {
|
||||
a, b := canonicalPair(one, other)
|
||||
conf := 0.0
|
||||
if confirmed {
|
||||
conf = 1.0
|
||||
}
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.DB.Exec(
|
||||
`INSERT INTO resource_links (id, from_id, to_id, link_type, confidence, created_by)
|
||||
VALUES ($1, $2, $3, $4, $5, 'user')
|
||||
ON CONFLICT (from_id, to_id, link_type)
|
||||
DO UPDATE SET confidence = EXCLUDED.confidence, created_by = 'user'`,
|
||||
id, a, b, linkType, conf,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// pruneAutoLinks drops auto edges of the symmetric types touching resourceID
|
||||
// whose pair is no longer a candidate (facts/session edited). keep holds
|
||||
// canonical "a\x00b\x00type" keys to preserve. User verdicts are never pruned.
|
||||
func (r *Resources) pruneAutoLinks(resourceID string, keep map[string]bool) error {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT id, from_id, to_id, link_type FROM resource_links
|
||||
WHERE (from_id = $1 OR to_id = $1) AND created_by = 'auto'`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var stale []string
|
||||
for rows.Next() {
|
||||
var id, a, b, typ string
|
||||
if err := rows.Scan(&id, &a, &b, &typ); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
if !keep[a+"\x00"+b+"\x00"+typ] {
|
||||
stale = append(stale, id)
|
||||
}
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range stale {
|
||||
if _, err := r.DB.Exec(`DELETE FROM resource_links WHERE id = $1`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// linkCandidate is one deterministic pair found by reconciliation.
|
||||
type linkCandidate struct {
|
||||
other string
|
||||
typ string
|
||||
confidence float64
|
||||
}
|
||||
|
||||
// ReconcileLinks recomputes the deterministic auto edges of a live owned
|
||||
// resource (same_scan, duplicate_of, same_issuer, same_reference), creates
|
||||
// missing ones and prunes stale auto ones. User verdicts are preserved and
|
||||
// block recreation. Idempotent — safe to call on every read (GET links).
|
||||
func (r *Resources) ReconcileLinks(ownerID, resourceID string) error {
|
||||
var session sql.NullString
|
||||
var hash sql.NullString
|
||||
var size int64
|
||||
err := r.DB.QueryRow(
|
||||
`SELECT COALESCE(scan_session_id, ''), COALESCE(content_hash, ''), COALESCE(size_bytes, 0)
|
||||
FROM resources WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
||||
resourceID, ownerID,
|
||||
).Scan(&session, &hash, &size)
|
||||
if err == sql.ErrNoRows {
|
||||
return ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var cands []linkCandidate
|
||||
|
||||
// same_scan : same opaque session, same user, live, not self.
|
||||
if session.Valid && session.String != "" {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id FROM resources
|
||||
WHERE user_id = $1 AND scan_session_id = $2 AND resource_id != $3 AND deleted_at IS NULL`,
|
||||
ownerID, session.String, resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var other string
|
||||
if err := rows.Scan(&other); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
cands = append(cands, linkCandidate{other, LinkSameScan, 1.0})
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// duplicate_of : same content hash (non-empty), size > 0, same user, live.
|
||||
if hash.Valid && hash.String != "" && size > 0 {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT resource_id FROM resources
|
||||
WHERE user_id = $1 AND content_hash = $2 AND size_bytes > 0
|
||||
AND resource_id != $3 AND deleted_at IS NULL`,
|
||||
ownerID, hash.String, resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var other string
|
||||
if err := rows.Scan(&other); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
cands = append(cands, linkCandidate{other, LinkDuplicateOf, 1.0})
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// same_issuer / same_reference : shared fact values (case-insensitive
|
||||
// for issuers, exact for reference codes), any origin (user corrections
|
||||
// count — they ARE the household truth). Effective facts only : un fait
|
||||
// auto est ignoré quand un fait user du même type existe sur la même
|
||||
// ressource (miroir de `displayFacts` côté mobile — sinon une correction
|
||||
// laisserait l'arête auto périmée).
|
||||
for _, typ := range []string{"issuer", "reference"} {
|
||||
linkTyp := LinkSameIssuer
|
||||
if typ == "reference" {
|
||||
linkTyp = LinkSameReference
|
||||
}
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT DISTINCT o.resource_id, LEAST(f.confidence, g.confidence)
|
||||
FROM resource_facts f
|
||||
JOIN resource_facts g ON g.fact_type = f.fact_type
|
||||
AND g.resource_id != f.resource_id
|
||||
AND CASE WHEN f.fact_type = 'issuer'
|
||||
THEN LOWER(g.value_text) = LOWER(f.value_text)
|
||||
ELSE g.value_text = f.value_text END
|
||||
JOIN resources o ON o.resource_id = g.resource_id
|
||||
WHERE f.resource_id = $1 AND f.fact_type = $2 AND f.value_text IS NOT NULL
|
||||
AND (f.origin = 'user' OR NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts u WHERE u.resource_id = f.resource_id
|
||||
AND u.fact_type = f.fact_type AND u.origin = 'user'))
|
||||
AND (g.origin = 'user' OR NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts u WHERE u.resource_id = g.resource_id
|
||||
AND u.fact_type = g.fact_type AND u.origin = 'user'))
|
||||
AND o.user_id = $3 AND o.deleted_at IS NULL`,
|
||||
resourceID, typ, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var other string
|
||||
var conf float64
|
||||
if err := rows.Scan(&other, &conf); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
cands = append(cands, linkCandidate{other, linkTyp, conf})
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
keep := make(map[string]bool, len(cands))
|
||||
for _, c := range cands {
|
||||
a, b := canonicalPair(resourceID, c.other)
|
||||
keep[a+"\x00"+b+"\x00"+c.typ] = true
|
||||
if err := r.ensureAutoLink(resourceID, c.other, c.typ, c.confidence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return r.pruneAutoLinks(resourceID, keep)
|
||||
}
|
||||
|
||||
// DuplicateGroup is one set of byte-identical live files (B2 screen).
|
||||
type DuplicateGroup struct {
|
||||
ContentHash string
|
||||
Count int
|
||||
TotalBytes int64
|
||||
ReclaimableBytes int64 // total - biggest member (garder un seul)
|
||||
Files []DuplicateFile
|
||||
}
|
||||
|
||||
// DuplicateFile is one member of a duplicate group, newest first.
|
||||
type DuplicateFile struct {
|
||||
ID string
|
||||
Name string
|
||||
Size int64
|
||||
UpdatedAt int64 // ms epoch
|
||||
}
|
||||
|
||||
// ListDuplicateGroups returns the owner's byte-identical file sets
|
||||
// (content_hash non-empty, size > 0, live only), biggest groups first.
|
||||
func (r *Resources) ListDuplicateGroups(ownerID string) ([]DuplicateGroup, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT content_hash, COUNT(*), COALESCE(SUM(size_bytes), 0)
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'file' AND deleted_at IS NULL
|
||||
AND content_hash IS NOT NULL AND content_hash <> '' AND size_bytes > 0
|
||||
GROUP BY content_hash HAVING COUNT(*) > 1
|
||||
ORDER BY SUM(size_bytes) DESC`,
|
||||
ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []DuplicateGroup{}
|
||||
for rows.Next() {
|
||||
var g DuplicateGroup
|
||||
if err := rows.Scan(&g.ContentHash, &g.Count, &g.TotalBytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
members, err := r.DB.Query(
|
||||
`SELECT resource_id, name, COALESCE(size_bytes, 0),
|
||||
(EXTRACT(EPOCH FROM updated_at) * 1000)::bigint
|
||||
FROM resources
|
||||
WHERE user_id = $1 AND type = 'file' AND deleted_at IS NULL AND content_hash = $2
|
||||
ORDER BY updated_at DESC`,
|
||||
ownerID, g.ContentHash,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var biggest int64
|
||||
for members.Next() {
|
||||
var f DuplicateFile
|
||||
if err := members.Scan(&f.ID, &f.Name, &f.Size, &f.UpdatedAt); err != nil {
|
||||
members.Close()
|
||||
return nil, err
|
||||
}
|
||||
if f.Size > biggest {
|
||||
biggest = f.Size
|
||||
}
|
||||
g.Files = append(g.Files, f)
|
||||
}
|
||||
members.Close()
|
||||
if err := members.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.ReclaimableBytes = g.TotalBytes - biggest
|
||||
out = append(out, g)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -51,6 +51,79 @@ func isForeignKeyViolation(err error) bool {
|
||||
return errors.As(err, &pqErr) && pqErr.Code == "23503"
|
||||
}
|
||||
|
||||
// SetContentHash stores the SHA-256 of the live bytes (V3 S5 duplicates).
|
||||
// Owner-scoped, live rows. Best-effort côté service (jamais d'erreur vers
|
||||
// le client).
|
||||
func (r *Resources) SetContentHash(ownerID, resourceID, hash string) error {
|
||||
result, err := r.DB.Exec(
|
||||
`UPDATE resources SET content_hash = NULLIF($3, ''), updated_at = NOW()
|
||||
WHERE resource_id = $1 AND user_id = $2 AND type = 'file' AND deleted_at IS NULL`,
|
||||
resourceID, ownerID, hash,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetScanSession records the opaque client scan-session of a resource
|
||||
// (V3 S5 same_scan — drift informatif, jamais autorisant). Owner-scoped.
|
||||
func (r *Resources) SetScanSession(ownerID, resourceID, sessionID string) error {
|
||||
result, err := r.DB.Exec(
|
||||
`UPDATE resources SET scan_session_id = NULLIF($3, ''), updated_at = NOW()
|
||||
WHERE resource_id = $1 AND user_id = $2 AND deleted_at IS NULL`,
|
||||
resourceID, ownerID, sessionID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MissingHash identifies a live file without content_hash (boot backfill).
|
||||
type MissingHash struct {
|
||||
UserID string
|
||||
ResourceID string
|
||||
}
|
||||
|
||||
// ListMissingHashes returns up to limit live files with bytes but no hash.
|
||||
func (r *Resources) ListMissingHashes(limit int) ([]MissingHash, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT user_id, resource_id FROM resources
|
||||
WHERE type = 'file' AND deleted_at IS NULL AND user_id IS NOT NULL AND has_content = TRUE
|
||||
AND (content_hash IS NULL OR content_hash = '')
|
||||
ORDER BY updated_at ASC LIMIT $1`,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []MissingHash
|
||||
for rows.Next() {
|
||||
var m MissingHash
|
||||
if err := rows.Scan(&m.UserID, &m.ResourceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
type FileRow struct {
|
||||
ID string
|
||||
Name string
|
||||
|
||||
@@ -34,7 +34,9 @@ type FactDTO struct {
|
||||
|
||||
// ProcessText extracts facts from text and replaces the resource's auto
|
||||
// facts. Empty text clears stale auto facts. Never touches user corrections.
|
||||
func (e *Extract) ProcessText(resourceID, text string) error {
|
||||
// Afterwards the deterministic links are reconciled (same_issuer /
|
||||
// same_reference may have appeared or vanished) — best-effort.
|
||||
func (e *Extract) ProcessText(ownerID, resourceID, text string) error {
|
||||
raw := extract.FromText(text)
|
||||
params := make([]repository.FactParam, 0, len(raw))
|
||||
for _, f := range raw {
|
||||
@@ -59,7 +61,13 @@ func (e *Extract) ProcessText(resourceID, text string) error {
|
||||
Confidence: f.Confidence,
|
||||
})
|
||||
}
|
||||
return e.Repository.Resources.UpsertAutoFacts(resourceID, params)
|
||||
if err := e.Repository.Resources.UpsertAutoFacts(resourceID, params); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := e.Repository.Resources.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
log.Printf("extract: reconcile %s: %v", resourceID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Backfill processes files with OCR text but no auto facts yet (one boot
|
||||
@@ -75,7 +83,7 @@ func (e *Extract) Backfill(batch int) (int, error) {
|
||||
return total, nil
|
||||
}
|
||||
for _, p := range pending {
|
||||
if err := e.ProcessText(p.ResourceID, p.Text); err != nil {
|
||||
if err := e.ProcessText(p.UserID, p.ResourceID, p.Text); err != nil {
|
||||
log.Printf("extract: backfill %s: %v", p.ResourceID, err)
|
||||
continue
|
||||
}
|
||||
@@ -140,7 +148,14 @@ func (s *Resources) CorrectFacts(userID, resourceID string, facts []FactDTO) err
|
||||
}
|
||||
params = append(params, p)
|
||||
}
|
||||
return s.Repository.Resources.CorrectFacts(resourceID, params)
|
||||
if err := s.Repository.Resources.CorrectFacts(resourceID, params); err != nil {
|
||||
return err
|
||||
}
|
||||
// Les faits ont changé : les arêtes same_issuer/same_reference suivent.
|
||||
if err := s.Repository.Resources.ReconcileLinks(userID, resourceID); err != nil {
|
||||
log.Printf("extract: reconcile %s: %v", resourceID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateFactParam(i int, f FactDTO) (repository.FactParam, error) {
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestProcessTextPersistsAutoFacts(t *testing.T) {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
@@ -74,11 +74,11 @@ func TestProcessTextPreservesUserCorrections(t *testing.T) {
|
||||
t.Fatalf("seed user fact: %v", err)
|
||||
}
|
||||
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
// Re-passe (idempotence du remplacement auto).
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("re-process: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
@@ -122,10 +122,10 @@ func TestProcessTextEmptyClearsStaleAuto(t *testing.T) {
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(fileID, ""); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, ""); err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
}
|
||||
facts, err := s.GetFacts(userID, fileID)
|
||||
@@ -145,7 +145,7 @@ func TestGetFactsScope(t *testing.T) {
|
||||
if err := s.Repository.Resources.InsertFile(owner, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(owner, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
|
||||
@@ -207,7 +207,7 @@ func TestCorrectFactsReplacesUserSet(t *testing.T) {
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
|
||||
@@ -311,7 +311,7 @@ func TestSyncOpCorrectFacts(t *testing.T) {
|
||||
if err := s.Repository.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(fileID, extractCorpus); err != nil {
|
||||
if err := ex.ProcessText(userID, fileID, extractCorpus); err != nil {
|
||||
t.Fatalf("process: %v", err)
|
||||
}
|
||||
foreignID := repository.NewID()
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// RelationDTO serializes one edge of GET /files/:id/relations (V3 S5).
|
||||
type RelationDTO struct {
|
||||
ID string `json:"id"`
|
||||
OtherID string `json:"otherId"`
|
||||
Type string `json:"type"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
OtherName string `json:"otherName"`
|
||||
OtherType string `json:"otherType"`
|
||||
OtherSize int64 `json:"otherSize"`
|
||||
}
|
||||
|
||||
// validLinkTypes mirrors the resource_links CHECK (migration 000021).
|
||||
var validLinkTypes = map[string]bool{
|
||||
repository.LinkSameScan: true,
|
||||
repository.LinkDuplicateOf: true,
|
||||
repository.LinkSameIssuer: true,
|
||||
repository.LinkSameReference: true,
|
||||
}
|
||||
|
||||
// ListRelations reconciles then returns the live edges of an owned resource
|
||||
// (owner-only : shared docs don't expose the owner's graph, S5 scope).
|
||||
func (s *Resources) ListRelations(ownerID, resourceID string) ([]RelationDTO, error) {
|
||||
owned, err := s.Repo.ExistsOwner(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !owned {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.Repo.ListLinks(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]RelationDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, RelationDTO{
|
||||
ID: row.ID, OtherID: row.OtherID, Type: row.Type,
|
||||
Confidence: row.Confidence, CreatedBy: row.CreatedBy,
|
||||
OtherName: row.OtherName, OtherType: row.OtherType, OtherSize: row.OtherSize,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// LinkVerdict records an explicit verdict on an edge (owner-only, both ends
|
||||
// owned-live). Confirm → 1.0, invalidate → 0.0 (kept, blocks re-creation).
|
||||
func (s *Resources) LinkVerdict(ownerID, resourceID, otherID, linkType string, confirmed bool) error {
|
||||
if !validLinkTypes[linkType] {
|
||||
return ErrInvalidLinkType
|
||||
}
|
||||
if otherID == resourceID {
|
||||
return ErrInvalidLinkType
|
||||
}
|
||||
for _, id := range []string{resourceID, otherID} {
|
||||
owned, err := s.Repo.ExistsOwner(ownerID, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !owned {
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
}
|
||||
return s.Repo.SetUserVerdict(resourceID, otherID, linkType, confirmed)
|
||||
}
|
||||
|
||||
// DuplicateGroupDTO serializes one set of byte-identical files (B2).
|
||||
type DuplicateGroupDTO struct {
|
||||
ContentHash string `json:"contentHash"`
|
||||
Count int `json:"count"`
|
||||
TotalBytes int64 `json:"totalBytes"`
|
||||
ReclaimableBytes int64 `json:"reclaimableBytes"`
|
||||
Files []DuplicateFileDTO `json:"files"`
|
||||
}
|
||||
|
||||
// DuplicateFileDTO serializes one member (newest first).
|
||||
type DuplicateFileDTO struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// ListDuplicates returns the owner's byte-identical file sets, biggest
|
||||
// first. Owner scope (own files only) — `[]` when clean.
|
||||
func (s *Resources) ListDuplicates(ownerID string) ([]DuplicateGroupDTO, error) {
|
||||
groups, err := s.Repo.ListDuplicateGroups(ownerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]DuplicateGroupDTO, 0, len(groups))
|
||||
for _, g := range groups {
|
||||
dto := DuplicateGroupDTO{
|
||||
ContentHash: g.ContentHash, Count: g.Count,
|
||||
TotalBytes: g.TotalBytes, ReclaimableBytes: g.ReclaimableBytes,
|
||||
Files: make([]DuplicateFileDTO, 0, len(g.Files)),
|
||||
}
|
||||
for _, f := range g.Files {
|
||||
dto.Files = append(dto.Files, DuplicateFileDTO{
|
||||
ID: f.ID, Name: f.Name, Size: f.Size, UpdatedAt: f.UpdatedAt,
|
||||
})
|
||||
}
|
||||
out = append(out, dto)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ErrInvalidLinkType marks client verdict-validation failures (400).
|
||||
var ErrInvalidLinkType = errors.New("invalid link type or pair")
|
||||
|
||||
// hashPhysical computes the SHA-256 of the live bytes (safe resolution,
|
||||
// same pattern as OpenContent). Absent → ErrNotFound.
|
||||
func (s *Resources) hashPhysical(ownerID, resourceID string) (string, error) {
|
||||
if !idPattern.MatchString(ownerID) || !idPattern.MatchString(resourceID) {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
base, err := filepath.EvalSymlinks(s.UploadDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
matches, err := filepath.Glob(filepath.Join(s.UploadDir, ownerID, resourceID+".*"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, m := range matches {
|
||||
if !strings.HasPrefix(filepath.Base(m), resourceID+".") {
|
||||
continue
|
||||
}
|
||||
fi, err := os.Lstat(m)
|
||||
if err != nil || !fi.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(m)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
rel, err := filepath.Rel(base, resolved)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||
continue
|
||||
}
|
||||
f, err := os.Open(resolved)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
f.Close()
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
|
||||
// refreshContentHash (re)computes and stores the hash after bytes arrive.
|
||||
// Best-effort : un échec ne fait jamais échouer l'upload (loggé).
|
||||
func (s *Resources) refreshContentHash(ownerID, resourceID string) {
|
||||
hash, err := s.hashPhysical(ownerID, resourceID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := s.Repo.SetContentHash(ownerID, resourceID, hash); err != nil {
|
||||
log.Printf("content: SetContentHash(%s): %v", resourceID, err)
|
||||
return
|
||||
}
|
||||
// Des octets identiques à une autre ressource = doublon à relier.
|
||||
if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
log.Printf("content: reconcile(%s): %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// BackfillHashes hashes live files missing content_hash (bounded batches,
|
||||
// boot pass — uploads d'avant la V3). Returns files hashed.
|
||||
func (s *Resources) BackfillHashes(batch int) (int, error) {
|
||||
total := 0
|
||||
for {
|
||||
pending, err := s.Repo.ListMissingHashes(batch)
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
if len(pending) == 0 {
|
||||
return total, nil
|
||||
}
|
||||
for _, p := range pending {
|
||||
hash, err := s.hashPhysical(p.UserID, p.ResourceID)
|
||||
if err != nil {
|
||||
continue // metadata-only : rien à hasher, re-tenté au prochain upload.
|
||||
}
|
||||
if err := s.Repo.SetContentHash(p.UserID, p.ResourceID, hash); err != nil {
|
||||
log.Printf("content: backfill hash %s: %v", p.ResourceID, err)
|
||||
continue
|
||||
}
|
||||
if err := s.Repo.ReconcileLinks(p.UserID, p.ResourceID); err != nil {
|
||||
log.Printf("content: backfill reconcile %s: %v", p.ResourceID, err)
|
||||
}
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
func mustLinkFile(t *testing.T, s *Resources, userID, name string) string {
|
||||
t.Helper()
|
||||
id := repository.NewID()
|
||||
if err := s.Repository.Resources.InsertFile(userID, id, name, "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert %q: %v", name, err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func relationTypes(rels []RelationDTO) map[string]int {
|
||||
out := map[string]int{}
|
||||
for _, r := range rels {
|
||||
out[r.Type]++
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestSameScanViaOutbox(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-scan")
|
||||
device := mustRegisterDevice(t, s.Repository, "d0000000000000000000000000000001")
|
||||
session := repository.NewID()
|
||||
page1 := repository.NewID()
|
||||
page2 := repository.NewID()
|
||||
|
||||
create := func(opID, resID, name string) {
|
||||
t.Helper()
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(opID, resID, OpCreateResource, "file", map[string]any{
|
||||
"name": resID + "-" + name, "scanSessionId": session,
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("create %s: %+v", name, res)
|
||||
}
|
||||
}
|
||||
create(repository.NewID(), page1, "p1.png")
|
||||
create(repository.NewID(), page2, "p2.png")
|
||||
|
||||
rels, err := s.ListRelations(userID, page1)
|
||||
if err != nil {
|
||||
t.Fatalf("relations: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkSameScan] != 1 || rels[0].OtherID != page2 {
|
||||
t.Fatalf("same_scan manquant: %+v", rels)
|
||||
}
|
||||
if rels[0].Confidence != 1.0 || rels[0].CreatedBy != "auto" {
|
||||
t.Fatalf("arête same_scan inattendue: %+v", rels[0])
|
||||
}
|
||||
|
||||
// Session invalide → halt INVALID_REQUEST (pas d'écriture partielle).
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), repository.NewID(), OpCreateResource, "file", map[string]any{
|
||||
"name": "bad.png", "scanSessionId": "nope",
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("apply: %v", err)
|
||||
}
|
||||
if res.Failed == nil || res.Failed.Code != "INVALID_REQUEST" {
|
||||
t.Fatalf("session invalide doit halter: %+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDuplicateOfViaUploadHash(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-dupe")
|
||||
|
||||
a, err := s.Upload(userID, multipartFileHeader(t, "a.txt", []byte("meme-contenu")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload a: %v", err)
|
||||
}
|
||||
b, err := s.Upload(userID, multipartFileHeader(t, "b.txt", []byte("meme-contenu")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload b: %v", err)
|
||||
}
|
||||
|
||||
rels, err := s.ListRelations(userID, a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("relations: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkDuplicateOf] != 1 || rels[0].OtherID != b.ID {
|
||||
t.Fatalf("duplicate_of manquant: %+v", rels)
|
||||
}
|
||||
|
||||
// Contenu différent → pas de doublon.
|
||||
c, err := s.Upload(userID, multipartFileHeader(t, "c.txt", []byte("autre-contenu")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload c: %v", err)
|
||||
}
|
||||
relsC, err := s.ListRelations(userID, c.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("relations c: %v", err)
|
||||
}
|
||||
if len(relsC) != 0 {
|
||||
t.Fatalf("faux doublon: %+v", relsC)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameIssuerViaFacts(t *testing.T) {
|
||||
s, ex := newExtractStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-issuer")
|
||||
f1 := mustLinkFile(t, s, userID, "f1.pdf")
|
||||
f2 := mustLinkFile(t, s, userID, "f2.pdf")
|
||||
|
||||
if err := ex.ProcessText(userID, f1, "EDF\nFacture 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract f1: %v", err)
|
||||
}
|
||||
if err := ex.ProcessText(userID, f2, "EDF\nFacture 20,00 €\n"); err != nil {
|
||||
t.Fatalf("extract f2: %v", err)
|
||||
}
|
||||
|
||||
rels, err := s.ListRelations(userID, f1)
|
||||
if err != nil {
|
||||
t.Fatalf("relations: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkSameIssuer] != 1 {
|
||||
t.Fatalf("same_issuer manquant: %+v", rels)
|
||||
}
|
||||
|
||||
// Correction de l'émetteur → l'arête disparaît (prune auto).
|
||||
if err := s.CorrectFacts(userID, f2, []FactDTO{{FactType: "issuer", ValueText: "ENGIE", Confidence: 1}}); err != nil {
|
||||
t.Fatalf("correct: %v", err)
|
||||
}
|
||||
rels, err = s.ListRelations(userID, f1)
|
||||
if err != nil {
|
||||
t.Fatalf("relations 2: %v", err)
|
||||
}
|
||||
if relationTypes(rels)[repository.LinkSameIssuer] != 0 {
|
||||
t.Fatalf("arête périmée non élaguée: %+v", rels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkVerdict(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-verdict")
|
||||
other := mustCreateUser(t, s.Repository, "links-verdict-x")
|
||||
device := mustRegisterDevice(t, s.Repository, "d0000000000000000000000000000002")
|
||||
session := repository.NewID()
|
||||
p1, p2 := repository.NewID(), repository.NewID()
|
||||
for _, id := range []string{p1, p2} {
|
||||
res, err := s.ApplyBatch(userID, device, []SyncOperation{
|
||||
syncOp(repository.NewID(), id, OpCreateResource, "file", map[string]any{
|
||||
"name": id, "scanSessionId": session,
|
||||
}),
|
||||
})
|
||||
if err != nil || res.Applied != 1 || res.Failed != nil {
|
||||
t.Fatalf("create %s: %+v %v", id, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Invalider : l'arête reste (confiance 0, verdict user) mais ne bloque rien.
|
||||
if err := s.LinkVerdict(userID, p1, p2, repository.LinkSameScan, false); err != nil {
|
||||
t.Fatalf("verdict: %v", err)
|
||||
}
|
||||
rels, _ := s.ListRelations(userID, p1)
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("verdict infirme inattendu: %+v", rels)
|
||||
}
|
||||
// Re-reconcile ne ressuscite pas l'arête invalidée.
|
||||
rels, _ = s.ListRelations(userID, p1)
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 {
|
||||
t.Fatalf("arête invalidée ressuscitée: %+v", rels)
|
||||
}
|
||||
|
||||
// Re-confirmer : retour à 1.0 user.
|
||||
if err := s.LinkVerdict(userID, p1, p2, repository.LinkSameScan, true); err != nil {
|
||||
t.Fatalf("re-verdict: %v", err)
|
||||
}
|
||||
rels, _ = s.ListRelations(userID, p1)
|
||||
if len(rels) != 1 || rels[0].Confidence != 1.0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("re-verdict inattendu: %+v", rels)
|
||||
}
|
||||
|
||||
// Type inconnu → erreur 400-candidate ; paire réflexive idem.
|
||||
if err := s.LinkVerdict(userID, p1, p2, "copains", true); !errors.Is(err, ErrInvalidLinkType) {
|
||||
t.Fatalf("type inconnu : attendu ErrInvalidLinkType, got %v", err)
|
||||
}
|
||||
if err := s.LinkVerdict(userID, p1, p1, repository.LinkSameScan, true); !errors.Is(err, ErrInvalidLinkType) {
|
||||
t.Fatalf("paire réflexive : attendu ErrInvalidLinkType, got %v", err)
|
||||
}
|
||||
// Autrui → NOT_FOUND (pas d'oracle).
|
||||
foreign := mustLinkFile(t, s, other, "x.pdf")
|
||||
if err := s.LinkVerdict(other, foreign, p1, repository.LinkSameScan, true); err != repository.ErrNotFound {
|
||||
t.Fatalf("verdict trans-compte : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
if _, err := s.ListRelations(other, p1); err != repository.ErrNotFound {
|
||||
t.Fatalf("lecture trans-compte : attendu NOT_FOUND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListDuplicates(t *testing.T) {
|
||||
s := newServiceStore(t)
|
||||
userID := mustCreateUser(t, s.Repository, "links-dupes")
|
||||
other := mustCreateUser(t, s.Repository, "links-dupes-x")
|
||||
|
||||
a, err := s.Upload(userID, multipartFileHeader(t, "a.txt", []byte("doublon")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload a: %v", err)
|
||||
}
|
||||
b, err := s.Upload(userID, multipartFileHeader(t, "b.txt", []byte("doublon")), "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("upload b: %v", err)
|
||||
}
|
||||
if _, err := s.Upload(userID, multipartFileHeader(t, "c.txt", []byte("unique-contenu")), "", ""); err != nil {
|
||||
t.Fatalf("upload c: %v", err)
|
||||
}
|
||||
// Même contenu chez un autre compte : pas mon doublon.
|
||||
if _, err := s.Upload(other, multipartFileHeader(t, "z.txt", []byte("doublon")), "", ""); err != nil {
|
||||
t.Fatalf("upload other: %v", err)
|
||||
}
|
||||
|
||||
groups, err := s.ListDuplicates(userID)
|
||||
if err != nil {
|
||||
t.Fatalf("duplicates: %v", err)
|
||||
}
|
||||
if len(groups) != 1 || groups[0].Count != 2 {
|
||||
t.Fatalf("groupes inattendus: %+v", groups)
|
||||
}
|
||||
g := groups[0]
|
||||
if g.TotalBytes != 14 || g.ReclaimableBytes != 7 {
|
||||
t.Fatalf("tailles inattendues: %+v", g)
|
||||
}
|
||||
ids := map[string]bool{}
|
||||
for _, f := range g.Files {
|
||||
ids[f.ID] = true
|
||||
}
|
||||
if !ids[a.ID] || !ids[b.ID] {
|
||||
t.Fatalf("membres inattendus: %+v", g.Files)
|
||||
}
|
||||
|
||||
// Compte propre → [] (jamais null).
|
||||
empty, err := s.ListDuplicates(other)
|
||||
if err != nil {
|
||||
t.Fatalf("duplicates other: %v", err)
|
||||
}
|
||||
if len(empty) != 0 {
|
||||
t.Fatalf("compte sans doublon: %+v", empty)
|
||||
}
|
||||
}
|
||||
@@ -412,6 +412,9 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
|
||||
}
|
||||
return FileDTO{}, err
|
||||
}
|
||||
// V3 S5 : octets frais → hash (doublons) + réconciliation des liens.
|
||||
// Best-effort (jamais d'échec sur du hashing).
|
||||
s.refreshContentHash(ownerID, resourceID)
|
||||
return toFileDTO(row), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ type createResourcePayload struct {
|
||||
ParentResourceID string `json:"parentResourceId"` // dossier parent (racine si vide), cf. docs/api-v1.md §6.1
|
||||
MimeType string `json:"mimeType"`
|
||||
Extension string `json:"extension"`
|
||||
// V3 S5 : session de scan opaque (32-hex mobile) — le serveur en déduit
|
||||
// les arêtes same_scan. Absent = ressource isolée (jamais d'erreur).
|
||||
ScanSessionID string `json:"scanSessionId"`
|
||||
}
|
||||
|
||||
type renamePayload struct {
|
||||
@@ -241,6 +244,11 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
if p.Name == "" {
|
||||
return errors.New("payload.name required")
|
||||
}
|
||||
// V3 S5 : session de scan opaque (dérive same_scan, jamais d'erreur
|
||||
// bloquante au-delà du format).
|
||||
if p.ScanSessionID != "" && !resourceIDPattern.MatchString(p.ScanSessionID) {
|
||||
return errors.New("payload.scanSessionId must be 32 lowercase hex chars")
|
||||
}
|
||||
// Le parent doit exister et appartenir à l'utilisateur (NOT_FOUND sinon,
|
||||
// cohérent avec move_resource). Ordre du SAF walk : parent avant enfant.
|
||||
if p.ParentResourceID != "" {
|
||||
@@ -270,6 +278,15 @@ func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) err
|
||||
}
|
||||
return insertErr
|
||||
}
|
||||
// V3 S5 : session de scan → arêtes same_scan (best-effort, jamais
|
||||
// bloquant : la ressource existe, seuls les liens manqueraient).
|
||||
if p.ScanSessionID != "" {
|
||||
if err := s.Repo.SetScanSession(ownerID, resourceID, p.ScanSessionID); err != nil {
|
||||
log.Printf("sync: scan session %s: %v", resourceID, err)
|
||||
} else if err := s.Repo.ReconcileLinks(ownerID, resourceID); err != nil {
|
||||
log.Printf("sync: reconcile %s: %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
case OpUpdateMetadata:
|
||||
|
||||
+5
-1
@@ -51,6 +51,9 @@ Références : `V2.md` (modèle cible), `mobile/services/db/` (conventions sync)
|
||||
| GET | `/files/:id/ocr` | — | `FileOcr` (`{ text, updatedAt }`) | `NOT_FOUND` |
|
||||
| GET | `/files/:id/facts` | — (viewer+, même portée que l'OCR) | `Fact[]` (`{ factType: date|due_date|period|amount|issuer|reference|doc_type, valueText?, valueNum?, valueDate? (ISO), confidence (0..1), origin: auto|user }`, `[]` si rien d'extractible) | `NOT_FOUND` (trashé/absent/étranger) |
|
||||
| PATCH | `/files/:id/facts` | `{ facts: [{ factType, valueText?, valueNum?, valueDate?, confidence? }] }` (jeu `user` complet, full-replace, editor+) | `Fact[]` (jeu complet auto + user) | `NOT_FOUND` (viewer-/absent/trashé), `INVALID_REQUEST` (type inconnu, date non ISO, fait sans valeur, corps malformé) |
|
||||
| GET | `/files/:id/relations` | — (owner uniquement) | `Relation[]` (`{ id, otherId, type: same_scan|duplicate_of|same_issuer|same_reference, confidence, createdBy: auto|user, otherName, otherType, otherSize }`, réconcilié à la lecture, autre bout vivant) | `NOT_FOUND` (étranger/trashé/absent — pas d'énumération) |
|
||||
| POST | `/files/:id/relations/verdict` | `{ otherId, linkType, confirmed }` | `Relation[]` (jeu réconcilié) | `NOT_FOUND` (bouts non possédés), `INVALID_REQUEST` (type/paire invalide, corps malformé) |
|
||||
| GET | `/duplicates` | — (compte connecté) | `DuplicateGroup[]` (`{ contentHash, count, totalBytes, reclaimableBytes, files: [{ id, name, size, updatedAt }] }`, plus gros d'abord, `[]` si propre) | — |
|
||||
| GET | `/files/:id/shares` | — (owner uniquement) | `ShareGrant[]` (`{ granteeUserId, granteeKind: "user"|"group", username, access, inherit, expiresAt (ms|null), updatedAt (ms) }`) | `NOT_FOUND` (non-owner ou inconnue — pas d'énumération) |
|
||||
| GET | `/files/:id/links` | — (owner uniquement) | `ShareLink[]` (même forme que `GET /shares/links/:token`) | `NOT_FOUND` (non-owner ou inconnue) |
|
||||
| GET | `/files/:id/placements` | — (owner uniquement) | `Placement[]` (`{ locationType: "server"|"mobile", deviceId?, hasBytes, updatedAt }`, entrée serveur en premier) | `NOT_FOUND` (non-owner ou inconnue) |
|
||||
@@ -128,11 +131,12 @@ type FileOcr = { text: string; updatedAt?: string | null };
|
||||
- Réponse : `2xx` avec `{ "applied": int, "failed": { "operation_id": "…32-hex", "code": string, "message": string } | null }` (`applied` = index de la prochaine op à envoyer).
|
||||
- Côté client, le `pushStatus` (pending/synced/failed) des shares/share_links est **dérivé** de l'état des opérations de l'outbox ; **dead-letter immédiat** sur erreur 4xx non-idempotente (`failed`, non resélectionné ; `attempts` reste un compteur diagnostic, pas un seuil) — seul le transitoire (`NETWORK_ERROR`/5xx) est rejoué avec backoff. **Côté serveur, les ops partage créent un état réel** (tables `shares`/`share_links`, migration `000009`) — l'outbox est le seul chemin d'écriture des droits.
|
||||
- Sémantique d'application (côté serveur) :
|
||||
- `create_resource` : crée la ressource ; **déjà présente → no-op** (rejeu idempotent). `payload.name` obligatoire ; `payload.parentResourceId` (32-hex, optionnel) = dossier parent — absent → racine. **Parent inexistant → `NOT_FOUND`** (cohérent avec `move_resource`). Ordre garanti par construction client : le walk SAF émet les `create` des dossiers (ordre préfixe) avant ceux des fichiers, dans la même transaction Room → `id ASC` = parent avant enfant.
|
||||
- `create_resource` : crée la ressource ; **déjà présente → no-op** (rejeu idempotent). `payload.name` obligatoire ; `payload.parentResourceId` (32-hex, optionnel) = dossier parent — absent → racine. **Parent inexistant → `NOT_FOUND`** (cohérent avec `move_resource`). Ordre garanti par construction client : le walk SAF émet les `create` des dossiers (ordre préfixe) avant ceux des fichiers, dans la même transaction Room → `id ASC` = parent avant enfant. `payload.scanSessionId` (32-hex, optionnel, V3 S5) = session de scan opaque — le serveur en déduit les arêtes `same_scan` (même session, même compte) ; format invalide → `INVALID_REQUEST`.
|
||||
- `update_metadata` : ressource absente → **no-op** (état terminal atteint) ; **ressource partagée : nécessite `editor+`** (§6.2) — sinon `NOT_FOUND` (une ressource visible mais sans droit de mutation n'est pas renommée) ; une ressource sans relation applicable (ni owner, ni grant) reste un no-op (pas d'énumération). `move_resource` / `delete_resource` restent **owner-only** (scoping par `user_id`, les ops d'un non-owner sont des no-op).
|
||||
- `delete_resource` : **legacy → mappé vers `trash_resource`** (corbeille, restaurable, même idempotence : absent/déjà trashé = succès, vivant d'autrui = `NOT_FOUND` halt).
|
||||
- **Corbeille V3** (`trash_resource` / `restore_resource` / `purge_resource`, owner-only) : `trash` pose `deleted_at` sur la ressource **et son subtree** (dossier → contenu) ; `restore` lève le flag sur la ressource, son subtree **et sa chaîne d'ancêtres** (un fichier restauré dont le dossier est encore trashé resterait injoignable) ; `purge` détruit lignes + versions + octets. Sémantique : absent → no-op (`trash`/`restore`/`purge`), vivant d'autrui → `NOT_FOUND` halt, `restore` d'un live → no-op succès. Une ressource trashée reste `NOT_FOUND` en lecture normale, visible seulement via `GET /trash`. Passe serveur : destruction définitive après 30j (`TrashRetention`).
|
||||
- **Corrections V3** (`correct_facts`, editor+) : `payload = { "facts": [{ "factType", "valueText"?, "valueNum"?, "valueDate"? (ISO `YYYY-MM-DD`), "confidence"? }] }` — remplace le jeu `user` complet (full-replace, jamais de diff ; jeu vide = efface les corrections, l'auto survit). Mêmes gardes qu'`update_metadata` (viewer- → `NOT_FOUND` halt) ; fait invalide (type inconnu, date non ISO, sans valeur) → `INVALID_REQUEST` avec arrêt du batch. Le client corrige en un tap depuis la fiche document et pousse dans la même transaction Room que l'écriture optimiste.
|
||||
- **Graphe V3 (lecture)** : `GET /files/:id/relations` (owner-only) réconcilie puis renvoie les arêtes vivantes — `same_scan` (même `scan_session_id`, 1.0), `duplicate_of` (même SHA-256, 1.0 — hash posé à l'upload + backfill boot), `same_issuer`/`same_reference` (faits égaux, confiance = min, auto ignoré si correction `user` du même type — miroir de l'affichage). Stockage canonique (`from < to`), verdicts `user` préservés et bloquant toute re-création auto, élagage des auto périmées. `POST /files/:id/relations/verdict` (confirmé → 1.0, infirmé → 0.0 conservé). `version_of` non matérialisé : les versions vivent DANS la ressource (`resource_versions`). `GET /duplicates` (compte connecté) : groupes par hash avec gain récupérable (total − plus gros).
|
||||
- **Versions V3** : chaque overwrite d'octets (upload ciblé) fige l'ancienne génération (`resource_versions`, N=3, élagage auto) ; `POST /files/:id/versions/:n/restore` recopie vers le live (l'actuel est à son tour versionné). Upload ciblé vers une ressource trashée → `NOT_FOUND` (jamais de résurrection d'octets).
|
||||
- **Partage** (`share` / `update_share`, même sémantique d'upsert) : `payload = { "granteeUserId"?: "…32-hex", "granteeGroupId"?: "…32-hex", "access": "viewer"|"commenter"|"editor", "inherit"?: bool, "expiresAt"?: ms }` — **exactement un** principal (user XOR groupe, V2-6). La ressource doit **appartenir** au user appelant (sinon `NOT_FOUND`) ; le grantee user doit exister (sinon `GRANTEE_NOT_FOUND`), le groupe doit exister (sinon `GROUP_NOT_FOUND`) ; accès déjà partagé → mis à jour (idempotent). Privilégier `viewer` < `commenter` < `editor` (< `owner` réservé à l'ownership, non partageable → `INVALID_REQUEST`). Les membres d'un groupe partagé héritent du grant (un niveau, sans imbrication) dans le snapshot comme dans l'enforcement.
|
||||
- `revoke_share` : `payload = { "granteeUserId": "…32-hex" }`. **Idempotent** (déjà révoqué/absent → no-op).
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,8 @@ import androidx.room.RoomDatabase
|
||||
import com.kazier.mobile.data.local.dao.FileDao
|
||||
import com.kazier.mobile.data.local.dao.FactsDao
|
||||
import com.kazier.mobile.data.local.dao.FolderDao
|
||||
import com.kazier.mobile.data.local.dao.LinksDao
|
||||
import com.kazier.mobile.data.local.dao.CollectionDao
|
||||
import com.kazier.mobile.data.local.dao.PendingOperationDao
|
||||
import com.kazier.mobile.data.local.dao.ResourcePermissionDao
|
||||
import com.kazier.mobile.data.local.dao.ScanDao
|
||||
@@ -12,8 +14,10 @@ import com.kazier.mobile.data.local.dao.UserPreferenceDao
|
||||
import com.kazier.mobile.data.local.entity.FileEntity
|
||||
import com.kazier.mobile.data.local.entity.FilesFts
|
||||
import com.kazier.mobile.data.local.entity.FolderEntity
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import com.kazier.mobile.data.local.entity.PendingOperationEntity
|
||||
import com.kazier.mobile.data.local.entity.ResourceFact
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
import com.kazier.mobile.data.local.entity.ResourcePermissionEntity
|
||||
import com.kazier.mobile.data.local.entity.ScanPageEntity
|
||||
import com.kazier.mobile.data.local.entity.ScanSessionEntity
|
||||
@@ -41,18 +45,20 @@ import com.kazier.mobile.data.local.entity.UserPreferenceEntity
|
||||
* restore exists=1, purge locale après 30j+).
|
||||
* v20: table resource_facts (faits génériques V3 — miroir GET /facts,
|
||||
* remplacement en bloc au sync, corrections `user` poussées en outbox).
|
||||
* v21: table virtuelle FTS4 `files_fts` (nom + corps ocr+faits) + triggers
|
||||
* de maintien (files insert/update/delete, facts insert/update/delete) —
|
||||
* recherche plein-texte offline S4. Pas d'entité Room (table virtuelle
|
||||
* maintenue en SQL migration uniquement, requêtes via `FileDao`).
|
||||
* v21: table virtuelle FTS4 `files_fts` (nom + corps ocr+faits, entité
|
||||
* `FilesFts` déclarée pour KSP) + triggers de maintien (`FtsTriggers` :
|
||||
* migration + callback fresh-install) — recherche plein-texte offline S4.
|
||||
* v22: `resource_links` (miroir `GET /relations`, remplacement en bloc) +
|
||||
* `collections` (requêtes sauvegardées locales, jamais syncées).
|
||||
*/
|
||||
@Database(
|
||||
entities = [
|
||||
FolderEntity::class, UserPreferenceEntity::class, FileEntity::class,
|
||||
PendingOperationEntity::class, ScanSessionEntity::class, ScanPageEntity::class,
|
||||
ResourcePermissionEntity::class, ResourceFact::class, FilesFts::class,
|
||||
ResourceLink::class, CollectionEntity::class,
|
||||
],
|
||||
version = 21,
|
||||
version = 22,
|
||||
// m19 : schéma exporté (`app/schemas/`) — toute dérive non migrée casse
|
||||
// la compilation au lieu de briqueter les utilisateurs au démarrage.
|
||||
exportSchema = true,
|
||||
@@ -61,6 +67,8 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun folderDao(): FolderDao
|
||||
abstract fun factsDao(): FactsDao
|
||||
abstract fun linksDao(): LinksDao
|
||||
abstract fun collectionDao(): CollectionDao
|
||||
abstract fun userPreferenceDao(): UserPreferenceDao
|
||||
abstract fun fileDao(): FileDao
|
||||
abstract fun pendingOperationDao(): PendingOperationDao
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.kazier.mobile.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CollectionDao {
|
||||
|
||||
@Query("SELECT * FROM collections ORDER BY created_at DESC")
|
||||
fun observeAll(): Flow<List<CollectionEntity>>
|
||||
|
||||
@Query("SELECT * FROM collections WHERE id = :id LIMIT 1")
|
||||
suspend fun getById(id: Long): CollectionEntity?
|
||||
|
||||
@Insert
|
||||
suspend fun insert(collection: CollectionEntity): Long
|
||||
|
||||
@Query("DELETE FROM collections WHERE id = :id")
|
||||
suspend fun deleteById(id: Long)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.kazier.mobile.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface LinksDao {
|
||||
|
||||
/** Arêtes touchant la ressource (les deux sens, confiance décroissante). */
|
||||
@Query("""
|
||||
SELECT * FROM resource_links
|
||||
WHERE from_id = :resourceId OR to_id = :resourceId
|
||||
ORDER BY confidence DESC
|
||||
""")
|
||||
fun observeForResource(resourceId: String): Flow<List<ResourceLink>>
|
||||
|
||||
@Query("""
|
||||
SELECT * FROM resource_links
|
||||
WHERE from_id = :resourceId OR to_id = :resourceId
|
||||
ORDER BY confidence DESC
|
||||
""")
|
||||
suspend fun getForResource(resourceId: String): List<ResourceLink>
|
||||
|
||||
@Insert
|
||||
suspend fun insertAll(links: List<ResourceLink>)
|
||||
|
||||
/** Remplacement en bloc (sync serveur, source de vérité) — voir repository. */
|
||||
@Query("DELETE FROM resource_links WHERE from_id = :resourceId OR to_id = :resourceId")
|
||||
suspend fun deleteForResource(resourceId: String)
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.kazier.mobile.data.local.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* Collection locale V3 (S5) : requête de recherche sauvegardée
|
||||
* (texte + facettes JSON) — pur local, jamais synchronisée (un device, pas
|
||||
* de conflit ; sync serveur = V4). Ex. « Impôts 2026 ».
|
||||
*/
|
||||
@Entity(
|
||||
tableName = "collections",
|
||||
indices = [
|
||||
Index(value = ["name"], unique = true),
|
||||
],
|
||||
)
|
||||
data class CollectionEntity(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0L,
|
||||
@ColumnInfo(name = "name")
|
||||
val name: String,
|
||||
/** Texte de recherche (peut être vide : facettes seules). */
|
||||
@ColumnInfo(name = "query")
|
||||
val query: String = "",
|
||||
/** Facettes sérialisées (`SearchFacets` JSON, Moshi Map). */
|
||||
@ColumnInfo(name = "facets_json")
|
||||
val facetsJson: String = "{}",
|
||||
@ColumnInfo(name = "category")
|
||||
val category: String? = null,
|
||||
@ColumnInfo(name = "created_at")
|
||||
val createdAt: Long,
|
||||
)
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.kazier.mobile.data.local.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
/**
|
||||
* Miroir local d'une arête `GET /files/:id/relations` (V3 S5).
|
||||
*
|
||||
* Stockée orientée (`fromId`/`toId` canoniques serveur) mais lue dans les
|
||||
* deux sens (`LinksRepository.observeRelations`). Remplacement en bloc au
|
||||
* sync par ressource (le serveur réconcilie à la lecture). Les verdicts
|
||||
* (`createdBy = user`) sont écrits d'abord en Room (optimiste) puis poussés
|
||||
* en REST direct (pas d'outbox — acte rare, en ligne uniquement, S5).
|
||||
*/
|
||||
@Entity(
|
||||
tableName = "resource_links",
|
||||
indices = [
|
||||
Index(value = ["from_id", "to_id", "link_type"], unique = true),
|
||||
Index(value = ["to_id"]),
|
||||
],
|
||||
)
|
||||
data class ResourceLink(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Long = 0L,
|
||||
@ColumnInfo(name = "from_id")
|
||||
val fromId: String,
|
||||
@ColumnInfo(name = "to_id")
|
||||
val toId: String,
|
||||
@ColumnInfo(name = "link_type")
|
||||
val linkType: String,
|
||||
@ColumnInfo(name = "confidence")
|
||||
val confidence: Double = 0.0,
|
||||
@ColumnInfo(name = "created_by")
|
||||
val createdBy: String = LinkOrigin.AUTO,
|
||||
@ColumnInfo(name = "updated_at")
|
||||
val updatedAt: Long,
|
||||
) {
|
||||
/** L'autre bout vu depuis [resourceId] (arête symétrique). */
|
||||
fun otherEnd(resourceId: String): String =
|
||||
if (fromId == resourceId) toId else fromId
|
||||
}
|
||||
|
||||
/** Origines d'une arête — miroir du CHECK serveur (migration 000021). */
|
||||
object LinkOrigin {
|
||||
const val AUTO = "auto"
|
||||
const val USER = "user"
|
||||
}
|
||||
|
||||
/** Types d'arêtes — miroir du CHECK serveur (pas de `version_of` : les
|
||||
* versions vivent DANS la ressource). */
|
||||
object LinkType {
|
||||
const val SAME_SCAN = "same_scan"
|
||||
const val DUPLICATE_OF = "duplicate_of"
|
||||
const val SAME_ISSUER = "same_issuer"
|
||||
const val SAME_REFERENCE = "same_reference"
|
||||
}
|
||||
+50
-1
@@ -389,7 +389,56 @@ object Migrations {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* V3 relations + collections (S5) : `resource_links` (miroir
|
||||
* `GET /relations`, remplacement en bloc) + `collections` (requêtes
|
||||
* sauvegardées locales). Schémas alignés sur les entités (exports
|
||||
* `app/schemas/22.json`).
|
||||
*/
|
||||
private val MIGRATION_21_22 = object : Migration(21, 22) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `resource_links` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`from_id` TEXT NOT NULL,
|
||||
`to_id` TEXT NOT NULL,
|
||||
`link_type` TEXT NOT NULL,
|
||||
`confidence` REAL NOT NULL,
|
||||
`created_by` TEXT NOT NULL,
|
||||
`updated_at` INTEGER NOT NULL
|
||||
)
|
||||
""".trimIndent(),
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `index_resource_links_from_id_to_id_link_type` " +
|
||||
"ON `resource_links` (`from_id`, `to_id`, `link_type`)",
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE INDEX IF NOT EXISTS `index_resource_links_to_id` " +
|
||||
"ON `resource_links` (`to_id`)",
|
||||
)
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `collections` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` TEXT NOT NULL,
|
||||
`query` TEXT NOT NULL,
|
||||
`facets_json` TEXT NOT NULL,
|
||||
`category` TEXT,
|
||||
`created_at` INTEGER NOT NULL
|
||||
)
|
||||
""".trimIndent(),
|
||||
)
|
||||
db.execSQL(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `index_collections_name` " +
|
||||
"ON `collections` (`name`)",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** En dernier : l'ordre d'init Kotlin exige les migrations avant. */
|
||||
val ALL: Array<Migration> = arrayOf(
|
||||
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16, MIGRATION_16_17, MIGRATION_17_18, MIGRATION_18_19, MIGRATION_19_20, MIGRATION_20_21,
|
||||
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16, MIGRATION_16_17, MIGRATION_17_18, MIGRATION_18_19, MIGRATION_19_20, MIGRATION_20_21, MIGRATION_21_22,
|
||||
)
|
||||
}
|
||||
@@ -30,7 +30,10 @@ import com.kazier.mobile.data.remote.dto.SyncOpDto
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
||||
import com.kazier.mobile.data.remote.dto.CorrectFactsDto
|
||||
import com.kazier.mobile.data.remote.dto.DuplicateGroupDto
|
||||
import com.kazier.mobile.data.remote.dto.FactDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationVerdictDto
|
||||
import com.kazier.mobile.data.remote.dto.TrashDto
|
||||
import com.kazier.mobile.data.remote.dto.VersionDto
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
@@ -175,6 +178,18 @@ class ApiClient @Inject constructor(
|
||||
suspend fun correctFacts(fileId: String, facts: List<FactDto>): List<FactDto> =
|
||||
unwrap({ apiService.correctFacts(fileId, CorrectFactsDto(facts)) })
|
||||
|
||||
/** Graphe documentaire V3 — `GET /files/:id/relations` (owner-only). */
|
||||
suspend fun listRelations(fileId: String): List<RelationDto> =
|
||||
unwrap({ apiService.listRelations(fileId) })
|
||||
|
||||
/** Verdict sur une arête — renvoie le jeu réconcilié. */
|
||||
suspend fun verdictRelation(fileId: String, otherId: String, linkType: String, confirmed: Boolean): List<RelationDto> =
|
||||
unwrap({ apiService.verdictRelation(fileId, RelationVerdictDto(otherId, linkType, confirmed)) })
|
||||
|
||||
/** Doublons d'octets — `GET /duplicates` (owner). */
|
||||
suspend fun listDuplicates(): List<DuplicateGroupDto> =
|
||||
unwrap({ apiService.listDuplicates() })
|
||||
|
||||
/** Recherche serveur paginée — `GET /files/search?q=…` (+ `meta`). */
|
||||
suspend fun searchFilesPaged(
|
||||
query: String,
|
||||
|
||||
@@ -23,7 +23,10 @@ import com.kazier.mobile.data.remote.dto.ShareLinkDto
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsRequest
|
||||
import com.kazier.mobile.data.remote.dto.SyncOpsResult
|
||||
import com.kazier.mobile.data.remote.dto.CorrectFactsDto
|
||||
import com.kazier.mobile.data.remote.dto.DuplicateGroupDto
|
||||
import com.kazier.mobile.data.remote.dto.FactDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationDto
|
||||
import com.kazier.mobile.data.remote.dto.RelationVerdictDto
|
||||
import com.kazier.mobile.data.remote.dto.TrashDto
|
||||
import com.kazier.mobile.data.remote.dto.VersionDto
|
||||
import okhttp3.MultipartBody
|
||||
@@ -197,6 +200,29 @@ interface ApiService {
|
||||
@Body body: CorrectFactsDto,
|
||||
): Response<ApiEnvelope<List<FactDto>>>
|
||||
|
||||
/**
|
||||
* Graphe documentaire V3 (`GET /files/:id/relations`, owner-only) —
|
||||
* arêtes réconciliées à la lecture, autre bout vivant, confiance décroissante.
|
||||
*/
|
||||
@GET("files/{id}/relations")
|
||||
suspend fun listRelations(
|
||||
@Path("id") fileId: String,
|
||||
): Response<ApiEnvelope<List<RelationDto>>>
|
||||
|
||||
/** Verdict explicite sur une arête (confirmé → 1.0, infirmé → 0.0). */
|
||||
@POST("files/{id}/relations/verdict")
|
||||
suspend fun verdictRelation(
|
||||
@Path("id") fileId: String,
|
||||
@Body body: RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<RelationDto>>>
|
||||
|
||||
/**
|
||||
* Doublons d'octets du compte (`GET /duplicates`, owner) — groupes par
|
||||
* hash, plus gros d'abord, avec le gain récupérable (B2).
|
||||
*/
|
||||
@GET("duplicates")
|
||||
suspend fun listDuplicates(): Response<ApiEnvelope<List<DuplicateGroupDto>>>
|
||||
|
||||
/**
|
||||
* Recherche serveur (`GET /files/search?q=…`, paginé `meta`). Non consommée
|
||||
* en V1 (recherche 100 % locale via Room) — exposée pour parité contrat.
|
||||
|
||||
@@ -297,3 +297,42 @@ data class FactDto(
|
||||
data class CorrectFactsDto(
|
||||
@Json(name = "facts") val facts: List<FactDto>,
|
||||
)
|
||||
|
||||
/**
|
||||
* Arête `GET /files/{id}/relations` (V3 S5) — miroir de `RelationDTO` Go.
|
||||
* Graphe owner-only, réconcilié à la lecture côté serveur.
|
||||
*/
|
||||
data class RelationDto(
|
||||
@Json(name = "id") val id: String,
|
||||
@Json(name = "otherId") val otherId: String,
|
||||
@Json(name = "type") val type: String,
|
||||
@Json(name = "confidence") val confidence: Double = 0.0,
|
||||
@Json(name = "createdBy") val createdBy: String = "auto",
|
||||
@Json(name = "otherName") val otherName: String = "",
|
||||
@Json(name = "otherType") val otherType: String = "file",
|
||||
@Json(name = "otherSize") val otherSize: Long = 0L,
|
||||
)
|
||||
|
||||
/** Body `POST /files/{id}/relations/verdict` — verdict explicite (S5). */
|
||||
data class RelationVerdictDto(
|
||||
@Json(name = "otherId") val otherId: String,
|
||||
@Json(name = "linkType") val linkType: String,
|
||||
@Json(name = "confirmed") val confirmed: Boolean,
|
||||
)
|
||||
|
||||
/** Membre d'un groupe de doublons `GET /duplicates` (S5 B2). */
|
||||
data class DuplicateFileDto(
|
||||
@Json(name = "id") val id: String,
|
||||
@Json(name = "name") val name: String,
|
||||
@Json(name = "size") val size: Long = 0L,
|
||||
@Json(name = "updatedAt") val updatedAt: Long = 0L,
|
||||
)
|
||||
|
||||
/** Groupe de fichiers aux octets identiques (S5 B2). */
|
||||
data class DuplicateGroupDto(
|
||||
@Json(name = "contentHash") val contentHash: String,
|
||||
@Json(name = "count") val count: Int = 0,
|
||||
@Json(name = "totalBytes") val totalBytes: Long = 0L,
|
||||
@Json(name = "reclaimableBytes") val reclaimableBytes: Long = 0L,
|
||||
@Json(name = "files") val files: List<DuplicateFileDto> = emptyList(),
|
||||
)
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
import com.kazier.mobile.data.local.dao.CollectionDao
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Collections locales V3 (S5) : recherches sauvegardées (texte + facettes),
|
||||
* purement locales — jamais synchronisées (un device, pas de conflit).
|
||||
*/
|
||||
@Singleton
|
||||
class CollectionsRepository @Inject constructor(
|
||||
private val collectionDao: CollectionDao,
|
||||
moshi: Moshi,
|
||||
) {
|
||||
|
||||
private val facetsAdapter: JsonAdapter<Map<String, Any?>> = moshi.adapter(
|
||||
Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java),
|
||||
)
|
||||
|
||||
fun observeAll(): Flow<List<CollectionEntity>> = collectionDao.observeAll()
|
||||
|
||||
suspend fun save(name: String, query: String, category: String?, facets: SearchFacets): Long {
|
||||
val trimmed = name.trim()
|
||||
require(trimmed.isNotEmpty()) { "collection name required" }
|
||||
return collectionDao.insert(
|
||||
CollectionEntity(
|
||||
name = trimmed,
|
||||
query = query.trim(),
|
||||
facetsJson = facetsAdapter.toJson(facets.toMap()),
|
||||
category = category,
|
||||
createdAt = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun delete(id: Long) = collectionDao.deleteById(id)
|
||||
|
||||
suspend fun get(id: Long): CollectionEntity? = collectionDao.getById(id)
|
||||
|
||||
/** Rejoue une collection : texte + facettes désérialisées (inconnues ignorées). */
|
||||
fun apply(collection: CollectionEntity): AppliedCollection {
|
||||
val map = runCatching {
|
||||
facetsAdapter.fromJson(collection.facetsJson)
|
||||
}.getOrNull() ?: emptyMap()
|
||||
return AppliedCollection(
|
||||
query = collection.query,
|
||||
category = collection.category,
|
||||
facets = SearchFacets(
|
||||
issuer = map["issuer"] as? String,
|
||||
docType = map["docType"] as? String,
|
||||
minAmount = (map["minAmount"] as? Number)?.toDouble(),
|
||||
maxAmount = (map["maxAmount"] as? Number)?.toDouble(),
|
||||
fromDate = map["fromDate"] as? String,
|
||||
toDate = map["toDate"] as? String,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun SearchFacets.toMap(): Map<String, Any?> = buildMap {
|
||||
issuer?.let { put("issuer", it) }
|
||||
docType?.let { put("docType", it) }
|
||||
minAmount?.let { put("minAmount", it) }
|
||||
maxAmount?.let { put("maxAmount", it) }
|
||||
fromDate?.let { put("fromDate", it) }
|
||||
toDate?.let { put("toDate", it) }
|
||||
}
|
||||
}
|
||||
|
||||
/** Collection rejouée : à appliquer à l'état de recherche. */
|
||||
data class AppliedCollection(
|
||||
val query: String,
|
||||
val category: String?,
|
||||
val facets: SearchFacets,
|
||||
)
|
||||
@@ -320,6 +320,7 @@ class FileRepository @Inject constructor(
|
||||
parentResourceId = entity.folderResourceId,
|
||||
mimeType = entity.mimeType,
|
||||
extension = entity.extension,
|
||||
scanSessionId = input.scanSessionId,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -506,6 +507,11 @@ data class SaveFileInput(
|
||||
val syncStatus: String? = null,
|
||||
/** Le fichier arrive déjà « traité » (ex. scan) ou à traiter dans la review. */
|
||||
val processed: Boolean = false,
|
||||
/**
|
||||
* V3 S5 : session de scan opaque (pages d'un même export) — propagée au
|
||||
* `create_resource` pour les arêtes `same_scan` serveur. NULL sinon.
|
||||
*/
|
||||
val scanSessionId: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import com.kazier.mobile.data.local.AppDatabase
|
||||
import com.kazier.mobile.data.local.dao.LinksDao
|
||||
import com.kazier.mobile.data.local.entity.LinkOrigin
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
import com.kazier.mobile.data.remote.ApiClient
|
||||
import com.kazier.mobile.data.remote.dto.RelationDto
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* Graphe documentaire V3 (S5) côté client.
|
||||
*
|
||||
* - [syncFromServer] : `GET /files/:id/relations` → remplacement en bloc
|
||||
* Room (le serveur réconcilie à la lecture — même pattern que les faits).
|
||||
* - [setVerdict] : verdict optimiste local + REST direct (**pas d'outbox** :
|
||||
* acte rare, en ligne uniquement — hors-ligne = erreur remontée, la fiche
|
||||
* garde l'état serveur). Le serveur renvoie le jeu réconcilié qui écrase
|
||||
* le miroir (convergence immédiate).
|
||||
*/
|
||||
@Singleton
|
||||
class LinksRepository @Inject constructor(
|
||||
private val appDatabase: AppDatabase,
|
||||
private val linksDao: LinksDao,
|
||||
private val apiClient: ApiClient,
|
||||
) {
|
||||
|
||||
fun observeRelations(resourceId: String): Flow<List<ResourceLink>> =
|
||||
linksDao.observeForResource(resourceId)
|
||||
|
||||
suspend fun getRelations(resourceId: String): List<ResourceLink> =
|
||||
linksDao.getForResource(resourceId)
|
||||
|
||||
/** Rapatrie le jeu serveur (auto + verdicts) et remplace le miroir local. */
|
||||
suspend fun syncFromServer(resourceId: String): List<ResourceLink> {
|
||||
val dtos = apiClient.listRelations(resourceId)
|
||||
val now = System.currentTimeMillis()
|
||||
val entities = dtos.map { it.toEntity(resourceId, now) }
|
||||
appDatabase.withTransaction {
|
||||
linksDao.deleteForResource(resourceId)
|
||||
if (entities.isNotEmpty()) linksDao.insertAll(entities)
|
||||
}
|
||||
return entities
|
||||
}
|
||||
|
||||
/**
|
||||
* Verdict explicite (confirmé → 1.0, infirmé → 0.0, origine `user`) :
|
||||
* écriture optimiste + REST. Le jeu renvoyé remplace le miroir.
|
||||
* Lève en hors-ligne (le verdict attendra un réseau — pas de file).
|
||||
*/
|
||||
suspend fun setVerdict(resourceId: String, otherId: String, linkType: String, confirmed: Boolean) {
|
||||
val now = System.currentTimeMillis()
|
||||
val optimistic = ResourceLink(
|
||||
fromId = minOf(resourceId, otherId),
|
||||
toId = maxOf(resourceId, otherId),
|
||||
linkType = linkType,
|
||||
confidence = if (confirmed) 1.0 else 0.0,
|
||||
createdBy = LinkOrigin.USER,
|
||||
updatedAt = now,
|
||||
)
|
||||
appDatabase.withTransaction {
|
||||
linksDao.deleteForResource(resourceId)
|
||||
val current = linksDao.getForResource(resourceId)
|
||||
.filterNot { it.otherEnd(resourceId) == otherId && it.linkType == linkType }
|
||||
linksDao.insertAll(current + optimistic)
|
||||
}
|
||||
val dtos = apiClient.verdictRelation(resourceId, otherId, linkType, confirmed)
|
||||
val entities = dtos.map { it.toEntity(resourceId, now) }
|
||||
appDatabase.withTransaction {
|
||||
linksDao.deleteForResource(resourceId)
|
||||
if (entities.isNotEmpty()) linksDao.insertAll(entities)
|
||||
}
|
||||
}
|
||||
|
||||
private fun RelationDto.toEntity(resourceId: String, now: Long): ResourceLink {
|
||||
// Ordre canonique serveur (from < to) — conservé tel quel.
|
||||
val (from, to) = if (resourceId <= otherId) resourceId to otherId else otherId to resourceId
|
||||
return ResourceLink(
|
||||
fromId = from,
|
||||
toId = to,
|
||||
linkType = type,
|
||||
confidence = confidence,
|
||||
createdBy = createdBy.ifBlank { LinkOrigin.AUTO },
|
||||
updatedAt = now,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,9 @@ class OutboxRepository @Inject constructor(
|
||||
parentResourceId: String? = null,
|
||||
mimeType: String? = null,
|
||||
extension: String? = null,
|
||||
// V3 S5 : session de scan opaque (même export multi-pages) — le
|
||||
// serveur en déduit les arêtes `same_scan`. NULL = ressource isolée.
|
||||
scanSessionId: String? = null,
|
||||
): String = enqueue(
|
||||
operation = PendingOperationType.CREATE_RESOURCE,
|
||||
resourceId = resourceId,
|
||||
@@ -86,6 +89,7 @@ class OutboxRepository @Inject constructor(
|
||||
parentResourceId?.let { put("parentResourceId", it) }
|
||||
mimeType?.let { put("mimeType", it) }
|
||||
extension?.let { put("extension", it) }
|
||||
scanSessionId?.let { put("scanSessionId", it) }
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import com.kazier.mobile.data.local.AppDatabase
|
||||
import com.kazier.mobile.data.local.dao.FileDao
|
||||
import com.kazier.mobile.data.local.dao.FactsDao
|
||||
import com.kazier.mobile.data.local.dao.FolderDao
|
||||
import com.kazier.mobile.data.local.dao.LinksDao
|
||||
import com.kazier.mobile.data.local.dao.CollectionDao
|
||||
import com.kazier.mobile.data.local.dao.PendingOperationDao
|
||||
import com.kazier.mobile.data.local.dao.ResourcePermissionDao
|
||||
import com.kazier.mobile.data.local.dao.ScanDao
|
||||
@@ -67,4 +69,10 @@ object DatabaseModule {
|
||||
|
||||
@Provides
|
||||
fun provideFactsDao(db: AppDatabase): FactsDao = db.factsDao()
|
||||
|
||||
@Provides
|
||||
fun provideLinksDao(db: AppDatabase): LinksDao = db.linksDao()
|
||||
|
||||
@Provides
|
||||
fun provideCollectionDao(db: AppDatabase): CollectionDao = db.collectionDao()
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.kazier.mobile.ui.duplicates
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.ContentCopy
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.kazier.mobile.R
|
||||
import com.kazier.mobile.data.remote.dto.DuplicateGroupDto
|
||||
import com.kazier.mobile.ui.components.formatFileSize
|
||||
|
||||
/**
|
||||
* Doublons V3 (S5 B2) : groupes identiques avec gain récupérable + action
|
||||
* « garder le plus récent » (trash FULL du reste). Point d'entrée :
|
||||
* Réglages → Doublons.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun DuplicatesScreen(
|
||||
onBack: () -> Unit,
|
||||
viewModel: DuplicatesViewModel = hiltViewModel(),
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
LaunchedEffect(state.error) {
|
||||
state.error?.let {
|
||||
snackbarHostState.showSnackbar(it)
|
||||
viewModel.clearError()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(R.string.duplicates_title)) },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onBack) {
|
||||
Icon(
|
||||
Icons.AutoMirrored.Filled.ArrowBack,
|
||||
contentDescription = stringResource(R.string.back),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
|
||||
) { padding ->
|
||||
if (state.isLoading && state.groups.isEmpty()) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize().padding(padding),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
} else if (state.groups.isEmpty()) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize().padding(padding).padding(32.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.duplicates_empty),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.Center,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize().padding(padding),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
item(key = "hint") {
|
||||
Text(
|
||||
text = stringResource(R.string.duplicates_hint),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
items(state.groups, key = { it.contentHash }) { group ->
|
||||
DuplicateGroupCard(
|
||||
group = group,
|
||||
busy = state.busyGroup == group.contentHash,
|
||||
onKeepNewest = { viewModel.keepNewest(group) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun DuplicateGroupCard(
|
||||
group: DuplicateGroupDto,
|
||||
busy: Boolean,
|
||||
onKeepNewest: () -> Unit,
|
||||
) {
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(12.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
Icons.Filled.ContentCopy,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(24.dp),
|
||||
)
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = stringResource(R.string.duplicates_group, group.count),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
fontWeight = FontWeight.Medium,
|
||||
)
|
||||
Text(
|
||||
text = stringResource(
|
||||
R.string.duplicates_reclaimable,
|
||||
formatFileSize(group.reclaimableBytes),
|
||||
),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
if (busy) {
|
||||
CircularProgressIndicator(modifier = Modifier.size(24.dp))
|
||||
} else {
|
||||
IconButton(onClick = onKeepNewest) {
|
||||
Icon(
|
||||
Icons.Filled.Delete,
|
||||
contentDescription = stringResource(R.string.duplicates_keep_newest),
|
||||
tint = MaterialTheme.colorScheme.error,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(8.dp))
|
||||
group.files.forEachIndexed { index, file ->
|
||||
Text(
|
||||
text = (if (index == 0) "✓ " else " ") + file.name,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = if (index == 0) MaterialTheme.colorScheme.onSurface
|
||||
else MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.kazier.mobile.ui.duplicates
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.kazier.mobile.data.remote.ApiClient
|
||||
import com.kazier.mobile.data.remote.dto.DuplicateGroupDto
|
||||
import com.kazier.mobile.data.repository.FileRepository
|
||||
import com.kazier.mobile.features.saf.FileDeleter
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Doublons V3 (S5 B2) : groupes de fichiers aux octets identiques, avec le
|
||||
* gain récupérable. « Garder le plus récent » trashe le reste (FULL :
|
||||
* corbeille serveur + suppression locale) — le plus récent (1er, tri
|
||||
* serveur) est conservé.
|
||||
*/
|
||||
@HiltViewModel
|
||||
class DuplicatesViewModel @Inject constructor(
|
||||
private val apiClient: ApiClient,
|
||||
private val fileRepository: FileRepository,
|
||||
private val fileDeleter: FileDeleter,
|
||||
) : ViewModel() {
|
||||
|
||||
data class DuplicatesUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val groups: List<DuplicateGroupDto> = emptyList(),
|
||||
val error: String? = null,
|
||||
val busyGroup: String? = null,
|
||||
)
|
||||
|
||||
private val _uiState = MutableStateFlow(DuplicatesUiState())
|
||||
val uiState: StateFlow<DuplicatesUiState> = _uiState.asStateFlow()
|
||||
|
||||
init {
|
||||
refresh()
|
||||
}
|
||||
|
||||
fun refresh() {
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(isLoading = true, error = null) }
|
||||
runCatching { apiClient.listDuplicates() }
|
||||
.onSuccess { groups ->
|
||||
_uiState.update { it.copy(isLoading = false, groups = groups) }
|
||||
}
|
||||
.onFailure { e ->
|
||||
Timber.d("duplicates refresh failed: %s", e.message)
|
||||
_uiState.update {
|
||||
it.copy(isLoading = false, error = e.message ?: e.javaClass.simpleName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearError() {
|
||||
_uiState.update { it.copy(error = null) }
|
||||
}
|
||||
|
||||
/** Trashe tous les membres sauf le plus récent (files[0], tri serveur). */
|
||||
fun keepNewest(group: DuplicateGroupDto) {
|
||||
if (group.files.size < 2) return
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(busyGroup = group.contentHash) }
|
||||
runCatching {
|
||||
val evicted = group.files.drop(1).mapNotNull { fileRepository.getFile(it.id) }
|
||||
fileDeleter.deleteFiles(evicted, FileDeleter.DeleteMode.FULL)
|
||||
}.onSuccess { report ->
|
||||
if (report.failed > 0) {
|
||||
_uiState.update { it.copy(error = "${report.failed} fichier(s) non trashé(s)") }
|
||||
}
|
||||
refresh()
|
||||
}.onFailure { e ->
|
||||
Timber.w(e, "duplicates keep newest failed")
|
||||
_uiState.update { it.copy(error = e.message ?: e.javaClass.simpleName) }
|
||||
}
|
||||
_uiState.update { it.copy(busyGroup = null) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,19 @@ import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Collections
|
||||
import androidx.compose.material.icons.filled.ContentCopy
|
||||
import androidx.compose.material.icons.filled.Edit
|
||||
import androidx.compose.material.icons.filled.Link
|
||||
import androidx.compose.material.icons.filled.RestartAlt
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.AssistChip
|
||||
import androidx.compose.material3.AssistChipDefaults
|
||||
@@ -40,22 +47,29 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.kazier.mobile.R
|
||||
import com.kazier.mobile.data.local.entity.FactOrigin
|
||||
import com.kazier.mobile.data.local.entity.FactType
|
||||
import com.kazier.mobile.data.local.entity.LinkOrigin
|
||||
import com.kazier.mobile.data.local.entity.LinkType
|
||||
import com.kazier.mobile.data.local.entity.ResourceFact
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
|
||||
/**
|
||||
* Fiche « Ce qu'on a compris » (V3 S3) : faits génériques extraits du
|
||||
* document, avec correction en un tap.
|
||||
* Fiche « Ce qu'on a compris » (V3 S3) + « Voir aussi » (V3 S5) : faits
|
||||
* génériques extraits du document (correction en un tap) et graphe
|
||||
* documentaire (doublons, même émetteur, pages du même scan — invalidables).
|
||||
*
|
||||
* - Confiance < 0.5 → chip grisé + « (suggéré) » (humble par design).
|
||||
* - Crayon → dialogue d'édition (texte / nombre / date ISO) ; la correction
|
||||
* devient un fait `user` (badge « corrigé ») et part en outbox.
|
||||
* - « Rétablir auto » → efface les corrections (l'auto réapparaît).
|
||||
* - Arête infirmée (×) → confiance 0 conservée (ne revient pas) ;
|
||||
* ré-affichée grisée avec option de re-confirmation.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
@@ -66,10 +80,15 @@ fun FactsBottomSheet(
|
||||
viewModel: FactsViewModel = hiltViewModel(),
|
||||
) {
|
||||
val facts by viewModel.facts.collectAsStateWithLifecycle()
|
||||
val links by viewModel.links.collectAsStateWithLifecycle()
|
||||
val linkNames by viewModel.linkNames.collectAsStateWithLifecycle()
|
||||
val syncing by viewModel.syncing.collectAsStateWithLifecycle()
|
||||
val error by viewModel.error.collectAsStateWithLifecycle()
|
||||
|
||||
LaunchedEffect(resourceId) { viewModel.watch(resourceId) }
|
||||
LaunchedEffect(links, resourceId) {
|
||||
viewModel.resolveLinkNames(links.map { it.otherEnd(resourceId) }.toSet())
|
||||
}
|
||||
|
||||
var editing by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
var showResetConfirm by rememberSaveable { mutableStateOf(false) }
|
||||
@@ -139,6 +158,24 @@ fun FactsBottomSheet(
|
||||
)
|
||||
}
|
||||
}
|
||||
if (links.isNotEmpty()) {
|
||||
Text(
|
||||
text = stringResource(R.string.links_title),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
modifier = Modifier.padding(top = 4.dp),
|
||||
)
|
||||
links.forEach { link ->
|
||||
val otherId = link.otherEnd(resourceId)
|
||||
LinkRow(
|
||||
link = link,
|
||||
displayName = linkNames[otherId] ?: otherId,
|
||||
onVerdict = { confirmed ->
|
||||
viewModel.setVerdict(otherId, link.linkType, confirmed)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(4.dp))
|
||||
}
|
||||
}
|
||||
@@ -248,6 +285,78 @@ private fun formatFactAmount(num: Double): String {
|
||||
return if (num == asLong.toDouble()) "$asLong €" else "$num €"
|
||||
}
|
||||
|
||||
/**
|
||||
* Ligne « Voir aussi » : icône par type, nom de l'autre bout, libellé du
|
||||
* lien, action de verdict (× = infirmer, ↺ = re-confirmer un lien infirmé).
|
||||
* Les liens infirmés (confiance 0, verdict user) restent affichés grisés —
|
||||
* ils ne reviendront pas tout seuls.
|
||||
*/
|
||||
@Composable
|
||||
private fun LinkRow(
|
||||
link: ResourceLink,
|
||||
displayName: String,
|
||||
onVerdict: (Boolean) -> Unit,
|
||||
) {
|
||||
val dismissed = link.confidence <= 0.0
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Icon(
|
||||
imageVector = when (link.linkType) {
|
||||
LinkType.DUPLICATE_OF -> Icons.Filled.ContentCopy
|
||||
LinkType.SAME_SCAN -> Icons.Filled.Collections
|
||||
else -> Icons.Filled.Link
|
||||
},
|
||||
contentDescription = null,
|
||||
tint = if (dismissed) MaterialTheme.colorScheme.onSurfaceVariant
|
||||
else MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(24.dp),
|
||||
)
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = displayName,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
color = if (dismissed) MaterialTheme.colorScheme.onSurfaceVariant
|
||||
else MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
Text(
|
||||
text = stringResource(linkTypeLabel(link.linkType)),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
)
|
||||
}
|
||||
if (dismissed && link.createdBy == LinkOrigin.USER) {
|
||||
IconButton(onClick = { onVerdict(true) }) {
|
||||
Icon(
|
||||
Icons.Filled.RestartAlt,
|
||||
contentDescription = stringResource(R.string.links_reconfirm),
|
||||
)
|
||||
}
|
||||
} else if (!dismissed) {
|
||||
IconButton(onClick = { onVerdict(false) }) {
|
||||
Icon(
|
||||
Icons.Filled.Close,
|
||||
contentDescription = stringResource(R.string.links_dismiss),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun linkTypeLabel(type: String): Int = when (type) {
|
||||
LinkType.SAME_SCAN -> R.string.links_type_same_scan
|
||||
LinkType.DUPLICATE_OF -> R.string.links_type_duplicate
|
||||
LinkType.SAME_ISSUER -> R.string.links_type_same_issuer
|
||||
LinkType.SAME_REFERENCE -> R.string.links_type_same_reference
|
||||
else -> R.string.links_type_other
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FactEditDialog(
|
||||
fact: ResourceFact,
|
||||
|
||||
@@ -3,8 +3,12 @@ package com.kazier.mobile.ui.facts
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.kazier.mobile.data.local.entity.ResourceFact
|
||||
import com.kazier.mobile.data.local.entity.ResourceLink
|
||||
import com.kazier.mobile.data.local.entity.displayFacts
|
||||
import com.kazier.mobile.data.repository.FactsRepository
|
||||
import com.kazier.mobile.data.repository.FileRepository
|
||||
import com.kazier.mobile.data.repository.FolderRepository
|
||||
import com.kazier.mobile.data.repository.LinksRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
@@ -20,16 +24,21 @@ import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Fiche « Ce qu'on a compris » (V3 S3) : faits génériques d'un document.
|
||||
* Fiche « Ce qu'on a compris » (V3 S3) + « Voir aussi » (V3 S5) : faits
|
||||
* génériques et graphe documentaire d'un document.
|
||||
*
|
||||
* [watch] charge le miroir Room puis rafraîchit depuis le serveur
|
||||
* [watch] charge les miroirs Room puis rafraîchit depuis le serveur
|
||||
* (best-effort — offline = cache). [saveCorrection] remplace le fait du même
|
||||
* type dans le jeu `user` (optimiste + outbox `correct_facts`) ;
|
||||
* [resetToAuto] efface les corrections (l'auto réapparaît à l'affichage).
|
||||
* [resetToAuto] efface les corrections (l'auto réapparaît). [setVerdict]
|
||||
* confirme/infirme une arête (REST direct, en ligne uniquement).
|
||||
*/
|
||||
@HiltViewModel
|
||||
class FactsViewModel @Inject constructor(
|
||||
private val factsRepository: FactsRepository,
|
||||
private val linksRepository: LinksRepository,
|
||||
private val fileRepository: FileRepository,
|
||||
private val folderRepository: FolderRepository,
|
||||
) : ViewModel() {
|
||||
|
||||
private val resourceId = MutableStateFlow<String?>(null)
|
||||
@@ -41,12 +50,36 @@ class FactsViewModel @Inject constructor(
|
||||
}
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val links: StateFlow<List<ResourceLink>> = resourceId
|
||||
.flatMapLatest { id: String? ->
|
||||
id?.let { linksRepository.observeRelations(it) } ?: flowOf(emptyList())
|
||||
}
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
|
||||
|
||||
private val _syncing = MutableStateFlow(false)
|
||||
val syncing: StateFlow<Boolean> = _syncing.asStateFlow()
|
||||
|
||||
private val _error = MutableStateFlow<String?>(null)
|
||||
val error: StateFlow<String?> = _error.asStateFlow()
|
||||
|
||||
/** Noms résolus des autres bouts (fichier puis dossier, sinon id brut). */
|
||||
private val _linkNames = MutableStateFlow<Map<String, String>>(emptyMap())
|
||||
val linkNames: StateFlow<Map<String, String>> = _linkNames.asStateFlow()
|
||||
|
||||
fun resolveLinkNames(ids: Set<String>) {
|
||||
val missing = ids.filter { it !in _linkNames.value }.toSet()
|
||||
if (missing.isEmpty()) return
|
||||
viewModelScope.launch {
|
||||
val resolved = missing.associateWith { id ->
|
||||
fileRepository.getFile(id)?.name
|
||||
?: folderRepository.getFolder(id)?.name
|
||||
?: id
|
||||
}
|
||||
_linkNames.value = _linkNames.value + resolved
|
||||
}
|
||||
}
|
||||
|
||||
fun watch(id: String) {
|
||||
if (resourceId.value == id) {
|
||||
refresh()
|
||||
@@ -66,6 +99,10 @@ class FactsViewModel @Inject constructor(
|
||||
Timber.d("facts sync failed for %s: %s", id, e.message)
|
||||
_error.value = e.message ?: e.javaClass.simpleName
|
||||
}
|
||||
runCatching { linksRepository.syncFromServer(id) }
|
||||
.onFailure { e ->
|
||||
Timber.d("links sync failed for %s: %s", id, e.message)
|
||||
}
|
||||
_syncing.value = false
|
||||
}
|
||||
}
|
||||
@@ -105,4 +142,20 @@ class FactsViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verdict sur une arête (confirmé → 1.0, infirmé → 0.0 conservé).
|
||||
* REST direct, en ligne uniquement : hors-ligne = erreur affichée,
|
||||
* la fiche garde l'état serveur.
|
||||
*/
|
||||
fun setVerdict(otherId: String, linkType: String, confirmed: Boolean) {
|
||||
val id = resourceId.value ?: return
|
||||
viewModelScope.launch {
|
||||
runCatching { linksRepository.setVerdict(id, otherId, linkType, confirmed) }
|
||||
.onFailure { e ->
|
||||
Timber.w(e, "link verdict failed for %s", id)
|
||||
_error.value = e.message ?: e.javaClass.simpleName
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.kazier.mobile.ui.search.SearchScreen
|
||||
import com.kazier.mobile.ui.settings.SettingsScreen
|
||||
import com.kazier.mobile.ui.share.OpenLinkScreen
|
||||
import com.kazier.mobile.ui.trash.TrashScreen
|
||||
import com.kazier.mobile.ui.duplicates.DuplicatesScreen
|
||||
import com.kazier.mobile.ui.watchedfolders.WatchedFoldersScreen
|
||||
|
||||
object Routes {
|
||||
@@ -40,6 +41,7 @@ object Routes {
|
||||
const val REVIEW = "review"
|
||||
const val WATCHED_FOLDERS = "watched"
|
||||
const val TRASH = "trash"
|
||||
const val DUPLICATES = "duplicates"
|
||||
const val FOLDER_DETAIL = "folder/{folderResourceId}"
|
||||
const val ARG_FOLDER = "folderResourceId"
|
||||
const val DOCUMENT = "document/{documentResourceId}"
|
||||
@@ -149,6 +151,7 @@ fun NavGraph(
|
||||
onTabSelected = onTabSelected,
|
||||
onOpenWatchedFolders = onOpenWatchedFolders,
|
||||
onOpenTrash = { navController.navigate(Routes.TRASH) },
|
||||
onOpenDuplicates = { navController.navigate(Routes.DUPLICATES) },
|
||||
authViewModel = authViewModel,
|
||||
connectionStatusViewModel = connectionStatusViewModel,
|
||||
syncViewModel = syncViewModel,
|
||||
@@ -187,6 +190,11 @@ fun NavGraph(
|
||||
onBack = { navController.popBackStack() },
|
||||
)
|
||||
}
|
||||
composable(Routes.DUPLICATES) {
|
||||
DuplicatesScreen(
|
||||
onBack = { navController.popBackStack() },
|
||||
)
|
||||
}
|
||||
composable(
|
||||
route = Routes.FOLDER_DETAIL,
|
||||
arguments = listOf(
|
||||
|
||||
@@ -329,6 +329,9 @@ class ScanViewModel @Inject constructor(
|
||||
exists = true,
|
||||
// Déjà traité au scan : ne pas remonter dans la review.
|
||||
processed = true,
|
||||
// V3 S5 : pages d'un même export → arêtes
|
||||
// `same_scan` serveur (via le create outbox).
|
||||
scanSessionId = currentSession.resourceId,
|
||||
),
|
||||
folderResourceId = folderId,
|
||||
)
|
||||
@@ -427,6 +430,8 @@ class ScanViewModel @Inject constructor(
|
||||
lastModified = System.currentTimeMillis(),
|
||||
exists = true,
|
||||
processed = true,
|
||||
// V3 S5 : voir export page — même session de scan.
|
||||
scanSessionId = currentSession.resourceId,
|
||||
),
|
||||
folderResourceId = folderId,
|
||||
)
|
||||
|
||||
@@ -22,7 +22,9 @@ import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Clear
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Search
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FilterChip
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -33,6 +35,7 @@ import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
@@ -54,6 +57,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.kazier.mobile.R
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import com.kazier.mobile.data.local.entity.FileEntity
|
||||
import com.kazier.mobile.domain.FileCategory
|
||||
import com.kazier.mobile.features.connection.ConnectionStatusViewModel
|
||||
@@ -94,6 +98,7 @@ fun SearchScreen(
|
||||
) {
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val docTypes by viewModel.docTypes.collectAsStateWithLifecycle()
|
||||
val collections by viewModel.collections.collectAsStateWithLifecycle()
|
||||
val moveFolders by viewModel.moveFolders.collectAsStateWithLifecycle()
|
||||
val moveError by viewModel.moveError.collectAsStateWithLifecycle()
|
||||
val deleteError by viewModel.deleteError.collectAsStateWithLifecycle()
|
||||
@@ -200,6 +205,7 @@ fun SearchScreen(
|
||||
SearchContent(
|
||||
uiState = uiState,
|
||||
docTypes = docTypes,
|
||||
collections = collections,
|
||||
selection = selection,
|
||||
onQueryChange = viewModel::onQueryChange,
|
||||
onCategorySelect = viewModel::onCategorySelect,
|
||||
@@ -210,6 +216,9 @@ fun SearchScreen(
|
||||
onFromDateChange = viewModel::onFromDateChange,
|
||||
onToDateChange = viewModel::onToDateChange,
|
||||
onClearFacets = viewModel::clearFacets,
|
||||
onSaveCollection = viewModel::saveCollection,
|
||||
onApplyCollection = viewModel::applyCollection,
|
||||
onDeleteCollection = viewModel::deleteCollection,
|
||||
onOpenDocument = onOpenDocument,
|
||||
modifier = Modifier.padding(padding),
|
||||
)
|
||||
@@ -278,6 +287,7 @@ fun SearchScreen(
|
||||
private fun SearchContent(
|
||||
uiState: SearchUiState,
|
||||
docTypes: List<String>,
|
||||
collections: List<CollectionEntity>,
|
||||
selection: SelectionState,
|
||||
onQueryChange: (String) -> Unit,
|
||||
onCategorySelect: (String?) -> Unit,
|
||||
@@ -288,11 +298,15 @@ private fun SearchContent(
|
||||
onFromDateChange: (String) -> Unit,
|
||||
onToDateChange: (String) -> Unit,
|
||||
onClearFacets: () -> Unit,
|
||||
onSaveCollection: (String) -> Unit,
|
||||
onApplyCollection: (CollectionEntity) -> Unit,
|
||||
onDeleteCollection: (Long) -> Unit,
|
||||
onOpenDocument: (String) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
var filtersExpanded by rememberSaveable { mutableStateOf(false) }
|
||||
var showSaveDialog by rememberSaveable { mutableStateOf(false) }
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
@@ -364,6 +378,25 @@ private fun SearchContent(
|
||||
onClearFacets = onClearFacets,
|
||||
)
|
||||
|
||||
CollectionsRow(
|
||||
collections = collections,
|
||||
enabled = !selection.active,
|
||||
onApply = onApplyCollection,
|
||||
onDelete = onDeleteCollection,
|
||||
onSave = { showSaveDialog = true },
|
||||
)
|
||||
|
||||
if (showSaveDialog) {
|
||||
SaveCollectionDialog(
|
||||
initialName = uiState.query.trim().takeIf { it.isNotEmpty() }.orEmpty(),
|
||||
onDismiss = { showSaveDialog = false },
|
||||
onConfirm = { name ->
|
||||
showSaveDialog = false
|
||||
onSaveCollection(name)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
SearchResults(
|
||||
uiState = uiState,
|
||||
selection = selection,
|
||||
@@ -398,6 +431,100 @@ private fun CategoryChip(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Collections locales (S5) : chips des recherches sauvegardées + sauvegarde
|
||||
* de la recherche courante. Tap = rejoue (texte + catégorie + facettes),
|
||||
* appui long = supprime (avec confirmation).
|
||||
*/
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
private fun CollectionsRow(
|
||||
collections: List<CollectionEntity>,
|
||||
enabled: Boolean,
|
||||
onApply: (CollectionEntity) -> Unit,
|
||||
onDelete: (Long) -> Unit,
|
||||
onSave: () -> Unit,
|
||||
) {
|
||||
var pendingDelete by rememberSaveable { mutableStateOf<CollectionEntity?>(null) }
|
||||
LazyRow(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
item(key = "save") {
|
||||
FilterChip(
|
||||
selected = false,
|
||||
onClick = onSave,
|
||||
enabled = enabled,
|
||||
label = { Text(stringResource(R.string.collection_save)) },
|
||||
leadingIcon = {
|
||||
Icon(Icons.Filled.Add, contentDescription = null)
|
||||
},
|
||||
)
|
||||
}
|
||||
items(collections, key = { it.id }) { collection ->
|
||||
FilterChip(
|
||||
selected = false,
|
||||
onClick = { onApply(collection) },
|
||||
enabled = enabled,
|
||||
label = { Text(collection.name) },
|
||||
modifier = Modifier.combinedClickable(
|
||||
onClick = { onApply(collection) },
|
||||
onLongClick = { pendingDelete = collection },
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
val toDelete = pendingDelete
|
||||
if (toDelete != null) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { pendingDelete = null },
|
||||
title = { Text(stringResource(R.string.collection_delete_title, toDelete.name)) },
|
||||
text = { Text(stringResource(R.string.collection_delete_message)) },
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
pendingDelete = null
|
||||
onDelete(toDelete.id)
|
||||
}) { Text(stringResource(R.string.collection_delete_confirm)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { pendingDelete = null }) {
|
||||
Text(stringResource(R.string.delete_cancel))
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SaveCollectionDialog(
|
||||
initialName: String,
|
||||
onDismiss: () -> Unit,
|
||||
onConfirm: (String) -> Unit,
|
||||
) {
|
||||
var name by rememberSaveable(initialName) { mutableStateOf(initialName) }
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text(stringResource(R.string.collection_save_title)) },
|
||||
text = {
|
||||
OutlinedTextField(
|
||||
value = name,
|
||||
onValueChange = { name = it },
|
||||
label = { Text(stringResource(R.string.collection_name)) },
|
||||
singleLine = true,
|
||||
)
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = { onConfirm(name) },
|
||||
enabled = name.trim().isNotEmpty(),
|
||||
) { Text(stringResource(R.string.collection_save_confirm)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) { Text(stringResource(R.string.delete_cancel)) }
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Facettes V3 (S4) : émetteur, type suggéré (chips dynamiques), montants,
|
||||
* période ISO. Repliées par défaut (bouton avec compteur) ; saisies
|
||||
|
||||
@@ -6,6 +6,9 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.kazier.mobile.R
|
||||
import com.kazier.mobile.data.local.entity.FileEntity
|
||||
import com.kazier.mobile.data.local.entity.FolderEntity
|
||||
import com.kazier.mobile.data.local.entity.CollectionEntity
|
||||
import com.kazier.mobile.data.repository.CollectionsRepository
|
||||
import com.kazier.mobile.data.repository.AppliedCollection
|
||||
import com.kazier.mobile.data.repository.FileRepository
|
||||
import com.kazier.mobile.data.repository.FolderRepository
|
||||
import com.kazier.mobile.data.repository.SearchFacets
|
||||
@@ -32,6 +35,7 @@ import javax.inject.Inject
|
||||
class SearchViewModel @Inject constructor(
|
||||
private val fileRepository: FileRepository,
|
||||
private val folderRepository: FolderRepository,
|
||||
private val collectionsRepository: CollectionsRepository,
|
||||
private val fileMover: FileMover,
|
||||
private val fileDeleter: FileDeleter,
|
||||
@ApplicationContext private val context: Context,
|
||||
@@ -223,6 +227,53 @@ class SearchViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/** Collections locales (requêtes sauvegardées, jamais syncées). */
|
||||
val collections: StateFlow<List<CollectionEntity>> =
|
||||
collectionsRepository.observeAll()
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(STOP_TIMEOUT_MS), emptyList())
|
||||
|
||||
/** Sauvegarde la recherche courante (texte + catégorie + facettes). */
|
||||
fun saveCollection(name: String) {
|
||||
viewModelScope.launch {
|
||||
runCatching {
|
||||
collectionsRepository.save(
|
||||
name = name,
|
||||
query = _searchQuery.value,
|
||||
category = _selectedCategory.value,
|
||||
facets = SearchFacets(
|
||||
issuer = _issuer.value.trim().takeIf { it.isNotEmpty() },
|
||||
docType = _docType.value,
|
||||
minAmount = _minAmount.value.toDoubleOrNull(),
|
||||
maxAmount = _maxAmount.value.toDoubleOrNull(),
|
||||
fromDate = _fromDate.value.trim().takeIf { it.matches(DATE_RE) },
|
||||
toDate = _toDate.value.trim().takeIf { it.matches(DATE_RE) },
|
||||
),
|
||||
)
|
||||
}.onFailure {
|
||||
_deleteError.value = context.getString(R.string.collection_save_error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteCollection(id: Long) {
|
||||
viewModelScope.launch {
|
||||
runCatching { collectionsRepository.delete(id) }
|
||||
}
|
||||
}
|
||||
|
||||
/** Rejoue une collection (texte + catégorie + facettes). */
|
||||
fun applyCollection(collection: CollectionEntity) {
|
||||
val applied: AppliedCollection = collectionsRepository.apply(collection)
|
||||
_searchQuery.value = applied.query
|
||||
_selectedCategory.value = applied.category
|
||||
_issuer.value = applied.facets.issuer.orEmpty()
|
||||
_docType.value = applied.facets.docType
|
||||
_minAmount.value = applied.facets.minAmount?.toString().orEmpty()
|
||||
_maxAmount.value = applied.facets.maxAmount?.toString().orEmpty()
|
||||
_fromDate.value = applied.facets.fromDate.orEmpty()
|
||||
_toDate.value = applied.facets.toDate.orEmpty()
|
||||
}
|
||||
|
||||
fun clearDeleteError() {
|
||||
_deleteError.value = null
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.CloudOff
|
||||
import androidx.compose.material.icons.filled.ContentCopy
|
||||
import androidx.compose.material.icons.filled.DarkMode
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.Dns
|
||||
@@ -71,6 +72,7 @@ fun SettingsScreen(
|
||||
onTabSelected: (NavTab) -> Unit,
|
||||
onOpenWatchedFolders: () -> Unit,
|
||||
onOpenTrash: () -> Unit,
|
||||
onOpenDuplicates: () -> Unit,
|
||||
authViewModel: AuthViewModel,
|
||||
connectionStatusViewModel: ConnectionStatusViewModel,
|
||||
syncViewModel: SyncViewModel,
|
||||
@@ -163,6 +165,14 @@ fun SettingsScreen(
|
||||
onClick = onOpenTrash,
|
||||
)
|
||||
|
||||
HorizontalDivider()
|
||||
SettingsRow(
|
||||
icon = { Icon(Icons.Filled.ContentCopy, contentDescription = null) },
|
||||
title = stringResource(R.string.duplicates_title),
|
||||
subtitle = stringResource(R.string.duplicates_hint),
|
||||
onClick = onOpenDuplicates,
|
||||
)
|
||||
|
||||
HorizontalDivider()
|
||||
SettingsRow(
|
||||
icon = { Icon(Icons.Filled.Dns, contentDescription = null) },
|
||||
|
||||
@@ -418,6 +418,22 @@
|
||||
<string name="facts_type_reference">Reference</string>
|
||||
<string name="facts_type_doc_type">Document type</string>
|
||||
<string name="facts_type_other">Info</string>
|
||||
<!-- Graph V3 S5 -->
|
||||
<string name="links_title">See also</string>
|
||||
<string name="links_dismiss">This link is wrong</string>
|
||||
<string name="links_reconfirm">Re-confirm this link</string>
|
||||
<string name="links_type_same_scan">Same scan</string>
|
||||
<string name="links_type_duplicate">Duplicate</string>
|
||||
<string name="links_type_same_issuer">Same issuer</string>
|
||||
<string name="links_type_same_reference">Same reference</string>
|
||||
<string name="links_type_other">Linked</string>
|
||||
<!-- Duplicates V3 S5 B2 -->
|
||||
<string name="duplicates_title">Duplicates</string>
|
||||
<string name="duplicates_hint">Files with identical bytes — keep just one.</string>
|
||||
<string name="duplicates_empty">No duplicates detected</string>
|
||||
<string name="duplicates_keep_newest">Keep newest</string>
|
||||
<string name="duplicates_reclaimable">%1$s reclaimable</string>
|
||||
<string name="duplicates_group">%1$d identical files</string>
|
||||
<!-- Search facets V3 S4 -->
|
||||
<string name="search_filters">Filters</string>
|
||||
<string name="search_filters_count">Filters (%1$d)</string>
|
||||
@@ -428,4 +444,13 @@
|
||||
<string name="search_facet_max_amount">Max amount (€)</string>
|
||||
<string name="search_facet_from">From (YYYY-MM-DD)</string>
|
||||
<string name="search_facet_to">To (YYYY-MM-DD)</string>
|
||||
<!-- Collections V3 S5 -->
|
||||
<string name="collection_save">Save search</string>
|
||||
<string name="collection_save_title">Save as collection</string>
|
||||
<string name="collection_name">Name</string>
|
||||
<string name="collection_save_confirm">Save</string>
|
||||
<string name="collection_save_error">Cannot save</string>
|
||||
<string name="collection_delete_title">Delete “%1$s”?</string>
|
||||
<string name="collection_delete_message">The collection disappears from this device.</string>
|
||||
<string name="collection_delete_confirm">Delete</string>
|
||||
</resources>
|
||||
@@ -419,6 +419,22 @@
|
||||
<string name="facts_type_reference">Référence</string>
|
||||
<string name="facts_type_doc_type">Type de document</string>
|
||||
<string name="facts_type_other">Info</string>
|
||||
<!-- Graphe V3 S5 -->
|
||||
<string name="links_title">Voir aussi</string>
|
||||
<string name="links_dismiss">Ce lien est faux</string>
|
||||
<string name="links_reconfirm">Re-confirmer ce lien</string>
|
||||
<string name="links_type_same_scan">Même scan</string>
|
||||
<string name="links_type_duplicate">Doublon</string>
|
||||
<string name="links_type_same_issuer">Même émetteur</string>
|
||||
<string name="links_type_same_reference">Même référence</string>
|
||||
<string name="links_type_other">Lié</string>
|
||||
<!-- Doublons V3 S5 B2 -->
|
||||
<string name="duplicates_title">Doublons</string>
|
||||
<string name="duplicates_hint">Fichiers aux octets identiques — gardez-en un seul.</string>
|
||||
<string name="duplicates_empty">Aucun doublon détecté</string>
|
||||
<string name="duplicates_keep_newest">Garder le plus récent</string>
|
||||
<string name="duplicates_reclaimable">%1$s récupérables</string>
|
||||
<string name="duplicates_group">%1$d fichiers identiques</string>
|
||||
<!-- Recherche facettes V3 S4 -->
|
||||
<string name="search_filters">Filtres</string>
|
||||
<string name="search_filters_count">Filtres (%1$d)</string>
|
||||
@@ -429,4 +445,13 @@
|
||||
<string name="search_facet_max_amount">Montant max. (€)</string>
|
||||
<string name="search_facet_from">Du (AAAA-MM-JJ)</string>
|
||||
<string name="search_facet_to">Au (AAAA-MM-JJ)</string>
|
||||
<!-- Collections V3 S5 -->
|
||||
<string name="collection_save">Sauver la recherche</string>
|
||||
<string name="collection_save_title">Sauver comme collection</string>
|
||||
<string name="collection_name">Nom</string>
|
||||
<string name="collection_save_confirm">Sauver</string>
|
||||
<string name="collection_save_error">Sauvegarde impossible</string>
|
||||
<string name="collection_delete_title">Supprimer « %1$s » ?</string>
|
||||
<string name="collection_delete_message">La collection disparaît de cet appareil.</string>
|
||||
<string name="collection_delete_confirm">Supprimer</string>
|
||||
</resources>
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.kazier.mobile.data.local.migration
|
||||
|
||||
import android.content.Context
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import androidx.sqlite.db.SupportSQLiteOpenHelper
|
||||
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
/**
|
||||
* V3 relations + collections (S5) : la migration 21→22 crée `resource_links`
|
||||
* (miroir `GET /relations`) + `collections` (requêtes sauvegardées locales).
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class Migration21To22Test {
|
||||
|
||||
@Test
|
||||
fun migration_cree_links_et_collections() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val helper = FrameworkSQLiteOpenHelperFactory().create(
|
||||
SupportSQLiteOpenHelper.Configuration.builder(context)
|
||||
.name(null)
|
||||
.callback(V21Callback())
|
||||
.build(),
|
||||
)
|
||||
val db = helper.writableDatabase
|
||||
|
||||
val migration = Migrations.ALL.first { it.startVersion == 21 && it.endVersion == 22 }
|
||||
migration.migrate(db)
|
||||
|
||||
val tables = mutableSetOf<String>()
|
||||
db.query("SELECT `name` FROM `sqlite_master` WHERE `type` = 'table'").use { cursor ->
|
||||
while (cursor.moveToNext()) tables += cursor.getString(0)
|
||||
}
|
||||
assertTrue("resource_links manquante", tables.contains("resource_links"))
|
||||
assertTrue("collections manquante", tables.contains("collections"))
|
||||
|
||||
val indexes = mutableSetOf<String>()
|
||||
db.query("SELECT `name` FROM `sqlite_master` WHERE `type` = 'index'").use { cursor ->
|
||||
while (cursor.moveToNext()) indexes += cursor.getString(0)
|
||||
}
|
||||
assertTrue(indexes.contains("index_resource_links_from_id_to_id_link_type"))
|
||||
assertTrue(indexes.contains("index_resource_links_to_id"))
|
||||
assertTrue(indexes.contains("index_collections_name"))
|
||||
|
||||
// Idempotence : re-passe sans erreur.
|
||||
migration.migrate(db)
|
||||
helper.close()
|
||||
}
|
||||
|
||||
/** Schéma v21 minimal (sans links ni collections). */
|
||||
private class V21Callback : SupportSQLiteOpenHelper.Callback(21) {
|
||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"CREATE TABLE `files` (" +
|
||||
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
|
||||
"`resource_id` TEXT NOT NULL, " +
|
||||
"`name` TEXT NOT NULL, " +
|
||||
"`folder_resource_id` TEXT NOT NULL, " +
|
||||
"`size` INTEGER NOT NULL, " +
|
||||
"`added_at` INTEGER NOT NULL, " +
|
||||
"`updated_at` INTEGER NOT NULL)",
|
||||
)
|
||||
}
|
||||
|
||||
override fun onUpgrade(
|
||||
db: SupportSQLiteDatabase,
|
||||
oldVersion: Int,
|
||||
newVersion: Int,
|
||||
) = Unit
|
||||
}
|
||||
}
|
||||
@@ -343,6 +343,20 @@ class Auth401Test {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
@@ -217,6 +217,20 @@ class PaginationTest {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
@@ -150,6 +150,15 @@ class RemoteContentTest {
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
|
||||
@@ -356,6 +356,15 @@ class SessionReloginTest {
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
|
||||
@@ -189,6 +189,15 @@ class ShareManagementTest {
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
|
||||
@@ -283,6 +283,15 @@ class BytesUploadTest {
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import com.kazier.mobile.data.local.AppDatabase
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
/**
|
||||
* Collections locales V3 (S5) : sauvegarde, relecture, application,
|
||||
* suppression. Pur local, jamais synchronisé.
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class CollectionsRepositoryTest {
|
||||
|
||||
private lateinit var db: AppDatabase
|
||||
private lateinit var collectionsRepository: CollectionsRepository
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||
.allowMainThreadQueries()
|
||||
.build()
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
collectionsRepository = CollectionsRepository(db.collectionDao(), moshi)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sauver_puis_rejouer() = runTest {
|
||||
val id = collectionsRepository.save(
|
||||
name = "Impôts 2026",
|
||||
query = "impot",
|
||||
category = null,
|
||||
facets = SearchFacets(docType = "impot", minAmount = 10.0, fromDate = "2026-01-01"),
|
||||
)
|
||||
|
||||
val all = collectionsRepository.observeAll().first()
|
||||
assertEquals(1, all.size)
|
||||
assertEquals("Impôts 2026", all[0].name)
|
||||
|
||||
val stored = collectionsRepository.get(id)!!
|
||||
val applied = collectionsRepository.apply(stored)
|
||||
assertEquals("impot", applied.query)
|
||||
assertEquals(null, applied.category)
|
||||
assertEquals("impot", applied.facets.docType)
|
||||
assertEquals(10.0, applied.facets.minAmount!!, 0.0)
|
||||
assertEquals("2026-01-01", applied.facets.fromDate)
|
||||
assertEquals(null, applied.facets.issuer)
|
||||
|
||||
collectionsRepository.delete(id)
|
||||
assertTrue(collectionsRepository.observeAll().first().isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nom_vide_refuse() = runTest {
|
||||
var failed = false
|
||||
try {
|
||||
collectionsRepository.save(" ", "x", null, SearchFacets())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
failed = true
|
||||
}
|
||||
assertTrue("nom vide doit être refusé", failed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun json_corrompu_rejoue_vide() = runTest {
|
||||
db.collectionDao().insert(
|
||||
com.kazier.mobile.data.local.entity.CollectionEntity(
|
||||
name = "Cassée", query = "x", facetsJson = "{pas du json", createdAt = 1,
|
||||
),
|
||||
)
|
||||
val stored = collectionsRepository.observeAll().first().first()
|
||||
val applied = collectionsRepository.apply(stored)
|
||||
assertEquals("x", applied.query)
|
||||
assertTrue(applied.facets.isEmpty())
|
||||
}
|
||||
}
|
||||
+3
@@ -192,6 +192,9 @@ class FactsRepositoryTest {
|
||||
override suspend fun listVersions(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.VersionDto>>> = TODO()
|
||||
override suspend fun restoreVersion(fileId: String, versionNo: Int): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.FileDto>> = TODO()
|
||||
override suspend fun correctFacts(fileId: String, body: CorrectFactsDto): Response<ApiEnvelope<List<FactDto>>> = TODO()
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> = TODO()
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> = TODO()
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> = TODO()
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FileDto>>> = TODO()
|
||||
override suspend fun changePassword(body: com.kazier.mobile.data.remote.dto.ChangePasswordDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun getShareLink(token: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.ShareLinkDto>> = TODO()
|
||||
|
||||
+14
@@ -403,6 +403,20 @@ private class FolderMergeApiService : ApiService {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
package com.kazier.mobile.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import com.kazier.mobile.data.local.AppDatabase
|
||||
import com.kazier.mobile.data.local.dao.LinksDao
|
||||
import com.kazier.mobile.data.local.entity.LinkOrigin
|
||||
import com.kazier.mobile.data.local.entity.LinkType
|
||||
import com.kazier.mobile.data.remote.ApiClient
|
||||
import com.kazier.mobile.data.remote.ApiService
|
||||
import com.kazier.mobile.data.remote.dto.ApiEnvelope
|
||||
import com.kazier.mobile.data.remote.dto.RelationDto
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import retrofit2.Response
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
|
||||
/**
|
||||
* Graphe V3 (S5) côté client : sync remplace-en-bloc, verdict optimiste +
|
||||
* REST direct (pas d'outbox — acte rare, en ligne uniquement).
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class LinksRepositoryTest {
|
||||
|
||||
private lateinit var db: AppDatabase
|
||||
private lateinit var linksDao: LinksDao
|
||||
private lateinit var linksRepository: LinksRepository
|
||||
private lateinit var fakeApi: LinksFakeApiService
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||
.allowMainThreadQueries()
|
||||
.build()
|
||||
linksDao = db.linksDao()
|
||||
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
fakeApi = LinksFakeApiService()
|
||||
val apiClient = ApiClient(fakeApi, moshi)
|
||||
linksRepository = LinksRepository(db, linksDao, apiClient)
|
||||
assertTrue(Retrofit.Builder().baseUrl("http://localhost:1/").build().baseUrl().toString().startsWith("http://localhost"))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sync_remplace_en_bloc() = runTest {
|
||||
fakeApi.relations = listOf(
|
||||
RelationDto("1", "b", LinkType.SAME_SCAN, 1.0, "auto", "p2.png", "file", 10),
|
||||
)
|
||||
linksRepository.syncFromServer("a")
|
||||
|
||||
val rows = linksDao.getForResource("a")
|
||||
assertEquals(1, rows.size)
|
||||
assertEquals("b", rows[0].otherEnd("a"))
|
||||
|
||||
// Re-passe avec un jeu différent : remplacement, pas d'accumulation.
|
||||
fakeApi.relations = listOf(
|
||||
RelationDto("2", "c", LinkType.SAME_ISSUER, 0.7, "auto", "f.pdf", "file", 5),
|
||||
)
|
||||
linksRepository.syncFromServer("a")
|
||||
val rows2 = linksRepository.observeRelations("a").first()
|
||||
assertEquals(1, rows2.size)
|
||||
assertEquals("c", rows2[0].otherEnd("a"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verdict_optimiste_puis_convergence() = runTest {
|
||||
fakeApi.relations = listOf(
|
||||
RelationDto("1", "b", LinkType.SAME_ISSUER, 0.7, "auto", "f.pdf", "file", 5),
|
||||
)
|
||||
linksRepository.syncFromServer("a")
|
||||
|
||||
// Le serveur renvoie le jeu réconcilié après verdict.
|
||||
fakeApi.relations = listOf(
|
||||
RelationDto("1", "b", LinkType.SAME_ISSUER, 0.0, "user", "f.pdf", "file", 5),
|
||||
)
|
||||
linksRepository.setVerdict("a", "b", LinkType.SAME_ISSUER, confirmed = false)
|
||||
|
||||
assertEquals("b", fakeApi.lastVerdict?.otherId)
|
||||
assertEquals(LinkType.SAME_ISSUER, fakeApi.lastVerdict?.linkType)
|
||||
assertEquals(false, fakeApi.lastVerdict?.confirmed)
|
||||
|
||||
val rows = linksDao.getForResource("a")
|
||||
assertEquals(1, rows.size)
|
||||
assertEquals(0.0, rows[0].confidence, 0.0)
|
||||
assertEquals(LinkOrigin.USER, rows[0].createdBy)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verdict_hors_ligne_remonte_une_erreur() = runTest {
|
||||
fakeApi.failVerdict = true
|
||||
var failed = false
|
||||
try {
|
||||
linksRepository.setVerdict("a", "b", LinkType.SAME_SCAN, confirmed = false)
|
||||
} catch (e: Exception) {
|
||||
failed = true
|
||||
}
|
||||
assertTrue("verdict hors-ligne doit lever", failed)
|
||||
// L'optimiste local reste (réessai possible au retour réseau).
|
||||
assertEquals(1, linksDao.getForResource("a").size)
|
||||
}
|
||||
|
||||
data class VerdictCall(val otherId: String, val linkType: String, val confirmed: Boolean)
|
||||
|
||||
class LinksFakeApiService : ApiService {
|
||||
var relations: List<RelationDto> = emptyList()
|
||||
var lastVerdict: VerdictCall? = null
|
||||
var failVerdict: Boolean = false
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = relations))
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<RelationDto>>> {
|
||||
if (failVerdict) throw com.kazier.mobile.data.remote.ApiException("NETWORK_ERROR", "offline", 0)
|
||||
lastVerdict = VerdictCall(body.otherId, body.linkType, body.confirmed)
|
||||
return Response.success(ApiEnvelope(data = relations))
|
||||
}
|
||||
|
||||
override suspend fun listFolders(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FolderDto>>> = TODO()
|
||||
override suspend fun listFiles(folderId: String?, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FileDto>>> = TODO()
|
||||
override suspend fun registerDevice(body: com.kazier.mobile.data.remote.dto.DeviceRegistrationDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.DeviceRegistrationDto>> = TODO()
|
||||
override suspend fun login(body: com.kazier.mobile.data.remote.dto.LoginRequestDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.LoginResponseDto>> = TODO()
|
||||
override suspend fun syncOps(body: com.kazier.mobile.data.remote.dto.SyncOpsRequest): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.SyncOpsResult>> = TODO()
|
||||
override suspend fun resolveUser(username: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.ResolvedUserDto>> = TODO()
|
||||
override suspend fun syncPermissions(after: Long?): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.ResourcePermissionDto>>> = TODO()
|
||||
override suspend fun uploadFile(file: okhttp3.MultipartBody.Part, resourceId: okhttp3.RequestBody?, folderId: okhttp3.RequestBody?): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.FileDto>> = TODO()
|
||||
override suspend fun submitOcr(body: com.kazier.mobile.data.remote.dto.OcrSubmitDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.OcrJobDto>> = TODO()
|
||||
override suspend fun getOcrJob(jobId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.OcrJobDto>> = TODO()
|
||||
override suspend fun getFileOcr(fileId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.FileOcrDto>> = TODO()
|
||||
override suspend fun getFile(fileId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.FileDto>> = TODO()
|
||||
override suspend fun downloadContent(fileId: String): Response<okhttp3.ResponseBody> = TODO()
|
||||
override suspend fun deleteFile(fileId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun listTrash(page: Int?, pageSize: Int?): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.TrashDto>>> = TODO()
|
||||
override suspend fun restoreFile(fileId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun listVersions(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.VersionDto>>> = TODO()
|
||||
override suspend fun restoreVersion(fileId: String, versionNo: Int): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.FileDto>> = TODO()
|
||||
override suspend fun listFacts(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> = TODO()
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> = TODO()
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> = TODO()
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FileDto>>> = TODO()
|
||||
override suspend fun changePassword(body: com.kazier.mobile.data.remote.dto.ChangePasswordDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun getShareLink(token: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.ShareLinkDto>> = TODO()
|
||||
override suspend fun listShares(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.ShareGrantDto>>> = TODO()
|
||||
override suspend fun listLinks(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.ShareLinkDto>>> = TODO()
|
||||
override suspend fun listPlacements(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.PlacementDto>>> = TODO()
|
||||
override suspend fun createGroup(body: com.kazier.mobile.data.remote.dto.CreateGroupDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.GroupDto>> = TODO()
|
||||
override suspend fun listGroups(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.GroupDto>>> = TODO()
|
||||
override suspend fun addGroupMember(groupId: String, body: com.kazier.mobile.data.remote.dto.AddGroupMemberDto): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun listGroupMembers(groupId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.GroupMemberDto>>> = TODO()
|
||||
override suspend fun removeGroupMember(groupId: String, userId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun deleteGroup(groupId: String): Response<ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
}
|
||||
}
|
||||
+14
@@ -492,6 +492,20 @@ private class MoveProtectionApiService : ApiService {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
+14
@@ -404,6 +404,20 @@ private class OcrApiServiceFake : ApiService {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
+9
@@ -291,6 +291,15 @@ class OfflineDownloadTest {
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): Response<ApiEnvelope<List<FileDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
|
||||
+14
@@ -259,6 +259,20 @@ private class RefreshVsMoveApiService : ApiService {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
+14
@@ -513,6 +513,20 @@ private class FakeApiService : ApiService {
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listRelations(
|
||||
fileId: String,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun verdictRelation(
|
||||
fileId: String,
|
||||
body: com.kazier.mobile.data.remote.dto.RelationVerdictDto,
|
||||
): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
Response.success(ApiEnvelope(data = emptyList()))
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
@@ -161,6 +161,9 @@ class ReleaseLocalCopyTest {
|
||||
override suspend fun restoreVersion(fileId: String, versionNo: Int): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<com.kazier.mobile.data.remote.dto.FileDto>> = TODO()
|
||||
override suspend fun listFacts(fileId: String): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> = TODO()
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> = TODO()
|
||||
override suspend fun listRelations(fileId: String): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> = TODO()
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> = TODO()
|
||||
override suspend fun listDuplicates(): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> = TODO()
|
||||
override suspend fun searchFiles(query: String, page: Int?, pageSize: Int?): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FileDto>>> = TODO()
|
||||
override suspend fun changePassword(body: com.kazier.mobile.data.remote.dto.ChangePasswordDto): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<com.kazier.mobile.data.remote.dto.IdResultDto>> = TODO()
|
||||
override suspend fun getShareLink(token: String): retrofit2.Response<com.kazier.mobile.data.remote.dto.ApiEnvelope<com.kazier.mobile.data.remote.dto.ShareLinkDto>> = TODO()
|
||||
|
||||
+9
@@ -513,6 +513,15 @@ class OutboxSyncWorkerTest {
|
||||
override suspend fun correctFacts(fileId: String, body: com.kazier.mobile.data.remote.dto.CorrectFactsDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.FactDto>>> =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override suspend fun listRelations(fileId: String): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override suspend fun verdictRelation(fileId: String, body: com.kazier.mobile.data.remote.dto.RelationVerdictDto): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.RelationDto>>> =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override suspend fun listDuplicates(): Response<ApiEnvelope<List<com.kazier.mobile.data.remote.dto.DuplicateGroupDto>>> =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override suspend fun searchFiles(
|
||||
query: String,
|
||||
page: Int?,
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.kazier.mobile.data.remote.ApiClient
|
||||
import com.kazier.mobile.data.remote.ApiService
|
||||
import com.kazier.mobile.data.repository.FileRepository
|
||||
import com.kazier.mobile.data.repository.FolderRepository
|
||||
import com.kazier.mobile.data.repository.CollectionsRepository
|
||||
import com.kazier.mobile.data.repository.OutboxRepository
|
||||
import com.kazier.mobile.data.repository.SaveFileInput
|
||||
import com.kazier.mobile.domain.ActiveUserStore
|
||||
@@ -207,6 +208,7 @@ class SearchFieldTest {
|
||||
return SearchViewModel(
|
||||
fileRepo,
|
||||
folderRepository,
|
||||
CollectionsRepository(db.collectionDao(), moshi),
|
||||
FileMover(context, fileRepo, folderRepository),
|
||||
FileDeleter(context, fileRepo, outboxRepository, db, ThumbnailStore(context)),
|
||||
context,
|
||||
|
||||
Reference in New Issue
Block a user