Skip to content

Commit c97453d

Browse files
committed
search
1 parent 60e7978 commit c97453d

File tree

6 files changed

+741
-90
lines changed

6 files changed

+741
-90
lines changed

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

Lines changed: 130 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns logseq.publish.meta-store
2-
(:require [clojure.string :as string]
2+
(:require [cljs-bean.core :as bean]
3+
[clojure.string :as string]
34
[logseq.publish.common :as publish-common])
45
(:require-macros [logseq.publish.async :refer [js-await]]))
56

@@ -71,6 +72,15 @@
7172
"source_block_format TEXT,"
7273
"updated_at INTEGER,"
7374
"PRIMARY KEY (graph_uuid, tag_page_uuid, source_block_uuid)"
75+
");"))
76+
(publish-common/sql-exec sql
77+
(str "CREATE TABLE IF NOT EXISTS page_blocks ("
78+
"graph_uuid TEXT NOT NULL,"
79+
"page_uuid TEXT NOT NULL,"
80+
"block_uuid TEXT NOT NULL,"
81+
"block_content TEXT,"
82+
"updated_at INTEGER,"
83+
"PRIMARY KEY (graph_uuid, block_uuid)"
7484
");"))))
7585

7686
(defn parse-page-tags [value]
@@ -101,67 +111,71 @@
101111
(cond
102112
(= "POST" (.-method request))
103113
(js-await [body (.json request)]
104-
(publish-common/sql-exec sql
105-
(str "INSERT INTO pages ("
106-
"page_uuid,"
107-
"page_title,"
108-
"page_tags,"
109-
"graph_uuid,"
110-
"schema_version,"
111-
"block_count,"
112-
"content_hash,"
113-
"content_length,"
114-
"r2_key,"
115-
"owner_sub,"
116-
"owner_username,"
117-
"created_at,"
118-
"updated_at,"
119-
"short_id,"
120-
"password_hash"
121-
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
122-
" ON CONFLICT(graph_uuid, page_uuid) DO UPDATE SET"
123-
" page_uuid=excluded.page_uuid,"
124-
" page_title=excluded.page_title,"
125-
" page_tags=excluded.page_tags,"
126-
" schema_version=excluded.schema_version,"
127-
" block_count=excluded.block_count,"
128-
" content_hash=excluded.content_hash,"
129-
" content_length=excluded.content_length,"
130-
" r2_key=excluded.r2_key,"
131-
" owner_sub=excluded.owner_sub,"
132-
" owner_username=excluded.owner_username,"
133-
" updated_at=excluded.updated_at,"
134-
" short_id=excluded.short_id,"
135-
" password_hash=excluded.password_hash;")
136-
(aget body "page_uuid")
137-
(aget body "page_title")
138-
(aget body "page_tags")
139-
(aget body "graph")
140-
(aget body "schema_version")
141-
(aget body "block_count")
142-
(aget body "content_hash")
143-
(aget body "content_length")
144-
(aget body "r2_key")
145-
(aget body "owner_sub")
146-
(aget body "owner_username")
147-
(aget body "created_at")
148-
(aget body "updated_at")
149-
(aget body "short_id")
150-
(aget body "password_hash"))
151-
(let [refs (aget body "refs")
152-
tagged-nodes (aget body "tagged_nodes")
153-
graph-uuid (aget body "graph")
154-
page-uuid (aget body "page_uuid")]
155-
(when (and graph-uuid page-uuid)
114+
(let [page-uuid (aget body "page_uuid")
115+
graph-uuid (aget body "graph")]
116+
(if (and (string? page-uuid) (string? graph-uuid))
156117
(publish-common/sql-exec sql
157-
"DELETE FROM page_refs WHERE graph_uuid = ? AND source_page_uuid = ?;"
118+
(str "INSERT INTO pages ("
119+
"page_uuid,"
120+
"page_title,"
121+
"page_tags,"
122+
"graph_uuid,"
123+
"schema_version,"
124+
"block_count,"
125+
"content_hash,"
126+
"content_length,"
127+
"r2_key,"
128+
"owner_sub,"
129+
"owner_username,"
130+
"created_at,"
131+
"updated_at,"
132+
"short_id,"
133+
"password_hash"
134+
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
135+
" ON CONFLICT(graph_uuid, page_uuid) DO UPDATE SET"
136+
" page_uuid=excluded.page_uuid,"
137+
" page_title=excluded.page_title,"
138+
" page_tags=excluded.page_tags,"
139+
" schema_version=excluded.schema_version,"
140+
" block_count=excluded.block_count,"
141+
" content_hash=excluded.content_hash,"
142+
" content_length=excluded.content_length,"
143+
" r2_key=excluded.r2_key,"
144+
" owner_sub=excluded.owner_sub,"
145+
" owner_username=excluded.owner_username,"
146+
" updated_at=excluded.updated_at,"
147+
" short_id=excluded.short_id,"
148+
" password_hash=excluded.password_hash;")
149+
page-uuid
150+
(aget body "page_title")
151+
(aget body "page_tags")
158152
graph-uuid
159-
page-uuid)
160-
(publish-common/sql-exec sql
161-
"DELETE FROM page_tags WHERE graph_uuid = ? AND source_page_uuid = ?;"
162-
graph-uuid
163-
page-uuid)
164-
(when (seq refs)
153+
(aget body "schema_version")
154+
(aget body "block_count")
155+
(aget body "content_hash")
156+
(aget body "content_length")
157+
(aget body "r2_key")
158+
(aget body "owner_sub")
159+
(aget body "owner_username")
160+
(aget body "created_at")
161+
(aget body "updated_at")
162+
(aget body "short_id")
163+
(aget body "password_hash"))
164+
(throw (js/Error. "publish: missing page_uuid or graph")))
165+
(let [refs (aget body "refs")
166+
tagged-nodes (aget body "tagged_nodes")
167+
blocks (aget body "blocks")
168+
graph-uuid (aget body "graph")
169+
page-uuid (aget body "page_uuid")]
170+
(when (and graph-uuid page-uuid)
171+
(publish-common/sql-exec sql
172+
"DELETE FROM page_refs WHERE graph_uuid = ? AND source_page_uuid = ?;"
173+
graph-uuid
174+
page-uuid)
175+
(publish-common/sql-exec sql
176+
"DELETE FROM page_tags WHERE graph_uuid = ? AND source_page_uuid = ?;"
177+
graph-uuid
178+
page-uuid)
165179
(doseq [ref refs]
166180
(publish-common/sql-exec sql
167181
(str "INSERT OR REPLACE INTO page_refs ("
@@ -178,8 +192,8 @@
178192
(aget ref "source_block_uuid")
179193
(aget ref "source_block_content")
180194
(aget ref "source_block_format")
181-
(aget ref "updated_at"))))
182-
(when (seq tagged-nodes)
195+
(aget ref "updated_at")))
196+
183197
(doseq [tag tagged-nodes]
184198
(publish-common/sql-exec sql
185199
(str "INSERT OR REPLACE INTO page_tags ("
@@ -195,7 +209,21 @@
195209
(aget tag "source_block_uuid")
196210
(aget tag "source_block_content")
197211
(aget tag "source_block_format")
198-
(aget tag "updated_at")))))
212+
(aget tag "updated_at"))))
213+
(publish-common/sql-exec sql
214+
"DELETE FROM page_blocks WHERE graph_uuid = ? AND page_uuid = ?;"
215+
graph-uuid
216+
page-uuid)
217+
(doseq [block blocks]
218+
(publish-common/sql-exec sql
219+
(str "INSERT OR REPLACE INTO page_blocks ("
220+
"graph_uuid, page_uuid, block_uuid, block_content, updated_at"
221+
") VALUES (?, ?, ?, ?, ?);")
222+
(aget body "graph")
223+
(aget block "page_uuid")
224+
(aget block "block_uuid")
225+
(aget block "block_content")
226+
(aget block "updated_at"))))
199227
(publish-common/json-response {:ok true})))
200228

201229
(= "GET" (.-method request))
@@ -204,6 +232,45 @@
204232
graph-uuid (nth parts 2 nil)
205233
page-uuid (nth parts 3 nil)]
206234
(cond
235+
(= (nth parts 1 nil) "search")
236+
(let [graph-uuid (nth parts 2 nil)
237+
query (.get (.-searchParams url) "q")
238+
query (some-> query string/trim)
239+
query (when (and query (not (string/blank? query)))
240+
(string/lower-case query))]
241+
(if (or (string/blank? graph-uuid) (string/blank? query))
242+
(publish-common/bad-request "missing graph uuid or query")
243+
(let [like-query (str "%" query "%")
244+
pages (publish-common/get-sql-rows
245+
(publish-common/sql-exec sql
246+
(str "SELECT page_uuid, page_title, short_id "
247+
"FROM pages "
248+
"WHERE graph_uuid = ? "
249+
"AND password_hash IS NULL "
250+
"AND page_title IS NOT NULL "
251+
"AND lower(page_title) LIKE ? "
252+
"ORDER BY updated_at DESC "
253+
"LIMIT 20;")
254+
graph-uuid
255+
like-query))
256+
blocks (publish-common/get-sql-rows
257+
(publish-common/sql-exec sql
258+
(str "SELECT page_blocks.page_uuid, page_blocks.block_uuid, "
259+
"page_blocks.block_content, pages.page_title, pages.short_id "
260+
"FROM page_blocks "
261+
"LEFT JOIN pages "
262+
"ON pages.graph_uuid = page_blocks.graph_uuid "
263+
"AND pages.page_uuid = page_blocks.page_uuid "
264+
"WHERE page_blocks.graph_uuid = ? "
265+
"AND pages.password_hash IS NULL "
266+
"AND page_blocks.block_content IS NOT NULL "
267+
"AND lower(page_blocks.block_content) LIKE ? "
268+
"ORDER BY page_blocks.updated_at DESC "
269+
"LIMIT 50;")
270+
graph-uuid
271+
like-query))]
272+
(publish-common/json-response {:pages pages :blocks blocks}))))
273+
207274
(= (nth parts 1 nil) "tag")
208275
(let [tag-name (when-let [raw (nth parts 2 nil)]
209276
(js/decodeURIComponent raw))

0 commit comments

Comments
 (0)