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

fix(sync): clear graphs-txid.edn after deleting related graph

update graphs-txid.edn structure
now:
graphs-txid = [user-uuid graph-uuid local-txid]
before:
graphs-txid = [graph-uuid local-txid]
rcmerci 3 лет назад
Родитель
Сommit
1ef5a2f59a
3 измененных файлов с 108 добавлено и 77 удалено
  1. 24 11
      src/main/frontend/dicts.cljc
  2. 73 58
      src/main/frontend/fs/sync.cljs
  3. 11 8
      src/main/frontend/handler/file_sync.cljs

+ 24 - 11
src/main/frontend/dicts.cljc

@@ -324,6 +324,7 @@
         :select.graph/add-graph "Yes, add another graph"
 
         :file-sync/other-user-graph "Current local graph is bound to other user's remote graph. So can't start syncing."
+        :file-sync/graph-deleted "Current remote graph has been deleted"
         }
 
    :de {:help/about "Über Logseq"
@@ -599,7 +600,9 @@
         :user/delete-account "Konto löschen"
         :user/delete-your-account "Ihr Konto löschen"
 
-        :file-sync/other-user-graph "Aktuelle lokale Grafik ist an das Remote-Graph des anderen Benutzers gebunden. Kann also nicht mit der Synchronisierung beginnen."}
+        :file-sync/other-user-graph "Aktuelle lokale Grafik ist an das Remote-Graph des anderen Benutzers gebunden. Kann also nicht mit der Synchronisierung beginnen."
+        :file-sync/graph-deleted "Das aktuelle Ferndiagramm wurde gelöscht"
+        }
 
    :fr {:help/about "A propos de Logseq"
         :help/bug "Signaler une anomalie"
@@ -737,7 +740,8 @@
         :dark "Foncé"
         :remove-background "Retirer le fond"
 
-        :file-sync/other-user-graph "Le graphique local actuel est lié à l'autre graphique à distance de l'utilisateur. Alors ne peut pas commencer à la synchroniser."}
+        :file-sync/other-user-graph "Le graphique local actuel est lié à l'autre graphique à distance de l'utilisateur. Alors ne peut pas commencer à la synchroniser."
+        :file-sync/graph-deleted "Le graphe distant actuel a été supprimé"}
 
    :zh-CN {:on-boarding/demo-graph "这是一个图谱的样例,在上面做的修改不会被保存,除非打开本地文件夹"
            :on-boarding/add-graph "添加图谱"
@@ -1041,7 +1045,8 @@
 
            :command-palette/prompt "输入指令"
 
-           :file-sync/other-user-graph "当前本地 graph 绑定在其他用户的远程 graph 上。因此无法启动同步。"}
+           :file-sync/other-user-graph "当前本地 graph 绑定在其他用户的远程 graph 上。因此无法启动同步。"
+           :file-sync/graph-deleted "当前远程 graph 已经删除"}
 
    :zh-Hant {:on-boarding/demo-graph "This is a demo graph, changes will not be saved until you open a local folder."
              :on-boarding/add-graph "Add a graph"
@@ -1205,7 +1210,8 @@
              :pdf/copy-text "復製文本"
              :pdf/linked-ref "轉到註解"
 
-             :file-sync/other-user-graph "當前本地 graph 綁定到其他用戶的遠程 graph 上。因此無法啟動同步。"}
+             :file-sync/other-user-graph "當前本地 graph 綁定到其他用戶的遠程 graph 上。因此無法啟動同步。"
+             :file-sync/graph-deleted "當前遠程 graph 已被刪除"}
 
    :af {:on-boarding/demo-graph "This is a demo graph, changes will not be saved until you open a local folder."
         :on-boarding/add-graph "Add a graph"
@@ -1347,7 +1353,8 @@
         :white "Wit"
         :dark "Swart"
 
-        :file-sync/other-user-graph "Huidige plaaslike grafiek is gebonde aan ander gebruiker se afgeleë grafiek. So kan nie begin om te sinkroniseer nie."}
+        :file-sync/other-user-graph "Huidige plaaslike grafiek is gebonde aan ander gebruiker se afgeleë grafiek. So kan nie begin om te sinkroniseer nie."
+        :file-sync/graph-deleted "Huidige afstandgrafiek is geskrap"}
 
    :es {:on-boarding/add-graph "Añade un grafo"
         :help/about "Acerca de Logseq"
@@ -1547,7 +1554,8 @@
         :select.graph/prompt "Seleccione un grafo"
         :select.graph/empty-placeholder-description "No encontramos un grafo. Queries añadir otro?"
         :select.graph/add-graph "Si, añadame otro grafo"
-        :file-sync/other-user-graph "El gráfico local actual está unido al gráfico remoto de otro usuario. Así que no se puede empezar a sincronizar"}
+        :file-sync/other-user-graph "El gráfico local actual está unido al gráfico remoto de otro usuario. Así que no se puede empezar a sincronizar"
+        :file-sync/graph-deleted "El gráfico remoto actual se ha eliminado"}
 
    :nb-NO {:on-boarding/demo-graph "This is a demo graph, changes will not be saved until you open a local folder."
            :on-boarding/add-graph "Add a graph"
@@ -1845,7 +1853,8 @@
            :select.graph/empty-placeholder-description "Ingen grafer matcher. Vil du legge til en ny?"
            :select.graph/add-graph "Ja, legg til en ny graf"
 
-           :file-sync/other-user-graph "Nåværende lokal graf er bundet til annen brukers fjernkontroll. Så kan ikke begynne å synkronisere."}
+           :file-sync/other-user-graph "Nåværende lokal graf er bundet til annen brukers fjernkontroll. Så kan ikke begynne å synkronisere."
+           :file-sync/graph-deleted "Nåværende fjernkontrollen er slettet"}
 
    :pt-BR {:on-boarding/demo-graph "Esse é um gráfico de demonstração, mudanças não serão salvas enquanto uma pasta local não for aberta."
            :on-boarding/add-graph "Adicionar gráfico"
@@ -2155,7 +2164,8 @@
            :settings-page/plugin-system "Sistema de Plugins"
            :settings-page/network-proxy "Proxy de Rede"
 
-           :file-sync/other-user-graph "O gráfico local atual é obrigado ao gráfico remoto de outro usuário. Portanto, não consigo iniciar a sincronização."}
+           :file-sync/other-user-graph "O gráfico local atual é obrigado ao gráfico remoto de outro usuário. Portanto, não consigo iniciar a sincronização."
+           :file-sync/graph-deleted "O gráfico remoto atual foi excluído"}
 
    :pt-PT {:on-boarding/demo-graph "Isto é um grafo de demonstração, nenhuma mudança será guardada até abrir uma pasta local."
            :on-boarding/add-graph "Adicionar grafo"
@@ -2447,7 +2457,8 @@
         :select.graph/empty-placeholder-description "Sem grafos correspondentes. Quer adicionar outro?"
         :select.graph/add-graph "Sim, adicionar outro grafo"
 
-        :file-sync/other-user-graph "O gráfico local atual é obrigado ao gráfico remoto de outro usuário. Portanto, não consigo iniciar a sincronização."}
+        :file-sync/other-user-graph "O gráfico local atual é obrigado ao gráfico remoto de outro usuário. Portanto, não consigo iniciar a sincronização."
+        :file-sync/graph-deleted "O gráfico remoto atual foi excluído"}
 
    :ru {:on-boarding/demo-graph "Это демонстрационный граф, изменения не будут сохранены, пока вы не откроете локальный файл."
         :on-boarding/add-graph "Добавить новый граф"
@@ -2724,7 +2735,8 @@
 
         :command-palette/prompt "Набери команду"
 
-        :file-sync/other-user-graph "Текущий локальный график привязан к удаленному диаграмму другого пользователя. Так что не могу начать синхронизирование"}
+        :file-sync/other-user-graph "Текущий локальный график привязан к удаленному диаграмму другого пользователя. Так что не могу начать синхронизирование"
+        :file-sync/graph-deleted "Текущий удаленный график был удален"}
 
    :ja {:tutorial/text #?(:cljs (rc/inline "tutorial-ja.md")
                                 :default "tutorial-ja.md")
@@ -3027,7 +3039,8 @@
         :select.graph/empty-placeholder-description "マッチするグラフがありません。新しいグラフを追加しますか?"
         :select.graph/add-graph "はい、新規グラフを追加します。"
 
-        :file-sync/other-user-graph "現在のローカルグラフは他のユーザーのリモートグラフにバインドされています。同期を開始できません。"}
+        :file-sync/other-user-graph "現在のローカルグラフは他のユーザーのリモートグラフにバインドされています。同期を開始できません。"
+        :file-sync/graph-deleted "現在のリモートグラフが削除されました"}
 
    :tongue/fallback :en})
 

+ 73 - 58
src/main/frontend/fs/sync.cljs

@@ -22,7 +22,7 @@
 ;;; ### Commentary
 ;; file-sync related local files/dirs:
 ;; - logseq/graphs-txid.edn
-;;   this file contains graph-uuid & transaction-id
+;;   this file contains [user-uuid graph-uuid transaction-id]
 ;;   graph-uuid: the unique identifier of the graph on the server
 ;;   transaction-id: sync progress of local files
 ;; - logseq/version-files
@@ -107,8 +107,12 @@
 
 (def graphs-txid (persist-var/persist-var nil "graphs-txid"))
 
-(defn- update-graphs-txid! [latest-txid graph-uuid repo]
-  (persist-var/-reset-value! graphs-txid [graph-uuid latest-txid] repo)
+(defn update-graphs-txid! [latest-txid graph-uuid user-uuid repo]
+  (persist-var/-reset-value! graphs-txid [user-uuid graph-uuid latest-txid] repo)
+  (persist-var/persist-save graphs-txid))
+
+(defn clear-graphs-txid! [repo]
+  (persist-var/-reset-value! graphs-txid nil repo)
   (persist-var/persist-save graphs-txid))
 
 (defn- ws-stop! [*ws]
@@ -143,7 +147,7 @@
     remote-changes-chan))
 
 (defn- get-json-body [body]
-  (or (and (map? body) body)
+  (or (and (not (string? body)) body)
       (or (string/blank? body) nil)
       (js->clj (js/JSON.parse body) :keywordize-keys true)))
 
@@ -555,7 +559,7 @@
                                       (js/decodeURIComponent (:FilePath %))
                                       (:LastModified %)
                                       true nil))
-                (:Files r))))))
+                r)))))
 
   (get-remote-graph [this graph-name-opt graph-uuid-opt]
     {:pre [(or graph-name-opt graph-uuid-opt)]}
@@ -622,7 +626,7 @@
 
 (defn- apply-filetxns-partitions
   "won't call update-graph-txid! when *txid is nil"
-  [*sync-state graph-uuid base-path filetxns-partitions repo *txid *stopped]
+  [*sync-state user-uuid graph-uuid base-path filetxns-partitions repo *txid *stopped]
   (go-loop [filetxns-partitions* filetxns-partitions]
     (if @*stopped
       {:stop true}
@@ -637,7 +641,7 @@
             (let [latest-txid (apply max (map #(.-txid ^FileTxn %) filetxns))]
               (when *txid
                 (reset! *txid latest-txid)
-                (update-graphs-txid! latest-txid graph-uuid repo))
+                (update-graphs-txid! latest-txid graph-uuid user-uuid repo))
               (recur (next filetxns-partitions*)))))))))
 
 (defmulti need-sync-remote? (fn [v] (cond
@@ -732,22 +736,21 @@
   (sync-local->remote-all-files! [this] "compare all local files to remote ones, sync when not equal.
   if local-txid != remote-txid, return {:need-sync-remote true}"))
 
-(defrecord Remote->LocalSyncer [graph-uuid base-path repo *txid *sync-state
+(defrecord Remote->LocalSyncer [user-uuid graph-uuid base-path repo *txid *sync-state
                               ^:mutable local->remote-syncer *stopped]
   Object
   (set-local->remote-syncer! [_ s] (set! local->remote-syncer s))
   (sync-files-remote->local!
     [_ relative-filepaths latest-txid]
     (go
-      (if-let [user-uuid (user/user-uuid)]
-        (let [partitioned-filetxns
+      (let [partitioned-filetxns
               (sequence (filepaths->partitioned-filetxns 10 graph-uuid user-uuid)
                         relative-filepaths)
               r
               (if (empty? (flatten partitioned-filetxns))
                 {:succ true}
                 (<! (apply-filetxns-partitions
-                     *sync-state graph-uuid base-path partitioned-filetxns repo
+                     *sync-state user-uuid graph-uuid base-path partitioned-filetxns repo
                      nil *stopped)))]
           (cond
             (instance? ExceptionInfo r)
@@ -757,11 +760,9 @@
             {:stop true}
 
             :else
-            (do (update-graphs-txid! latest-txid graph-uuid repo)
+            (do (update-graphs-txid! latest-txid graph-uuid user-uuid repo)
                 (reset! *txid latest-txid)
-                {:succ true})))
-        ;; not found user-uuid
-        {:unknown (ex-info "user-uuid not found" {})})))
+                {:succ true})))))
 
   IRemote->LocalSync
   (stop-remote->local! [_] (vreset! *stopped true))
@@ -785,11 +786,11 @@
 
                         ;; TODO: precheck etag
                         (if (empty? (flatten partitioned-filetxns))
-                          (do (update-graphs-txid! latest-txid graph-uuid repo)
+                          (do (update-graphs-txid! latest-txid graph-uuid user-uuid repo)
                               (reset! *txid latest-txid)
                               {:succ true})
                           (<! (apply-filetxns-partitions
-                               *sync-state graph-uuid base-path partitioned-filetxns repo *txid *stopped)))))))))]
+                               *sync-state user-uuid graph-uuid base-path partitioned-filetxns repo *txid *stopped)))))))))]
         (cond
           (instance? ExceptionInfo r)
           {:unknown r}
@@ -862,7 +863,7 @@
 
 
 (defrecord ^:large-vars/cleanup-todo
-    Local->RemoteSyncer [graph-uuid base-path repo *sync-state
+    Local->RemoteSyncer [user-uuid graph-uuid base-path repo *sync-state
                          ^:mutable rate *txid ^:mutable remote->local-syncer stop-chan ^:mutable stopped]
     Object
     (filter-file-change-events-fn [this]
@@ -950,7 +951,7 @@
                   (do
                     (println "sync-local->remote! update txid" r*)
                     ;; persist txid
-                    (update-graphs-txid! r* graph-uuid repo)
+                    (update-graphs-txid! r* graph-uuid user-uuid repo)
                     (reset! *txid r*)
                     {:succ true})
 
@@ -1196,15 +1197,15 @@
       (debug/pprint ["stop sync-manager, graph-uuid" graph-uuid "base-path" base-path])
       (swap! *sync-state sync-state--update-state ::stop))))
 
-(defn sync-manager [graph-uuid base-path repo txid *sync-state full-sync-chan stop-sync-chan
+(defn sync-manager [user-uuid graph-uuid base-path repo txid *sync-state full-sync-chan stop-sync-chan
                     remote->local-sync-chan local->remote-sync-chan local-changes-chan]
   (let [*txid (atom txid)
-        local->remote-syncer (->Local->RemoteSyncer graph-uuid
+        local->remote-syncer (->Local->RemoteSyncer user-uuid graph-uuid
                                                     base-path
                                                     repo *sync-state
                                                     20000
                                                     *txid nil (chan) false)
-        remote->local-syncer (->Remote->LocalSyncer graph-uuid
+        remote->local-syncer (->Remote->LocalSyncer user-uuid graph-uuid
                                                     base-path
                                                     repo *txid *sync-state nil (volatile! false))]
     (.set-remote->local-syncer! local->remote-syncer remote->local-syncer)
@@ -1225,52 +1226,66 @@
 
 
 (defn- check-graph-belong-to-current-user
-  [graph-uuid]
+  [current-user-uuid graph-user-uuid]
+  (let [result (= current-user-uuid graph-user-uuid)]
+    (when-not result
+      (notification/show! (t :file-sync/other-user-graph) :warning false))
+    result))
+
+(defn check-remote-graph-exists
+  [local-graph-uuid]
   (go
     (let [result (->> (<! (list-remote-graphs remoteapi))
                       :Graphs
                       (mapv :GraphUUID)
                       set
-                      (#(contains? % graph-uuid)))]
+                      (#(contains? % local-graph-uuid)))]
       (when-not result
-        (notification/show! (t :file-sync/other-user-graph) :warning false))
+        (notification/show! (t :file-sync/graph-deleted) :warning false))
       result)))
 
 (defn sync-start []
-  (let [graph-uuid (first @graphs-txid)
-        txid (second @graphs-txid)
+  (let [[user-uuid graph-uuid txid] @graphs-txid
         *sync-state (atom (sync-state))
-        sm (sync-manager graph-uuid
-                         (config/get-repo-dir (state/get-current-repo)) (state/get-current-repo)
+        current-user-uuid (user/user-uuid)
+        repo (state/get-current-repo)
+        sm (sync-manager current-user-uuid graph-uuid
+                         (config/get-repo-dir repo) repo
                          txid *sync-state full-sync-chan stop-sync-chan remote->local-sync-chan local->remote-sync-chan
                          local-changes-chan)]
-    ;; check this graph belong to current logged-in user
     (go
-      (when (<! (check-graph-belong-to-current-user graph-uuid))
-        ;; set-env
-        (set-env rsapi config/FILE-SYNC-PROD?)
-
-        ;; drain `local-changes-chan`
-        (->> (repeatedly #(poll! local-changes-chan))
-             (take-while identity))
-        (poll! stop-sync-chan)
-        ;; update global state when *sync-state changes
-        (add-watch *sync-state ::update-global-state
-                   (fn [_ _ _ n]
-                     (state/set-file-sync-state n)))
-        (.start sm)
-
-        (state/set-file-sync-manager sm)
-
-        (offer! full-sync-chan true)
-
-        ;; watch :network/online?
-        (add-watch (rum/cursor state/state :network/online?) "sync-manage"
-                   (fn [_k _r _o n]
-                     (when (false? n)
-                       (sync-stop))))
-        ;; watch :auth/id-token
-        (add-watch (rum/cursor state/state :auth/id-token) "sync-manage"
-                   (fn [_k _r _o n]
-                     (when (nil? n)
-                       (sync-stop))))))))
+      ;; 1. if remote graph has been deleted, clear graphs-txid.edn
+      ;; 2. if graphs-txid.edn's content isn't [user-uuid graph-uuid txid], clear it
+      (if (not= 3 (count @graphs-txid))
+        (clear-graphs-txid! repo)
+        (when (check-graph-belong-to-current-user current-user-uuid user-uuid)
+          (if-not (<! (check-remote-graph-exists graph-uuid))
+            (clear-graphs-txid! repo)
+            (do
+              ;; set-env
+              (set-env rsapi config/FILE-SYNC-PROD?)
+
+              ;; drain `local-changes-chan`
+              (->> (repeatedly #(poll! local-changes-chan))
+                   (take-while identity))
+              (poll! stop-sync-chan)
+              ;; update global state when *sync-state changes
+              (add-watch *sync-state ::update-global-state
+                         (fn [_ _ _ n]
+                           (state/set-file-sync-state n)))
+              (.start sm)
+
+              (state/set-file-sync-manager sm)
+
+              (offer! full-sync-chan true)
+
+              ;; watch :network/online?
+              (add-watch (rum/cursor state/state :network/online?) "sync-manage"
+                         (fn [_k _r _o n]
+                           (when (false? n)
+                             (sync-stop))))
+              ;; watch :auth/id-token
+              (add-watch (rum/cursor state/state :auth/id-token) "sync-manage"
+                         (fn [_k _r _o n]
+                           (when (nil? n)
+                             (sync-stop)))))))))))

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

@@ -9,14 +9,15 @@
             [frontend.handler.notification :as notification]
             [frontend.state :as state]
             [frontend.util :as util]
-            [frontend.util.persist-var :as persist-var]))
+            [frontend.util.persist-var :as persist-var]
+            [frontend.handler.user :as user]))
 
 (def hiding-login&file-sync (not config/dev?))
 (def refresh-file-sync-component (atom false))
 
 (defn graph-txid-exists?
   []
-  (let [[graph-uuid _txid] @sync/graphs-txid]
+  (let [[_user-uuid graph-uuid _txid] @sync/graphs-txid]
     (some? graph-uuid)))
 
 
@@ -28,8 +29,7 @@
       (if (and (not (instance? ExceptionInfo r))
                (string? r))
         (do
-          (persist-var/-reset-value! sync/graphs-txid [r 0] (state/get-current-repo))
-          (persist-var/persist-save sync/graphs-txid)
+          (sync/update-graphs-txid! 0 r (user/user-uuid) (state/get-current-repo))
           (swap! refresh-file-sync-component not))
         (if (= 404 (get-in (ex-data r) [:err :status]))
           (notification/show! (str "Create graph failed: already existed graph: " name) :warning)
@@ -42,15 +42,18 @@
     (let [r (<! (sync/delete-graph sync/remoteapi graph-uuid))]
       (if (instance? ExceptionInfo r)
         (notification/show! (str "Delete graph failed: " graph-uuid) :warning)
-        (notification/show! (str "Graph deleted") :success)))))
+        (let [[_ local-graph-uuid _] @sync/graphs-txid]
+          (when (= graph-uuid local-graph-uuid)
+            (sync/clear-graphs-txid! (state/get-current-repo))
+            (swap! refresh-file-sync-component not))
+          (notification/show! (str "Graph deleted") :success))))))
 
 (defn list-graphs
   []
   (go (:Graphs (<! (sync/list-remote-graphs sync/remoteapi)))))
 
 (defn switch-graph [graph-uuid]
-  (persist-var/-reset-value! sync/graphs-txid [graph-uuid 0] (state/get-current-repo))
-  (persist-var/persist-save sync/graphs-txid)
+  (sync/update-graphs-txid! 0 graph-uuid (user/user-uuid) (state/get-current-repo))
   (swap! refresh-file-sync-component not))
 
 (defn- download-version-file [graph-uuid file-uuid version-uuid]
@@ -90,4 +93,4 @@
                                        (util/time-ago (tc/from-string (:CreateTime version)))]]))]
                                 :success false)))))))
 
-(defn get-current-graph-uuid [] (first @sync/graphs-txid))
+(defn get-current-graph-uuid [] (second @sync/graphs-txid))