askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
inbox.py · 4.3 KB · 108 lines
| 1 | """Inbox page — built from metadata, not markup. |
| 2 | |
| 3 | Lists the signed-in owner's message_threads via the shared list generator |
| 4 | (/list -> window.dbbasicList), same shape as app-invoices' invoices.py and |
| 5 | app-forum's forum.py: a breadcrumb, a toolbar, a mount point, nothing |
| 6 | hand-rendered per row. Sorting reuses the generator's built-in |
| 7 | newest/oldest toggle, which sorts by created_at (thread creation order) — |
| 8 | window.dbbasicList's sort is hardcoded to that field, not configurable per |
| 9 | page (see packages/app-theme/objects/site/list.py's sortList). Ordering by |
| 10 | last_message_at (most-recently-active thread first) would need a generator |
| 11 | enhancement; out of scope for a data-model package. last_message_at is |
| 12 | still carried on every thread record and shown in each row's subtitle. |
| 13 | |
| 14 | The "Save Draft" button opens a message_drafts create form with no |
| 15 | thread_id preset (a brand-new compose, not yet attached to any thread) — |
| 16 | see dbbasic-package.json: nothing is ever sent from this package, drafts |
| 17 | are data only. Served through a site route /inbox -> site_inbox, |
| 18 | documented in dbbasic-package.json, not seeded, same precedent as |
| 19 | app-invoices' /invoices/{invoice_id:uuid}. |
| 20 | |
| 21 | A mailbox is private (see permissions/rules.json: owner-scoped CRUD, no |
| 22 | public read on any collection in this package). Anonymous visitors get a |
| 23 | sign-in prompt only. |
| 24 | """ |
| 25 | |
| 26 | _SCRIPT = """ |
| 27 | const panel = document.getElementById("draftpanel"); |
| 28 | const list = window.dbbasicList("message_threads", { |
| 29 | mount: "#list", search: "#search", sort: "#sort", owner: OWNER_ID, |
| 30 | title: (r) => r.subject || "(no subject)", |
| 31 | href: (r) => "/inbox/" + r.id, |
| 32 | subtitle: (r) => { |
| 33 | const bits = []; |
| 34 | if (r.participant_summary) bits.push(r.participant_summary); |
| 35 | bits.push((r.message_count || "0") + " messages"); |
| 36 | if (r.is_read !== "true") bits.push("unread"); |
| 37 | if (r.is_starred === "true") bits.push("starred"); |
| 38 | if (r.is_archived === "true") bits.push("archived"); |
| 39 | return bits.join(" \\u00b7 "); |
| 40 | }, |
| 41 | created: (r) => r.last_message_at || r.created_at, |
| 42 | }); |
| 43 | function openDraftForm() { |
| 44 | document.getElementById("drafttitle").textContent = "Save Draft"; |
| 45 | panel.style.display = "block"; |
| 46 | window.dbbasicForm("message_drafts", { |
| 47 | mount: "#draftformmount", owner: OWNER_ID, |
| 48 | onSaved: () => { panel.style.display = "none"; }, |
| 49 | onCancel: () => { panel.style.display = "none"; }, |
| 50 | }); |
| 51 | } |
| 52 | document.getElementById("adddraft").addEventListener("click", openDraftForm); |
| 53 | """ |
| 54 | |
| 55 | |
| 56 | def GET(request): |
| 57 | identity = request.get("_identity", {}) |
| 58 | user_id = identity.get("user_id") |
| 59 | _logger.info("site_inbox served", user_id=user_id or "anonymous") |
| 60 | |
| 61 | if not user_id: |
| 62 | body = '<p class="hint"><a href="/login?next=/inbox">Sign in</a> to see your inbox.</p>' |
| 63 | script = "" |
| 64 | else: |
| 65 | body = """ |
| 66 | <div class="breadcrumb"><a href="/">Home</a> / Inbox</div> |
| 67 | <div class="pagehead"><h1>Inbox</h1><button class="btn primary" id="adddraft">+ Save Draft</button></div> |
| 68 | <p class="hint">Save Draft only saves a draft record -- nothing is sent. Composing and sending |
| 69 | a real message needs mail transport (IMAP/SMTP), which this package does not build.</p> |
| 70 | <div id="draftpanel" style="display:none; margin-bottom:1rem"> |
| 71 | <h2 id="drafttitle" style="font-size:1rem; margin:0 0 0.5rem">Save Draft</h2> |
| 72 | <div id="draftformmount"></div> |
| 73 | </div> |
| 74 | <div class="toolbar"> |
| 75 | <input class="search grow" id="search" placeholder="Search threads…" autocomplete="off"> |
| 76 | <select id="sort"><option value="newest">Newest</option><option value="oldest">Oldest</option></select> |
| 77 | </div> |
| 78 | <div id="list"><div class="state">loading…</div></div> |
| 79 | """ |
| 80 | script = ( |
| 81 | f"<script>const OWNER_ID = {user_id!r};</script>" |
| 82 | '<script src="/list"></script><script src="/form"></script>' |
| 83 | f"<script>{_SCRIPT}</script>" |
| 84 | ) |
| 85 | |
| 86 | who = ( |
| 87 | f"signed in as <strong>{user_id}</strong>" |
| 88 | if user_id |
| 89 | else '<a href="/login?next=/inbox">sign in</a>' |
| 90 | ) |
| 91 | html = f"""<!doctype html> |
| 92 | <html lang="en"> |
| 93 | <head> |
| 94 | <meta charset="utf-8"> |
| 95 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 96 | <title>Inbox</title> |
| 97 | <link rel="stylesheet" href="/style"> |
| 98 | </head> |
| 99 | <body> |
| 100 | <div class="wrap"> |
| 101 | <header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header> |
| 102 | {body} |
| 103 | </div> |
| 104 | {script} |
| 105 | <script src="/nav"></script> |
| 106 | </body> |
| 107 | </html>""" |
| 108 | return {"content_type": "text/html; charset=utf-8", "body": html} |