Fix: failed to load when refreshing with invalid block refs

pull/2679/head
Tienson Qin 2021-08-19 12:27:52 +08:00
parent 65d7ae5ae7
commit bfd614e1fe
3 changed files with 31 additions and 12 deletions

View File

@ -1211,6 +1211,12 @@
(reset! blocks-count-cache n)
n)))))
(defn get-all-block-uuids
[]
(when-let [conn (conn/get-conn)]
(->> (d/datoms conn :avet :block/uuid)
(map :v))))
;; block/uuid and block/content
(defn get-all-block-contents
[]

View File

@ -15,6 +15,18 @@
[frontend.handler.editor :as editor]
[frontend.handler.extract :as extract]))
(defn- set-missing-block-ids!
[content]
(when (string? content)
(doseq [block-id (extract/extract-all-block-refs content)]
(when-let [block (try
(model/get-block-by-uuid block-id)
(catch js/Error _e
nil))]
(let [id-property (:id (:block/properties block))]
(when-not (= (str id-property) (str block-id))
(editor/set-block-property! block-id "id" block-id)))))))
(defn handle-changed!
[type {:keys [dir path content stat] :as payload}]
(when dir
@ -26,9 +38,7 @@
(when-not (db/file-exists? repo path)
(let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(doseq [block-id (extract/extract-all-block-refs content)]
(if (model/get-block-by-uuid block-id)
(editor/set-block-property! block-id "id" block-id)))
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime)
;; return nil, otherwise the entire db will be transfered by ipc
nil))
@ -42,9 +52,7 @@
(> mtime last-modified-at)))
(let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(doseq [block-id (extract/extract-all-block-refs content)]
(if (model/get-block-by-uuid block-id)
(editor/set-block-property! block-id "id" block-id)))
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime)
nil)

View File

@ -5,6 +5,7 @@
[frontend.config :as config]
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.model :as db-model]
[frontend.dicts :as dicts]
[frontend.encrypt :as encrypt]
[frontend.format :as format]
@ -26,7 +27,8 @@
[frontend.util :as util]
[lambdaisland.glogi :as log]
[promesa.core :as p]
[shadow.resource :as rc]))
[shadow.resource :as rc]
[clojure.set :as set]))
;; Project settings should be checked in two situations:
;; 1. User changes the config.edn directly in logseq.com (fn: alter-file)
@ -147,10 +149,11 @@
(state/pub-event! [:page/create-today-journal repo-url])))))
(defn- remove-non-exists-refs!
[data]
(let [block-ids (->> (map :block/uuid data)
(remove nil?)
(set))
[data all-block-ids]
(let [block-ids (->> (->> (map :block/uuid data)
(remove nil?)
(set))
(set/union (set all-block-ids)))
keep-block-ref-f (fn [refs]
(filter (fn [ref]
(if (and (vector? ref)
@ -169,7 +172,9 @@
(let [files (map #(select-keys % [:file/path :file/last-modified-at]) files)
all-data (-> (concat delete-files delete-blocks files blocks-pages)
(util/remove-nils))
all-data (if refresh? all-data (remove-non-exists-refs! all-data))]
all-data (if refresh?
(remove-non-exists-refs! all-data (db-model/get-all-block-uuids))
(remove-non-exists-refs! all-data nil))]
(db/transact! repo-url all-data)))
(defn- load-pages-metadata!