Browse Source

fix(sync): config.edn & custom.css overwrite by remote versions (#9138)

fix(sync): config.edn & custom.css overwrite by remote versions
rcmerci 2 years ago
parent
commit
b8b16548d2

+ 0 - 2
src/main/frontend/components/whiteboard.cljs

@@ -10,7 +10,6 @@
             [frontend.db.model :as model]
             [frontend.handler.common :as common-handler]
             [frontend.handler.route :as route-handler]
-            [frontend.handler.config :as config-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
             [frontend.rum :refer [use-bounding-client-rect use-breakpoint
                                   use-click-outside]]
@@ -311,7 +310,6 @@
     (ui/button (t :on-boarding/welcome-whiteboard-modal-skip) :on-click close-fn :background "gray" :class "opacity-60")
     (ui/button (t :on-boarding/welcome-whiteboard-modal-start)
                :on-click (fn []
-                           (config-handler/set-config! :feature/enable-whiteboards? true)
                            (quick-tour/ready
                             (fn []
                               (quick-tour/start-whiteboard)

+ 6 - 1
src/main/frontend/config.cljs

@@ -8,7 +8,9 @@
             [logseq.common.path :as path]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.util :as gp-util]
-            [shadow.resource :as rc]))
+            [shadow.resource :as rc]
+            [goog.crypt.Md5]
+            [goog.crypt :as crypt]))
 
 (goog-define DEV-RELEASE false)
 (defonce dev-release? DEV-RELEASE)
@@ -338,6 +340,9 @@
 (def export-css-file "export.css")
 (def custom-js-file "custom.js")
 (def config-default-content (rc/inline "config.edn"))
+(def config-default-content-md5 (let [md5 (new crypt/Md5)]
+                                  (.update md5 (crypt/stringToUtf8ByteArray config-default-content))
+                                  (crypt/byteArrayToHex (.digest md5))))
 
 ;; NOTE: repo-url is the unique identifier of a repo.
 ;; - `local` => in-memory demo graph

+ 35 - 25
src/main/frontend/fs/sync.cljs

@@ -592,10 +592,16 @@
   "when diff all remote files and local files, following remote files always need to download(when checksum not matched),
   even local-file's last-modified > remote-file's last-modified.
   because these files will be auto created when the graph created, we dont want them to re-write related remote files."
-  #{"logseq/config.edn" "logseq/custom.css"
-    "pages/contents.md" "pages/contents.org"
+  #{"pages/contents.md" "pages/contents.org"
     "logseq/metadata.edn"})
 
+(def ^:private ignore-default-value-files
+  "when create a new local graph, some files will be created (config.edn, custom.css).
+  And related remote files wins if these files have default template value."
+  #{"logseq/config.edn" "logseq/custom.css"})
+
+(def ^:private empty-custom-css-md5 "d41d8cd98f00b204e9800998ecf8427e")
+
 ;; TODO: use fn some to filter FileMetadata here, it cause too much loop
 (defn diff-file-metadata-sets
   "Find the `FileMetadata`s that exists in s1 and does not exist in s2,
@@ -605,29 +611,33 @@
   keep this `FileMetadata` in result"
   [s1 s2]
   (reduce
-     (fn [result item]
-       (let [path (:path item)
-             lower-case-path (some-> path string/lower-case)
-             ;; encrypted-path (:encrypted-path item)
-             checksum (:etag item)
-             last-modified (:last-modified item)]
-         (if (some
-              #(cond
-                 (not= lower-case-path (some-> (:path %) string/lower-case))
-                 false
-                 (= checksum (:etag %))
-                 true
-                 (>= last-modified (:last-modified %))
-                 false
-                 ;; these special files have higher priority in s1
-                 (contains? higher-priority-remote-files path)
-                 false
-                 (< last-modified (:last-modified %))
-                 true)
-              s2)
-           result
-           (conj result item))))
-     #{} s1))
+   (fn [result item]
+     (let [path (:path item)
+           lower-case-path (some-> path string/lower-case)
+           ;; encrypted-path (:encrypted-path item)
+           checksum (:etag item)
+           last-modified (:last-modified item)]
+       (if (some
+            #(cond
+               (not= lower-case-path (some-> (:path %) string/lower-case))
+               false
+               (= checksum (:etag %))
+               true
+               (>= last-modified (:last-modified %))
+               false
+               ;; these special files have higher priority in s1
+               (contains? higher-priority-remote-files path)
+               false
+               ;; higher priority in s1 when config.edn=default value or empty custom.css
+               (and (contains? ignore-default-value-files path)
+                    (#{config/config-default-content-md5 empty-custom-css-md5} (:etag %)))
+               false
+               (< last-modified (:last-modified %))
+               true)
+            s2)
+         result
+         (conj result item))))
+   #{} s1))
 
 (comment
  (defn map->FileMetadata [m]

+ 1 - 5
src/main/frontend/handler/events.cljs

@@ -37,7 +37,6 @@
             [frontend.fs.watcher-handler :as fs-watcher]
             [frontend.handler.command-palette :as cp]
             [frontend.handler.common :as common-handler]
-            [frontend.handler.config :as config-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.file :as file-handler]
             [frontend.handler.file-sync :as file-sync-handler]
@@ -89,10 +88,7 @@
 (defn- enable-beta-features!
   []
   (when-not (false? (state/enable-sync?)) ; user turns it off
-    (file-sync-handler/set-sync-enabled! true))
-
-  (when-not (false? (state/enable-whiteboards?))
-    (config-handler/set-config! :feature/enable-whiteboards? true)))
+    (file-sync-handler/set-sync-enabled! true)))
 
 (defmethod handle :user/fetch-info-and-graphs [[_]]
   (state/set-state! [:ui/loading? :login] false)