Browse Source

Merge branch 'master' into refactor/refs-from-property-values

Tienson Qin 3 years ago
parent
commit
585e0dbf9d

+ 37 - 32
src/main/frontend/components/file_sync.cljs

@@ -182,11 +182,12 @@
 
         status                 (:state sync-state)
         status                 (or (nil? status) (keyword (name status)))
-        off?                   (file-sync-handler/sync-off? sync-state)
+        off?                   (fs-sync/sync-off? sync-state)
         full-syncing?          (contains? #{:local->remote-full-sync :remote->local-full-sync} status)
         syncing?               (or full-syncing? (contains? #{:local->remote :remote->local} status))
         idle?                  (contains? #{:idle} status)
-        need-password?         (contains? #{:need-password} status)
+        need-password?         (and (contains? #{:need-password} status)
+                                    (not (fs-sync/graph-encrypted?)))
         queuing?               (and idle? (boolean (seq queuing-files)))
         no-active-files?       (empty? (concat downloading-files queuing-files uploading-files))
         create-remote-graph-fn #(when (and current-repo (not (config/demo-graph? current-repo)))
@@ -198,37 +199,41 @@
                                           (create-remote-graph-panel current-repo graph-name close-fn))]
 
                                     (state/set-modal! confirm-fn {:center? true :close-btn? false})))
-        turn-on                #(async/go
-                                  (async/<! (p->c (persist-var/-load fs-sync/graphs-txid)))
-                                  (cond
-                                    @*beta-unavailable?
-                                    (state/pub-event! [:file-sync/onboarding-tip :unavailable])
-
-                                    ;; current graph belong to other user, do nothing
-                                    (and (first @fs-sync/graphs-txid)
-                                         (not (fs-sync/check-graph-belong-to-current-user (user-handler/user-uuid)
-                                                                                          (first @fs-sync/graphs-txid))))
-                                    nil
-
-                                    (and synced-file-graph?
-                                         (file-sync-handler/graph-sync-off? current-repo)
-                                         (second @fs-sync/graphs-txid)
-                                         (async/<! (fs-sync/<check-remote-graph-exists (second @fs-sync/graphs-txid))))
-                                    (fs-sync/sync-start)
-
-
-                                    ;; remote graph already has been deleted, clear repos first, then create-remote-graph
-                                    synced-file-graph?      ; <check-remote-graph-exists -> false
-                                    (do (state/set-repos!
-                                         (map (fn [r]
-                                                (if (= (:url r) current-repo)
-                                                  (dissoc r :GraphUUID :GraphName :remote?)
-                                                  r))
+        turn-on                (fn []
+                                 (when-not (file-sync-handler/current-graph-sync-on?)
+                                   (async/go
+                                     (async/<! (p->c (persist-var/-load fs-sync/graphs-txid)))
+                                     (cond
+                                       @*beta-unavailable?
+                                       (state/pub-event! [:file-sync/onboarding-tip :unavailable])
+
+                                       ;; current graph belong to other user, do nothing
+                                       (and (first @fs-sync/graphs-txid)
+                                            (not (fs-sync/check-graph-belong-to-current-user (user-handler/user-uuid)
+                                                                                             (first @fs-sync/graphs-txid))))
+                                       nil
+
+                                       (and synced-file-graph?
+                                            (fs-sync/graph-sync-off? current-repo)
+                                            (second @fs-sync/graphs-txid)
+                                            (async/<! (fs-sync/<check-remote-graph-exists (second @fs-sync/graphs-txid))))
+                                       (fs-sync/sync-start)
+
+                                       ;; remote graph already has been deleted, clear repos first, then create-remote-graph
+                                       synced-file-graph?      ; <check-remote-graph-exists -> false
+                                       (do (state/set-repos!
+                                            (map (fn [r]
+                                                   (if (= (:url r) current-repo)
+                                                     (dissoc r :GraphUUID :GraphName :remote?)
+                                                     r))
                                               (state/get-repos)))
-                                        (create-remote-graph-fn))
+                                           (create-remote-graph-fn))
 
-                                    :else
-                                    (create-remote-graph-fn)))]
+                                       (second @fs-sync/graphs-txid) ; sync not started yet
+                                       nil
+
+                                       :else
+                                       (create-remote-graph-fn)))))]
 
     (if creating-remote-graph?
       (ui/loading "")
@@ -262,7 +267,7 @@
                (if need-password?
                  [{:title   [:div.file-item
                              (ui/icon "lock") "Password is required"]
-                   :options {:on-click #(state/pub-event! [:file-sync/restart])}}]
+                   :options {:on-click fs-sync/sync-need-password!}}]
                  [{:title   [:div.file-item.is-first ""]
                    :options {:class "is-first-placeholder"}}]))
 

+ 1 - 1
src/main/frontend/components/query_table.cljs

@@ -110,7 +110,7 @@
                   keys))
           desc? (desc? *desc? p-desc?)
           result (sort-result-by (fn [item]
-                                   (sort-by-fn sort-by-item item))
+                                   (block/normalize-block (sort-by-fn sort-by-item item)))
                                  desc?
                                  result)]
       [:div.overflow-x-auto {:on-mouse-down (fn [e] (.stopPropagation e))

+ 29 - 4
src/main/frontend/format/block.cljs

@@ -1,13 +1,15 @@
 (ns frontend.format.block
   "Block code needed by app but not graph-parser"
-  (:require [clojure.string :as string]
-            [logseq.graph-parser.block :as gp-block]
+  (:require ["@sentry/react" :as Sentry]
+            [cljs-time.format :as tf]
+            [clojure.string :as string]
             [frontend.config :as config]
+            [frontend.date :as date]
             [frontend.db :as db]
             [frontend.format :as format]
-            [frontend.state :as state]
             [frontend.handler.notification :as notification]
-            ["@sentry/react" :as Sentry]
+            [frontend.state :as state]
+            [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.property :as gp-property]
             [logseq.graph-parser.mldoc :as gp-mldoc]
@@ -38,6 +40,29 @@ and handles unexpected failure."
   ([original-page-name with-id? with-timestamp?]
    (gp-block/page-name->map original-page-name with-id? (db/get-db (state/get-current-repo)) with-timestamp? (state/get-date-formatter))))
 
+(defn- normalize-as-percentage
+  ([block]
+   (some->> block
+            str
+            (re-matches #"(-?\d+\.?\d*)%")
+            second
+            (#(/ % 100)))))
+
+(defn- normalize-as-date
+  ([block]
+   (some->> block
+            str
+            date/valid?
+            (tf/unparse date/custom-formatter))))
+
+(defn normalize-block
+  "Normalizes supported formats such as dates and percentages."
+  ([block]
+   (->> [normalize-as-percentage normalize-as-date identity]
+        (map #(% block))
+        (remove nil?)
+        (first))))
+
 (defn parse-block
   ([block]
    (parse-block block nil))

+ 28 - 7
src/main/frontend/fs/sync.cljs

@@ -71,8 +71,9 @@
                  ::local->remote-full-sync
                  ;; remote->local full sync
                  ::remote->local-full-sync
-                 ::stop
-                 ::pause})
+                 ;; snapshot state when switching between apps on iOS
+                 ::pause
+                 ::stop})
 (s/def ::path string?)
 (s/def ::time t/date?)
 (s/def ::remote->local-type #{:delete :update
@@ -1679,6 +1680,10 @@
   [graph-uuid]
   (js/localStorage.removeItem (local-storage-pwd-path graph-uuid)))
 
+(defn get-pwd
+  [graph-uuid]
+  (js/localStorage.getItem (local-storage-pwd-path graph-uuid)))
+
 (defn remove-all-pwd!
   []
   (doseq [k (filter #(string/starts-with? % "encrypted-pwd/") (js->clj (js-keys js/localStorage)))]
@@ -1706,7 +1711,7 @@
   "restore pwd from persisted encrypted-pwd, update `pwd-map`"
   [graph-uuid]
   (go
-    (let [encrypted-pwd (js/localStorage.getItem (local-storage-pwd-path graph-uuid))]
+    (let [encrypted-pwd (get-pwd graph-uuid)]
       (if (nil? encrypted-pwd)
         {:restore-pwd-failed true}
         (let [[salt-value _expired-at gone?]
@@ -2713,6 +2718,11 @@
       (state/set-file-sync-manager nil))
     (reset! current-sm-graph-uuid nil)))
 
+(defn sync-need-password!
+  []
+  (when-let [sm ^SyncManager (state/get-file-sync-manager)]
+    (.need-password sm)))
+
 (defn check-graph-belong-to-current-user
   [current-user-uuid graph-user-uuid]
   (cond
@@ -2746,6 +2756,20 @@
         (notification/show! (t :file-sync/graph-deleted) :warning false))
       result)))
 
+(defn sync-off?
+  [sync-state]
+  (or (nil? sync-state) (sync-state--stopped? sync-state)))
+
+(defn graph-sync-off?
+  "Is sync not running for this `graph`?"
+  [graph]
+  (sync-off? (state/get-file-sync-state graph)))
+
+(defn graph-encrypted?
+  []
+  (when-let [graph-uuid (second @graphs-txid)]
+    (get-pwd graph-uuid)))
+
 (declare network-online-cursor)
 
 (defn sync-start []
@@ -2753,10 +2777,7 @@
         current-user-uuid           (user/user-uuid)
         repo                        (state/get-current-repo)]
     (go
-      (when @network-online-cursor
-        ;; stop previous sync
-        (<! (<sync-stop))
-
+      (when (and (graph-sync-off? repo) @network-online-cursor)
         (<! (p->c (persist-var/-load graphs-txid)))
 
         (let [[user-uuid graph-uuid txid] @graphs-txid]

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

@@ -88,7 +88,7 @@
                                     (vector? (:sync-meta %))
                                     (util/uuid-string? (first (:sync-meta %)))
                                     (util/uuid-string? (second (:sync-meta %)))) repos)
-                    (file-sync-restart!)))))
+                    (sync/sync-start)))))
             (file-sync/maybe-onboarding-show status)))))))
 
 (defmethod handle :user/logout [[_]]
@@ -639,9 +639,6 @@
   (notification/show! "file sync graph count exceed limit" :warning false)
   (file-sync-stop!))
 
-(defmethod handle :file-sync/restart [[_]]
-  (file-sync-restart!))
-
 (defmethod handle :graph/restored [[_ _graph]]
   (mobile/init!)
   (when-not (mobile-util/native-ios?)

+ 0 - 8
src/main/frontend/handler/file_sync.cljs

@@ -205,11 +205,3 @@
 (defn reset-user-state! []
   (vreset! *beta-unavailable? false)
   (state/set-state! :file-sync/onboarding-state nil))
-
-(defn sync-off?
-  [sync-state]
-  (or (nil? sync-state) (sync/sync-state--stopped? sync-state)))
-
-(defn graph-sync-off?
-  [graph]
-  (sync-off? (state/get-file-sync-state graph)))

+ 42 - 0
src/test/frontend/format/block_test.cljs

@@ -0,0 +1,42 @@
+(ns frontend.format.block-test 
+  (:require [cljs.test :refer [deftest testing are]]
+            [frontend.format.block :as block]))
+
+(deftest test-normalize-date
+  (testing "normalize date values"
+    (are [x y] (= (block/normalize-block x) y)
+         "Aug 12th, 2022"
+         "2022-08-12T00:00:00Z"
+
+         "2022-08-12T00:00:00Z"
+         "2022-08-12T00:00:00Z")))
+
+(deftest test-normalize-percentage
+  (testing "normalize percentages"
+    (are [x y] (= (block/normalize-block x) y)
+         "50%"
+         0.5
+
+         "0%"
+         0
+
+         "-5%"
+         -0.05)))
+
+(deftest test-random-values
+  (testing "random values should not be processed"
+    (are [x y] (= (block/normalize-block x) y)
+         "anreanre"
+         "anreanre"
+
+         ""
+         ""
+
+         "a.0%"
+         "a.0%"
+
+         "%"
+         "%"
+
+         "-%"
+         "-%")))