Browse Source

feat(sync): support version files

rcmerci 3 years ago
parent
commit
d714ebba70

+ 1 - 3
src/main/frontend/components/header.cljs

@@ -38,8 +38,6 @@
                    (route-handler/go-to-journals!))}
      (ui/icon "home" {:style {:fontSize ui/icon-size}})]))
 
-(def hiding-login&file-sync true)
-
 (rum/defc login < rum/reactive
   []
   (let [_ (state/sub :auth/id-token)]
@@ -100,7 +98,7 @@
                           :icon (ui/icon "arrow-narrow-down")}) downloading-files)
             (when sync-state
               (map (fn [f] (:time f)
-                     {:title [:div [:div (:path f)] [:div (util/time-ago (:time f))]]})
+                     {:title [:div [:div (:path f)] [:div.opacity-50 (util/time-ago (:time f))]]})
                    (take 10 (.-history sync-state))))))
 
          (cond-> {}

+ 16 - 9
src/main/frontend/components/page_menu.cljs

@@ -14,7 +14,9 @@
             [frontend.handler.plugin :as plugin-handler]
             [frontend.mobile.util :as mobile-util]
             [electron.ipc :as ipc]
-            [frontend.config :as config]))
+            [frontend.config :as config]
+            [frontend.handler.user :as user-handler]
+            [frontend.handler.file-sync :as file-sync-handler]))
 
 (defn- delete-page!
   [page-name]
@@ -68,7 +70,8 @@
           favorited? (contains? (set (map util/page-name-sanity-lc favorites))
                                 page-name)
           developer-mode? (state/sub [:ui/developer-mode?])
-          file-path (when (util/electron?) (page-handler/get-page-file-path))]
+          file-path (when (util/electron?) (page-handler/get-page-file-path))
+          _ (state/sub :auth/id-token)]
       (when (and page (not block?))
         (->>
          [{:title   (if favorited?
@@ -81,13 +84,13 @@
                          (page-handler/favorite-page! page-original-name)))}}
 
           (when-not (mobile-util/is-native-platform?)
-           {:title (t :page/presentation-mode)
-            :options {:on-click (fn []
-                                  (state/sidebar-add-block!
-                                   repo
-                                   (:db/id page)
-                                   :page-presentation
-                                   {:page page}))}})
+            {:title (t :page/presentation-mode)
+             :options {:on-click (fn []
+                                   (state/sidebar-add-block!
+                                    repo
+                                    (:db/id page)
+                                    :page-presentation
+                                    {:page page}))}})
 
           ;; TODO: In the future, we'd like to extract file-related actions
           ;; (such as open-in-finder & open-with-default-app) into a sub-menu of
@@ -123,6 +126,10 @@
              :options {:on-click
                        (fn []
                          (shell/get-file-latest-git-log page 100))}})
+          (when (and (user-handler/logged?) (not file-sync-handler/hiding-login&file-sync))
+            (when-let [graph-uuid (file-sync-handler/get-current-graph-uuid)]
+              {:title (t :page/file-sync-versions)
+               :options {:on-click #(file-sync-handler/list-file-versions graph-uuid page)}}))
 
           (when (and (util/electron?) file-path)
             {:title   (t :page/open-backup-directory)

+ 4 - 1
src/main/frontend/dicts.cljc

@@ -169,6 +169,7 @@
         :page/make-public "Make it public for publishing"
         :page/version-history "Check page history"
         :page/open-backup-directory "Open page backups directory"
+        :page/file-sync-versions "Page versions"
         :page/make-private "Make it private"
         :page/delete "Delete page"
         :page/publish "Publish this page on Logseq"
@@ -1072,6 +1073,7 @@
            :page/make-public "导出 HTML 时发布本页面"
            :page/version-history "查看页面历史记录"
            :page/open-backup-directory "打开页面备份文件夹"
+           :page/file-sync-versions "页面历史"
            :page/make-private "导出 HTML 时取消发布本页面"
            :page/delete "删除本页"
            :page/publish "将本页发布至 Logseq"
@@ -3412,7 +3414,8 @@
         :page/open-with-default-app "Открыть через приложение по умолчанию"
         :page/action-publish "Опубликовать"
         :page/make-public "Сделать доступным для публикации"
-        :page/version-history "Проверить историю страницы"
+        :page/version-history "проверить историю git страницы"
+        :page/file-sync-versions "история страницы"
         :page/make-private "Сделать приватным"
         :page/delete "Удалить страницу"
         :page/publish "Опубликовать эту страницу на Logseq"

+ 17 - 2
src/main/frontend/fs/sync.cljs

@@ -16,6 +16,18 @@
             [frontend.util.persist-var :as persist-var]
             [rum.core :as rum]))
 
+;;; Commentary
+;;; file-sync related local files/dirs:
+;;; - logseq/graphs-txid.edn
+;;;   this file contains 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
+;;;   downloaded version-files
+;;; files included by `get-ignore-files` will not be synchronized, see also `get-ignore-files`
+;;; files in these `get-monitored-dirs` dirs will be synchronized.
+
+
 ;;; TODO: add some spec validate
 
 (def ws-addr "wss://og96xf1si7.execute-api.us-east-2.amazonaws.com/production?graphuuid=%s")
@@ -252,6 +264,7 @@
   (get-remote-all-files-meta [this graph-uuid] "get all remote files' metadata")
   (get-remote-files-meta [this graph-uuid filepaths] "get remote files' metadata")
   (get-remote-graph [this graph-name-opt graph-uuid-opt] "get graph info by GRAPH-NAME-OPT or GRAPH-UUID-OPT")
+  (get-remote-file-versions [this graph-uuid filepath] "get file's version list")
   (list-remote-graphs [this] "list all remote graphs")
   (get-diff [this graph-uuid from-txid] "get diff from FROM-TXID, return [txns, latest-txid]")
   (create-graph [this graph-name] "create graph"))
@@ -406,6 +419,8 @@
                                  (assoc :GraphName graph-name-opt)
                                  (seq graph-uuid-opt)
                                  (assoc :GraphUUID graph-uuid-opt))))
+  (get-remote-file-versions [this graph-uuid filepath]
+    (.request this "get_file_version_list" {:GraphUUID graph-uuid :File filepath}))
   (list-remote-graphs [this]
     (.request this "list_graphs"))
 
@@ -440,6 +455,7 @@
 (defn- update-txn [^FileTxnSet filetxnset txn]
   (let [{:keys [TXType TXContent]} txn]
     (let [files (->> (string/split-lines TXContent)
+                     (remove empty?)
                      (mapv #(remove-user-graph-uuid-prefix %)))]
       (case TXType
         "update_files"
@@ -608,7 +624,7 @@
   (set-remote->local-syncer! [_ s] (set! remote->local-syncer s))
 
   ILocal->RemoteSync
-  (get-ignore-files [_] #{#"logseq/graphs-txid.edn$" #"logseq/bak/.*"})
+  (get-ignore-files [_] #{#"logseq/graphs-txid.edn$" #"logseq/bak/.*" #"logseq/version-files/.*"})
   (get-monitored-dirs [_] #{"assets/" "journals/" "logseq/" "pages/"})
   (stop-local->remote! [_] (async/close! stop-chan))
 
@@ -709,7 +725,6 @@
 
                 (or need-sync-remote unknown) r))))))))
 
-;;; TODO: add synced-files history
 (deftype SyncState [^:mutable state ^:mutable current-local->remote-files ^:mutable current-remote->local-files
                     ^:mutable history]
   Object

+ 49 - 4
src/main/frontend/handler/file_sync.cljs

@@ -1,12 +1,18 @@
 (ns frontend.handler.file-sync
-  (:require [cljs.core.async :as async :refer [go timeout go-loop offer! poll! chan <! >!]]
+  (:require ["path" :as path]
+            [cljs-time.coerce :as tc]
+            [cljs.core.async :as async :refer [go timeout go-loop offer! poll! chan <! >!]]
+            [clojure.string :as string]
+            [frontend.config :as config]
+            [frontend.db :as db]
             [frontend.fs.macro :refer [exception-> exception->>]]
             [frontend.fs.sync :as sync]
+            [frontend.handler.notification :as notification]
             [frontend.state :as state]
-            [frontend.util.persist-var :as persist-var]
-            [frontend.handler.notification :as notification]))
-
+            [frontend.util :as util]
+            [frontend.util.persist-var :as persist-var]))
 
+(def hiding-login&file-sync true)
 (def refresh-file-sync-component (atom false))
 
 (defn graph-txid-exists?
@@ -42,3 +48,42 @@
   (persist-var/-reset-value! sync/graphs-txid [graph-uuid 0] (state/get-current-repo))
   (persist-var/persist-save sync/graphs-txid)
   (swap! refresh-file-sync-component not))
+
+(defn- download-version-file [graph-uuid file-uuid version-uuid]
+
+  (go
+    (let [key (path/join "version-files" file-uuid version-uuid)
+          r (<! (sync/update-local-files
+                 sync/rsapi graph-uuid (config/get-repo-dir (state/get-current-repo)) [key]))]
+      (if (instance? ExceptionInfo r)
+        (notification/show! (ex-cause r) :error)
+        (notification/show! [:div
+                             [:div "Downloaded version file at: "]
+                             [:div key]] :success false)))))
+
+(defn list-file-versions [graph-uuid page]
+  (let [file-id (:db/id (:block/file page))]
+    (when-let [path (:file/path (db/entity file-id))]
+      (let [base-path (config/get-repo-dir (state/get-current-repo))
+            path* (string/replace-first path base-path "")]
+        (go
+          (let [version-list (:VersionList
+                              (<! (sync/get-remote-file-versions sync/remoteapi graph-uuid path*)))]
+            (notification/show! [:div
+                                 [:div.font-bold "File history - " path*]
+                                 [:hr.my-2]
+                                 (for [version version-list]
+                                   (let [version-uuid (:VersionUUID version)]
+                                     [:div.my-4 {:key version-uuid}
+                                      [:div
+                                       [:a.text-xs.inline
+                                        {:on-click #(download-version-file graph-uuid
+                                                                           (:FileUUID version)
+                                                                           (:VersionUUID version))}
+                                        version-uuid]
+                                       [:div.opacity-70 (str "Size: " (:Size version))]]
+                                      [:div.opacity-50
+                                       (util/time-ago (tc/from-string (:CreateTime version)))]]))]
+                                :success false)))))))
+
+(defn get-current-graph-uuid [] (first @sync/graphs-txid))