Browse Source

fix: prevent extends cycle

Tienson Qin 4 months ago
parent
commit
326ed9f753

+ 2 - 1
deps/outliner/src/logseq/outliner/property.cljs

@@ -342,6 +342,7 @@
            property (d/entity @conn property-id)
            _ (when (= (:db/ident property) :logseq.property.class/extends)
                (outliner-validate/validate-extends-property
+                @conn
                 (if (number? v) (d/entity @conn v) v)
                 (map #(d/entity @conn %) block-eids)))
            _ (assert (some? property) (str "Property " property-id " doesn't exist yet"))
@@ -415,7 +416,7 @@
       (when (= property-id :block/tags)
         (outliner-validate/validate-tags-property @conn [block-eid] v))
       (when (= property-id :logseq.property.class/extends)
-        (outliner-validate/validate-extends-property v [block]))
+        (outliner-validate/validate-extends-property @conn v [block]))
       (cond
         db-attribute?
         (when-not (and (= property-id :block/alias) (= v (:db/id block))) ; alias can't be itself

+ 14 - 1
deps/outliner/src/logseq/outliner/validate.cljs

@@ -170,8 +170,21 @@
                      :payload {:message "Can't change the parent of a built-in tag"
                                :type :error}}))))
 
+(defn- disallow-extends-cycle
+  [db parent-ent child-ents]
+  (doseq [child child-ents]
+    (let [children-ids (set (cons (:db/id child)
+                                  (map :db/id (db-class/get-structured-children db (:db/id child)))))]
+      (when (contains? children-ids (:db/id parent-ent))
+        (throw (ex-info "Extends cycle"
+                        {:type :notification
+                         :payload {:message "Tag extends cycle"
+                                   :type :error
+                                   :blocks (map #(select-keys % [:db/id :block/title]) [child])}}))))))
+
 (defn validate-extends-property
-  [parent-ent child-ents]
+  [db parent-ent child-ents]
+  (disallow-extends-cycle db parent-ent child-ents)
   (disallow-built-in-class-extends-change parent-ent child-ents)
   (validate-extends-property-have-correct-type parent-ent child-ents))