From b36dd1bfb699f670b22a71cbe2dafd9079bd9af9 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Thu, 10 Aug 2023 01:41:24 +0800 Subject: [PATCH] download remote graph and transact into sqlite --- src/main/frontend/db/rtc/core.cljs | 17 +++- .../db/rtc/full_upload_download_graph.cljs | 77 +++++++++++++++++-- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/main/frontend/db/rtc/core.cljs b/src/main/frontend/db/rtc/core.cljs index 5597b8168b..8dbe45a1e0 100644 --- a/src/main/frontend/db/rtc/core.cljs +++ b/src/main/frontend/db/rtc/core.cljs @@ -34,6 +34,7 @@ (def state-schema " + | :user-uuid | string | | :data-from-ws-chan | channel for receive messages from server websocket | | :data-from-ws-pub | pub of :data-from-ws-chan, dispatch by :req-id | | :client-op-update-chan | channel to notify that there're some new operations | @@ -42,6 +43,7 @@ | :ws | websocket | " [:map + [:user-uuid :string] [:data-from-ws-chan :any] [:data-from-ws-pub :any] [:client-op-update-chan :any] @@ -177,23 +179,30 @@ nil)))) (defn init-state - [ws data-from-ws-chan] + [ws data-from-ws-chan user-uuid] (m/parse state-schema - {:data-from-ws-chan data-from-ws-chan + {:user-uuid user-uuid + :data-from-ws-chan data-from-ws-chan :data-from-ws-pub (async/pub data-from-ws-chan :req-id) :client-op-update-chan (chan) :upload-graph-chan (chan) :download-graph-chan (chan) :ws ws})) +(defn ensure-ws-connected + [state] + + ) + (defn c]] [cljs-http.client :as http] - [cognitect.transit :as transit])) + [cognitect.transit :as transit] + [logseq.db.schema :as db-schema] + [logseq.db.sqlite.util :as sqlite-util] + [electron.ipc :as ipc])) (defn- export-as-blocks @@ -40,7 +44,70 @@ (defn- (assoc block :db/id (str db-id)) + block-parent (assoc :block/parent (str block-parent)) + block-left (assoc :block/left (str block-left))))) + blocks)) + +(def page-of-block + (memoize + (fn [id->block-map block] + (when-let [parent-id (:block/parent block)] + (when-let [parent (id->block-map parent-id)] + (if (:block/name parent) + parent + (page-of-block id->block-map parent))))))) + +(defn- fill-block-fields + [blocks] + (let [groups (group-by #(boolean (:block/name %)) blocks) + ;; _page-blocks (get groups true) + other-blocks (set (get groups false)) + id->block (into {} (map (juxt :db/id identity) blocks)) + block-id->page-id (into {} (map (fn [b] [(:db/id b) (:db/id (page-of-block id->block b))]) other-blocks))] + (mapv (fn [b] + (let [b (assoc b :block/format :markdown)] + (if-let [page-id (block-id->page-id (:db/id b))] + (assoc b :block/page page-id) + b))) + blocks))) + + +(defn- (assoc b :datoms (sqlite-util/block-map->datoms-str blocks* b)) + (:block/parent b) (assoc :page_uuid (str (:block/uuid (d/entity db (:db/id (:block/page b)))))))) + blocks*) + repo (str "rtc-" graph-uuid)] + (c (ipc/ipc :db-new repo))) + (c (ipc/ipc :db-transact-data repo (pr-str {:blocks blocks**}))))))))