file_sync.cljs 10 KB

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