package service import ( "time" "github.com/kazier/backend/repository" ) // ShareGrantDTO serializes one active grant for GET /files/:id/shares. // Milliseconds epoch (null = never), mirroring the permissions snapshot // (docs/api-v1.md §6.2) for cheap client-side expiry checks. // `granteeKind` is "user" or "group" (V2-6) ; `granteeUserId` carries the // principal id either way, `username` its display name. type ShareGrantDTO struct { GranteeUserID string `json:"granteeUserId"` GranteeKind string `json:"granteeKind"` Username string `json:"username"` Access string `json:"access"` Inherit bool `json:"inherit"` ExpiresAt *int64 `json:"expiresAt"` UpdatedAt int64 `json:"updatedAt"` } // LinkDTO serializes one active public link for GET /files/:id/links — // same shape as the public GET /shares/links/:token resolution. type LinkDTO struct { Token string `json:"token"` ResourceID string `json:"resource_id"` ResourceType string `json:"resourceType"` Name string `json:"name"` Access string `json:"access"` ExpiresAt *string `json:"expiresAt"` } // ListShares returns the active grants on a resource the user OWNS. // Non-owner (or unknown resource) → repository.ErrNotFound (no enumeration, // same scoping as the share outbox ops). func (s *Resources) ListShares(userID, resourceID string) ([]ShareGrantDTO, error) { ownerID, err := s.Repo.GetResourceOwner(resourceID) if err != nil { return nil, err } if ownerID != userID { return nil, repository.ErrNotFound } grants, err := s.Repository.Shares.ListActiveForResource(resourceID) if err != nil { return nil, err } out := make([]ShareGrantDTO, 0, len(grants)) for _, g := range grants { var expires *int64 if g.ExpiresAt != nil { ms := g.ExpiresAt.UnixMilli() expires = &ms } granteeID := g.GranteeUserID if g.GranteeGroupID != "" { granteeID = g.GranteeGroupID } out = append(out, ShareGrantDTO{ GranteeUserID: granteeID, GranteeKind: g.GrantKind(), Username: g.Username, Access: g.Access, Inherit: g.Inherit, ExpiresAt: expires, UpdatedAt: g.UpdatedAt.UnixMilli(), }) } return out, nil } // ListLinks returns the active public links of a resource the user OWNS. // Same owner-only scoping as ListShares. func (s *Resources) ListLinks(userID, resourceID string) ([]LinkDTO, error) { ownerID, err := s.Repo.GetResourceOwner(resourceID) if err != nil { return nil, err } if ownerID != userID { return nil, repository.ErrNotFound } links, err := s.Repository.Shares.ListActiveLinksForResource(resourceID) if err != nil { return nil, err } out := make([]LinkDTO, 0, len(links)) for _, l := range links { var expires *string if l.ExpiresAt != nil { // Même forme que ShareLinkGet (time.Time marshaled en RFC3339). str := l.ExpiresAt.UTC().Format(time.RFC3339) expires = &str } out = append(out, LinkDTO{ Token: l.Token, ResourceID: l.ResourceID, ResourceType: l.ResourceType, Name: l.Name, Access: l.Access, ExpiresAt: expires, }) } return out, nil }