diff --git a/src/main/frontend/worker/pipeline.cljs b/src/main/frontend/worker/pipeline.cljs index ebd19490b68..0131a635f8b 100644 --- a/src/main/frontend/worker/pipeline.cljs +++ b/src/main/frontend/worker/pipeline.cljs @@ -104,6 +104,85 @@ default = false") (assoc :logseq.property.code/lang (:kv/value (d/entity db :logseq.kv/latest-code-lang))))]))) datoms)) +(defn- invoke-hooks-for-imported-graph [conn {:keys [tx-meta] :as tx-report}] + (let [{:keys [refs-tx-report path-refs-tx-report]} + (outliner-pipeline/transact-new-db-graph-refs conn tx-report) + full-tx-data (concat (:tx-data tx-report) + (:tx-data refs-tx-report) + (:tx-data path-refs-tx-report)) + final-tx-report (-> (or path-refs-tx-report refs-tx-report tx-report) + (assoc :tx-data full-tx-data + :tx-meta tx-meta + :db-before (:db-before tx-report)))] + {:tx-report final-tx-report})) + +(defn- invoke-hooks-default [repo conn {:keys [tx-meta] :as tx-report} context] + (try + (let [display-blocks-tx-data (add-missing-properties-to-typed-display-blocks (:db-after tx-report) (:tx-data tx-report)) + tx-report* (if (seq display-blocks-tx-data) + (let [result (ldb/transact! conn display-blocks-tx-data {:pipeline-replace? true})] + (assoc tx-report + :tx-data (concat (:tx-data tx-report) (:tx-data result)) + :db-after (:db-after result))) + tx-report) + {:keys [pages blocks]} (ds-report/get-blocks-and-pages tx-report*) + _ (when (sqlite-util/local-file-based-graph? repo) + (let [page-ids (distinct (map :db/id pages))] + (doseq [page-id page-ids] + (when (d/entity @conn page-id) + (file/sync-to-file repo page-id tx-meta))))) + deleted-block-uuids (set (outliner-pipeline/filter-deleted-blocks (:tx-data tx-report*))) + deleted-assets (keep (fn [id] + (let [e (d/entity (:db-before tx-report*) [:block/uuid id])] + (when (ldb/asset? e) + {:block/uuid (:block/uuid e) + :ext (:logseq.property.asset/type e)}))) deleted-block-uuids) + blocks' (remove (fn [b] (deleted-block-uuids (:block/uuid b))) blocks) + block-refs (when (seq blocks') + (rebuild-block-refs repo tx-report* blocks')) + refs-tx-report (when (seq block-refs) + (ldb/transact! conn block-refs {:pipeline-replace? true})) + replace-tx (concat + ;; block path refs + (when (seq blocks') + (let [db-after (or (:db-after refs-tx-report) (:db-after tx-report*)) + blocks' (keep (fn [b] (d/entity db-after (:db/id b))) blocks')] + (compute-block-path-refs-tx tx-report* blocks'))) + + ;; update block/tx-id + (let [updated-blocks (remove (fn [b] (contains? (set deleted-block-uuids) (:block/uuid b))) + (concat pages blocks)) + tx-id (get-in (or refs-tx-report tx-report*) [:tempids :db/current-tx])] + (keep (fn [b] + (when-let [db-id (:db/id b)] + {:db/id db-id + :block/tx-id tx-id})) updated-blocks))) + tx-report' (if (seq replace-tx) + (ldb/transact! conn replace-tx {:pipeline-replace? true}) + (do + (when-not (or (exists? js/process) + (:skip-store-conn tx-meta false)) + (d/store @conn)) + tx-report*)) + _ (validate-db! repo conn tx-report* tx-meta context) + full-tx-data (concat (:tx-data tx-report*) + (:tx-data refs-tx-report) + (:tx-data tx-report')) + final-tx-report (assoc tx-report' + :tx-data full-tx-data + :tx-meta tx-meta + :db-before (:db-before tx-report)) + affected-query-keys (when-not (:importing? context) + (worker-react/get-affected-queries-keys final-tx-report))] + {:tx-report final-tx-report + :affected-keys affected-query-keys + :deleted-block-uuids deleted-block-uuids + :deleted-assets deleted-assets + :pages pages + :blocks blocks}) + (catch :default e + (js/console.error e)))) + (defn invoke-hooks [repo conn {:keys [tx-meta] :as tx-report} context] (when-not (:pipeline-replace? tx-meta) @@ -128,80 +207,7 @@ default = false") {:tx-report final-tx-report}) (::gp-exporter/new-graph? tx-meta) - (let [{:keys [refs-tx-report path-refs-tx-report]} - (outliner-pipeline/transact-new-db-graph-refs conn tx-report) - full-tx-data (concat (:tx-data tx-report) - (:tx-data refs-tx-report) - (:tx-data path-refs-tx-report)) - final-tx-report (-> (or path-refs-tx-report refs-tx-report tx-report) - (assoc :tx-data full-tx-data - :tx-meta tx-meta - :db-before (:db-before tx-report)))] - {:tx-report final-tx-report}) + (invoke-hooks-for-imported-graph conn tx-report) :else - (try - (let [display-blocks-tx-data (add-missing-properties-to-typed-display-blocks (:db-after tx-report) (:tx-data tx-report)) - tx-report* (if (seq display-blocks-tx-data) - (let [result (ldb/transact! conn display-blocks-tx-data {:pipeline-replace? true})] - (assoc tx-report - :tx-data (concat (:tx-data tx-report) (:tx-data result)) - :db-after (:db-after result))) - tx-report) - {:keys [pages blocks]} (ds-report/get-blocks-and-pages tx-report*) - _ (when (sqlite-util/local-file-based-graph? repo) - (let [page-ids (distinct (map :db/id pages))] - (doseq [page-id page-ids] - (when (d/entity @conn page-id) - (file/sync-to-file repo page-id tx-meta))))) - deleted-block-uuids (set (outliner-pipeline/filter-deleted-blocks (:tx-data tx-report*))) - deleted-assets (keep (fn [id] - (let [e (d/entity (:db-before tx-report*) [:block/uuid id])] - (when (ldb/asset? e) - {:block/uuid (:block/uuid e) - :ext (:logseq.property.asset/type e)}))) deleted-block-uuids) - blocks' (remove (fn [b] (deleted-block-uuids (:block/uuid b))) blocks) - block-refs (when (seq blocks') - (rebuild-block-refs repo tx-report* blocks')) - refs-tx-report (when (seq block-refs) - (ldb/transact! conn block-refs {:pipeline-replace? true})) - replace-tx (concat - ;; block path refs - (when (seq blocks') - (let [db-after (or (:db-after refs-tx-report) (:db-after tx-report*)) - blocks' (keep (fn [b] (d/entity db-after (:db/id b))) blocks')] - (compute-block-path-refs-tx tx-report* blocks'))) - - ;; update block/tx-id - (let [updated-blocks (remove (fn [b] (contains? (set deleted-block-uuids) (:block/uuid b))) - (concat pages blocks)) - tx-id (get-in (or refs-tx-report tx-report*) [:tempids :db/current-tx])] - (keep (fn [b] - (when-let [db-id (:db/id b)] - {:db/id db-id - :block/tx-id tx-id})) updated-blocks))) - tx-report' (if (seq replace-tx) - (ldb/transact! conn replace-tx {:pipeline-replace? true}) - (do - (when-not (or (exists? js/process) - (:skip-store-conn tx-meta false)) - (d/store @conn)) - tx-report*)) - _ (validate-db! repo conn tx-report* tx-meta context) - full-tx-data (concat (:tx-data tx-report*) - (:tx-data refs-tx-report) - (:tx-data tx-report')) - final-tx-report (assoc tx-report' - :tx-data full-tx-data - :tx-meta tx-meta - :db-before (:db-before tx-report)) - affected-query-keys (when-not (:importing? context) - (worker-react/get-affected-queries-keys final-tx-report))] - {:tx-report final-tx-report - :affected-keys affected-query-keys - :deleted-block-uuids deleted-block-uuids - :deleted-assets deleted-assets - :pages pages - :blocks blocks}) - (catch :default e - (js/console.error e))))))) + (invoke-hooks-default repo conn tx-report context)))))