snotes updates, actions, attachments, easy user space

This commit is contained in:
2026-09-22 17:31:55 -07:00
parent ce30c6f4f7
commit abfbf3de1f
21 changed files with 669 additions and 53 deletions

View File

@@ -5,6 +5,7 @@ import uuid
import aiosqlite
from backend import actions
from backend.config import settings
from backend.database import get_user_id
@@ -75,6 +76,17 @@ def new_note_id() -> str:
return uuid.uuid4().hex
async def store_attachment(username: str, original_name: str, data: bytes) -> str:
from pathlib import Path
ext = re.sub(r"[^A-Za-z0-9.]", "", Path(original_name or "").suffix)[:16]
filename = new_note_id() + ext
full = Path(settings.data_dir) / username / "attachments" / filename
full.parent.mkdir(parents=True, exist_ok=True)
full.write_bytes(data)
return filename
# ---------------------------------------------------------------------------
# Note CRUD (metadata + tags + FTS)
# ---------------------------------------------------------------------------
@@ -86,6 +98,7 @@ async def create_note(
ip: str | None = None,
ua: str | None = None,
) -> dict:
body = await actions.process_actions(body)
title = extract_title(body)
tags = extract_tags(body)
note_id = new_note_id()
@@ -123,7 +136,9 @@ async def update_note(
body: str,
ip: str | None = None,
ua: str | None = None,
username: str | None = None,
) -> dict | None:
body = await actions.process_actions(body)
title = extract_title(body)
tags = extract_tags(body)
@@ -138,14 +153,31 @@ async def update_note(
if row is None:
return None
await write_note_file(row["username"], note_id, body)
await db.execute(
"UPDATE notes SET title = ?, updated_at = datetime('now') WHERE id = ?",
(title, note_id),
)
new_username = username or row["username"]
user_id = row["user_id"]
rel = row["path"]
if new_username != row["username"]:
new_user_id = await get_user_id(db, new_username)
if new_user_id is None:
raise ValueError(f"unknown user: {new_username}")
await delete_note_file(row["path"])
rel = await write_note_file(new_username, note_id, body)
user_id = new_user_id
await db.execute(
"UPDATE notes SET title = ?, path = ?, user_id = ?,"
" updated_at = datetime('now') WHERE id = ?",
(title, rel, user_id, note_id),
)
else:
await write_note_file(row["username"], note_id, body)
await db.execute(
"UPDATE notes SET title = ?, updated_at = datetime('now') WHERE id = ?",
(title, note_id),
)
await _set_tags(db, note_id, tags)
await _index_note(db, note_id, row["user_id"], title, body)
await _record_history(db, note_id, row["user_id"], "update", ip, ua)
await _index_note(db, note_id, user_id, title, body)
await _record_history(db, note_id, user_id, "update", ip, ua)
await db.commit()
return await get_note(note_id)