|
|
|
@@ -39,7 +39,10 @@ func scanNode(scan func(dest ...any) error) (*model.Node, error) {
|
|
|
|
return &n, err
|
|
|
|
return &n, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *NodeRepository) List(ctx context.Context, userID string, subordinateIDs []string, scope string, parentID *string) ([]model.Node, error) {
|
|
|
|
func (r *NodeRepository) List(ctx context.Context, userID string, subordinateIDs []string, scope string, parentID *string, search string) ([]model.Node, error) {
|
|
|
|
|
|
|
|
if strings.TrimSpace(search) != "" {
|
|
|
|
|
|
|
|
return r.Search(ctx, userID, subordinateIDs, scope, search)
|
|
|
|
|
|
|
|
}
|
|
|
|
args := []any{userID, subordinateIDs}
|
|
|
|
args := []any{userID, subordinateIDs}
|
|
|
|
where := []string{"n.deleted_at IS NULL"}
|
|
|
|
where := []string{"n.deleted_at IS NULL"}
|
|
|
|
if scope == "trash" {
|
|
|
|
if scope == "trash" {
|
|
|
|
@@ -127,6 +130,71 @@ func (r *NodeRepository) List(ctx context.Context, userID string, subordinateIDs
|
|
|
|
return out, rows.Err()
|
|
|
|
return out, rows.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (r *NodeRepository) Search(ctx context.Context, userID string, subordinateIDs []string, scope string, search string) ([]model.Node, error) {
|
|
|
|
|
|
|
|
pattern := "%" + strings.ToLower(strings.TrimSpace(search)) + "%"
|
|
|
|
|
|
|
|
args := []any{userID, subordinateIDs, pattern}
|
|
|
|
|
|
|
|
where := []string{`(
|
|
|
|
|
|
|
|
lower(n.title) LIKE $3
|
|
|
|
|
|
|
|
OR lower(COALESCE(n.original_filename, '')) LIKE $3
|
|
|
|
|
|
|
|
OR lower(COALESCE(n.extension, '')) LIKE $3
|
|
|
|
|
|
|
|
OR lower(COALESCE(n.mime_type, '')) LIKE $3
|
|
|
|
|
|
|
|
)`}
|
|
|
|
|
|
|
|
accessExpr := "effective_node_access(n.id, $1, $2::text[])"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if scope == "trash" {
|
|
|
|
|
|
|
|
where = append(where, "n.deleted_at IS NOT NULL")
|
|
|
|
|
|
|
|
where = append(where, `(
|
|
|
|
|
|
|
|
n.owner_user_id = $1
|
|
|
|
|
|
|
|
OR n.owner_user_id::text = ANY($2::text[])
|
|
|
|
|
|
|
|
OR EXISTS (
|
|
|
|
|
|
|
|
SELECT 1 FROM files_access a
|
|
|
|
|
|
|
|
WHERE a.node_id = n.id
|
|
|
|
|
|
|
|
AND a.user_id = $1
|
|
|
|
|
|
|
|
AND a.access_level = 'edit'
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)`)
|
|
|
|
|
|
|
|
accessExpr = "'edit'"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
where = append(where, "n.deleted_at IS NULL")
|
|
|
|
|
|
|
|
switch scope {
|
|
|
|
|
|
|
|
case "shared":
|
|
|
|
|
|
|
|
where = append(where, "n.owner_user_id <> $1")
|
|
|
|
|
|
|
|
where = append(where, `(
|
|
|
|
|
|
|
|
has_node_access(n.id, $1)
|
|
|
|
|
|
|
|
OR n.owner_user_id::text = ANY($2::text[])
|
|
|
|
|
|
|
|
)`)
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
where = append(where, "n.owner_user_id = $1")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query := `
|
|
|
|
|
|
|
|
SELECT n.id, n.parent_id, n.node_type, n.title, n.owner_user_id, n.owner_department_id,
|
|
|
|
|
|
|
|
n.created_by, n.updated_by, n.storage_key, n.original_filename, n.mime_type,
|
|
|
|
|
|
|
|
n.extension, n.size_bytes, n.office_format, n.external_url, n.version,
|
|
|
|
|
|
|
|
` + accessExpr + `,
|
|
|
|
|
|
|
|
n.created_at, n.updated_at, n.trashed_at, n.purge_after, n.deleted_at
|
|
|
|
|
|
|
|
FROM files_nodes n
|
|
|
|
|
|
|
|
WHERE ` + strings.Join(where, " AND ") + `
|
|
|
|
|
|
|
|
ORDER BY CASE WHEN n.node_type = 'folder' THEN 0 ELSE 1 END, lower(n.title), n.created_at DESC`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rows, err := r.pool.Query(ctx, query, args...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out := make([]model.Node, 0)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
|
|
|
n, err := scanNode(rows.Scan)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, *n)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, rows.Err()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *NodeRepository) ListChildrenForPublic(ctx context.Context, parentID string) ([]model.Node, error) {
|
|
|
|
func (r *NodeRepository) ListChildrenForPublic(ctx context.Context, parentID string) ([]model.Node, error) {
|
|
|
|
rows, err := r.pool.Query(ctx, `
|
|
|
|
rows, err := r.pool.Query(ctx, `
|
|
|
|
SELECT n.id, n.parent_id, n.node_type, n.title, n.owner_user_id, n.owner_department_id,
|
|
|
|
SELECT n.id, n.parent_id, n.node_type, n.title, n.owner_user_id, n.owner_department_id,
|
|
|
|
@@ -209,8 +277,7 @@ func (r *NodeRepository) ListOfficeExternalURLs(ctx context.Context, userID stri
|
|
|
|
rows, err := r.pool.Query(ctx, `
|
|
|
|
rows, err := r.pool.Query(ctx, `
|
|
|
|
SELECT DISTINCT n.external_url
|
|
|
|
SELECT DISTINCT n.external_url
|
|
|
|
FROM files_nodes n
|
|
|
|
FROM files_nodes n
|
|
|
|
WHERE n.deleted_at IS NULL
|
|
|
|
WHERE n.node_type = 'office_document'
|
|
|
|
AND n.node_type = 'office_document'
|
|
|
|
|
|
|
|
AND n.external_url IS NOT NULL
|
|
|
|
AND n.external_url IS NOT NULL
|
|
|
|
AND (
|
|
|
|
AND (
|
|
|
|
n.owner_user_id = $1
|
|
|
|
n.owner_user_id = $1
|
|
|
|
|