tag clouds by user space
This commit is contained in:
@@ -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")
|
||||||
|
|||||||
@@ -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()]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user