|
|
@@ -9,14 +9,21 @@
|
|
|
[frontend.worker.util :as worker-util]))
|
|
|
|
|
|
(defn- fix-parent-broken-chain
|
|
|
- [db parent-id]
|
|
|
+ [db parent-id tx-report]
|
|
|
(let [parent (d/entity db parent-id)
|
|
|
parent-id (:db/id parent)
|
|
|
blocks (:block/_parent parent)]
|
|
|
(when (seq blocks)
|
|
|
(let [children-ids (set (map :db/id blocks))
|
|
|
sorted (ldb/sort-by-left blocks parent)
|
|
|
- broken-chain? (not= (count sorted) (count blocks))]
|
|
|
+ valid-left-ids (set (cons (:db/id parent) (map :db/id blocks)))
|
|
|
+ broken-chain? (or (not= (count sorted) (count blocks))
|
|
|
+ ;; :block/left points to other blocks that're not current parent or its children
|
|
|
+ (not (every? (fn [b] (contains? valid-left-ids (:db/id (:block/left b)))) blocks)))]
|
|
|
+ (when (and (exists? js/process) broken-chain?)
|
|
|
+ (throw (ex-info "outliner broken chain" {:tx-meta (:tx-meta tx-report)
|
|
|
+ :tx-data (:tx-data tx-report)
|
|
|
+ :db-before (:db-before tx-report)})))
|
|
|
(when broken-chain?
|
|
|
(let [error-data {:parent {:db/id parent-id
|
|
|
:block/uuid (:block/uuid parent)
|
|
|
@@ -92,9 +99,9 @@
|
|
|
(apply concat))))))))
|
|
|
|
|
|
(defn- fix-broken-chain
|
|
|
- [db parent-left->es]
|
|
|
+ [db parent-left->es tx-report]
|
|
|
(let [parents (distinct (map first (keys parent-left->es)))]
|
|
|
- (mapcat #(fix-parent-broken-chain db %) parents)))
|
|
|
+ (mapcat #(fix-parent-broken-chain db % tx-report) parents)))
|
|
|
|
|
|
(defn- build-parent-left->es
|
|
|
[db page-id]
|
|
|
@@ -142,6 +149,8 @@
|
|
|
[conn page-id transact-opts *fix-tx-data]
|
|
|
(let [db @conn
|
|
|
conflicts (get-conflicts db page-id)
|
|
|
+ _ (when (and (exists? js/process) (seq conflicts))
|
|
|
+ (throw (ex-info "outliner core conflicts" {:conflicts conflicts})))
|
|
|
fix-conflicts-tx (when (seq conflicts)
|
|
|
(fix-parent-left-conflicts db conflicts page-id))]
|
|
|
(when (seq fix-conflicts-tx)
|
|
|
@@ -154,7 +163,7 @@
|
|
|
|
|
|
(defn fix-page-if-broken!
|
|
|
"Fix the page if it has either parent-left conflicts or broken chains."
|
|
|
- [conn page-id {:keys [fix-parent-left? fix-broken-chain? replace-tx?]
|
|
|
+ [conn page-id {:keys [fix-parent-left? fix-broken-chain? replace-tx? tx-report]
|
|
|
:or {fix-parent-left? true
|
|
|
fix-broken-chain? true
|
|
|
replace-tx? false}
|
|
|
@@ -170,7 +179,7 @@
|
|
|
(when fix-broken-chain?
|
|
|
(let [db' @conn
|
|
|
parent-left->es' (build-parent-left->es db page-id)
|
|
|
- fix-broken-chain-tx (fix-broken-chain db' parent-left->es')]
|
|
|
+ fix-broken-chain-tx (fix-broken-chain db' parent-left->es' tx-report)]
|
|
|
(when (seq fix-broken-chain-tx)
|
|
|
(let [tx-data (:tx-data (ldb/transact! conn fix-broken-chain-tx transact-opts))]
|
|
|
(swap! *fix-tx-data (fn [old-data] (concat old-data tx-data)))))))
|