V2-4 : placements multi-device (report_bytes, tables 000015, copies UI)
This commit is contained in:
+39
-7
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user