- tests/test_smoke_uncovered.py : +6 tests pour library.py (1/10 → 8 routes
couvertes) : les 5 listes en boucle (recents/favorites/published/private/
workspace → 200 + items), /private avec page seedée retrouvée, /children/{id}
avec parent/enfant seedés, /repository vide ET clé (string, aucun réseau),
non-régression 404 sur les routes supprimées.
- DÉCOUVERTE (les smokes l'ont prouvé) : `/api/library/local-workspace-children`
renvoyait un 500 systématique (test vert → ASGI double response.start) et
`/api/library/local-workspace` 500 dès qu'un workspace existe — les deux
lisaient `local_workspace_items`, table AUCUNEMENT créée dans le codebase
(0 CREATE TABLE) et sans 1 seule référence front. Supprimés avec
`library._format_size` devenu mort (la copie de dashboard.py est inchangée).
`local_workspace_items` : 0 occurrence restante dans app/.
- helper `_seed_page` minimal (workspace NOT NULL inclus) + nettoyage en
finally (pas de pollution des autres tests).
suite **1053/1053** (229 s) · `ruff check app tests` OK · docs à jour
restent phase 2b : api.py 3/23, dashboard.py 17/63, api_v2.py 50/115
414 lines
13 KiB
Python
414 lines
13 KiB
Python
"""FlowDeck — Library API: recents, favorites, shared, published, private, workspace."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Query, Request
|
|
|
|
from app.auth.session import SessionManager
|
|
from app.db import get_conn
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(tags=["library"], prefix="/api/library")
|
|
|
|
SOURCE_TYPES = {"all", "local", "gitea", "github"}
|
|
|
|
|
|
def _get_user_id(request: Request) -> int:
|
|
user = SessionManager.decode_session(request.cookies.get("flowdeck_session", ""))
|
|
return user["id"] if user and user.get("id") else 1
|
|
|
|
|
|
def _detect_source_type(workspace: str) -> str:
|
|
ws = (workspace or "").strip()
|
|
if not ws:
|
|
return "local"
|
|
if ws.startswith("github:"):
|
|
return "github"
|
|
if "/" in ws:
|
|
return "gitea"
|
|
return "local"
|
|
|
|
|
|
def _source_label(source_type: str, workspace: str) -> str:
|
|
if source_type == "gitea":
|
|
return workspace
|
|
elif source_type == "github":
|
|
return workspace.replace("github:", "", 1)
|
|
return workspace or "Private"
|
|
|
|
|
|
def _build_item(db_row: dict, uid: int = 1) -> dict:
|
|
workspace = db_row.get("workspace", "") or ""
|
|
source_type = _detect_source_type(workspace)
|
|
page_id = db_row["id"]
|
|
title = db_row.get("title") or "Untitled"
|
|
|
|
# URL
|
|
if source_type in ("gitea", "github"):
|
|
ws = workspace.replace("github:", "", 1) if source_type == "github" else workspace
|
|
parts = ws.split("/", 1)
|
|
url = f"/gitea-workspace?owner={parts[0]}&repo={parts[1] if len(parts) > 1 else ''}"
|
|
else:
|
|
url = f"/pages/{page_id}"
|
|
|
|
# Icon
|
|
content_format = db_row.get("content_format", "blocks")
|
|
if db_row.get("is_folder"):
|
|
icon = "📁"
|
|
elif content_format == "file":
|
|
icon = "📄"
|
|
fn = title.lower()
|
|
if fn.endswith(".pdf"):
|
|
icon = "📕"
|
|
elif any(fn.endswith(e) for e in [".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg"]):
|
|
icon = "🖼️"
|
|
elif any(fn.endswith(e) for e in [".py", ".js", ".ts", ".go", ".rs"]):
|
|
icon = "📜"
|
|
else:
|
|
icon = "📝"
|
|
|
|
return {
|
|
"id": page_id,
|
|
"title": title,
|
|
"icon": icon,
|
|
"page_icon": db_row.get("page_icon") or "",
|
|
"is_folder": bool(db_row.get("is_folder", 0)),
|
|
"source_type": source_type,
|
|
"source_label": _source_label(source_type, workspace),
|
|
"workspace": workspace,
|
|
"workspace_name": _source_label(source_type, workspace),
|
|
"author": db_row.get("author") or "",
|
|
"author_initial": (db_row.get("author") or "?")[0].upper(),
|
|
"updated_at": db_row.get("updated_at", ""),
|
|
"visited_at": db_row.get("visited_at") or "",
|
|
"has_children": False,
|
|
"children": [],
|
|
"url": url,
|
|
"content_format": content_format,
|
|
"favorited": bool(db_row.get("favorited", 0)),
|
|
"share_mode": db_row.get("share_mode", "private"),
|
|
"tags": [],
|
|
}
|
|
|
|
|
|
def _apply_source_filter(query: str, params: list, source_type: str) -> tuple[str, list]:
|
|
if source_type == "local":
|
|
query += " AND (p.workspace = '' OR (p.workspace NOT LIKE '%/%' AND p.workspace NOT LIKE 'github:%'))"
|
|
elif source_type == "gitea":
|
|
query += " AND p.workspace LIKE '%/%' AND p.workspace NOT LIKE 'github:%'"
|
|
elif source_type == "github":
|
|
query += " AND p.workspace LIKE 'github:%'"
|
|
return query, params
|
|
|
|
|
|
def _rows_to_items(rows, uid: int = 1) -> list:
|
|
items = [_build_item(dict(r), uid) for r in rows]
|
|
return _attach_tags(items)
|
|
|
|
|
|
def _attach_tags(items: list) -> list:
|
|
"""Batch-load page tags for library items."""
|
|
if not items:
|
|
return items
|
|
ids = [it["id"] for it in items]
|
|
placeholders = ",".join("?" for _ in ids)
|
|
with get_conn() as conn:
|
|
rows = conn.execute(
|
|
f"SELECT pt.page_id, t.id, t.name, t.color FROM page_tags pt "
|
|
f"JOIN tags t ON t.id = pt.tag_id WHERE pt.page_id IN ({placeholders})",
|
|
ids,
|
|
).fetchall()
|
|
tag_map: dict = {}
|
|
for r in rows:
|
|
tag_map.setdefault(r["page_id"], []).append({
|
|
"id": r["id"], "name": r["name"], "color": r["color"],
|
|
})
|
|
for it in items:
|
|
it["tags"] = tag_map.get(it["id"], [])
|
|
return items
|
|
|
|
|
|
def _enrich_children(items: list) -> list:
|
|
"""Add has_children info to items in a single batch query."""
|
|
if not items:
|
|
return items
|
|
ids = [it["id"] for it in items]
|
|
placeholders = ",".join(["?" for _ in ids])
|
|
with get_conn() as conn:
|
|
child_rows = conn.execute(
|
|
f"SELECT parent_id, COUNT(*) as cnt FROM pages "
|
|
f"WHERE parent_id IN ({placeholders}) AND deleted_at IS NULL "
|
|
f"GROUP BY parent_id",
|
|
ids,
|
|
).fetchall()
|
|
child_counts = {r["parent_id"]: r["cnt"] for r in child_rows}
|
|
for it in items:
|
|
it["has_children"] = bool(child_counts.get(it["id"], 0))
|
|
return items
|
|
|
|
|
|
# ═══════════ Tab endpoints ═══════════
|
|
|
|
BASE_SELECT = (
|
|
"SELECT p.id, p.title, p.workspace, p.updated_at, p.content_format, "
|
|
"p.parent_id, p.share_mode, p.parent_section, p.page_icon"
|
|
)
|
|
|
|
|
|
@router.get("/recents")
|
|
def library_recents(
|
|
request: Request,
|
|
source_type: str = Query(default="all"),
|
|
tree: int = Query(default=0),
|
|
workspace_id: int = Query(default=0),
|
|
):
|
|
if source_type not in SOURCE_TYPES:
|
|
source_type = "all"
|
|
|
|
uid = _get_user_id(request)
|
|
query = f"{BASE_SELECT} FROM pages p WHERE p.parent_section != 'Trash' AND p.deleted_at IS NULL"
|
|
params: list = []
|
|
|
|
# Hierarchical view: show only root-level pages at the top
|
|
if tree:
|
|
query += " AND p.parent_id IS NULL"
|
|
|
|
# Filter by active workspace
|
|
if workspace_id:
|
|
query += " AND p.workspace_id = ?"
|
|
params.append(workspace_id)
|
|
|
|
query, params = _apply_source_filter(query, params, source_type)
|
|
query += " ORDER BY p.updated_at DESC LIMIT 50"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/favorites")
|
|
def library_favorites(
|
|
request: Request,
|
|
source_type: str = Query(default="all"),
|
|
tree: int = Query(default=0),
|
|
):
|
|
if source_type not in SOURCE_TYPES:
|
|
source_type = "all"
|
|
|
|
uid = _get_user_id(request)
|
|
query = (
|
|
"SELECT p.id, p.title, p.workspace, p.updated_at, p.content_format, "
|
|
"p.parent_id, p.share_mode, p.parent_section, 1 as favorited "
|
|
"FROM favorites f JOIN pages p ON p.id = f.page_id "
|
|
"WHERE f.user_id = ? AND p.parent_section != 'Trash' AND p.deleted_at IS NULL"
|
|
)
|
|
params: list = [uid]
|
|
if tree:
|
|
query += " AND p.parent_id IS NULL"
|
|
query, params = _apply_source_filter(query, params, source_type)
|
|
query += " ORDER BY f.position"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/shared")
|
|
def library_shared(
|
|
request: Request,
|
|
source_type: str = Query(default="all"),
|
|
tree: int = Query(default=0),
|
|
dir: str = Query(default="all"),
|
|
):
|
|
if source_type not in SOURCE_TYPES:
|
|
source_type = "all"
|
|
if dir not in ("made", "received", "all"):
|
|
dir = "all"
|
|
|
|
uid = _get_user_id(request)
|
|
|
|
# Page ids the user shares toward others (nominal page_shares) or receives
|
|
# (direct shares + group shares via group_members)
|
|
with get_conn() as conn:
|
|
made_rows = conn.execute(
|
|
"SELECT DISTINCT s.page_id FROM page_shares s WHERE s.created_by=?",
|
|
(uid,),
|
|
).fetchall()
|
|
try:
|
|
recv_rows = conn.execute(
|
|
"""SELECT DISTINCT s.page_id FROM page_shares s
|
|
LEFT JOIN group_members gm ON gm.group_id = s.shared_with_group_id AND gm.user_id=?
|
|
WHERE s.shared_with_user_id=? OR gm.user_id=?""",
|
|
(uid, uid, uid),
|
|
).fetchall()
|
|
except Exception:
|
|
recv_rows = conn.execute(
|
|
"SELECT DISTINCT s.page_id FROM page_shares s WHERE s.shared_with_user_id=?",
|
|
(uid,),
|
|
).fetchall()
|
|
made_ids = {r[0] for r in made_rows}
|
|
recv_ids = {r[0] for r in recv_rows}
|
|
|
|
conds: list[str] = []
|
|
params: list = []
|
|
if dir in ("all", "made"):
|
|
conds.append("p.share_mode != 'private'")
|
|
if dir in ("all", "made") and made_ids:
|
|
conds.append(f"p.id IN ({','.join('?' for _ in made_ids)})")
|
|
params.extend(made_ids)
|
|
if dir in ("all", "received") and recv_ids:
|
|
conds.append(f"p.id IN ({','.join('?' for _ in recv_ids)})")
|
|
params.extend(recv_ids)
|
|
if dir == "received" and not recv_ids:
|
|
return {"items": []}
|
|
|
|
query = (
|
|
f"{BASE_SELECT} FROM pages p "
|
|
f"WHERE ({' OR '.join(conds)}) AND p.parent_section != 'Trash' AND p.deleted_at IS NULL"
|
|
)
|
|
if tree:
|
|
query += " AND p.parent_id IS NULL"
|
|
query, params = _apply_source_filter(query, params, source_type)
|
|
query += " ORDER BY p.updated_at DESC LIMIT 50"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
for it in items:
|
|
sid = it["id"]
|
|
is_made = sid in made_ids or it.get("share_mode", "private") != "private"
|
|
is_recv = sid in recv_ids
|
|
it["share_dir"] = "both" if (is_made and is_recv) else ("made" if is_made else ("received" if is_recv else ""))
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/published")
|
|
def library_published(
|
|
request: Request,
|
|
source_type: str = Query(default="all"),
|
|
tree: int = Query(default=0),
|
|
):
|
|
if source_type not in SOURCE_TYPES:
|
|
source_type = "all"
|
|
|
|
uid = _get_user_id(request)
|
|
query = f"{BASE_SELECT} FROM pages p WHERE p.published = 1 AND p.parent_section != 'Trash' AND p.deleted_at IS NULL"
|
|
params: list = []
|
|
if tree:
|
|
query += " AND p.parent_id IS NULL"
|
|
query, params = _apply_source_filter(query, params, source_type)
|
|
query += " ORDER BY p.updated_at DESC LIMIT 50"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/private")
|
|
def library_private(
|
|
request: Request,
|
|
source_type: str = Query(default="all"),
|
|
tree: int = Query(default=0),
|
|
):
|
|
if source_type not in SOURCE_TYPES:
|
|
source_type = "all"
|
|
|
|
uid = _get_user_id(request)
|
|
query = f"{BASE_SELECT} FROM pages p WHERE p.parent_section = 'Private' AND p.deleted_at IS NULL"
|
|
params: list = []
|
|
if tree:
|
|
query += " AND p.parent_id IS NULL"
|
|
query, params = _apply_source_filter(query, params, source_type)
|
|
query += " ORDER BY p.updated_at DESC LIMIT 50"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/children/{page_id:int}")
|
|
def library_children(page_id: int, request: Request):
|
|
"""Return child pages for a given parent page (for tree expansion in Library)."""
|
|
uid = _get_user_id(request)
|
|
with get_conn() as conn:
|
|
rows = conn.execute(
|
|
f"{BASE_SELECT}, (SELECT COUNT(*) FROM pages c WHERE c.parent_id = p.id AND c.deleted_at IS NULL) as child_count "
|
|
f"FROM pages p WHERE p.parent_id = ? AND p.deleted_at IS NULL "
|
|
f"ORDER BY p.title COLLATE NOCASE",
|
|
[page_id],
|
|
).fetchall()
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/repository")
|
|
def library_repository(
|
|
request: Request,
|
|
gitea_owner: str = Query(default=""),
|
|
gitea_repo: str = Query(default=""),
|
|
):
|
|
"""Return Gitea repository pages/files for the Library Repository tab."""
|
|
if not gitea_owner or not gitea_repo:
|
|
return {"items": []}
|
|
|
|
uid = _get_user_id(request)
|
|
ws_key = f"{gitea_owner}/{gitea_repo}"
|
|
query = f"{BASE_SELECT} FROM pages p WHERE p.workspace = ? AND p.deleted_at IS NULL"
|
|
params = [ws_key]
|
|
query += " ORDER BY p.updated_at DESC LIMIT 100"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|
|
|
|
|
|
@router.get("/workspace")
|
|
def library_workspace(
|
|
request: Request,
|
|
source_type: str = Query(default="all"),
|
|
tree: int = Query(default=0),
|
|
workspace_id: int = Query(default=0),
|
|
):
|
|
if source_type not in SOURCE_TYPES:
|
|
source_type = "all"
|
|
|
|
uid = _get_user_id(request)
|
|
query = f"{BASE_SELECT} FROM pages p WHERE (p.workspace != '' OR p.workspace_id IS NOT NULL) AND p.parent_section != 'Trash' AND p.deleted_at IS NULL"
|
|
params: list = []
|
|
|
|
if tree:
|
|
query += " AND p.parent_id IS NULL"
|
|
|
|
if workspace_id:
|
|
query += " AND p.workspace_id = ?"
|
|
params.append(workspace_id)
|
|
|
|
query, params = _apply_source_filter(query, params, source_type)
|
|
query += " ORDER BY p.updated_at DESC LIMIT 50"
|
|
|
|
with get_conn() as conn:
|
|
rows = conn.execute(query, params).fetchall()
|
|
|
|
items = _rows_to_items(rows, uid)
|
|
_enrich_children(items)
|
|
return {"items": items}
|