From bc3a6142d3f4a7976555139dafd6e1546d4fba4e Mon Sep 17 00:00:00 2001 From: Alex Iwaniuk Date: Mon, 11 Apr 2022 13:58:08 +0100 Subject: [PATCH] feat: Page link creation reporting also from page titles. --- src/cljc/athens/common_events/graph/ops.cljc | 72 +++++++++++-------- test/athens/common_events/graph/ops_test.cljc | 25 ++++++- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/cljc/athens/common_events/graph/ops.cljc b/src/cljc/athens/common_events/graph/ops.cljc index 95b5c1c52c..4616ce2ddd 100644 --- a/src/cljc/athens/common_events/graph/ops.cljc +++ b/src/cljc/athens/common_events/graph/ops.cljc @@ -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])) diff --git a/test/athens/common_events/graph/ops_test.cljc b/test/athens/common_events/graph/ops_test.cljc index 1df5290e3d..0bb5479afd 100644 --- a/test/athens/common_events/graph/ops_test.cljc +++ b/test/athens/common_events/graph/ops_test.cljc @@ -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 [])) @@ -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))))