Browse Source

fix: disable creating remote graph when sync is not started yet

Also, don't show `password is required` when starting the sync.
Tienson Qin 3 years ago
parent
commit
376cda0bd3

+ 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"}}]))
 

+ 56 - 34
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,43 +2756,55 @@
         (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 []
   (let [*sync-state                 (atom (sync-state))
         current-user-uuid           (user/user-uuid)
         repo                        (state/get-current-repo)]
-    (go
-      (when @network-online-cursor
-        ;; stop previous sync
-        (<! (<sync-stop))
-
-        (<! (p->c (persist-var/-load graphs-txid)))
-
-        (let [[user-uuid graph-uuid txid] @graphs-txid]
-          (when (and user-uuid graph-uuid txid
-                     (user/logged-in?)
-                     repo
-                     (not (config/demo-graph? repo)))
-            (when-some [sm (sync-manager-singleton current-user-uuid graph-uuid
-                                                   (config/get-repo-dir repo) repo
-                                                   txid *sync-state)]
-              (when (check-graph-belong-to-current-user current-user-uuid user-uuid)
-                (if-not (<! (<check-remote-graph-exists graph-uuid)) ; remote graph has been deleted
-                  (clear-graphs-txid! repo)
-                  (do
-                    (state/set-file-sync-state repo @*sync-state)
-                    (state/set-file-sync-manager sm)
-
-                    ;; update global state when *sync-state changes
-                    (add-watch *sync-state ::update-global-state
-                               (fn [_ _ _ n]
-                                 (state/set-file-sync-state repo n)))
-
-                    (.start sm)
-
-                    (offer! remote->local-full-sync-chan true)
-                    (offer! full-sync-chan true)))))))))))
+    (when (graph-sync-off? repo)
+      (go
+        (when @network-online-cursor
+          (<! (p->c (persist-var/-load graphs-txid)))
+
+          (let [[user-uuid graph-uuid txid] @graphs-txid]
+            (when (and user-uuid graph-uuid txid
+                       (user/logged-in?)
+                       repo
+                       (not (config/demo-graph? repo)))
+              (when-some [sm (sync-manager-singleton current-user-uuid graph-uuid
+                                                     (config/get-repo-dir repo) repo
+                                                     txid *sync-state)]
+                (when (check-graph-belong-to-current-user current-user-uuid user-uuid)
+                  (if-not (<! (<check-remote-graph-exists graph-uuid)) ; remote graph has been deleted
+                    (clear-graphs-txid! repo)
+                    (do
+                      (state/set-file-sync-state repo @*sync-state)
+                      (state/set-file-sync-manager sm)
+
+                      ;; update global state when *sync-state changes
+                      (add-watch *sync-state ::update-global-state
+                                 (fn [_ _ _ n]
+                                   (state/set-file-sync-state repo n)))
+
+                      (.start sm)
+
+                      (offer! remote->local-full-sync-chan true)
+                      (offer! full-sync-chan true))))))))))))
 
 ;;; ### some add-watches
 

+ 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)))