Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Page link creation reporting also from page titles #2130

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))))