From b112ee366001b819521e54cace1367829e08a9a0 Mon Sep 17 00:00:00 2001 From: lichenblankie Date: Mon, 30 Mar 2026 22:36:58 +0000 Subject: [PATCH] added reticulum hash option to add page --- db.py | 17 +++++++++++------ handlers.py | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 19 deletions(-) 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..a5e66d4 100644 --- a/handlers.py +++ b/handlers.py @@ -341,9 +341,12 @@ def handle_search(query): def handle_add_form(msg=""): return _respond( f"

add url

" + f"

Add a site via URL or Reticulum destination hash

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

' + f'
' + f'or
' + f'

' f'

' f'

' f'' @@ -357,15 +360,25 @@ def handle_add_submit(body): url = clean_url(body.get("url", [""])[0].strip()) note = body.get("note", [""])[0].strip() tags = body.get("tags", [""])[0].strip() + reticulum_dest = body.get("reticulum_dest", [""])[0].strip().replace("<", "").replace(">", "") - if not url: - return handle_add_form("URL is required.") - if not url.startswith(("http://", "https://")): + if reticulum_dest and url: + return handle_add_form("Please provide either a URL or a Reticulum destination hash, not both.") + + if not url and not reticulum_dest: + return handle_add_form("URL or Reticulum destination hash is required.") + + if reticulum_dest and (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.") + + if url and not url.startswith(("http://", "https://")): return handle_add_form("URL must start with http:// or https://") - # Try auto-index first + if reticulum_dest and not url: + url = f"reticulum:{reticulum_dest}" + try: - title = index_url(url, note) + title = index_url(url, note, reticulum_dest) if tags: db = get_db() try: @@ -375,7 +388,9 @@ def handle_add_submit(body): db.commit() finally: return_db(db) - return handle_add_form(f'Indexed: {esc(title)}') + + display_url = url if url.startswith("http") else reticulum_dest + return handle_add_form(f'Indexed: {esc(display_url)}') except ValueError as e: return handle_add_form(f"Error: {esc(str(e))}") @@ -394,6 +409,7 @@ def handle_add_submit(body): f'' f'' f'' + f'' f'
' f'

' f'
' @@ -409,11 +425,16 @@ def handle_add_manual_submit(body): url = clean_url(body.get("url", [""])[0].strip()) note = body.get("note", [""])[0].strip() tags = body.get("tags", [""])[0].strip() + reticulum_dest = body.get("reticulum_dest", [""])[0].strip() manual_title = body.get("manual_title", [""])[0].strip() manual_desc = body.get("manual_description", [""])[0].strip() - if not url: - return handle_add_form("URL is required.") + if not url and not reticulum_dest: + return handle_add_form("URL or Reticulum destination hash is required.") + + if not url and reticulum_dest: + url = f"reticulum:{reticulum_dest}" + if not manual_title or not manual_desc: return handle_add_form("Title and description are required for manual entry.") @@ -421,12 +442,11 @@ 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 (?, ?, ?, ?, ?, ?) " + "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, manual_title, manual_desc, note, now, manual_desc[:200]), + "note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary, reticulum_dest=excluded.reticulum_dest", + (url, manual_title, manual_desc, note, now, manual_desc[:200], reticulum_dest), ) # Get the page ID