summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreltrac <hi@eltr.ac>2026-11-03 13:35:05 +0800
committereltrac <hi@eltr.ac>2026-11-03 13:35:05 +0800
commit37fd5d4d31a7d9cb4dcff4a3f2bfd0802a52efe4 (patch)
tree33fef90887d7dbbaaf91ae85ff0f9c23a74d373f /src
parentffa7c25d273372db3d268d806f1eaf3dac1e7019 (diff)
migration: rehash -> re-normalize #81
calculating hash using old unnormalized URLs will most likely produce the same old invalid hash, this commit normalizes URLs first and then calculate new hash, updating old URLs as well.
Diffstat (limited to 'src')
-rw-r--r--src/weepinbell/migration.clj22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/weepinbell/migration.clj b/src/weepinbell/migration.clj
index 701ed8f..544f5d6 100644
--- a/src/weepinbell/migration.clj
+++ b/src/weepinbell/migration.clj
@@ -6,6 +6,7 @@
[weepinbell.storage.edn-storage :as edns]
[weepinbell.storage.spec]
[weepinbell.metadata.microformats :refer [parse-microformats]]
+ [weepinbell.utils.url :refer [normalize]]
[weepinbell.utils.digest :refer [digest]]
[clojure.java.io :as io]
[fast-edn.core :as edn]))
@@ -19,14 +20,19 @@
(assoc :jf2 (parse-microformats (:microformats wm)))
(dissoc :microformats)))
-(defn rehash
+(defn re-normalize
"Given a Webmention object, recalculate its hash and return a new map."
[wm]
{:pre [(map? wm) (contains? wm :webmention)]}
- (assoc wm :webmention
- (assoc (:webmention wm) :id
- (digest (str (get-in wm [:webmention :source])
- (get-in wm [:webmention :target]))))))
+ (let [{:keys [source target]} (:webmention wm)
+ normal-source (normalize source)
+ normal-target (normalize target)
+ newhash (digest (str normal-source normal-target))]
+ (assoc wm :webmention
+ (-> (:webmention wm)
+ (assoc :source normal-source)
+ (assoc :target normal-target)
+ (assoc :id newhash)))))
(defn edn->h2
"Given an EDN file and a SQLite DB path, migrate data from that
@@ -42,12 +48,12 @@
(fn [wms]
(doseq [wm wms]
(let [id (get-in wm [:webmention :id])]
- (println "Appending webmention" id "to database")
(try
(let [wm (if (contains? wm :microformats)
(mf->jf2 wm) wm)
- wm (rehash wm)]
- (h2/insert-webmention ds wm))
+ wm (re-normalize wm)]
+ (h2/insert-webmention ds wm)
+ (println id "->" (get-in wm [:webmention :id])))
(catch Exception e
(println "Something unexpected happened.")
(println (.getMessage e))))))))