tag clouds by user space

This commit is contained in:
2026-09-24 13:57:34 -07:00
parent 832168e845
commit 32f382353e
2 changed files with 13 additions and 4 deletions

View File

@@ -223,8 +223,8 @@ async def get_attachment(username: str, filename: str, _: None = Depends(require
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@app.get("/api/tags") @app.get("/api/tags")
async def tags(_: None = Depends(require_access)): async def tags(request: Request, _: None = Depends(require_access)):
return await notes.tag_counts() return await notes.tag_counts(current_user(request))
@app.get("/api/search") @app.get("/api/search")

View File

@@ -315,14 +315,23 @@ async def _attach_meta(notes: list[dict]) -> None:
note.pop("path", 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: async with aiosqlite.connect(settings.database_path) as db:
db.row_factory = aiosqlite.Row 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( rows = await db.execute(
"SELECT t.name, COUNT(nt.note_id) AS count" "SELECT t.name, COUNT(nt.note_id) AS count"
" FROM tags t JOIN note_tags nt ON nt.tag_id = t.id" " 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" " 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()] return [dict(r) for r in await rows.fetchall()]