file_sync.cljs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. (ns frontend.handler.file-sync
  2. "Provides util handler fns for file sync"
  3. (:require ["path" :as path]
  4. [cljs-time.format :as tf]
  5. [cljs.core.async :as async :refer [go <!]]
  6. [cljs.core.async.interop :refer [p->c]]
  7. [clojure.string :as string]
  8. [frontend.config :as config]
  9. [frontend.db :as db]
  10. [frontend.fs.sync :as sync]
  11. [frontend.handler.notification :as notification]
  12. [frontend.state :as state]
  13. [frontend.handler.user :as user]
  14. [frontend.fs :as fs]
  15. [cljs-time.coerce :as tc]
  16. [cljs-time.core :as t]
  17. [frontend.storage :as storage]
  18. [logseq.graph-parser.util :as gp-util]))
  19. (def *beta-unavailable? (volatile! false))
  20. (def refresh-file-sync-component (atom false))
  21. (defn get-current-graph-uuid []
  22. (state/get-current-file-sync-graph-uuid))
  23. (defn enable-sync?
  24. []
  25. (or (state/enable-sync?)
  26. config/dev?))
  27. (defn current-graph-sync-on?
  28. []
  29. (when-let [sync-state (state/sub-file-sync-state (state/get-current-file-sync-graph-uuid))]
  30. (not (sync/sync-state--stopped? sync-state))))
  31. (defn synced-file-graph?
  32. [graph]
  33. (some (fn [item] (and (= graph (:url item))
  34. (:GraphUUID item))) (state/get-repos)))
  35. (defn create-graph
  36. [name]
  37. (go
  38. (let [r* (<! (sync/<create-graph sync/remoteapi name))
  39. user-uuid-or-exp (<! (user/<user-uuid))
  40. r (if (instance? ExceptionInfo r*) r*
  41. (if (instance? ExceptionInfo user-uuid-or-exp)
  42. user-uuid-or-exp
  43. (:GraphUUID r*)))]
  44. (when-not (instance? ExceptionInfo user-uuid-or-exp)
  45. (if (and (not (instance? ExceptionInfo r))
  46. (string? r))
  47. (let [tx-info [0 r user-uuid-or-exp (state/get-current-repo)]]
  48. (<! (apply sync/<update-graphs-txid! tx-info))
  49. (swap! refresh-file-sync-component not)
  50. tx-info)
  51. (do
  52. (state/set-state! [:ui/loading? :graph/create-remote?] false)
  53. (cond
  54. ;; already processed this exception by events
  55. ;; - :file-sync/storage-exceed-limit
  56. ;; - :file-sync/graph-count-exceed-limit
  57. (or (sync/storage-exceed-limit? r)
  58. (sync/graph-count-exceed-limit? r))
  59. nil
  60. (contains? #{400 404} (get-in (ex-data r) [:err :status]))
  61. (notification/show! (str "Create graph failed: already existed graph: " name) :warning true nil 4000)
  62. :else
  63. (notification/show! (str "Create graph failed: " (ex-message r)) :warning true nil 4000))))))))
  64. (defn <delete-graph
  65. [graph-uuid]
  66. (go
  67. (let [same-graph? (= graph-uuid (get-current-graph-uuid))]
  68. (when same-graph?
  69. (<! (sync/<sync-stop)))
  70. (let [r (<! (sync/<delete-graph sync/remoteapi graph-uuid))]
  71. (if (instance? ExceptionInfo r)
  72. (notification/show! (str "Delete graph failed: " graph-uuid) :warning)
  73. (do
  74. (when same-graph?
  75. (sync/clear-graphs-txid! graph-uuid)
  76. (swap! refresh-file-sync-component not))
  77. (notification/show! (str "Graph deleted") :success)))))))
  78. (defn <list-graphs
  79. []
  80. (go (:Graphs (<! (sync/<list-remote-graphs sync/remoteapi)))))
  81. (defn load-session-graphs
  82. []
  83. (when-not (state/sub [:file-sync/remote-graphs :loading])
  84. (go (state/set-state! [:file-sync/remote-graphs :loading] true)
  85. (let [graphs (<! (<list-graphs))]
  86. (state/set-state! :file-sync/remote-graphs {:loading false :graphs graphs})))))
  87. (defn reset-session-graphs
  88. []
  89. (state/set-state! :file-sync/remote-graphs {:loading false :graphs nil}))
  90. (defn init-graph [graph-uuid]
  91. (go
  92. (let [repo (state/get-current-repo)
  93. user-uuid-or-exp (<! (user/<user-uuid))]
  94. (if (instance? ExceptionInfo user-uuid-or-exp)
  95. (notification/show! (ex-message user-uuid-or-exp) :error)
  96. (do
  97. (state/set-state! :sync-graph/init? true)
  98. (<! (sync/<update-graphs-txid! 0 graph-uuid user-uuid-or-exp repo))
  99. (swap! refresh-file-sync-component not)
  100. (state/pub-event! [:graph/switch repo {:persist? false}]))))))
  101. (defn download-version-file
  102. ([graph-uuid file-uuid version-uuid]
  103. (download-version-file graph-uuid file-uuid version-uuid false))
  104. ([graph-uuid file-uuid version-uuid silent-download?]
  105. (go
  106. (let [key (path/join file-uuid version-uuid)
  107. r (<! (sync/<download-version-files
  108. sync/rsapi graph-uuid (config/get-repo-dir (state/get-current-repo)) [key]))]
  109. (if (instance? ExceptionInfo r)
  110. (notification/show! (ex-cause r) :error)
  111. (when-not silent-download?
  112. (notification/show! [:div
  113. [:div "Downloaded version file at: "]
  114. [:div key]] :success false)))
  115. (when-not (instance? ExceptionInfo r)
  116. (path/join "logseq" "version-files" key))))))
  117. (defn- list-file-local-versions
  118. [page]
  119. (go
  120. (when-let [path (-> page :block/file :file/path)]
  121. (let [base-path (config/get-repo-dir (state/get-current-repo))
  122. rel-path (string/replace-first path base-path "")
  123. version-files-dir (->> (path/join "logseq/version-files/local" rel-path)
  124. path/parse
  125. (#(js->clj % :keywordize-keys true))
  126. ((juxt :dir :name))
  127. (apply path/join base-path))
  128. version-file-paths (<! (p->c (fs/readdir version-files-dir :path-only? true)))]
  129. (when-not (instance? ExceptionInfo version-file-paths)
  130. (when (seq version-file-paths)
  131. (mapv
  132. (fn [path]
  133. (let [create-time
  134. (-> (path/parse path)
  135. (js->clj :keywordize-keys true)
  136. :name
  137. (#(tf/parse (tf/formatter "yyyy-MM-dd'T'HH_mm_ss.SSSZZ") %)))]
  138. {:create-time create-time :path path :relative-path (string/replace-first path base-path "")}))
  139. version-file-paths)))))))
  140. (defn fetch-page-file-versions [graph-uuid page]
  141. []
  142. (let [file-id (:db/id (:block/file page))]
  143. (when-let [path (:file/path (db/entity file-id))]
  144. (let [base-path (config/get-repo-dir (state/get-current-repo))
  145. base-path (if (string/starts-with? base-path "file://")
  146. (gp-util/safe-decode-uri-component base-path)
  147. base-path)
  148. path* (string/replace-first (string/replace-first path base-path "") #"^/" "")]
  149. (go
  150. (let [version-list (:VersionList
  151. (<! (sync/<get-remote-file-versions sync/remoteapi graph-uuid path*)))
  152. local-version-list (<! (list-file-local-versions page))
  153. all-version-list (->> (concat version-list local-version-list)
  154. (sort-by #(or (:CreateTime %)
  155. (:create-time %))
  156. >))]
  157. all-version-list))))))
  158. (defn init-remote-graph
  159. [local graph]
  160. (when (and local graph)
  161. (notification/show!
  162. (str "Start syncing the remote graph "
  163. (:GraphName graph)
  164. " to "
  165. (config/get-string-repo-dir (config/get-local-dir local)))
  166. :success)
  167. (init-graph (:GraphUUID graph))
  168. (state/close-modal!)))
  169. (defn setup-file-sync-event-listeners
  170. []
  171. (let [c (async/chan 1)
  172. p sync/sync-events-publication
  173. topics [:finished-local->remote :finished-remote->local :start]]
  174. (doseq [topic topics]
  175. (async/sub p topic c))
  176. (async/go-loop []
  177. (let [{:keys [event data]} (async/<! c)]
  178. (case event
  179. (list :finished-local->remote :finished-remote->local)
  180. (when-let [current-uuid (state/get-current-file-sync-graph-uuid)]
  181. (state/clear-file-sync-progress! current-uuid)
  182. (state/set-state! [:file-sync/graph-state current-uuid :file-sync/last-synced-at] (:epoch data)))
  183. :start
  184. (when-let [current-uuid (state/get-current-file-sync-graph-uuid)]
  185. (state/set-state! [:file-sync/graph-state current-uuid :file-sync/start-time] data))
  186. nil)
  187. (when (and (:file-change-events data)
  188. (= :page (state/get-current-route)))
  189. (state/pub-event! [:file-sync/maybe-onboarding-show :sync-history])))
  190. (recur))
  191. #(doseq [topic topics]
  192. (async/unsub p topic c))))
  193. (defn reset-user-state! []
  194. (vreset! *beta-unavailable? false)
  195. (state/set-state! :file-sync/onboarding-state nil))
  196. (defn calculate-time-left
  197. "This assumes that the network speed is stable which could be wrong sometimes."
  198. [sync-state progressing]
  199. (when-let [start-time (get-in @state/state
  200. [:file-sync/graph-state
  201. (state/get-current-file-sync-graph-uuid)
  202. :file-sync/start-time
  203. :epoch])]
  204. (let [now (tc/to-epoch (t/now))
  205. diff-seconds (- now start-time)
  206. finished (reduce + (map (comp :progress second) progressing))
  207. local->remote-files (:full-local->remote-files sync-state)
  208. remote->local-files (:full-remote->local-files sync-state)
  209. total (if (seq remote->local-files)
  210. (reduce + (map (fn [m] (or (:size m) 0)) remote->local-files))
  211. (reduce + (map #(:size (.-stat %)) local->remote-files)))
  212. mins (int (/ (* (/ total finished) diff-seconds) 60))]
  213. (if (or (zero? total) (zero? finished))
  214. "waiting"
  215. (cond
  216. (zero? mins) "soon"
  217. (= mins 1) "1 min left"
  218. (> mins 30) "calculating..."
  219. :else (str mins " mins left"))))))
  220. (defn set-sync-enabled!
  221. [value]
  222. (storage/set :logseq-sync-enabled value)
  223. (state/set-state! :feature/enable-sync? value))