Skip to content

Commit be69c00

Browse files
committed
add user page
1 parent 9dab647 commit be69c00

4 files changed

Lines changed: 100 additions & 21 deletions

File tree

deps/publish/src/logseq/publish/meta_store.cljs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"content_length INTEGER,"
2121
"r2_key TEXT NOT NULL,"
2222
"owner_sub TEXT,"
23+
"owner_username TEXT,"
2324
"created_at INTEGER,"
2425
"updated_at INTEGER,"
2526
"password_hash TEXT,"
@@ -33,6 +34,8 @@
3334
(publish-common/sql-exec sql "ALTER TABLE pages ADD COLUMN page_tags TEXT;"))
3435
(when-not (contains? col-names "short_id")
3536
(publish-common/sql-exec sql "ALTER TABLE pages ADD COLUMN short_id TEXT;"))
37+
(when-not (contains? col-names "owner_username")
38+
(publish-common/sql-exec sql "ALTER TABLE pages ADD COLUMN owner_username TEXT;"))
3639
(when-not (contains? col-names "password_hash")
3740
(publish-common/sql-exec sql "ALTER TABLE pages ADD COLUMN password_hash TEXT;")))
3841
(let [cols (publish-common/get-sql-rows (publish-common/sql-exec sql "PRAGMA table_info(page_refs);"))
@@ -110,11 +113,12 @@
110113
"content_length,"
111114
"r2_key,"
112115
"owner_sub,"
116+
"owner_username,"
113117
"created_at,"
114118
"updated_at,"
115119
"short_id,"
116120
"password_hash"
117-
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
121+
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
118122
" ON CONFLICT(graph_uuid, page_uuid) DO UPDATE SET"
119123
" page_uuid=excluded.page_uuid,"
120124
" page_title=excluded.page_title,"
@@ -125,6 +129,7 @@
125129
" content_length=excluded.content_length,"
126130
" r2_key=excluded.r2_key,"
127131
" owner_sub=excluded.owner_sub,"
132+
" owner_username=excluded.owner_username,"
128133
" updated_at=excluded.updated_at,"
129134
" short_id=excluded.short_id,"
130135
" password_hash=excluded.password_hash;")
@@ -138,6 +143,7 @@
138143
(aget body "content_length")
139144
(aget body "r2_key")
140145
(aget body "owner_sub")
146+
(aget body "owner_username")
141147
(aget body "created_at")
142148
(aget body "updated_at")
143149
(aget body "short_id")
@@ -250,6 +256,19 @@
250256
row (first rows)]
251257
(publish-common/json-response {:page (when row (js->clj row :keywordize-keys false))}))
252258

259+
(= (nth parts 1 nil) "user")
260+
(let [raw-username (nth parts 2 nil)
261+
username (when raw-username (js/decodeURIComponent raw-username))
262+
rows (publish-common/get-sql-rows
263+
(publish-common/sql-exec sql
264+
(str "SELECT page_uuid, page_title, short_id, graph_uuid, updated_at, owner_username "
265+
"FROM pages WHERE owner_username = ? ORDER BY updated_at DESC;")
266+
username))]
267+
(publish-common/json-response {:user {:username username}
268+
:pages (map (fn [row]
269+
(js->clj row :keywordize-keys false))
270+
rows)}))
271+
253272
(= (nth parts 4 nil) "password")
254273
(let [rows (publish-common/get-sql-rows
255274
(publish-common/sql-exec sql
@@ -294,7 +313,7 @@
294313
(let [rows (publish-common/get-sql-rows
295314
(publish-common/sql-exec sql
296315
(str "SELECT page_uuid, page_title, page_tags, short_id, graph_uuid, schema_version, block_count, "
297-
"content_hash, content_length, r2_key, owner_sub, created_at, updated_at "
316+
"content_hash, content_length, r2_key, owner_sub, owner_username, created_at, updated_at "
298317
"FROM pages WHERE graph_uuid = ? AND page_uuid = ? LIMIT 1;")
299318
graph-uuid
300319
page-uuid))
@@ -307,7 +326,7 @@
307326
(let [rows (publish-common/get-sql-rows
308327
(publish-common/sql-exec sql
309328
(str "SELECT page_uuid, page_title, page_tags, short_id, graph_uuid, schema_version, block_count, "
310-
"content_hash, content_length, r2_key, owner_sub, created_at, updated_at "
329+
"content_hash, content_length, r2_key, owner_sub, owner_username, created_at, updated_at "
311330
"FROM pages WHERE graph_uuid = ? ORDER BY updated_at DESC;")
312331
graph-uuid))]
313332
(publish-common/json-response {:pages (map row->meta rows)}))
@@ -316,7 +335,7 @@
316335
(let [rows (publish-common/get-sql-rows
317336
(publish-common/sql-exec sql
318337
(str "SELECT page_uuid, page_title, page_tags, short_id, graph_uuid, schema_version, block_count, "
319-
"content_hash, content_length, r2_key, owner_sub, created_at, updated_at "
338+
"content_hash, content_length, r2_key, owner_sub, owner_username, created_at, updated_at "
320339
"FROM pages ORDER BY updated_at DESC;")))]
321340
(publish-common/json-response {:pages (map row->meta rows)}))))
322341

deps/publish/src/logseq/publish/render.cljs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[logseq.publish.common :as publish-common]
1010
[logseq.publish.model :as publish-model]))
1111

12-
(defonce version 1767019463259)
12+
(defonce version 1767021063531)
1313

1414
(def ref-regex
1515
(js/RegExp. "\\[\\[([0-9a-fA-F-]{36})\\]\\]|\\(\\(([0-9a-fA-F-]{36})\\)\\)" "g"))
@@ -1192,18 +1192,55 @@
11921192
[:h1 "Published pages"]
11931193
(if (seq rows)
11941194
[:ul.page-list
1195-
(for [{:keys [page-uuid page-title href updated-at short-id]} rows]
1195+
(for [{:keys [page-uuid page-title href updated-at]} rows]
11961196
[:li.page-item
11971197
[:div.page-links
1198-
[:a.page-link {:href href} (or page-title page-uuid)]
1199-
(when short-id
1200-
[:a.short-link {:href (str "/s/" short-id)}
1201-
(str "/s/" short-id)])]
1198+
[:a.page-link {:href href} (or page-title page-uuid)]]
12021199
[:span.page-meta (or (format-timestamp updated-at) "")]])]
12031200
[:p "No pages have been published yet."])
12041201
(publish-script)]]]]
12051202
(str "<!doctype html>" (render-hiccup doc))))
12061203

1204+
(defn render-user-html
1205+
[username user pages]
1206+
(let [username (or (aget user "username") username)
1207+
rows (->> pages
1208+
(map (fn [page]
1209+
(let [page-uuid (aget page "page_uuid")
1210+
page-title (aget page "page_title")
1211+
updated-at (aget page "updated_at")
1212+
graph-uuid (aget page "graph_uuid")
1213+
href (str "/page/" graph-uuid "/" page-uuid)
1214+
short-id (aget page "short_id")]
1215+
{:page-uuid page-uuid
1216+
:page-title page-title
1217+
:href href
1218+
:short-id short-id
1219+
:updated-at updated-at
1220+
:graph-uuid graph-uuid})))
1221+
(sort-by (fn [row]
1222+
(or (:updated-at row) 0)))
1223+
reverse)
1224+
title (str "Published by " username)
1225+
doc [:html
1226+
(head-node title nil)
1227+
[:body
1228+
[:main.wrap
1229+
(toolbar-node
1230+
(theme-toggle-node))
1231+
[:h1 title]
1232+
(if (seq rows)
1233+
[:ul.page-list
1234+
(for [{:keys [page-uuid page-title href updated-at]} rows]
1235+
[:li.page-item
1236+
[:div.page-links
1237+
[:a.page-link {:href href} (or page-title page-uuid)]]
1238+
[:span.page-meta
1239+
(or (format-timestamp updated-at) "")]])]
1240+
[:p "No pages have been published yet."])
1241+
(publish-script)]]]]
1242+
(str "<!doctype html>" (render-hiccup doc))))
1243+
12071244
(defn render-tag-html
12081245
[graph-uuid tag-uuid tag-title tag-items]
12091246
(let [rows tag-items
@@ -1245,17 +1282,13 @@
12451282
:let [graph-id (tag-item-val row :graph_uuid)
12461283
page-uuid (tag-item-val row :source_page_uuid)
12471284
page-title (tag-item-val row :source_page_title)
1248-
short-id (tag-item-val row :short_id)
12491285
href (when (and graph-id page-uuid)
12501286
(str "/page/" graph-id "/" page-uuid))]]
12511287
[:li.page-item
12521288
[:div.page-links
12531289
(if href
12541290
[:a.page-ref {:href href} (or page-title page-uuid)]
1255-
[:span (or page-title page-uuid)])
1256-
(when short-id
1257-
[:a.short-link {:href (str "/s/" short-id)}
1258-
(str "/s/" short-id)])]
1291+
[:span (or page-title page-uuid)])]
12591292
[:span.page-meta (or (format-timestamp (tag-item-val row :updated_at)) "")]])]
12601293
[:p "No published pages use this tag yet."])
12611294
(publish-script)]]]]
@@ -1281,17 +1314,13 @@
12811314
:let [graph-id (or (tag-item-val row :graph_uuid) graph-uuid)
12821315
page-uuid (tag-item-val row :source_page_uuid)
12831316
page-title (tag-item-val row :source_page_title)
1284-
short-id (tag-item-val row :short_id)
12851317
href (when (and graph-id page-uuid)
12861318
(str "/page/" graph-id "/" page-uuid))]]
12871319
[:li.page-item
12881320
[:div.page-links
12891321
(if href
12901322
[:a.page-ref {:href href} (or page-title page-uuid)]
1291-
[:span (or page-title page-uuid)])
1292-
(when short-id
1293-
[:a.short-link {:href (str "/s/" short-id)}
1294-
(str "/s/" short-id)])]
1323+
[:span (or page-title page-uuid)])]
12951324
[:span.page-meta (or (format-timestamp (tag-item-val row :updated_at)) "")]])]
12961325
[:p "No published pages reference this yet."])
12971326
(publish-script)]]]]

deps/publish/src/logseq/publish/routes.cljs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@
9696
page-tags (or (:page-tags payload)
9797
(get payload "page-tags"))
9898
short-id (publish-common/short-id-for-page graph-uuid page_uuid)
99+
owner-sub (:owner_sub meta)
100+
owner-username (:owner_username meta)
101+
_ (when-not (and owner-sub owner-username)
102+
(throw (ex-info "owner sub or username is missing"
103+
{:owner-sub owner-sub
104+
:owner-username owner-username})))
99105
payload (clj->js {:page_uuid page_uuid
100106
:page_title page-title
101107
:page_tags (when page-tags
@@ -107,7 +113,8 @@
107113
:content_hash content_hash
108114
:content_length content_length
109115
:r2_key r2-key
110-
:owner_sub (aget claims "sub")
116+
:owner_sub owner-sub
117+
:owner_username owner-username
111118
:created_at created_at
112119
:updated_at (.now js/Date)
113120
:short_id short-id
@@ -649,6 +656,27 @@
649656
#js {"location" location}
650657
(publish-common/cors-headers))}))))))))
651658

659+
(and (string/starts-with? path "/u/") (= method "GET"))
660+
(let [parts (string/split path #"/")
661+
username (nth parts 2 nil)]
662+
(if (string/blank? username)
663+
(publish-common/bad-request "missing username")
664+
(js-await [^js do-ns (aget env "PUBLISH_META_DO")
665+
index-id (.idFromName do-ns "index")
666+
index-stub (.get do-ns index-id)
667+
resp (.fetch index-stub (str "https://publish/user/" username)
668+
#js {:method "GET"})]
669+
(if-not (.-ok resp)
670+
(publish-common/not-found)
671+
(js-await [data (.json resp)
672+
user (aget data "user")
673+
rows (or (aget data "pages") #js [])]
674+
(js/Response.
675+
(publish-render/render-user-html username user rows)
676+
#js {:headers (publish-common/merge-headers
677+
#js {"content-type" "text/html; charset=utf-8"}
678+
(publish-common/cors-headers))}))))))
679+
652680
(and (string/starts-with? path "/pages/") (= method "GET"))
653681
(let [parts (string/split path #"/")]
654682
(cond

src/main/frontend/handler/publish.cljs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[frontend.fs :as fs]
88
[frontend.handler.notification :as notification]
99
[frontend.handler.property :as property-handler]
10+
[frontend.handler.user :as user-handler]
1011
[frontend.image :as image]
1112
[frontend.state :as state]
1213
[frontend.util :as util]
@@ -301,6 +302,8 @@
301302
:compression :none
302303
:content_hash content-hash
303304
:content_length (count body)
305+
:owner_sub (user-handler/user-uuid)
306+
:owner_username (user-handler/username)
304307
:created_at (util/time-ms)}
305308
publish-body (assoc payload :meta publish-meta)
306309
headers (assoc headers "x-publish-meta" (js/JSON.stringify (clj->js publish-meta)))

0 commit comments

Comments
 (0)