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
feed.py · 7.0 KB · 177 lines
1"""64 - Feed page: a client-rendered view over an already-composed,
2already-permission-gated read.
3
4No new data access of its own. GET /api/feed (object_server.py's
5_handle_feed) does the entire composition -- who the viewer follows, then
6each declared feed-source collection's own permission-gated
7/collections/{c}/records read filtered to that follow set -- so this page
8only has to fetch that one endpoint with the visitor's own session cookie
9and render the result. Same identity-aware, browser-polls-its-own-API
10pattern as app-dashboard/objects/site/dashboard.py and
11app-worker/objects/site/profile.py, and the same shared chrome (/style,
12/nav) as every other /style page in this codebase.
13
14Served at /feed by the single-segment site-object convention
15(/feed -> site_feed), the same convention app-dashboard's site_dashboard
16(-> /dashboard) and app-activity's site_activity (-> /activity) already
17rely on -- no site_routes record is seeded or required. The JSON
18composition itself lives at /api/feed, not bare /feed, because the bare
19path is this page's own route: mirrors the existing /api/activity (data)
20vs /activity (page, site_activity) split in this same dispatch, which the
21plan/vocabulary/64-feed-spec.md's own citation of activity as feed's
22closest sibling primitive makes the natural precedent to follow rather
23than colliding both surfaces on one path.
24
25Not rendered for anonymous visitors (per the spec's Surfaces section) --
26an anonymous request to /api/feed comes back {authenticated: false,
27items: []} rather than an error, and this page shows a sign-in prompt in
28that case instead of an empty list, so "nothing to show yet" and "sign in
29to see your feed" never look identical.
30"""
31
32_STYLE = """
33.feedcard { border: 1px solid var(--line, #38384a); border-radius: 8px; padding: 0.9rem 1rem;
34 margin-bottom: 0.75rem; }
35.feedcard .feedtitle { font-weight: 600; margin-bottom: 0.2rem; }
36.feedcard .feedtitle a { color: inherit; text-decoration: none; }
37.feedcard .feedtitle a:hover { text-decoration: underline; }
38.feedcard .feedsummary { color: var(--text); white-space: pre-wrap; word-break: break-word; }
39.feedcard .feedmeta { color: var(--muted); font-size: 0.8rem; margin-top: 0.35rem; }
40.feedmeta .coll { text-transform: uppercase; letter-spacing: 0.03em; }
41.feed-truncated { color: var(--muted); font-size: 0.8rem; margin-bottom: 0.75rem; }
42"""
43
44_SCRIPT = r"""
45const esc = (s) => String(s == null ? "" : s).replace(/[&<>"']/g,
46 (c) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
47const el = (id) => document.getElementById(id);
48
49// Only articles is wired as a feed source in this slice (see
50// packages/app-articles/schemas/articles.json's blocks.feed); a future
51// feed-source collection with no entry here still renders, just without
52// a clickable title, same "degrade, don't error" posture as the server
53// side of this feature.
54const SOURCE_URL = {
55 articles: (id) => "/articles/" + encodeURIComponent(id),
56};
57
58function relDate(iso) {
59 if (!iso) return "";
60 const d = new Date(iso); if (isNaN(d)) return String(iso);
61 const ms = Date.now() - d.getTime();
62 if (ms < 0) return d.toLocaleDateString(undefined, {month: "short", day: "numeric"});
63 if (ms < 3600000) { const m = Math.floor(ms / 60000); return m < 1 ? "just now" : m + "m ago"; }
64 if (ms < 86400000) return Math.floor(ms / 3600000) + "h ago";
65 if (ms < 7 * 86400000) return Math.floor(ms / 86400000) + "d ago";
66 return d.toLocaleDateString(undefined, {month: "short", day: "numeric"});
67}
68
69function feedCard(item) {
70 const urlFn = SOURCE_URL[item.source_collection];
71 const url = urlFn ? urlFn(item.source_id) : null;
72 const titleText = esc(item.summary || item.source_id || "(untitled)");
73 const title = url
74 ? '<a href="' + esc(url) + '">' + titleText + "</a>"
75 : titleText;
76 return '<div class="feedcard">'
77 + '<div class="feedtitle">' + title + "</div>"
78 + '<div class="feedmeta">'
79 + '<span class="who">' + esc(item.author_id || "unknown") + "</span> &middot; "
80 + '<span class="when">' + esc(relDate(item.time)) + "</span> &middot; "
81 + '<span class="coll">' + esc(item.source_collection || "") + "</span>"
82 + "</div></div>";
83}
84
85async function loadFeed() {
86 let res;
87 try {
88 res = await fetch("/api/feed", {credentials: "same-origin", headers: {accept: "application/json"}});
89 } catch (e) {
90 el("feed-body").innerHTML = '<p class="hint">Could not load your feed.</p>';
91 return;
92 }
93 if (!res.ok) {
94 el("feed-body").innerHTML = '<p class="hint">Could not load your feed.</p>';
95 return;
96 }
97 const body = await res.json();
98 if (body.enabled === false) {
99 el("feed-body").innerHTML = '<p class="hint">The feed is turned off right now.</p>';
100 return;
101 }
102 const items = body.items || [];
103 let html = "";
104 if (body.truncated_following) {
105 html += '<div class="feed-truncated">You follow more accounts than one feed read can '
106 + "scan at once, so this page only covers the first ones.</div>";
107 }
108 html += items.length
109 ? items.map(feedCard).join("")
110 : '<p class="hint">Nothing yet from the accounts you follow.</p>';
111 el("feed-body").innerHTML = html;
112}
113
114loadFeed();
115
116// Realtime: refresh when a declared feed-source collection changes. Only
117// articles is wired in this slice; a future source collection just adds
118// its name here alongside its blocks.feed schema entry.
119(function () {
120 let _lt = null;
121 const reload = () => { clearTimeout(_lt); _lt = setTimeout(loadFeed, 150); };
122 (function wait() {
123 if (window.dbbasicSubscribe) {
124 window.dbbasicSubscribe("follows", reload);
125 window.dbbasicSubscribe("articles", reload);
126 } else setTimeout(wait, 300);
127 })();
128})();
129"""
130
131
132def GET(request):
133 identity = request.get("_identity", {})
134 user_id = identity.get("user_id")
135 _logger.info("site_feed served", user_id=user_id or "anonymous")
136
137 if not user_id:
138 body = (
139 '<div class="breadcrumb">Home / Feed</div>'
140 '<div class="pagehead"><h1>Feed</h1></div>'
141 '<div class="hint">Your feed shows recent public posts from the accounts you '
142 'follow, once you sign in. <a href="/login?next=/feed">Sign in</a> to see yours.'
143 "</div>"
144 )
145 script = ""
146 else:
147 body = """
148<div class="breadcrumb"><a href="/">Home</a> / Feed</div>
149<div class="pagehead"><h1>Feed</h1></div>
150<div id="feed-body"><div class="state">loading&hellip;</div></div>
151"""
152 script = f"<script>{_SCRIPT}</script>"
153
154 who = (
155 f"signed in as <strong>{user_id}</strong>"
156 if user_id
157 else '<a href="/login?next=/feed">sign in</a>'
158 )
159 html = f"""<!doctype html>
160<html lang="en">
161<head>
162<meta charset="utf-8">
163<meta name="viewport" content="width=device-width, initial-scale=1">
164<title>Feed</title>
165<link rel="stylesheet" href="/style">
166<style>{_STYLE}</style>
167</head>
168<body>
169<div class="wrap">
170<header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header>
171{body}
172</div>
173{script}
174<script src="/nav"></script>
175</body>
176</html>"""
177 return {"content_type": "text/html; charset=utf-8", "body": html}