Skip to content

Commit

Permalink
Merge pull request #2130 from neotyk/dev/links-from-page-title
Browse files Browse the repository at this point in the history
feat: Page link creation reporting also from page titles
  • Loading branch information
filipesilva authored Apr 11, 2022
2 parents f1bcdf5 + 1dc0e98 commit 8a8829b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
72 changes: 42 additions & 30 deletions src/cljc/athens/common_events/graph/ops.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -201,34 +201,46 @@
(defn structural-diff
"Calculates removed and added links (block refs & page links)"
[db ops]
(let [block-save-ops (contains-op? ops :block/save)
new-blocks (->> block-save-ops
(map #(select-keys (:op/args %)
[:block/uid :block/string])))
new-block-uids (->> new-blocks
(map :block/uid)
set)
new-structures (->> new-blocks
(map :block/string)
(map structure/structure-parser->ast))
old-block-strings (->> new-block-uids
(map #(common-db/get-block-string db %)))
old-structures (->> old-block-strings
(map structure/structure-parser->ast))
links (fn [structs names renames]
(->> structs
(mapcat (partial tree-seq vector? identity))
(filter #(and (vector? %)
(contains? names (first %))))
(map #(vector (get renames (first %) (first %))
(-> % second :string)))
set))
old-links (links old-structures
#{:page-link :hashtag :block-ref}
{:hashtag :page-link})
new-links (links new-structures
#{:page-link :hashtag :block-ref}
{:hashtag :page-link})
removed-links (set/difference old-links new-links)
added-links (set/difference new-links old-links)]
(let [block-save-ops (contains-op? ops :block/save)
page-new-ops (contains-op? ops :page/new)
page-rename-ops (contains-op? ops :page/rename)
new-blocks (->> block-save-ops
(map #(select-keys (:op/args %)
[:block/uid :block/string])))
new-block-uids (->> new-blocks
(map :block/uid)
set)
new-page-titles (->> (concat page-new-ops page-rename-ops)
(map #(or (get-in (:op/args %) [:target :page/title])
(get-in (:op/args %) [:page/title]))))
old-page-titles (->> page-rename-ops
(map :page/title)
set)
new-block-structures (->> new-blocks
(map :block/string)
(map structure/structure-parser->ast))
new-title-structures (->> new-page-titles
(map structure/structure-parser->ast))
old-block-strings (->> new-block-uids
(map #(common-db/get-block-string db %)))
old-title-structures (->> old-page-titles
(map structure/structure-parser->ast))
old-structures (->> old-block-strings
(map structure/structure-parser->ast))
links (fn [structs names renames]
(->> structs
(mapcat (partial tree-seq vector? identity))
(filter #(and (vector? %)
(contains? names (first %))))
(map #(vector (get renames (first %) (first %))
(-> % second :string)))
set))
old-links (links (concat old-structures old-title-structures)
#{:page-link :hashtag :block-ref}
{:hashtag :page-link})
new-links (links (concat new-block-structures new-title-structures)
#{:page-link :hashtag :block-ref}
{:hashtag :page-link})
removed-links (set/difference old-links new-links)
added-links (set/difference new-links old-links)]
[removed-links added-links]))
25 changes: 22 additions & 3 deletions test/athens/common_events/graph/ops_test.cljc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns athens.common-events.graph.ops-test
(:require
[athens.common-events.fixture :as fixture]
[athens.common-events.graph.ops :as ops]
[clojure.test :as t]))
[athens.common-events.fixture :as fixture]
[athens.common-events.graph.atomic :as atomic-ops]
[athens.common-events.graph.ops :as ops]
[clojure.test :as t]))


(t/use-fixtures :each (partial fixture/integration-test-fixture []))
Expand Down Expand Up @@ -75,4 +76,22 @@
[removed added] (ops/structural-diff @@fixture/connection save-ops)]
(t/is (= #{[:block-ref test-uid-2]} removed))
(t/is (= #{[:page-link "def"] [:block-ref "test-uid-3"]} added)))
(fixture/teardown! setup-repr))

(t/testing "new page link in page rename"
(fixture/setup! setup-repr)
(let [new-title (str "[[" test-t-1 "]] " test-t-2)
save-op (atomic-ops/make-page-rename-op test-t-2 new-title)
[removed added] (ops/structural-diff @@fixture/connection save-op)]
(t/is (= #{} removed))
(t/is (= #{[:page-link test-t-1]} added)))
(fixture/teardown! setup-repr))

(t/testing "new page link in new page"
(fixture/setup! setup-repr)
(let [new-title (str "[[" test-t-1 "]] " test-t-2)
new-page-op (ops/build-page-new-op @@fixture/connection new-title)
[removed added] (ops/structural-diff @@fixture/connection new-page-op)]
(t/is (= #{} removed))
(t/is (= #{[:page-link test-t-1]} added)))
(fixture/teardown! setup-repr))))

0 comments on commit 8a8829b

Please sign in to comment.