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
activity.py · 4.9 KB · 123 lines
| 1 | """Activity feed page -- served at /activity, showing the signed-in user's |
| 2 | own recent activity: a client-rendered fold over the record-change ledger. |
| 3 | |
| 4 | The server renders only the thin shell (breadcrumb, /style, /nav); the |
| 5 | browser fetches /api/activity (scoped to the caller by session, see |
| 6 | object_activity.recent_activity and object_server._handle_activity) and |
| 7 | renders rows "actor ACTION collection title relative-time", reusing the |
| 8 | same .listrow markup/CSS the /list generator uses (app-theme/objects/site/ |
| 9 | list.py) so a feed row looks like any other row in the app, and the same |
| 10 | relative-time algorithm as that generator's `relDate` helper (not imported |
| 11 | -- list.py doesn't export it -- but copied verbatim; see that file's |
| 12 | docstring for the shape being matched). |
| 13 | |
| 14 | No live websocket subscription: the feed can include any collection that |
| 15 | has a record-change log, and that set grows as packages install. Rather |
| 16 | than hardcode (and keep in sync) a list of "high-traffic" collections to |
| 17 | subscribe to -- which would silently miss activity in every collection not |
| 18 | named -- this polls /api/activity every 30s. Simpler, and correct for any |
| 19 | collection without the page needing to know the collection set. |
| 20 | """ |
| 21 | |
| 22 | _STYLE = """ |
| 23 | .feed .listrow .rowtitle .verb { color: var(--accent-strong); } |
| 24 | .feed .listrow .rowtitle .coll { color: var(--muted); font-weight: 400; } |
| 25 | """ |
| 26 | |
| 27 | _SCRIPT = r""" |
| 28 | const esc = (s) => String(s == null ? "" : s).replace(/[&<>"']/g, |
| 29 | (c) => ({"&": "&", "<": "<", ">": ">", '"': """, "'": "'"}[c])); |
| 30 | const el = (id) => document.getElementById(id); |
| 31 | |
| 32 | // Same relative-time shape as app-theme/objects/site/list.py's relDate. |
| 33 | function relDate(iso) { |
| 34 | if (!iso) return ""; |
| 35 | const d = new Date(iso); if (isNaN(d)) return ""; |
| 36 | const ms = Date.now() - d.getTime(); |
| 37 | if (ms < 3600000) { const m = Math.floor(ms / 60000); return m < 1 ? "just now" : m + "m ago"; } |
| 38 | if (ms < 86400000) return Math.floor(ms / 3600000) + "h ago"; |
| 39 | if (ms < 7 * 86400000) return Math.floor(ms / 86400000) + "d ago"; |
| 40 | return d.toLocaleDateString(undefined, {month: "short", day: "numeric"}); |
| 41 | } |
| 42 | |
| 43 | // Best-effort permalink for collections known (by convention, see each |
| 44 | // package's docstring) to have a /collection/{id} record page. Not |
| 45 | // exhaustive -- collections missing here just render as plain text. |
| 46 | const RECORD_LINKS = { |
| 47 | notes: (id) => "/notes/" + id, |
| 48 | invoices: (id) => "/invoices/" + id, |
| 49 | articles: (id) => "/articles/" + id, |
| 50 | views: (id) => "/views/" + id, |
| 51 | }; |
| 52 | |
| 53 | function row(entry) { |
| 54 | const actor = entry.actor || "unattributed"; |
| 55 | const verb = String(entry.action || "").toUpperCase(); |
| 56 | const av = actor.trim().charAt(0).toUpperCase() || "?"; |
| 57 | const link = RECORD_LINKS[entry.collection]; |
| 58 | const titleHtml = link |
| 59 | ? '<a href="' + esc(link(entry.record_id)) + '" target="_blank" rel="noopener">' + esc(entry.title) + '</a>' |
| 60 | : esc(entry.title); |
| 61 | return '<div class="listrow"><div class="av">' + esc(av) + '</div><div class="body">' |
| 62 | + '<div class="rowtitle">' + esc(actor) + ' <span class="verb">' + esc(verb) + '</span> ' |
| 63 | + '<span class="coll">' + esc(entry.collection) + '</span> ' + titleHtml + '</div>' |
| 64 | + '<div class="rowmeta"><span class="when">' + esc(relDate(entry.timestamp)) + '</span></div>' |
| 65 | + '</div></div>'; |
| 66 | } |
| 67 | |
| 68 | async function load() { |
| 69 | const res = await fetch("/api/activity?limit=50", |
| 70 | {credentials: "same-origin", headers: {accept: "application/json"}}); |
| 71 | if (!res.ok) { el("feed").innerHTML = '<div class="state">Could not load activity.</div>'; return; } |
| 72 | const body = await res.json(); |
| 73 | const entries = body.activity || []; |
| 74 | el("feed").innerHTML = entries.length |
| 75 | ? entries.map(row).join("") |
| 76 | : '<div class="state">Nothing yet.</div>'; |
| 77 | } |
| 78 | |
| 79 | load(); |
| 80 | setInterval(load, 30000); |
| 81 | """ |
| 82 | |
| 83 | |
| 84 | def GET(request): |
| 85 | identity = request.get("_identity", {}) |
| 86 | user_id = identity.get("user_id") |
| 87 | _logger.info("site_activity served", user_id=user_id or "anonymous") |
| 88 | |
| 89 | if not user_id: |
| 90 | body = '<p class="hint"><a href="/login?next=/activity">Sign in</a> to see your activity.</p>' |
| 91 | script = "" |
| 92 | else: |
| 93 | body = """ |
| 94 | <div class="breadcrumb"><a href="/">Home</a> / Activity</div> |
| 95 | <div class="pagehead"><h1>Activity</h1></div> |
| 96 | <div class="feed" id="feed"><div class="state">loading…</div></div> |
| 97 | """ |
| 98 | script = f"<script>{_SCRIPT}</script>" |
| 99 | |
| 100 | who = ( |
| 101 | f"signed in as <strong>{user_id}</strong>" |
| 102 | if user_id |
| 103 | else '<a href="/login?next=/activity">sign in</a>' |
| 104 | ) |
| 105 | html = f"""<!doctype html> |
| 106 | <html lang="en"> |
| 107 | <head> |
| 108 | <meta charset="utf-8"> |
| 109 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 110 | <title>Activity</title> |
| 111 | <link rel="stylesheet" href="/style"> |
| 112 | <style>{_STYLE}</style> |
| 113 | </head> |
| 114 | <body> |
| 115 | <div class="wrap"> |
| 116 | <header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header> |
| 117 | {body} |
| 118 | </div> |
| 119 | {script} |
| 120 | <script src="/nav"></script> |
| 121 | </body> |
| 122 | </html>""" |
| 123 | return {"content_type": "text/html; charset=utf-8", "body": html} |