repo.cljs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. (ns frontend.handler.repo
  2. (:refer-clojure :exclude [clone])
  3. (:require [clojure.string :as string]
  4. [frontend.config :as config]
  5. [frontend.context.i18n :refer [t]]
  6. [frontend.date :as date]
  7. [frontend.db :as db]
  8. [frontend.fs :as fs]
  9. [frontend.fs.nfs :as nfs]
  10. [frontend.handler.common :as common-handler]
  11. [frontend.handler.file :as file-handler]
  12. [frontend.handler.route :as route-handler]
  13. [frontend.handler.ui :as ui-handler]
  14. [frontend.handler.metadata :as metadata-handler]
  15. [frontend.idb :as idb]
  16. [frontend.search :as search]
  17. [frontend.spec :as spec]
  18. [frontend.state :as state]
  19. [frontend.util :as util]
  20. [lambdaisland.glogi :as log]
  21. [promesa.core :as p]
  22. [shadow.resource :as rc]
  23. [frontend.db.persist :as db-persist]
  24. [logseq.graph-parser.util :as gp-util]
  25. [logseq.graph-parser :as graph-parser]
  26. [electron.ipc :as ipc]
  27. [clojure.core.async :as async]
  28. [frontend.encrypt :as encrypt]))
  29. ;; Project settings should be checked in two situations:
  30. ;; 1. User changes the config.edn directly in logseq.com (fn: alter-file)
  31. ;; 2. Git pulls the new change (fn: load-files)
  32. (defn create-config-file-if-not-exists
  33. [repo-url]
  34. (spec/validate :repos/url repo-url)
  35. (let [repo-dir (config/get-repo-dir repo-url)
  36. app-dir config/app-name
  37. dir (str repo-dir "/" app-dir)]
  38. (p/let [_ (fs/mkdir-if-not-exists dir)]
  39. (let [default-content config/config-default-content
  40. path (str app-dir "/" config/config-file)]
  41. (p/let [file-exists? (fs/create-if-not-exists repo-url repo-dir (str app-dir "/" config/config-file) default-content)]
  42. (when-not file-exists?
  43. (file-handler/reset-file! repo-url path default-content)
  44. (common-handler/reset-config! repo-url default-content)))))))
  45. (defn create-contents-file
  46. [repo-url]
  47. (spec/validate :repos/url repo-url)
  48. (p/let [repo-dir (config/get-repo-dir repo-url)
  49. pages-dir (state/get-pages-directory)
  50. [org-path md-path] (map #(str "/" pages-dir "/contents." %) ["org" "md"])
  51. contents-file-exist? (some #(fs/file-exists? repo-dir %) [org-path md-path])]
  52. (when-not contents-file-exist?
  53. (let [format (state/get-preferred-format)
  54. path (str pages-dir "/contents."
  55. (config/get-file-extension format))
  56. file-path (str "/" path)
  57. default-content (case (name format)
  58. "org" (rc/inline "contents.org")
  59. "markdown" (rc/inline "contents.md")
  60. "")]
  61. (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" pages-dir))
  62. file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
  63. (when-not file-exists?
  64. (file-handler/reset-file! repo-url path default-content)))))))
  65. (defn create-custom-theme
  66. [repo-url]
  67. (spec/validate :repos/url repo-url)
  68. (let [repo-dir (config/get-repo-dir repo-url)
  69. path (str config/app-name "/" config/custom-css-file)
  70. file-path (str "/" path)
  71. default-content ""]
  72. (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
  73. file-exists? (fs/create-if-not-exists repo-url repo-dir file-path default-content)]
  74. (when-not file-exists?
  75. (file-handler/reset-file! repo-url path default-content)))))
  76. (defn create-dummy-notes-page
  77. [repo-url content]
  78. (spec/validate :repos/url repo-url)
  79. (let [repo-dir (config/get-repo-dir repo-url)
  80. path (str (config/get-pages-directory) "/how_to_make_dummy_notes.md")
  81. file-path (str "/" path)]
  82. (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" (config/get-pages-directory)))
  83. _file-exists? (fs/create-if-not-exists repo-url repo-dir file-path content)]
  84. (file-handler/reset-file! repo-url path content))))
  85. (defn- create-today-journal-if-not-exists
  86. [repo-url {:keys [content]}]
  87. (spec/validate :repos/url repo-url)
  88. (when (state/enable-journals? repo-url)
  89. (let [repo-dir (config/get-repo-dir repo-url)
  90. format (state/get-preferred-format repo-url)
  91. title (date/today)
  92. file-name (date/journal-title->default title)
  93. default-content (util/default-content-with-title format)
  94. template (state/get-default-journal-template)
  95. template (when (and template
  96. (not (string/blank? template)))
  97. template)
  98. content (cond
  99. content
  100. content
  101. template
  102. (str default-content template)
  103. :else
  104. default-content)
  105. path (str (config/get-journals-directory) "/" file-name "."
  106. (config/get-file-extension format))
  107. file-path (str "/" path)
  108. page-exists? (db/entity repo-url [:block/name (util/page-name-sanity-lc title)])
  109. empty-blocks? (db/page-empty? repo-url (util/page-name-sanity-lc title))]
  110. (when (or empty-blocks? (not page-exists?))
  111. (p/let [_ (nfs/check-directory-permission! repo-url)
  112. _ (fs/mkdir-if-not-exists (str repo-dir "/" (config/get-journals-directory)))
  113. file-exists? (fs/file-exists? repo-dir file-path)]
  114. (when-not file-exists?
  115. (p/let [_ (file-handler/reset-file! repo-url path content)]
  116. (p/let [_ (fs/create-if-not-exists repo-url repo-dir file-path content)]
  117. (when-not (state/editing?)
  118. (ui-handler/re-render-root!)))))
  119. (when-not (state/editing?)
  120. (ui-handler/re-render-root!)))))))
  121. (defn create-default-files!
  122. ([repo-url]
  123. (create-default-files! repo-url false))
  124. ([repo-url encrypted?]
  125. (spec/validate :repos/url repo-url)
  126. (let [repo-dir (config/get-repo-dir repo-url)]
  127. (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
  128. _ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name "/" config/recycle-dir))
  129. _ (fs/mkdir-if-not-exists (str repo-dir "/" (config/get-journals-directory)))
  130. _ (file-handler/create-metadata-file repo-url encrypted?)
  131. _ (create-config-file-if-not-exists repo-url)
  132. _ (create-contents-file repo-url)
  133. _ (create-custom-theme repo-url)]
  134. (state/pub-event! [:page/create-today-journal repo-url])))))
  135. (defn- load-pages-metadata!
  136. "force?: if set true, skip the metadata timestamp range check"
  137. ([repo file-paths files]
  138. (load-pages-metadata! repo file-paths files false))
  139. ([repo file-paths files force?]
  140. (try
  141. (let [file (config/get-pages-metadata-path)]
  142. (when (contains? (set file-paths) file)
  143. (when-let [content (some #(when (= (:file/path %) file) (:file/content %)) files)]
  144. (let [metadata (common-handler/safe-read-string content "Parsing pages metadata file failed: ")
  145. pages (db/get-all-pages repo)
  146. pages (zipmap (map :block/name pages) pages)
  147. metadata (->>
  148. (filter (fn [{:block/keys [name created-at updated-at]}]
  149. (when-let [page (get pages name)]
  150. (and
  151. (>= updated-at created-at) ;; metadata validation
  152. (or force? ;; when force is true, shortcut timestamp range check
  153. (and (or (nil? (:block/created-at page))
  154. (>= created-at (:block/created-at page)))
  155. (or (nil? (:block/updated-at page))
  156. (>= updated-at (:block/created-at page)))))
  157. (or ;; persistent metadata is the gold standard
  158. (not= created-at (:block/created-at page))
  159. (not= updated-at (:block/created-at page)))))) metadata)
  160. (remove nil?))]
  161. (when (seq metadata)
  162. (db/transact! repo metadata {:new-graph? true}))))))
  163. (catch js/Error e
  164. (log/error :exception e)))))
  165. (defn update-pages-metadata!
  166. "update pages meta content -> db. Only accept non-encrypted content!"
  167. [repo content force?]
  168. (let [path (config/get-pages-metadata-path)
  169. files [{:file/path path
  170. :file/content content}]
  171. file-paths [path]]
  172. (load-pages-metadata! repo file-paths files force?)))
  173. (defn- parse-and-load-file!
  174. [repo-url file new-graph?]
  175. (try
  176. (file-handler/alter-file repo-url
  177. (:file/path file)
  178. (:file/content file)
  179. {:new-graph? new-graph?
  180. :re-render-root? false
  181. :from-disk? true})
  182. (catch :default e
  183. (state/set-parsing-state! (fn [m]
  184. (update m :failed-parsing-files conj [(:file/path file) e])))))
  185. (state/set-parsing-state! (fn [m]
  186. (update m :finished inc))))
  187. (defn- after-parse
  188. [repo-url files file-paths db-encrypted? re-render? re-render-opts opts graph-added-chan]
  189. (load-pages-metadata! repo-url file-paths files true)
  190. (when (or (:new-graph? opts) (not (:refresh? opts)))
  191. (if (and (not db-encrypted?) (state/enable-encryption? repo-url))
  192. (state/pub-event! [:modal/encryption-setup-dialog repo-url
  193. #(create-default-files! repo-url %)])
  194. (create-default-files! repo-url db-encrypted?)))
  195. (when re-render?
  196. (ui-handler/re-render-root! re-render-opts))
  197. (state/pub-event! [:graph/added repo-url opts])
  198. (state/reset-parsing-state!)
  199. (state/set-loading-files! repo-url false)
  200. (async/offer! graph-added-chan true))
  201. (defn- parse-files-and-create-default-files-inner!
  202. [repo-url files delete-files delete-blocks file-paths db-encrypted? re-render? re-render-opts opts]
  203. (let [supported-files (graph-parser/filter-files files)
  204. new-graph? (:new-graph? opts)
  205. delete-data (->> (concat delete-files delete-blocks)
  206. (remove nil?))
  207. chan (async/to-chan! supported-files)
  208. graph-added-chan (async/promise-chan)]
  209. (when (seq delete-data) (db/transact! repo-url delete-data))
  210. (state/set-current-repo! repo-url)
  211. (state/set-parsing-state! {:total (count supported-files)})
  212. ;; Synchronous for tests for not breaking anything
  213. (if util/node-test?
  214. (do
  215. (doseq [file supported-files]
  216. (state/set-parsing-state! (fn [m]
  217. (assoc m :current-parsing-file (:file/path file))))
  218. (parse-and-load-file! repo-url file new-graph?))
  219. (after-parse repo-url files file-paths db-encrypted? re-render? re-render-opts opts graph-added-chan))
  220. (async/go-loop []
  221. (if-let [file (async/<! chan)]
  222. (do
  223. (state/set-parsing-state! (fn [m]
  224. (assoc m :current-parsing-file (:file/path file))))
  225. (async/<! (async/timeout 10))
  226. (parse-and-load-file! repo-url file new-graph?)
  227. (recur))
  228. (after-parse repo-url files file-paths db-encrypted? re-render? re-render-opts opts graph-added-chan))))
  229. graph-added-chan))
  230. (defn- parse-files-and-create-default-files!
  231. [repo-url files delete-files delete-blocks file-paths db-encrypted? re-render? re-render-opts opts]
  232. (if db-encrypted?
  233. (p/let [files (p/all
  234. (map (fn [file]
  235. (p/let [content (encrypt/decrypt (:file/content file))]
  236. (assoc file :file/content content)))
  237. files))]
  238. (parse-files-and-create-default-files-inner! repo-url files delete-files delete-blocks file-paths db-encrypted? re-render? re-render-opts opts))
  239. (parse-files-and-create-default-files-inner! repo-url files delete-files delete-blocks file-paths db-encrypted? re-render? re-render-opts opts)))
  240. (defn parse-files-and-load-to-db!
  241. [repo-url files {:keys [delete-files delete-blocks re-render? re-render-opts _refresh?] :as opts
  242. :or {re-render? true}}]
  243. (let [file-paths (map :file/path files)
  244. metadata-file (config/get-metadata-path)
  245. metadata-content (some #(when (= (:file/path %) metadata-file)
  246. (:file/content %)) files)
  247. metadata (when metadata-content
  248. (common-handler/read-metadata! metadata-content))
  249. db-encrypted? (:db/encrypted? metadata)
  250. db-encrypted-secret (if db-encrypted? (:db/encrypted-secret metadata) nil)]
  251. (if db-encrypted?
  252. (let [close-fn #(parse-files-and-create-default-files! repo-url files delete-files delete-blocks file-paths db-encrypted? re-render? re-render-opts opts)]
  253. (state/set-state! :encryption/graph-parsing? true)
  254. (state/pub-event! [:modal/encryption-input-secret-dialog repo-url
  255. db-encrypted-secret
  256. close-fn]))
  257. (parse-files-and-create-default-files! repo-url files delete-files delete-blocks file-paths db-encrypted? re-render? re-render-opts opts))))
  258. (defn load-repo-to-db!
  259. [repo-url {:keys [diffs nfs-files refresh? new-graph? empty-graph?]}]
  260. (spec/validate :repos/url repo-url)
  261. (route-handler/redirect-to-home!)
  262. (state/set-parsing-state! {:graph-loading? true})
  263. (let [config (or (state/get-config repo-url)
  264. (when-let [content (some-> (first (filter #(= (config/get-config-path repo-url) (:file/path %)) nfs-files))
  265. :file/content)]
  266. (common-handler/read-config content)))
  267. relate-path-fn (fn [m k]
  268. (some-> (get m k)
  269. (string/replace (js/decodeURI (config/get-local-dir repo-url)) "")))
  270. nfs-files (common-handler/remove-hidden-files nfs-files config #(relate-path-fn % :file/path))
  271. diffs (common-handler/remove-hidden-files diffs config #(relate-path-fn % :path))
  272. load-contents (fn [files option]
  273. (file-handler/load-files-contents!
  274. repo-url
  275. files
  276. (fn [files-contents]
  277. (parse-files-and-load-to-db! repo-url files-contents (assoc option :refresh? refresh?)))))]
  278. (cond
  279. (and (not (seq diffs)) nfs-files)
  280. (parse-files-and-load-to-db! repo-url nfs-files {:new-graph? new-graph?
  281. :empty-graph? empty-graph?})
  282. :else
  283. (when (seq diffs)
  284. (let [filter-diffs (fn [type] (->> (filter (fn [f] (= type (:type f))) diffs)
  285. (map :path)))
  286. remove-files (filter-diffs "remove")
  287. modify-files (filter-diffs "modify")
  288. add-files (filter-diffs "add")
  289. delete-files (when (seq remove-files)
  290. (db/delete-files remove-files))
  291. delete-blocks (db/delete-blocks repo-url remove-files true)
  292. delete-blocks (->>
  293. (concat
  294. delete-blocks
  295. (db/delete-blocks repo-url modify-files false))
  296. (remove nil?))
  297. delete-pages (if (seq remove-files)
  298. (db/delete-pages-by-files remove-files)
  299. [])
  300. add-or-modify-files (some->>
  301. (concat modify-files add-files)
  302. (gp-util/remove-nils))
  303. options {:delete-files (concat delete-files delete-pages)
  304. :delete-blocks delete-blocks
  305. :re-render? true}]
  306. (if (seq nfs-files)
  307. (parse-files-and-load-to-db! repo-url nfs-files
  308. (assoc options
  309. :refresh? refresh?
  310. :re-render-opts {:clear-all-query-state? true}))
  311. (load-contents add-or-modify-files options)))))))
  312. (defn remove-repo!
  313. [{:keys [url] :as repo}]
  314. (let [delete-db-f (fn []
  315. (let [graph-exists? (db/get-db url)]
  316. (db/remove-conn! url)
  317. (db-persist/delete-graph! url)
  318. (search/remove-db! url)
  319. (state/delete-repo! repo)
  320. (when graph-exists? (ipc/ipc "graphUnlinked" repo))
  321. (when (= (state/get-current-repo) url)
  322. (state/set-current-repo! (:url (first (state/get-repos)))))))]
  323. (when (or (config/local-db? url) (= url "local"))
  324. (p/let [_ (idb/clear-local-db! url)] ; clear file handles
  325. (delete-db-f)))))
  326. (defn start-repo-db-if-not-exists!
  327. [repo]
  328. (state/set-current-repo! repo)
  329. (db/start-db-conn! repo))
  330. (defn- setup-local-repo-if-not-exists-impl!
  331. []
  332. ;; loop query if js/window.pfs is ready, interval 100ms
  333. (if js/window.pfs
  334. (let [repo config/local-repo]
  335. (p/do! (fs/mkdir-if-not-exists (str "/" repo))
  336. (state/set-current-repo! repo)
  337. (db/start-db-conn! repo)
  338. (when-not config/publishing?
  339. (let [dummy-notes (t :tutorial/dummy-notes)]
  340. (create-dummy-notes-page repo dummy-notes)))
  341. (when-not config/publishing?
  342. (let [tutorial (t :tutorial/text)
  343. tutorial (string/replace-first tutorial "$today" (date/today))]
  344. (create-today-journal-if-not-exists repo {:content tutorial})))
  345. (create-config-file-if-not-exists repo)
  346. (create-contents-file repo)
  347. (create-custom-theme repo)
  348. (state/set-db-restoring! false)
  349. (ui-handler/re-render-root!)))
  350. (p/then (p/delay 100) ;; TODO Junyi remove the string
  351. setup-local-repo-if-not-exists-impl!)))
  352. (defn setup-local-repo-if-not-exists!
  353. []
  354. ;; ensure `(state/set-db-restoring! false)` at exit
  355. (-> (setup-local-repo-if-not-exists-impl!)
  356. (p/timeout 3000)
  357. (p/catch (fn []
  358. (state/set-db-restoring! false)
  359. (prn "setup-local-repo failed! timeout 3000ms")))))
  360. (defn restore-and-setup-repo!
  361. "Restore the db of a graph from the persisted data, and setup. Create a new
  362. conn, or replace the conn in state with a new one."
  363. [repo]
  364. (p/let [_ (state/set-db-restoring! true)
  365. _ (db/restore-graph! repo)]
  366. (file-handler/restore-config! repo false)
  367. ;; Don't have to unlisten the old listerner, as it will be destroyed with the conn
  368. (db/listen-and-persist! repo)
  369. (ui-handler/add-style-if-exists!)
  370. (state/set-db-restoring! false)))
  371. (defn rebuild-index!
  372. [url]
  373. (when-not (state/unlinked-dir? (config/get-repo-dir url))
  374. (when url
  375. (search/reset-indice! url)
  376. (db/remove-conn! url)
  377. (db/clear-query-state!)
  378. (-> (p/do! (db-persist/delete-graph! url))
  379. (p/catch (fn [error]
  380. (prn "Delete repo failed, error: " error)))))))
  381. (defn re-index!
  382. [nfs-rebuild-index! ok-handler]
  383. (when-let [repo (state/get-current-repo)]
  384. (let [dir (config/get-repo-dir repo)]
  385. (when-not (state/unlinked-dir? dir)
  386. (route-handler/redirect-to-home!)
  387. (let [local? (config/local-db? repo)]
  388. (if local?
  389. (p/let [_ (metadata-handler/set-pages-metadata! repo)]
  390. (nfs-rebuild-index! repo ok-handler))
  391. (rebuild-index! repo))
  392. (js/setTimeout
  393. (route-handler/redirect-to-home!)
  394. 500))))))
  395. (defn persist-db!
  396. ([]
  397. (persist-db! {}))
  398. ([handlers]
  399. (persist-db! (state/get-current-repo) handlers))
  400. ([repo {:keys [before on-success on-error]}]
  401. (->
  402. (p/do!
  403. (when before
  404. (before))
  405. (metadata-handler/set-pages-metadata! repo)
  406. (db/persist! repo)
  407. (when on-success
  408. (on-success)))
  409. (p/catch (fn [error]
  410. (js/console.error error)
  411. (when on-error
  412. (on-error)))))))
  413. (defn broadcast-persist-db!
  414. "Only works for electron
  415. Call backend to handle persisting a specific db on other window
  416. Skip persisting if no other windows is open (controlled by electron)
  417. step 1. [In HERE] a window ---broadcastPersistGraph----> electron
  418. step 2. electron ---------persistGraph-------> window holds the graph
  419. step 3. window w/ graph --broadcastPersistGraphDone-> electron
  420. step 4. [In HERE] a window <---broadcastPersistGraph---- electron"
  421. [graph]
  422. (p/let [_ (ipc/ipc "broadcastPersistGraph" graph)] ;; invoke for chaining promise
  423. nil))
  424. (defn graph-ready!
  425. "Call electron that the graph is loaded."
  426. [graph]
  427. (ipc/ipc "graphReady" graph))