diff --git a/db.py b/db.py index a0a3580..4943855 100644 --- a/db.py +++ b/db.py @@ -131,7 +131,8 @@ def init_db(): " title TEXT," " body TEXT," " note TEXT DEFAULT ''," - " last_modified TEXT DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now'))" + " last_modified TEXT DEFAULT (strftime('%Y-%m-%dT%H:%M:%S','now'))," + " reticulum_dest TEXT DEFAULT ''" ")" ) db.execute( @@ -247,6 +248,11 @@ def init_db(): db.execute("ALTER TABLE pages ADD COLUMN summary TEXT DEFAULT ''") db.commit() + # Migrate pages: add reticulum_dest column if missing + if "reticulum_dest" not in page_cols: + db.execute("ALTER TABLE pages ADD COLUMN reticulum_dest TEXT DEFAULT ''") + db.commit() + # Chunks table for semantic search embeddings db.execute( "CREATE TABLE IF NOT EXISTS chunks (" @@ -359,19 +365,18 @@ def fetch_page(url): -def index_url(url, note=""): +def index_url(url, note="", reticulum_dest=""): url = clean_url(url) title, body, links, meta_desc = fetch_page(url) - # Use meta description if available and meaningful, otherwise generate from body summary = meta_desc if meta_desc and len(meta_desc) > 20 else "" db = get_db() try: now = __import__("datetime").datetime.now().strftime("%Y-%m-%dT%H:%M:%S") db.execute( - "INSERT INTO pages (url, title, body, note, last_modified, summary) VALUES (?, ?, ?, ?, ?, ?) " + "INSERT INTO pages (url, title, body, note, last_modified, summary, reticulum_dest) VALUES (?, ?, ?, ?, ?, ?, ?) " "ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, " - "note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary", - (url, title, body, note, now, summary), + "note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary, reticulum_dest=excluded.reticulum_dest", + (url, title, body, note, now, summary, reticulum_dest), ) page_id = db.execute("SELECT id FROM pages WHERE url = ?", (url,)).fetchone()[0] db.execute("DELETE FROM links WHERE page_id = ?", (page_id,)) diff --git a/handlers.py b/handlers.py index 5903d0b..1665cae 100644 --- a/handlers.py +++ b/handlers.py @@ -338,9 +338,23 @@ def handle_search(query): ) -def handle_add_form(msg=""): +def handle_add_form(msg="", action_type="index"): + if action_type == "subscribe": + return _respond( + f"

subscribe

" + f"

Subscribe to a friend's TinyWeb instance to sync their index

" + f'
' + f'{_csrf_field()}' + f'

' + f'' + f"
" + f"

or add a single site

" + f"

{msg}

" + f'back' + ) return _respond( f"

add url

" + f"

Add a site to your index

" f'
' f'{_csrf_field()}' f'

' @@ -354,18 +368,27 @@ def handle_add_form(msg=""): def handle_add_submit(body): - url = clean_url(body.get("url", [""])[0].strip()) + input_type = body.get("input_type", ["url"])[0] + url = body.get("url", [""])[0].strip() + reticulum_dest = body.get("reticulum_dest", [""])[0].strip().replace("<", "").replace(">", "") note = body.get("note", [""])[0].strip() tags = body.get("tags", [""])[0].strip() - if not url: - return handle_add_form("URL is required.") - if not url.startswith(("http://", "https://")): - return handle_add_form("URL must start with http:// or https://") + if input_type == "url": + if not url: + return handle_add_form("URL is required.") + url = clean_url(url) + if not url.startswith(("http://", "https://")): + return handle_add_form("URL must start with http:// or https://") + else: + if not reticulum_dest: + return handle_add_form("Reticulum destination hash is required.") + if len(reticulum_dest) != 32 or not all(c in "0123456789abcdefABCDEF" for c in reticulum_dest): + return handle_add_form("Invalid reticulum destination hash. Must be 32 hex characters.") + url = f"reticulum:{reticulum_dest}" - # Try auto-index first try: - title = index_url(url, note) + title = index_url(url, note, reticulum_dest if reticulum_dest else "") if tags: db = get_db() try: @@ -375,7 +398,8 @@ def handle_add_submit(body): db.commit() finally: return_db(db) - return handle_add_form(f'Indexed: {esc(title)}') + + return handle_add_form(f'Indexed: {esc(url)}') except ValueError as e: return handle_add_form(f"Error: {esc(str(e))}") @@ -414,6 +438,7 @@ def handle_add_manual_submit(body): if not url: return handle_add_form("URL is required.") + if not manual_title or not manual_desc: return handle_add_form("Title and description are required for manual entry.") @@ -421,7 +446,6 @@ def handle_add_manual_submit(body): try: now = __import__("datetime").datetime.now().strftime("%Y-%m-%dT%H:%M:%S") - # Insert the page db.execute( "INSERT INTO pages (url, title, body, note, last_modified, summary) VALUES (?, ?, ?, ?, ?, ?) " "ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, " @@ -455,6 +479,8 @@ def handle_add_manual_submit(body): def handle_pages(query=None): + msg = query.get("msg", [""])[0] if query else "" + msg_html = f'

{esc(msg)}

' if msg else "" page = _paginate(query or {}) offset = (page - 1) * BROWSE_PER_PAGE db = get_db() @@ -482,6 +508,7 @@ def handle_pages(query=None): return_db(db) return _respond( f"

indexed pages ({total})

" + f"{msg_html}" f"" f'{_page_nav(page, total, "/pages", BROWSE_PER_PAGE)}' f'

export | import

' @@ -492,20 +519,27 @@ def handle_pages(query=None): def handle_edit_form(page_id, msg=""): db = get_db() try: - row = db.execute("SELECT id, url, title, note FROM pages WHERE id = ?", (page_id,)).fetchone() + row = db.execute("SELECT id, url, title, body, note, summary FROM pages WHERE id = ?", (page_id,)).fetchone() if not row: return _error(404) tags = ", ".join(_get_page_tags(page_id, db)) finally: return_db(db) + return _respond( f"

edit page

" f"

{esc(row['title'])}
" f"{esc(row['url'])}

" f'' f'{_csrf_field()}' - f'

' - f'

' + f'
' + f'

' + f'
' + f'

' + f'
' + f'

' + f'
' + f'

' f'' f"
" f"

{msg}

" @@ -514,15 +548,25 @@ def handle_edit_form(page_id, msg=""): def handle_edit_submit(page_id, body): + title = body.get("title", [""])[0].strip() + summary = body.get("summary", [""])[0].strip() note = body.get("note", [""])[0].strip() tags = body.get("tags", [""])[0].strip() + db = get_db() try: - db.execute("UPDATE pages SET note = ? WHERE id = ?", (note, page_id)) + db.execute( + "UPDATE pages SET title = ?, summary = ?, note = ? WHERE id = ?", + (title, summary, note, page_id) + ) + _set_page_tags(page_id, tags, db) + db.commit() + finally: return_db(db) + return _redirect("/pages") @@ -881,6 +925,7 @@ def handle_subscriptions(msg=""): f' ' f'' f'' + f'

or subscribe to an instance

' f'

{msg}

' f'
{listing}' f'
back' @@ -1246,7 +1291,8 @@ def _dispatch_inner(data): if path == "/": return handle_search(query) elif path == "/add": - return handle_add_form() + action_type = query.get("type", ["index"])[0] + return handle_add_form(action_type=action_type if action_type == "subscribe" else "index") elif path == "/pages": return handle_pages(query) elif path.startswith("/edit/"):