Просмотр исходного кода

enhance: update timestamp on pages-metadata is watched

Junyi Du 4 лет назад
Родитель
Сommit
1905d1e32c
2 измененных файлов с 51 добавлено и 27 удалено
  1. 10 2
      src/main/frontend/fs/watcher_handler.cljs
  2. 41 25
      src/main/frontend/handler/repo.cljs

+ 10 - 2
src/main/frontend/fs/watcher_handler.cljs

@@ -8,6 +8,7 @@
             [frontend.handler.extract :as extract]
             [frontend.handler.file :as file-handler]
             [frontend.handler.page :as page-handler]
+            [frontend.handler.repo :as repo-handler]
             [frontend.handler.ui :as ui-handler]
             [frontend.util :as util]
             [lambdaisland.glogi :as log]
@@ -44,6 +45,7 @@
   (when dir
     (let [path (util/path-normalize path)
           repo (config/get-local-repo dir)
+          pages-metadata-path (config/get-pages-metadata-path)
           {:keys [mtime]} stat
           db-content (or (db/get-file repo path) "")]
       (when (and (or content (= type "unlink"))
@@ -52,7 +54,7 @@
         (cond
           (and (= "add" type)
                (not= (string/trim content) (string/trim db-content))
-               (not (string/includes? path "logseq/pages-metadata.edn")))
+               (not= path pages-metadata-path))
           (let [backup? (not (string/blank? db-content))]
             (handle-add-and-change! repo path content db-content mtime backup?))
 
@@ -62,7 +64,7 @@
 
           (and (= "change" type)
                (not= (string/trim content) (string/trim db-content))
-               (not (string/includes? path "logseq/pages-metadata.edn")))
+               (not= path pages-metadata-path))
           (when-not (and
                      (string/includes? path (str "/" (config/get-journals-directory) "/"))
                      (or
@@ -84,6 +86,12 @@
             (println "reloading custom.css")
             (ui-handler/add-style-if-exists!))
 
+          ;; When metadata is added to watcher, update timestamps in db accordingly
+          ;; This event is not triggered on re-index
+          (and (contains? #{"add"} type)
+               (= path pages-metadata-path))
+          (p/do! (repo-handler/update-pages-metadata! repo content true))
+
           (contains? #{"add" "change" "unlink"} type)
           nil
 

+ 41 - 25
src/main/frontend/handler/repo.cljs

@@ -5,6 +5,7 @@
             [frontend.config :as config]
             [frontend.date :as date]
             [frontend.db :as db]
+            [frontend.db.model :as db-model]
             [frontend.dicts :as dicts]
             [frontend.encrypt :as encrypt]
             [frontend.format :as format]
@@ -145,32 +146,47 @@
        (state/pub-event! [:page/create-today-journal repo-url])))))
 
 (defn- load-pages-metadata!
-  [repo file-paths files]
-  (try
-    (let [file (config/get-pages-metadata-path)]
-      (when (contains? (set file-paths) file)
-        (when-let [content (some #(when (= (:file/path %) file) (:file/content %)) files)]
-          (let [metadata (common-handler/safe-read-string content "Parsing pages metadata file failed: ")
-                pages (db/get-all-pages repo)
-                pages (zipmap (map :block/name pages) pages)
-                metadata (->>
-                          (filter (fn [{:block/keys [name created-at updated-at]}]
-                                    (when-let [page (get pages name)]
-                                      (and
-                                       (or
-                                        (nil? (:block/created-at page))
-                                        (>= created-at (:block/created-at page)))
-                                       (or
-                                        (nil? (:block/updated-at page))
-                                        (>= updated-at (:block/created-at page)))))) metadata)
-                          (remove nil?))]
-            (when (seq metadata)
-              (db/transact! repo metadata))))))
-    (catch js/Error e
-      (log/error :exception e))))
+  ([repo file-paths files]
+   (load-pages-metadata! repo file-paths files false))
+  ([repo file-paths files force?]
+   (try
+     (let [file (config/get-pages-metadata-path)]
+       (when (contains? (set file-paths) file)
+         (when-let [content (some #(when (= (:file/path %) file) (:file/content %)) files)]
+           (let [metadata (common-handler/safe-read-string content "Parsing pages metadata file failed: ")
+                 pages (db/get-all-pages repo)
+                 pages (zipmap (map :block/name pages) pages)
+                 metadata (->>
+                           (filter (fn [{:block/keys [name created-at updated-at]}]
+                                     (when-let [page (get pages name)]
+                                       (and
+                                        (>= updated-at created-at) ;; metadata validation
+                                        (or force? ;; when force is true, shortcut timestamp range check
+                                            (and (or (nil? (:block/created-at page))
+                                                     (>= created-at (:block/created-at page)))
+                                                 (or (nil? (:block/updated-at page))
+                                                     (>= updated-at (:block/created-at page)))))
+                                        (or ;; persistent metadata is the gold standard
+                                         (not= created-at (:block/created-at page))
+                                         (not= updated-at (:block/created-at page)))))) metadata)
+                           (remove nil?))]
+             (when (seq metadata)
+               (db/transact! repo metadata))))))
+     (catch js/Error e
+       (log/error :exception e)))))
+
+(defn update-pages-metadata!
+  "update pages meta content -> db. Only accept non-encrypted content!"
+  [repo content force?]
+  (let [path (config/get-pages-metadata-path)
+        files [{:file/path path
+                :file/content content}]
+        file-paths [path]]
+    (util/profile "update-pages-metadata!" (load-pages-metadata! repo file-paths files force?))))
 
 (defn- parse-files-and-create-default-files-inner!
   [repo-url files delete-files delete-blocks file-paths first-clone? db-encrypted? re-render? re-render-opts metadata opts]
+  (js/console.log "parse-files-and-create-default-files-inner!") ;; TODO JUNYI
   (let [support-files (filter
                        (fn [file]
                          (let [format (format/get-format (:file/path file))]
@@ -189,7 +205,7 @@
                                 :re-render-root? false
                                 :from-disk? true
                                 :metadata metadata}))
-    (load-pages-metadata! repo-url file-paths files)
+    (load-pages-metadata! repo-url file-paths files true)
     (when first-clone?
       (if (and (not db-encrypted?) (state/enable-encryption? repo-url))
         (state/pub-event! [:modal/encryption-setup-dialog repo-url
@@ -249,7 +265,7 @@
 
   (let [config (or (state/get-config repo-url)
                    (when-let [content (some-> (first (filter #(= (config/get-config-path repo-url) (:file/path %)) nfs-files))
-                                        :file/content)]
+                                              :file/content)]
                      (common-handler/read-config content)))
         relate-path-fn (fn [m k]
                          (some-> (get m k)