From 73e51a88b25d0a4d2a832678267418182f541a6c Mon Sep 17 00:00:00 2001 From: rcmerci Date: Wed, 8 Jan 2025 14:31:12 +0800 Subject: [PATCH] refactor: split rtc malli-schema into its own ns --- src/main/frontend/worker/rtc/client.cljs | 6 +- src/main/frontend/worker/rtc/client_op.cljs | 6 +- src/main/frontend/worker/rtc/const.cljs | 294 +----------------- .../frontend/worker/rtc/malli_schema.cljs | 294 ++++++++++++++++++ .../frontend/worker/rtc/remote_update.cljs | 3 +- src/main/frontend/worker/rtc/ws.cljs | 10 +- src/main/frontend/worker/rtc/ws_util.cljs | 6 +- .../frontend/worker/rtc/rtc_fns_test.cljs | 20 +- 8 files changed, 321 insertions(+), 318 deletions(-) create mode 100644 src/main/frontend/worker/rtc/malli_schema.cljs diff --git a/src/main/frontend/worker/rtc/client.cljs b/src/main/frontend/worker/rtc/client.cljs index 550ada9330a..f5e0c40204e 100644 --- a/src/main/frontend/worker/rtc/client.cljs +++ b/src/main/frontend/worker/rtc/client.cljs @@ -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 @@ -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) diff --git a/src/main/frontend/worker/rtc/client_op.cljs b/src/main/frontend/worker/rtc/client_op.cljs index 0ce929e2d71..9cd898f9406 100644 --- a/src/main/frontend/worker/rtc/client_op.cljs +++ b/src/main/frontend/worker/rtc/client_op.cljs @@ -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] @@ -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 diff --git a/src/main/frontend/worker/rtc/const.cljs b/src/main/frontend/worker/rtc/const.cljs index 674f64a1d7a..efb9a5e39e6 100644 --- a/src/main/frontend/worker/rtc/const.cljs +++ b/src/main/frontend/worker/rtc/const.cljs @@ -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 %)))) diff --git a/src/main/frontend/worker/rtc/malli_schema.cljs b/src/main/frontend/worker/rtc/malli_schema.cljs new file mode 100644 index 00000000000..41458af19eb --- /dev/null +++ b/src/main/frontend/worker/rtc/malli_schema.cljs @@ -0,0 +1,294 @@ +(ns frontend.worker.rtc.malli-schema + "Malli schema for rtc" + (:require [logseq.db.frontend.malli-schema :as db-malli-schema] + [malli.core :as m] + [malli.transform :as mt] + [malli.util :as mu])) + +(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 %)))) diff --git a/src/main/frontend/worker/rtc/remote_update.cljs b/src/main/frontend/worker/rtc/remote_update.cljs index 1d25a71f379..0f2d8dcab50 100644 --- a/src/main/frontend/worker/rtc/remote_update.cljs +++ b/src/main/frontend/worker/rtc/remote_update.cljs @@ -10,6 +10,7 @@ [frontend.worker.rtc.client-op :as client-op] [frontend.worker.rtc.const :as rtc-const] [frontend.worker.rtc.log-and-state :as rtc-log-and-state] + [frontend.worker.rtc.malli-schema :as rtc-schema] [frontend.worker.state :as worker-state] [frontend.worker.util :as worker-util] [logseq.clj-fractional-indexing :as index] @@ -557,7 +558,7 @@ "Apply remote-update(`remote-update-event`)" [graph-uuid repo conn date-formatter remote-update-event add-log-fn] (let [remote-update-data (:value remote-update-event)] - (assert (rtc-const/data-from-ws-validator remote-update-data) remote-update-data) + (assert (rtc-schema/data-from-ws-validator remote-update-data) remote-update-data) (let [remote-t (:t remote-update-data) remote-t-before (:t-before remote-update-data) local-tx (client-op/get-local-tx repo)] diff --git a/src/main/frontend/worker/rtc/ws.cljs b/src/main/frontend/worker/rtc/ws.cljs index bcb5d2c48b4..a14a3ba4ac7 100644 --- a/src/main/frontend/worker/rtc/ws.cljs +++ b/src/main/frontend/worker/rtc/ws.cljs @@ -3,7 +3,7 @@ based on https://github.com/ReilySiegel/missionary-websocket/blob/master/src/com/reilysiegel/missionary/websocket.cljs" (:require [cljs-http-missionary.client :as http] - [frontend.worker.rtc.const :as rtc-const] + [frontend.worker.rtc.malli-schema :as rtc-schema] [frontend.worker.rtc.exception :as r.ex] [frontend.common.missionary :as c.m] [missionary.core :as m])) @@ -133,8 +133,8 @@ "Returns a task: send message" [mws message] (m/sp - (let [decoded-message (rtc-const/data-to-ws-coercer message) - message-str (js/JSON.stringify (clj->js (rtc-const/data-to-ws-encoder decoded-message)))] + (let [decoded-message (rtc-schema/data-to-ws-coercer message) + message-str (js/JSON.stringify (clj->js (rtc-schema/data-to-ws-encoder decoded-message)))] (m/? ((:send mws) message-str))))) (defn- recv-flow* @@ -147,7 +147,7 @@ (if (= "Internal server error" (:message m)) (throw r.ex/ex-unknown-server-error) m))) - (map rtc-const/data-from-ws-coercer) + (map rtc-schema/data-from-ws-coercer) (:recv-flow m-ws))) (defn recv-flow @@ -160,7 +160,7 @@ (if-let [s3-presign-url (:s3-presign-url resp)] (let [{:keys [status body]} (m/? (http/get s3-presign-url {:with-credentials? false}))] (if (http/unexceptional-status? status) - (rtc-const/data-from-ws-coercer (js->clj (js/JSON.parse body) :keywordize-keys true)) + (rtc-schema/data-from-ws-coercer (js->clj (js/JSON.parse body) :keywordize-keys true)) {:req-id (:req-id resp) :ex-message "get s3 object failed" :ex-data {:type :rtc.exception/get-s3-object-failed :status status :body body}})) diff --git a/src/main/frontend/worker/rtc/ws_util.cljs b/src/main/frontend/worker/rtc/ws_util.cljs index 5b56e0920e3..0a24d660994 100644 --- a/src/main/frontend/worker/rtc/ws_util.cljs +++ b/src/main/frontend/worker/rtc/ws_util.cljs @@ -1,8 +1,8 @@ (ns frontend.worker.rtc.ws-util "Add RTC related logic to the function based on ws." (:require [cljs-http-missionary.client :as http] - [frontend.worker.rtc.const :as rtc-const] [frontend.worker.rtc.exception :as r.ex] + [frontend.worker.rtc.malli-schema :as rtc-schema] [frontend.worker.rtc.ws :as ws] [frontend.worker.state :as worker-state] [goog.string :as gstring] @@ -24,8 +24,8 @@ [ws message] {:pre [(= "apply-ops" (:action message))]} (m/sp - (let [decoded-message (rtc-const/data-to-ws-coercer (assoc message :req-id "temp-id")) - message-str (js/JSON.stringify (clj->js (select-keys (rtc-const/data-to-ws-encoder decoded-message) + (let [decoded-message (rtc-schema/data-to-ws-coercer (assoc message :req-id "temp-id")) + message-str (js/JSON.stringify (clj->js (select-keys (rtc-schema/data-to-ws-encoder decoded-message) ["graph-uuid" "ops" "t-before"]))) len (.-length (utf8/encode message-str))] (when (< 100000 len) diff --git a/src/test/frontend/worker/rtc/rtc_fns_test.cljs b/src/test/frontend/worker/rtc/rtc_fns_test.cljs index 7c28e2b37f1..fd8edb5526e 100644 --- a/src/test/frontend/worker/rtc/rtc_fns_test.cljs +++ b/src/test/frontend/worker/rtc/rtc_fns_test.cljs @@ -4,7 +4,7 @@ [frontend.db.conn :as conn] [frontend.state :as state] [frontend.test.helper :as test-helper] - [frontend.worker.rtc.const :as rtc-const] + [frontend.worker.rtc.malli-schema :as rtc-schema] [frontend.worker.rtc.remote-update :as r.remote] [frontend.worker.state :as worker-state] [logseq.common.config :as common-config] @@ -193,7 +193,7 @@ (:move-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws) data-from-ws) + (is (rtc-schema/data-from-ws-validator data-from-ws) data-from-ws) (#'r.remote/apply-remote-move-ops repo conn move-ops) (let [page-blocks (ldb/get-page-blocks @conn (:db/id (ldb/get-page @conn page-name)) {})] (is (= #{uuid1-remote uuid1-client uuid2-client} (set (map :block/uuid page-blocks))) @@ -217,7 +217,7 @@ (:move-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-move-ops repo conn move-ops) (let [page-blocks (ldb/get-page-blocks @conn (:db/id (ldb/get-page @conn page-name)) {})] (is (= #{uuid1-remote uuid2-remote uuid1-client uuid2-client} (set (map :block/uuid page-blocks)))) @@ -259,7 +259,7 @@ (vals (:remove-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-remove-ops repo conn date-formatter remove-ops) (let [page-blocks (ldb/get-page-blocks @conn (:db/id (ldb/get-page @conn page-name)) {})] (is (= #{uuid1-client uuid2-client} (set (map :block/uuid page-blocks))))))) @@ -271,7 +271,7 @@ remove-ops (vals (:remove-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-remove-ops repo conn date-formatter remove-ops) (let [page-blocks (ldb/get-page-blocks @conn (:db/id (ldb/get-page @conn page-name)) {})] (is (= #{uuid2-client} (set (map :block/uuid page-blocks))))))))) @@ -328,7 +328,7 @@ result: (vals (:remove-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-remove-ops repo conn date-formatter remove-ops) (let [page-blocks (ldb/get-page-blocks @conn (:db/id (ldb/get-page @conn page-name)))] (is (= [uuid3 uuid1] (map :block/uuid (sort-by :block/order page-blocks))))))))) @@ -348,7 +348,7 @@ result: update-page-ops (vals (:update-page-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-update-page-ops repo conn update-page-ops) (is (= page1-uuid (:block/uuid (d/entity @conn [:block/uuid page1-uuid])))))) @@ -362,7 +362,7 @@ result: update-page-ops (vals (:update-page-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-update-page-ops repo conn update-page-ops) (is (= (str page1-uuid "-rename") (:block/name (d/entity @conn [:block/uuid page1-uuid])))))) @@ -374,7 +374,7 @@ result: remove-page-ops (vals (:remove-page-ops-map (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws))))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-remove-page-ops repo conn remove-page-ops) (is (nil? (d/entity @conn [:block/uuid page1-uuid]))))))) @@ -426,7 +426,7 @@ result: all-ops (#'r.remote/affected-blocks->diff-type-ops repo (:affected-blocks data-from-ws)) update-page-ops (vals (:update-page-ops-map all-ops)) move-ops (#'r.remote/move-ops-map->sorted-move-ops (:move-ops-map all-ops))] - (is (rtc-const/data-from-ws-validator data-from-ws)) + (is (rtc-schema/data-from-ws-validator data-from-ws)) (#'r.remote/apply-remote-update-page-ops repo conn date-formatter update-page-ops) (#'r.remote/apply-remote-move-ops repo conn date-formatter move-ops) (let [page (ldb/get-page @conn page-name)]