Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
2own recent activity: a client-rendered fold over the record-change ledger.
3
4The server renders only the thin shell (breadcrumb, /style, /nav); the
5browser fetches /api/activity (scoped to the caller by session, see
6object_activity.recent_activity and object_server._handle_activity) and
7renders rows "actor ACTION collection title relative-time", reusing the
8same .listrow markup/CSS the /list generator uses (app-theme/objects/site/
9list.py) so a feed row looks like any other row in the app, and the same
10relative-time algorithm as that generator's `relDate` helper (not imported
11-- list.py doesn't export it -- but copied verbatim; see that file's
12docstring for the shape being matched).
13
14No live websocket subscription: the feed can include any collection that
15has a record-change log, and that set grows as packages install. Rather
16than hardcode (and keep in sync) a list of "high-traffic" collections to
17subscribe to -- which would silently miss activity in every collection not
18named -- this polls /api/activity every 30s. Simpler, and correct for any
19collection 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"""
28const esc = (s) => String(s == null ? "" : s).replace(/[&<>"']/g,
29 (c) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
30const el = (id) => document.getElementById(id);
31
32// Same relative-time shape as app-theme/objects/site/list.py's relDate.
33function 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.
46const RECORD_LINKS = {
47 notes: (id) => "/notes/" + id,
48 invoices: (id) => "/invoices/" + id,
49 articles: (id) => "/articles/" + id,
50 views: (id) => "/views/" + id,
51};
52
53function 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
68async 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
79load();
80setInterval(load, 30000);
81"""
82
83
84def 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&hellip;</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}