diff --git a/backend/main.py b/backend/main.py index ac9b1fc..e1fe7d9 100644 --- a/backend/main.py +++ b/backend/main.py @@ -223,8 +223,8 @@ async def get_attachment(username: str, filename: str, _: None = Depends(require # --------------------------------------------------------------------------- @app.get("/api/tags") -async def tags(_: None = Depends(require_access)): - return await notes.tag_counts() +async def tags(request: Request, _: None = Depends(require_access)): + return await notes.tag_counts(current_user(request)) @app.get("/api/search") diff --git a/backend/notes.py b/backend/notes.py index eab55aa..dd7147a 100644 --- a/backend/notes.py +++ b/backend/notes.py @@ -315,14 +315,23 @@ async def _attach_meta(notes: list[dict]) -> None: note.pop("path", None) -async def tag_counts() -> list[dict]: +async def tag_counts(username: str) -> list[dict]: async with aiosqlite.connect(settings.database_path) as db: db.row_factory = aiosqlite.Row + names = ( + [username, settings.common_space] + if username != settings.common_space + else [settings.common_space] + ) + placeholders = ",".join("?" for _ in names) rows = await db.execute( "SELECT t.name, COUNT(nt.note_id) AS count" " FROM tags t JOIN note_tags nt ON nt.tag_id = t.id" " JOIN notes n ON n.id = nt.note_id AND n.deleted_at IS NULL" - " GROUP BY t.id ORDER BY count DESC, t.name LIMIT 50" + " JOIN users u ON u.id = n.user_id" + f" WHERE u.name IN ({placeholders})" + " GROUP BY t.id ORDER BY count DESC, t.name LIMIT 50", + names, ) return [dict(r) for r in await rows.fetchall()]