V3 S6 : confiance (révocation, quota, appareils, conflits) + import + onboarding

Backend : token_version (000022, claim tv, 401 si révoqué, bump au
password — fin limite V1), quota (000023 idempotente, colonne héritée
de 000001, 413 QUOTA_EXCEEDED, GET /users/me/usage), devices
(GET + DELETE révocation, EnforceDeviceExists en prod), renames
concurrents transparents (baseUpdatedAt + conflicts[] non-fataux,
tolérance 2 s). Tests service + handlers + migrations + auth.

Mobile : pipeline conflits (DTO + SyncConflictStore + worker + dialogue),
baseUpdatedAt prêt, liens 7j par défaut, écran Appareils (+ quota),
import classeur unifié (scan/note/dossier), onboarding cercle Famille.

Contrat docs/api-v1.md, V3.md (S6 livré).
This commit is contained in:
venture
2026-09-26 11:56:39 +02:00
parent 37dc36f1f2
commit 1e38e6e4ee
65 changed files with 1880 additions and 53 deletions
+65
View File
@@ -18,6 +18,10 @@ import (
// ErrFileTooLarge signals an upload above MaxFileSize.
var ErrFileTooLarge = errors.New("file too large")
// ErrQuotaExceeded signals an upload that would overflow the account quota
// (V3 S6 A2). Handlers map it to 413 QUOTA_EXCEEDED (never 500).
var ErrQuotaExceeded = errors.New("storage quota exceeded")
// ErrInvalidUpload signals a rejected upload (display name or path component).
// Handlers map it to 400 INVALID_REQUEST (never 500).
var ErrInvalidUpload = errors.New("invalid upload name")
@@ -343,6 +347,14 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
if ferr != nil {
return FileDTO{}, ferr
}
} else {
// V3 S6 A2 : garde-fou quota sur les créations (les overwrites
// ciblées remplacent des octets existants — gain net ~nul, pas de
// check). Vérifié au header (rejet rapide) PUIS aux octets réels
// (vérité, le header peut mentir, cf. A2).
if err := s.checkQuotaFor(ownerID, file.Size); err != nil {
return FileDTO{}, err
}
}
written, err := copyMultipartAtomic(file, destDir, destPath, s.MaxFileSize)
@@ -352,6 +364,14 @@ func (s *Resources) Upload(ownerID string, file *multipart.FileHeader, folderID,
}
return FileDTO{}, err
}
if !targetExisting {
// Vérité aux octets réels (le header `file.Size` peut mentir) :
// dépassement → fichier supprimé + 413 (pas d'orphelin).
if err := s.checkQuotaFor(ownerID, written); err != nil {
_ = os.Remove(destPath)
return FileDTO{}, err
}
}
mimeType := sanitizeMimeType(file.Header.Get("Content-Type"))
var extPtr *string
@@ -486,3 +506,48 @@ func toFileDTO(row repository.FileRow) FileDTO {
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
}
}
// UsageDTO serializes GET /users/me/usage (V3 S6 A2) : quota + empreinte,
// dont la corbeille (octets récupérables à la purge).
type UsageDTO struct {
UsedBytes int64 `json:"usedBytes"`
QuotaBytes int64 `json:"quotaBytes"`
FileCount int `json:"fileCount"`
TrashCount int `json:"trashCount"`
TrashedBytes int64 `json:"trashedBytes"`
}
// Usage returns the account footprint (computed, never stored).
func (s *Resources) Usage(ownerID string) (UsageDTO, error) {
user, err := s.Repository.Users.GetByID(ownerID)
if err != nil {
return UsageDTO{}, err
}
usage, err := s.Repo.StorageUsage(ownerID)
if err != nil {
return UsageDTO{}, err
}
return UsageDTO{
UsedBytes: usage.UsedBytes, QuotaBytes: user.QuotaBytes,
FileCount: usage.FileCount, TrashCount: usage.TrashCount,
TrashedBytes: usage.TrashedBytes,
}, nil
}
// checkQuotaFor rejects a creation of incoming bytes that would overflow the
// account quota (V3 S6 A2). Compte absent → l'erreur remonte (jamais de
// défaut silencieux qui laisserait passer).
func (s *Resources) checkQuotaFor(ownerID string, incoming int64) error {
user, err := s.Repository.Users.GetByID(ownerID)
if err != nil {
return err
}
usage, err := s.Repo.StorageUsage(ownerID)
if err != nil {
return err
}
if usage.UsedBytes+incoming > user.QuotaBytes {
return ErrQuotaExceeded
}
return nil
}
+80 -13
View File
@@ -53,10 +53,22 @@ type FailedOperation struct {
Message string `json:"message"`
}
// SyncResult is the outbox response: applied = index of the next op to send.
// ConflictInfo reports a last-writer-wins rename applied over a concurrent
// change (V3 S6 A4). Non-fatal : le batch continue, le client affiche
// « renommé ailleurs » avec le nom serveur gagnant.
type ConflictInfo struct {
OperationID string `json:"operation_id"`
ResourceID string `json:"resource_id"`
ServerName string `json:"serverName"`
ServerUpdatedAt int64 `json:"serverUpdatedAt"`
}
// SyncResult is the outbox response: applied = index of the next op to send,
// conflicts = renames concurrents appliqués quand même (non-fataux, S6 A4).
type SyncResult struct {
Applied int `json:"applied"`
Failed *FailedOperation `json:"failed,omitempty"`
Applied int `json:"applied"`
Failed *FailedOperation `json:"failed,omitempty"`
Conflicts []ConflictInfo `json:"conflicts,omitempty"`
}
type createResourcePayload struct {
@@ -71,6 +83,11 @@ type createResourcePayload struct {
type renamePayload struct {
Name string `json:"name"`
// V3 S6 A4 : `updated_at` (ms epoch) vu par le client au moment du
// rename. Si la ressource a bougé depuis (concurrent), le rename
// s'applique quand même (last-writer-wins) mais est signalé en
// `conflicts` (transparence, pas de CRDT).
BaseUpdatedAt *int64 `json:"baseUpdatedAt"`
}
type movePayload struct {
@@ -156,33 +173,39 @@ var errCycle = errors.New("cannot move a resource into its own subtree")
// explicite avec code INTERNAL, jamais en erreur Go nue (500 nu) — le client
// sait exactement où reprendre.
func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (SyncResult, error) {
// Conflits de rename (S6 A4) : non-fataux, collectés tout au long du
// batch et renvoyés avec le résultat (le batch continue).
var conflicts []ConflictInfo
fail := func(i int, op *SyncOperation, code, message string) (SyncResult, error) {
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}, Conflicts: conflicts}, nil
}
for i := range ops {
op := &ops[i]
if err := validateSyncOp(op); err != nil {
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: err.Error()}}, nil
return fail(i, op, "INVALID_REQUEST", err.Error())
}
claimed, err := s.Repository.Operations.Claim(deviceID, op.OperationID, op.Operation, derefString(op.RefType), nullableInt64(derefInt64(op.RefID)), derefString(op.ResourceID), op.Payload)
if err != nil {
// Faute client (clé ou payload malformé) → INVALID_REQUEST halt,
// message statique sûr ; panne persistance → INTERNAL explicite.
if errors.Is(err, repository.ErrInvalidOperationID) {
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation_id must be 32 lowercase hex chars"}}, nil
return fail(i, op, "INVALID_REQUEST", "operation_id must be 32 lowercase hex chars")
}
if errors.Is(err, repository.ErrInvalidPayload) {
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "invalid payload JSON"}}, nil
return fail(i, op, "INVALID_REQUEST", "invalid payload JSON")
}
log.Printf("sync: claim device=%s op=%s: %v", deviceID, op.OperationID, err)
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to persist operation state"}}, nil
return fail(i, op, "INTERNAL", "failed to persist operation state")
}
if !claimed {
// Rejeu : même (device, op) déjà tracé — comparer (A9).
stored, err := s.Repository.Operations.Get(deviceID, op.OperationID)
if err != nil {
log.Printf("sync: replay read device=%s op=%s: %v", deviceID, op.OperationID, err)
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "failed to read operation state"}}, nil
return fail(i, op, "INTERNAL", "failed to read operation state")
}
if err := stored.MatchesReplay(op.Operation, derefString(op.ResourceID), op.Payload); err != nil {
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INVALID_REQUEST", Message: "operation replayed with different payload"}}, nil
return fail(i, op, "INVALID_REQUEST", "operation replayed with different payload")
}
if stored.Status == repository.OpStatusApplied {
continue // déjà appliquée à l'identique → skip idempotent
@@ -190,16 +213,18 @@ func (s *Resources) ApplyBatch(userID, deviceID string, ops []SyncOperation) (Sy
// Trace 'failed' à payload identique : la première tentative est
// morte avant MarkApplied (crash) → on ré-applique (recovery).
}
if err := s.applySyncOp(userID, deviceID, op); err != nil {
if conflict, err := s.applySyncOpWithConflict(userID, deviceID, op); err != nil {
code, message := classifySyncError(err)
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}}, nil
return fail(i, op, code, message)
} else if conflict != nil {
conflicts = append(conflicts, *conflict)
}
if err := s.Repository.Operations.MarkApplied(deviceID, op.OperationID); err != nil {
log.Printf("sync: promote device=%s op=%s: %v", deviceID, op.OperationID, err)
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: "INTERNAL", Message: "operation applied but trace update failed; safe to retry"}}, nil
return fail(i, op, "INTERNAL", "operation applied but trace update failed; safe to retry")
}
}
return SyncResult{Applied: len(ops)}, nil
return SyncResult{Applied: len(ops), Conflicts: conflicts}, nil
}
func validateSyncOp(op *SyncOperation) error {
@@ -226,6 +251,48 @@ func validateSyncOp(op *SyncOperation) error {
return nil
}
// conflictToleranceMs absorbe le skew d'horloge client/serveur ET la
// troncation à la seconde des `updatedAt` RFC3339 lus par le client : un
// écart ≤ 2 s n'est pas un conflit (ex. create suivi d'un rename immédiat
// par le même device). Au-delà, quelqu'un d'autre a écrit entre-temps.
const conflictToleranceMs = 2000
// applySyncOpWithConflict applique une op et détecte les renames concurrents
// (S6 A4) : si `baseUpdatedAt` est fourni et que la ressource a bougé depuis
// (au-delà de la tolérance), le rename s'applique quand même (LWW) mais est
// signalé. Sans base (vieux clients) : aucun signalement, comportement V1.
func (s *Resources) applySyncOpWithConflict(ownerID, deviceID string, op *SyncOperation) (*ConflictInfo, error) {
var base *int64
resID := derefString(op.ResourceID)
if op.Operation == OpUpdateMetadata {
var p renamePayload
if err := json.Unmarshal(op.Payload, &p); err == nil && p.BaseUpdatedAt != nil {
base = p.BaseUpdatedAt
}
}
var preMs int64
havePre := false
if base != nil {
if _, ms, err := s.Repo.LiveNameAndUpdatedAt(resID); err == nil {
preMs, havePre = ms, true
}
}
if err := s.applySyncOp(ownerID, deviceID, op); err != nil {
return nil, err
}
if base != nil && havePre && preMs > *base+conflictToleranceMs {
name, ms, err := s.Repo.LiveNameAndUpdatedAt(resID)
if err != nil {
return nil, nil // renommé puis supprimé dans le même batch : rien à signaler.
}
return &ConflictInfo{
OperationID: op.OperationID, ResourceID: resID,
ServerName: name, ServerUpdatedAt: ms,
}, nil
}
return nil, nil
}
func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) error {
resourceID := derefString(op.ResourceID)
switch op.Operation {
+83
View File
@@ -0,0 +1,83 @@
package service
import (
"testing"
"github.com/kazier/backend/repository"
)
// TestRenameConflictReporting : un rename avec baseUpdatedAt périmée
// s'applique quand même (LWW) mais est signalé en conflicts[] (S6 A4) ;
// avec une base fraîche, aucun signalement.
func TestRenameConflictReporting(t *testing.T) {
s := newServiceStore(t)
userID := mustCreateUser(t, s.Repository, "conflict-user")
deviceA := mustRegisterDevice(t, s.Repository, "c0000000000000000000000000000001")
fileID := repository.NewID()
if err := s.Repository.Resources.InsertFile(userID, fileID, "origine.txt", "", 0, nil, nil); err != nil {
t.Fatalf("insert: %v", err)
}
// Base fraîche : pas de conflit.
name, ms, err := s.Repository.Resources.LiveNameAndUpdatedAt(fileID)
if err != nil || name != "origine.txt" {
t.Fatalf("live: %q %d %v", name, ms, err)
}
res, err := s.ApplyBatch(userID, deviceA, []SyncOperation{
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{
"name": "v2.txt", "baseUpdatedAt": ms,
}),
})
if err != nil {
t.Fatalf("apply: %v", err)
}
if res.Applied != 1 || res.Failed != nil || len(res.Conflicts) != 0 {
t.Fatalf("rename frais doit être silencieux: %+v", res)
}
// Base périmée (concurrent théorique entre-temps) : appliqué + signalé.
stale := ms - 60_000
res, err = s.ApplyBatch(userID, deviceA, []SyncOperation{
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{
"name": "v3.txt", "baseUpdatedAt": stale,
}),
})
if err != nil {
t.Fatalf("apply: %v", err)
}
if res.Applied != 1 || res.Failed != nil {
t.Fatalf("rename concurrent doit s'appliquer (LWW): %+v", res)
}
if len(res.Conflicts) != 1 {
t.Fatalf("conflit manquant: %+v", res)
}
c := res.Conflicts[0]
if c.ResourceID != fileID || c.ServerName != "v3.txt" || c.ServerUpdatedAt <= 0 {
t.Fatalf("conflit inattendu: %+v", c)
}
// Sans base (vieux client) : appliqué, jamais signalé.
res, err = s.ApplyBatch(userID, deviceA, []SyncOperation{
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{"name": "v4.txt"}),
})
if err != nil {
t.Fatalf("apply: %v", err)
}
if res.Applied != 1 || res.Failed != nil || len(res.Conflicts) != 0 {
t.Fatalf("sans base : silencieux: %+v", res)
}
// Tolérance 2 s : un écart d'1 s (skew/troncation) n'est pas un conflit.
_, nowMs, _ := s.Repository.Resources.LiveNameAndUpdatedAt(fileID)
res, err = s.ApplyBatch(userID, deviceA, []SyncOperation{
syncOp(repository.NewID(), fileID, OpUpdateMetadata, "file", map[string]any{
"name": "v5.txt", "baseUpdatedAt": nowMs - 1000,
}),
})
if err != nil {
t.Fatalf("apply: %v", err)
}
if len(res.Conflicts) != 0 {
t.Fatalf("écart 1 s ne doit pas signaler: %+v", res)
}
}