feat: list active public file links
This commit is contained in:
@@ -465,19 +465,49 @@ func normalizeAccess(access []model.Access) []model.Access {
|
||||
return out
|
||||
}
|
||||
|
||||
func (r *NodeRepository) CreatePublicLink(ctx context.Context, nodeID, actorID, token string, expiresAt time.Time) (string, error) {
|
||||
func (r *NodeRepository) CreatePublicLink(ctx context.Context, nodeID, actorID, token string, expiresAt time.Time) (*model.PublicLinkResponse, error) {
|
||||
hash := TokenHash(token)
|
||||
var id string
|
||||
var link model.PublicLinkResponse
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
INSERT INTO files_public_links (node_id, token_hash, expires_at, created_by)
|
||||
SELECT $1, $2, $3, $4
|
||||
WHERE effective_node_access($1, $4, '{}'::text[]) = 'edit'
|
||||
RETURNING id
|
||||
`, nodeID, hash, expiresAt, actorID).Scan(&id)
|
||||
INSERT INTO files_public_links (node_id, token_hash, public_token, expires_at, created_by)
|
||||
SELECT $1, $2, $3, $4, $5
|
||||
WHERE effective_node_access($1, $5, '{}'::text[]) = 'edit'
|
||||
RETURNING id, expires_at, created_at
|
||||
`, nodeID, hash, token, expiresAt, actorID).Scan(&link.ID, &link.ExpiresAt, &link.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", ErrNotFound
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return id, err
|
||||
return &link, err
|
||||
}
|
||||
|
||||
func (r *NodeRepository) ListPublicLinks(ctx context.Context, nodeID, actorID string) ([]model.PublicLinkResponse, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, COALESCE(public_token, ''), expires_at, created_at
|
||||
FROM files_public_links
|
||||
WHERE node_id = $1
|
||||
AND revoked_at IS NULL
|
||||
AND expires_at > now()
|
||||
AND effective_node_access($1, $2, '{}'::text[]) = 'edit'
|
||||
ORDER BY expires_at DESC, created_at DESC
|
||||
`, nodeID, actorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]model.PublicLinkResponse, 0)
|
||||
for rows.Next() {
|
||||
var link model.PublicLinkResponse
|
||||
var token string
|
||||
if err := rows.Scan(&link.ID, &token, &link.ExpiresAt, &link.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if token != "" {
|
||||
link.URL = token
|
||||
}
|
||||
out = append(out, link)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (r *NodeRepository) GetByPublicToken(ctx context.Context, token string) (*model.Node, error) {
|
||||
|
||||
Reference in New Issue
Block a user