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

Also, don't show `password is required` when starting the sync.
pull/6731/head
Tienson Qin 2022-09-14 17:28:21 +08:00
parent 14e87963ef
commit 376cda0bd3
4 changed files with 87 additions and 71 deletions

View File

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

View File

@ -71,8 +71,9 @@
::local->remote-full-sync ::local->remote-full-sync
;; remote->local full sync ;; remote->local full sync
::remote->local-full-sync ::remote->local-full-sync
::stop ;; snapshot state when switching between apps on iOS
::pause}) ::pause
::stop})
(s/def ::path string?) (s/def ::path string?)
(s/def ::time t/date?) (s/def ::time t/date?)
(s/def ::remote->local-type #{:delete :update (s/def ::remote->local-type #{:delete :update
@ -1679,6 +1680,10 @@
[graph-uuid] [graph-uuid]
(js/localStorage.removeItem (local-storage-pwd-path 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! (defn remove-all-pwd!
[] []
(doseq [k (filter #(string/starts-with? % "encrypted-pwd/") (js->clj (js-keys js/localStorage)))] (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`" "restore pwd from persisted encrypted-pwd, update `pwd-map`"
[graph-uuid] [graph-uuid]
(go (go
(let [encrypted-pwd (js/localStorage.getItem (local-storage-pwd-path graph-uuid))] (let [encrypted-pwd (get-pwd graph-uuid)]
(if (nil? encrypted-pwd) (if (nil? encrypted-pwd)
{:restore-pwd-failed true} {:restore-pwd-failed true}
(let [[salt-value _expired-at gone?] (let [[salt-value _expired-at gone?]
@ -2713,6 +2718,11 @@
(state/set-file-sync-manager nil)) (state/set-file-sync-manager nil))
(reset! current-sm-graph-uuid 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 (defn check-graph-belong-to-current-user
[current-user-uuid graph-user-uuid] [current-user-uuid graph-user-uuid]
(cond (cond
@ -2746,43 +2756,55 @@
(notification/show! (t :file-sync/graph-deleted) :warning false)) (notification/show! (t :file-sync/graph-deleted) :warning false))
result))) 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) (declare network-online-cursor)
(defn sync-start [] (defn sync-start []
(let [*sync-state (atom (sync-state)) (let [*sync-state (atom (sync-state))
current-user-uuid (user/user-uuid) current-user-uuid (user/user-uuid)
repo (state/get-current-repo)] repo (state/get-current-repo)]
(go (when (graph-sync-off? repo)
(when @network-online-cursor (go
;; stop previous sync (when @network-online-cursor
(<! (<sync-stop)) (<! (p->c (persist-var/-load graphs-txid)))
(<! (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)
(let [[user-uuid graph-uuid txid] @graphs-txid] ;; update global state when *sync-state changes
(when (and user-uuid graph-uuid txid (add-watch *sync-state ::update-global-state
(user/logged-in?) (fn [_ _ _ n]
repo (state/set-file-sync-state repo n)))
(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 (.start sm)
(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))))))))))))
(offer! remote->local-full-sync-chan true)
(offer! full-sync-chan true)))))))))))
;;; ### some add-watches ;;; ### some add-watches

View File

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

View File

@ -205,11 +205,3 @@
(defn reset-user-state! [] (defn reset-user-state! []
(vreset! *beta-unavailable? false) (vreset! *beta-unavailable? false)
(state/set-state! :file-sync/onboarding-state nil)) (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)))