Skip to content

Commit

Permalink
refactor: split rtc malli-schema into its own ns
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed Jan 8, 2025
1 parent 6105476 commit 73e51a8
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 318 deletions.
6 changes: 3 additions & 3 deletions src/main/frontend/worker/rtc/client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"Fns about push local updates"
(:require [clojure.string :as string]
[datascript.core :as d]
[frontend.common.missionary :as c.m]
[frontend.worker.rtc.client-op :as client-op]
[frontend.worker.rtc.const :as rtc-const]
[frontend.worker.rtc.exception :as r.ex]
[frontend.worker.rtc.log-and-state :as rtc-log-and-state]
[frontend.worker.rtc.malli-schema :as rtc-schema]
[frontend.worker.rtc.remote-update :as r.remote-update]
[frontend.worker.rtc.skeleton :as r.skeleton]
[frontend.worker.rtc.ws :as ws]
[frontend.worker.rtc.ws-util :as ws-util]
[frontend.common.missionary :as c.m]
[missionary.core :as m]))

(defn- register-graph-updates
Expand Down Expand Up @@ -330,7 +330,7 @@
(m/sp
(let [block-ops-map-coll (client-op/get&remove-all-block-ops repo)]
(when-let [block-uuid->remote-ops (not-empty (gen-block-uuid->remote-ops @conn block-ops-map-coll))]
(when-let [ops-for-remote (rtc-const/to-ws-ops-decoder
(when-let [ops-for-remote (rtc-schema/to-ws-ops-decoder
(sort-remote-ops
block-uuid->remote-ops))]
(let [local-tx (client-op/get-local-tx repo)
Expand Down
6 changes: 3 additions & 3 deletions src/main/frontend/worker/rtc/client_op.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns frontend.worker.rtc.client-op
"Store client-ops in a persisted datascript"
(:require [datascript.core :as d]
[frontend.worker.rtc.const :as rtc-const]
[frontend.worker.state :as worker-state]
[frontend.common.missionary :as c.m]
[frontend.worker.rtc.malli-schema :as rtc-schema]
[frontend.worker.state :as worker-state]
[logseq.db.sqlite.util :as sqlite-util]
[malli.core :as ma]
[malli.transform :as mt]
Expand Down Expand Up @@ -41,7 +41,7 @@
[:t :int]
[:value [:map
[:block-uuid :uuid]
[:av-coll [:sequential rtc-const/av-schema]]]]]]
[:av-coll [:sequential rtc-schema/av-schema]]]]]]

[:update-asset
[:catn
Expand Down
294 changes: 1 addition & 293 deletions src/main/frontend/worker/rtc/const.cljs
Original file line number Diff line number Diff line change
@@ -1,297 +1,5 @@
(ns frontend.worker.rtc.const
"RTC constants/schema"
(:require [logseq.db.frontend.malli-schema :as db-malli-schema]
[malli.core :as m]
[malli.transform :as mt]
[malli.util :as mu]))
"RTC constants")

(goog-define RTC-E2E-TEST* false)
(def RTC-E2E-TEST RTC-E2E-TEST*)

(def block-pos-schema
[:catn
[:parent-uuid [:maybe :uuid]]
[:order [:maybe db-malli-schema/block-order]]])

(def av-schema
[:cat
:keyword
[:or
:uuid ;; reference type
:string ;; all other type value convert to string by transit
]
:int ;; t
:boolean ;; add(true) or retract
])

(def to-ws-op-schema
[:multi {:dispatch first :decode/string #(update % 0 keyword)}
[:move
[:cat :keyword
[:map
[:block-uuid :uuid]
[:pos block-pos-schema]]]]
[:remove
[:cat :keyword
[:map
[:block-uuids [:sequential :uuid]]]]]
[:update-page
[:cat :keyword
[:map
[:block-uuid :uuid]
[:page-name :string]
[:block/title :string]]]]
[:remove-page
[:cat :keyword
[:map
[:block-uuid :uuid]]]]
[:update
[:cat :keyword
[:map
[:block-uuid :uuid]
[:db/ident {:optional true} :keyword]
[:pos block-pos-schema]
[:av-coll [:sequential av-schema]]
[:card-one-attrs {:optional true} [:sequential :keyword]]]]]
[:update-schema
[:cat :keyword
[:map
[:block-uuid :uuid]
[:db/ident :keyword]
[:db/valueType :keyword]
[:db/cardinality {:optional true} :keyword]
[:db/index {:optional true} :boolean]]]]])

(comment
(def to-ws-ops-validator (m/validator [:sequential to-ws-op-schema])))

(def to-ws-ops-decoder (m/decoder [:sequential to-ws-op-schema] mt/string-transformer))

(def ^:private extra-attr-map-schema
[:map-of
:keyword
[:or
[:or :uuid :string]
[:sequential [:or :uuid :string]]]])

(def data-from-ws-schema
"TODO: split this mix schema to multiple ones"
[:map
[:req-id :string]
[:profile {:optional true} :map]
[:t {:optional true} :int]
[:t-before {:optional true} :int]
[:failed-ops {:optional true} [:sequential to-ws-op-schema]]
[:s3-presign-url {:optional true} :string]
[:server-schema-version {:optional true} :int]
[:server-builtin-db-idents {:optional true} [:set :keyword]]
[:server-only-db-ident-blocks {:optional true} [:maybe :string] ;;transit
]
[:users {:optional true} [:sequential
[:map {:closed true}
[:user/uuid :uuid]
[:user/name :string]
[:user/email :string]
[:user/online? :boolean]
[:user/avatar {:optional true} :string]
[:graph<->user/user-type :keyword]]]]
[:online-users {:optional true} [:sequential
[:map {:closed true}
[:user/uuid :uuid]
[:user/name :string]
[:user/email :string]
[:user/avatar {:optional true} :string]]]]
[:refed-blocks {:optional true}
[:maybe
[:sequential
[:map
[:block/uuid :uuid]
[:db/ident {:optional true} :keyword]
[:block/order {:optional true} db-malli-schema/block-order]
[:block/parent {:optional true} :uuid]
[::m/default extra-attr-map-schema]]]]]
[:affected-blocks {:optional true}
[:map-of :uuid
[:multi {:dispatch :op :decode/string #(update % :op keyword)}
[:move
[:map {:closed true}
[:op :keyword]
[:self :uuid]
[:parents [:sequential :uuid]]
[:block/order {:optional true} db-malli-schema/block-order]
[:hash {:optional true} :int]
[:db/ident {:optional true} :keyword]]]
[:remove
[:map
[:op :keyword]
[:block-uuid :uuid]]]
[:update-attrs
[:map
[:op :keyword]
[:self :uuid]
[:parents {:optional true} [:sequential :uuid]]
[:block/order {:optional true} db-malli-schema/block-order]
[:hash {:optional true} :int]
[:db/ident {:optional true} :keyword]
[::m/default extra-attr-map-schema]]]
[:move+update-attrs
[:map
[:op :keyword]
[:self :uuid]
[:parents {:optional true} [:sequential :uuid]]
[:block/order {:optional true} db-malli-schema/block-order]
[:hash {:optional true} :int]
[:db/ident {:optional true} :keyword]
[::m/default extra-attr-map-schema]]]
[:update-page
[:map
[:op :keyword]
[:self :uuid]
[:block/title :string]
[:db/ident {:optional true} :keyword]
[:block/order {:optional true} db-malli-schema/block-order]
[::m/default extra-attr-map-schema]]]
[:remove-page
[:map
[:op :keyword]
[:block-uuid :uuid]]]]]]
[:asset-uuid->url {:optional true} [:map-of :uuid :string]]
[:uploaded-assets {:optional true} [:map-of :uuid :map]]
[:ex-data {:optional true} [:map [:type :keyword]]]
[:ex-message {:optional true} :string]])

(def data-from-ws-coercer (m/coercer data-from-ws-schema mt/string-transformer))
(def data-from-ws-validator (m/validator data-from-ws-schema))

(defn- with-shared-schema-attrs
[malli-schema]
(let [[head api-schema-seq] (split-at 2 malli-schema)]
(vec
(concat
head
(map
(fn [api-schema]
(let [[api-name [type']] api-schema]
(if (= :map type')
[api-name (vec (concat (second api-schema) [[:req-id :string]
[:action :string]
[:profile {:optional true} :boolean]]))]
api-schema)))
api-schema-seq)))))

(def ^:large-vars/data-var data-to-ws-schema
(mu/closed-schema
(with-shared-schema-attrs
[:multi {:dispatch :action}
["list-graphs"
[:map]]
["register-graph-updates"
[:map
[:graph-uuid :string]]]
["apply-ops"
[:or
[:map
[:req-id :string]
[:action :string]
[:profile {:optional true} :boolean]
[:graph-uuid :string]
[:ops [:sequential to-ws-op-schema]]
[:t-before :int]]
[:map
[:req-id :string]
[:action :string]
[:profile {:optional true} :boolean]
[:s3-key :string]]]]
["presign-put-temp-s3-obj"
[:map]]
["upload-graph"
[:map
[:s3-key :string]
[:graph-name :string]]]
["download-graph"
[:map
[:graph-uuid :string]]]
["download-info-list"
[:map
[:graph-uuid :string]]]
["snapshot-list"
[:map
[:graph-uuid :string]]]
["snapshot-graph"
[:map
[:graph-uuid :string]]]
["grant-access"
[:map
[:graph-uuid :uuid]
[:target-user-uuids {:optional true} [:sequential :uuid]]
[:target-user-emails {:optional true} [:sequential :string]]]]
["get-users-info"
[:map
[:graph-uuid :uuid]]]
["inject-users-info"
[:map
[:graph-uuid :uuid]]]
["delete-graph"
[:map
[:graph-uuid :uuid]]]
["query-block-content-versions"
[:map
[:graph-uuid :string]
[:block-uuids [:sequential :uuid]]]]
["calibrate-graph-skeleton"
[:map
[:graph-uuid :string]
[:t :int]
[:schema-version :int]
[:db-ident-blocks [:sequential
[:map
[:db/ident :keyword]
[::m/default extra-attr-map-schema]]]]]]
["get-graph-skeleton"
[:map
[:graph-uuid :string]]]
["get-assets-upload-urls"
[:map
[:graph-uuid :string]
[:asset-uuid->metadata [:map-of :uuid [:map-of :string :string]]]]]
["get-assets-download-urls"
[:map
[:graph-uuid :string]
[:asset-uuids [:sequential :uuid]]]]
["delete-assets"
[:map
[:graph-uuid :string]
[:asset-uuids [:sequential :uuid]]]]
["get-user-devices"
[:map]]
["add-user-device"
[:map
[:device-name :string]]]
["remove-user-device"
[:map
[:device-uuid :uuid]]]
["update-user-device-name"
[:map
[:device-uuid :uuid]
[:device-name :string]]]
["add-device-public-key"
[:map
[:device-uuid :uuid]
[:key-name :string]
[:public-key :string]]]
["remove-device-public-key"
[:map
[:device-uuid :uuid]
[:key-name :string]]]
["sync-encrypted-aes-key"
[:map
[:device-uuid->encrypted-aes-key [:map-of :uuid :string]]
[:graph-uuid :uuid]]]])))

(def data-to-ws-encoder (m/encoder data-to-ws-schema (mt/transformer
mt/string-transformer
(mt/key-transformer {:encode m/-keyword->string}))))
(def data-to-ws-coercer (m/coercer data-to-ws-schema mt/string-transformer nil
#(do
(prn ::data-to-ws-schema %)
(m/-fail! ::data-to-ws-schema %))))
Loading

0 comments on commit 73e51a8

Please sign in to comment.