V2-4 : placements multi-device (report_bytes, tables 000015, copies UI)

This commit is contained in:
venture
2026-09-25 19:45:33 +02:00
parent 0f90aad938
commit a91ddf7aa8
42 changed files with 714 additions and 25 deletions
+1
View File
@@ -20,6 +20,7 @@ var expectedRoutes = []string{
"GET /api/v1/files/:id/content",
"GET /api/v1/files/:id/shares",
"GET /api/v1/files/:id/links",
"GET /api/v1/files/:id/placements",
"DELETE /api/v1/files/:id",
"POST /api/v1/files/upload",
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS resource_placements;
DROP TABLE IF EXISTS storage_locations;
@@ -0,0 +1,25 @@
-- V2 placements multi-device : où existent les octets d'une ressource.
-- `storage_locations` modélise chaque device (le serveur reste implicite via
-- `resources.has_content`, pas de ligne). `resource_placements` matérialise
-- les reports clients (`report_bytes` via l'outbox) : hint de présence,
-- jamais une preuve — le serveur ne vérifie pas les octets du device.
CREATE TABLE storage_locations (
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
device_id TEXT NOT NULL UNIQUE CHECK (device_id ~ '^[0-9a-f]{32}$'),
type TEXT NOT NULL DEFAULT 'mobile' CHECK (type IN ('mobile', 'desktop', 'server')),
last_seen_at TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_storage_locations_user ON storage_locations(user_id);
CREATE TABLE resource_placements (
resource_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
location_id TEXT NOT NULL REFERENCES storage_locations(id) ON DELETE CASCADE,
has_bytes BOOLEAN NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (resource_id, location_id)
);
CREATE INDEX idx_placements_location ON resource_placements(location_id);
+17
View File
@@ -333,6 +333,23 @@ func TestMigrationsUpDown(t *testing.T) {
t.Error("index unique partiel (id) WHERE revoked_at IS NULL manquant sur share_links")
}
// --- 000014 : has_content + 000015 : placements -------------------------
var hasContentCol int
err = conn.QueryRow(`
SELECT COUNT(*) FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'has_content'`).Scan(&hasContentCol)
if err != nil {
t.Fatalf("has_content column: %v", err)
}
if hasContentCol == 0 {
t.Error("colonne resources.has_content manquante après 000014")
}
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "schema_migrations")
assertHexCheck(t, conn, "storage_locations", "id")
assertHexCheck(t, conn, "storage_locations", "device_id")
if err := MigrateDownDatabase(url); err != nil {
t.Fatalf("migrate down: %v", err)
}
+65
View File
@@ -900,3 +900,68 @@ func TestFilesSharesLinksListing(t *testing.T) {
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/shares", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-invalid-id")
}
func TestFilesPlacementsListing(t *testing.T) {
r, _, repo := setup(t)
ownerDevice := repository.NewID()
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "pl"), "placements-password", ownerDevice)
granteeDevice := repository.NewID()
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "pg"), "placements-password-b", granteeDevice)
outsiderDevice := repository.NewID()
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "ph"), "placements-password-c", outsiderDevice)
fileID := repository.NewID()
if err := repo.Resources.InsertFile(owner, fileID, "placed.txt", "", 4, nil, nil); err != nil {
t.Fatalf("insert file: %v", err)
}
// Sans report : seule l'entrée serveur (hasContent false, metadata-only).
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", ownerToken, nil, "")
env := expectOK(t, rec, "placements-server-only")
var only []map[string]any
if err := json.Unmarshal(env.Data, &only); err != nil {
t.Fatalf("placements-server-only: unmarshal: %v", err)
}
if len(only) != 1 || only[0]["locationType"] != "server" || only[0]["hasBytes"] != false {
t.Errorf("placements-server-only = %v, attendu 1 entrée serveur false", only)
}
// Report via l'outbox (owner) : l'entrée device apparaît.
reportBody := fmt.Sprintf(
`{"operations":[{"operation_id":%q,"resource_id":%q,"resource_type":"file","operation":"report_bytes","payload":{"hasBytes":true}}]}`,
repository.NewID(), fileID,
)
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", ownerToken, []byte(reportBody), "application/json")
if rec.Code != http.StatusOK {
t.Fatalf("sync/ops report: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
}
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", ownerToken, nil, "")
env = expectOK(t, rec, "placements-owner")
var entries []struct {
LocationType string `json:"locationType"`
DeviceID string `json:"deviceId"`
HasBytes bool `json:"hasBytes"`
}
if err := json.Unmarshal(env.Data, &entries); err != nil {
t.Fatalf("placements-owner: unmarshal: %v", err)
}
if len(entries) != 2 || entries[0].LocationType != "server" {
t.Fatalf("placements-owner = %+v, attendu serveur + 1 device", entries)
}
if entries[1].LocationType != "mobile" || !entries[1].HasBytes || entries[1].DeviceID != ownerDevice {
t.Errorf("placements-owner[1] = %+v, attendu device owner true", entries[1])
}
// Granté et outsider : 404 (owner-only, pas d'énumération).
if err := repo.Shares.Upsert(fileID, grantee, "viewer", false, nil, owner); err != nil {
t.Fatalf("share: %v", err)
}
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", granteeToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-grantee")
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", outsiderToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-outsider")
// ID invalide → 404.
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/placements", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-invalid-id")
}
+1
View File
@@ -36,6 +36,7 @@ func RegisterRoutes(r *gin.Engine) {
protected.GET("/files/:id/content", FilesContent)
protected.GET("/files/:id/shares", SharesList)
protected.GET("/files/:id/links", LinksList)
protected.GET("/files/:id/placements", PlacementsList)
protected.DELETE("/files/:id", FilesDelete)
protected.POST("/files/upload", FilesUpload)
+22
View File
@@ -50,3 +50,25 @@ func LinksList(c *gin.Context) {
}
api.OK(c, links)
}
// PlacementsList exposes byte presence of a resource the caller OWNS
// (server entry + device reports, newest first) : GET /files/:id/placements.
// Owner-only (404 otherwise, no enumeration).
func PlacementsList(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
}
placements, err := Store.ListPlacements(userID, id)
if err != nil {
writeError(c, err)
return
}
api.OK(c, placements)
}
+85
View File
@@ -0,0 +1,85 @@
package repository
import (
"database/sql"
"time"
)
// Placement is one device's reported byte presence on a resource.
type Placement struct {
LocationType string
DeviceID string
HasBytes bool
UpdatedAt time.Time
}
// Placements implements the V2 multi-device presence hints
// (`storage_locations` + `resource_placements`, cf. V2.md §3.4-3.5, réduit :
// pas de `status` pending, juste `has_bytes` + fraîcheur).
type Placements struct {
DB *sql.DB
}
// EnsureLocation returns the location id for (user, device), creating it on
// first report. `user_id` tracks the last reporter (a device may change
// accounts) ; rows of departed users stay but are filtered at read time by
// resource visibility.
func (p *Placements) EnsureLocation(userID, deviceID string) (string, error) {
var id string
err := p.DB.QueryRow(
`INSERT INTO storage_locations (id, user_id, device_id, type)
VALUES ($1, $2, $3, 'mobile')
ON CONFLICT (device_id) DO UPDATE SET user_id = EXCLUDED.user_id, last_seen_at = NOW()
RETURNING id`,
NewID(), userID, deviceID,
).Scan(&id)
if err != nil {
return "", err
}
return id, nil
}
// Report upserts a device's byte presence for a resource. Naturally
// idempotent (same state twice = no-op) ; the outbox replay guard (A9)
// requires a FRESH operation_id per state change, documented in api-v1.md.
func (p *Placements) Report(userID, deviceID, resourceID string, hasBytes bool) error {
locationID, err := p.EnsureLocation(userID, deviceID)
if err != nil {
return err
}
_, err = p.DB.Exec(
`INSERT INTO resource_placements (resource_id, location_id, has_bytes, updated_at)
VALUES ($1, $2, $3, NOW())
ON CONFLICT (resource_id, location_id)
DO UPDATE SET has_bytes = EXCLUDED.has_bytes, updated_at = NOW()`,
resourceID, locationID, hasBytes,
)
return err
}
// ListForResource returns all device placements of a resource, newest first.
// Server-side bytes (`resources.has_content`) are NOT a row — the caller
// prepends the implicit server entry.
func (p *Placements) ListForResource(resourceID string) ([]Placement, error) {
rows, err := p.DB.Query(
`SELECT sl.type, sl.device_id, rp.has_bytes, rp.updated_at
FROM resource_placements rp
JOIN storage_locations sl ON sl.id = rp.location_id
WHERE rp.resource_id = $1
ORDER BY rp.updated_at DESC`,
resourceID,
)
if err != nil {
return nil, err
}
defer rows.Close()
var out []Placement
for rows.Next() {
var pl Placement
if err := rows.Scan(&pl.LocationType, &pl.DeviceID, &pl.HasBytes, &pl.UpdatedAt); err != nil {
return nil, err
}
out = append(out, pl)
}
return out, rows.Err()
}
+2
View File
@@ -12,6 +12,7 @@ type Repository struct {
OcrJobs *OcrJobs
Users *Users
Shares *Shares
Placements *Placements
}
func NewRepository(conn *sql.DB) *Repository {
@@ -22,5 +23,6 @@ func NewRepository(conn *sql.DB) *Repository {
OcrJobs: &OcrJobs{DB: conn},
Users: &Users{DB: conn},
Shares: &Shares{DB: conn},
Placements: &Placements{DB: conn},
}
}
+1
View File
@@ -543,6 +543,7 @@ func (r *Resources) SetHasContent(ownerID, resourceID string, has bool) error {
}
return nil
}
// UpdateOcrText writes the server-computed OCR extract onto the resource
// (owner-scoped, bumping updated_at so the change flows through the sync
// snapshot). The text is then readable by every device of the user AND by
+56
View File
@@ -0,0 +1,56 @@
package service
import (
"time"
"github.com/kazier/backend/repository"
)
// PlacementDTO serializes one byte-presence entry for
// GET /files/:id/placements. The `server` entry is implicit (derived from
// `resources.has_content`, no row) ; device entries come from client reports
// (`report_bytes` ops). `updatedAt` is RFC3339 (server entry: resource
// timestamp ; device entries: report timestamp).
type PlacementDTO struct {
LocationType string `json:"locationType"`
DeviceID string `json:"deviceId,omitempty"`
HasBytes bool `json:"hasBytes"`
UpdatedAt string `json:"updatedAt,omitempty"`
}
// ListPlacements returns byte presence of a resource the user OWNS (server
// entry + device reports, newest first). Non-owner → ErrNotFound (same
// owner-only scoping as shares/links reads).
func (s *Resources) ListPlacements(userID, resourceID string) ([]PlacementDTO, error) {
ownerID, err := s.Repo.GetResourceOwner(resourceID)
if err != nil {
return nil, err
}
if ownerID != userID {
return nil, repository.ErrNotFound
}
row, err := s.Repo.GetFileVisible(userID, resourceID)
if err != nil {
return nil, err
}
out := []PlacementDTO{
{
LocationType: "server",
HasBytes: row.HasContent,
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
},
}
placements, err := s.Repository.Placements.ListForResource(resourceID)
if err != nil {
return nil, err
}
for _, p := range placements {
out = append(out, PlacementDTO{
LocationType: p.LocationType,
DeviceID: p.DeviceID,
HasBytes: p.HasBytes,
UpdatedAt: p.UpdatedAt.UTC().Format(time.RFC3339),
})
}
return out, nil
}
+39 -7
View File
@@ -23,6 +23,8 @@ const (
OpUpdateShare = "update_share"
OpCreateLink = "create_link"
OpRevokeLink = "revoke_link"
// V2 placements : un device signale la présence de ses octets.
OpReportBytes = "report_bytes"
)
// SyncOperation is one outbox entry (shadow of mobile PendingOperationRow).
@@ -85,6 +87,10 @@ type revokeLinkPayload struct {
Token string `json:"token"`
}
type reportBytesPayload struct {
HasBytes *bool `json:"hasBytes"`
}
func classifySyncError(err error) (string, string) {
switch {
case errors.Is(err, repository.ErrNameConflict):
@@ -166,7 +172,7 @@ 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, op); err != nil {
if err := s.applySyncOp(userID, deviceID, op); err != nil {
code, message := classifySyncError(err)
return SyncResult{Applied: i, Failed: &FailedOperation{OperationID: op.OperationID, Code: code, Message: message}}, nil
}
@@ -190,7 +196,8 @@ func validateSyncOp(op *SyncOperation) error {
}
switch op.Operation {
case OpCreateResource, OpUpdateMetadata, OpMoveResource, OpDeleteResource,
OpShare, OpRevokeShare, OpUpdateShare, OpCreateLink, OpRevokeLink:
OpShare, OpRevokeShare, OpUpdateShare, OpCreateLink, OpRevokeLink,
OpReportBytes:
default:
return errors.New("unknown operation " + op.Operation)
}
@@ -200,7 +207,7 @@ func validateSyncOp(op *SyncOperation) error {
return nil
}
func (s *Resources) applySyncOp(ownerID string, op *SyncOperation) error {
func (s *Resources) applySyncOp(ownerID, deviceID string, op *SyncOperation) error {
resourceID := derefString(op.ResourceID)
switch op.Operation {
case OpCreateResource:
@@ -272,10 +279,10 @@ func (s *Resources) applySyncOp(ownerID string, op *SyncOperation) error {
return errors.New("payload.name required")
}
// NOTE : UpdateNameByID (non-scopé) est volontaire ici — un editor
// non-owner peut renommer (EffectiveAccess vérifié plus haut), donc
// UpdateNameOwned(ownerID, ...) rejetterait à tort. La garde
// d'autorisation précède immédiatement l'écriture.
return s.Repo.UpdateNameByID(resourceID, p.Name)
// non-owner peut renommer (EffectiveAccess vérifié plus haut), donc
// UpdateNameOwned(ownerID, ...) rejetterait à tort. La garde
// d'autorisation précède immédiatement l'écriture.
return s.Repo.UpdateNameByID(resourceID, p.Name)
case OpMoveResource:
// A8 : move owner-only. Source non-owned (absente OU d'autrui) →
@@ -333,11 +340,36 @@ func (s *Resources) applySyncOp(ownerID string, op *SyncOperation) error {
case OpRevokeLink:
return s.applyRevokeLinkOp(ownerID, resourceID, op)
case OpReportBytes:
return s.applyReportBytesOp(ownerID, deviceID, resourceID, op)
default:
return errors.New("unknown operation " + op.Operation)
}
}
// applyReportBytesOp handles OpReportBytes: a device reports its byte
// presence (V2 placements). Scope is viewer+ (owned or shared,
// GetFileVisible) — a grantee downloading a shared file reports too.
// Invisible → ErrNotFound (no enumeration). Naturally idempotent ; the A9
// replay guard requires a FRESH operation_id per state change.
func (s *Resources) applyReportBytesOp(userID, deviceID, resourceID string, op *SyncOperation) error {
if derefString(op.ResourceType) != "file" {
return errors.New("resource_type must be 'file' for report_bytes")
}
if _, err := s.Repo.GetFileVisible(userID, resourceID); err != nil {
return err
}
var p reportBytesPayload
if err := json.Unmarshal(op.Payload, &p); err != nil {
return errors.New("invalid payload: " + err.Error())
}
if p.HasBytes == nil {
return errors.New("payload.hasBytes required")
}
return s.Repository.Placements.Report(userID, deviceID, resourceID, *p.HasBytes)
}
// applyShareOp handles OpShare and OpUpdateShare: upsert a user-to-user grant.
func (s *Resources) applyShareOp(ownerID, resourceID string, op *SyncOperation) error {
exists, err := s.Repo.ExistsOwner(ownerID, resourceID)
+73
View File
@@ -1485,3 +1485,76 @@ func TestDeleteAbsentIsIdempotentNoop(t *testing.T) {
t.Errorf("delete absent: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
}
func TestApplyBatchReportBytes(t *testing.T) {
s := newServiceStore(t)
ownerID := mustCreateUser(t, s.Repository, "report-owner")
granteeID := mustCreateUser(t, s.Repository, "report-grantee")
outsiderID := mustCreateUser(t, s.Repository, "report-outsider")
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
otherDevice := mustRegisterDevice(t, s.Repository, "b0000000000000000000000000000001")
fileID := repository.NewID()
if _, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(repository.NewID(), fileID, OpCreateResource, "file", map[string]any{"name": "r.txt"}),
syncOp(repository.NewID(), fileID, OpShare, "file", map[string]any{"granteeUserId": granteeID, "access": "viewer"}),
}); err != nil {
t.Fatalf("setup: %v", err)
}
report := func(user, device, res string, hasBytes bool) (SyncResult, error) {
return s.ApplyBatch(user, device, []SyncOperation{
syncOp(repository.NewID(), res, OpReportBytes, "file", map[string]any{"hasBytes": hasBytes}),
})
}
// Owner + grantee (viewer) peuvent reporter ; l'owner lit serveur + device.
if result, err := report(ownerID, deviceID, fileID, true); err != nil || result.Applied != 1 || result.Failed != nil {
t.Fatalf("report owner: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
if result, err := report(granteeID, otherDevice, fileID, true); err != nil || result.Applied != 1 || result.Failed != nil {
t.Fatalf("report grantee: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
placements, err := s.ListPlacements(ownerID, fileID)
if err != nil {
t.Fatalf("ListPlacements: %v", err)
}
if len(placements) != 3 {
t.Fatalf("attendu serveur + 2 devices, got %+v", placements)
}
if placements[0].LocationType != "server" || placements[0].HasBytes {
t.Errorf("entrée serveur = %+v, attendu hasBytes=false", placements[0])
}
// Changement d'état (drop) avec un NOUVEL operation_id ; hasBytes manquant → halt.
if result, err := report(ownerID, deviceID, fileID, false); err != nil || result.Applied != 1 || result.Failed != nil {
t.Fatalf("report drop: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
bad, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(repository.NewID(), fileID, OpReportBytes, "file", map[string]any{}),
})
if err != nil || bad.Applied != 0 || bad.Failed == nil || bad.Failed.Code != "INVALID_REQUEST" {
t.Errorf("hasBytes manquant: attendu halt INVALID_REQUEST, got %+v err=%v", bad, err)
}
// Outsider (invisible) → halt NOT_FOUND, sans énumération.
denied, err := report(outsiderID, otherDevice, fileID, true)
if err != nil || denied.Applied != 0 || denied.Failed == nil || denied.Failed.Code != "NOT_FOUND" {
t.Errorf("report outsider: attendu halt NOT_FOUND, got %+v err=%v", denied, err)
}
// Rejeu à l'identique → idempotent (skip, pas d'erreur).
replayID := repository.NewID()
first, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(replayID, fileID, OpReportBytes, "file", map[string]any{"hasBytes": true}),
})
if err != nil || first.Applied != 1 || first.Failed != nil {
t.Fatalf("report initial: %+v err=%v", first, err)
}
second, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(replayID, fileID, OpReportBytes, "file", map[string]any{"hasBytes": true}),
})
if err != nil || second.Applied != 1 || second.Failed != nil {
t.Errorf("rejeu identique: attendu skip idempotent, got %+v err=%v", second, err)
}
}