const.cljs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. (ns frontend.worker.rtc.const
  2. "RTC constants/schema"
  3. (:require [logseq.db.frontend.malli-schema :as db-malli-schema]
  4. [malli.core :as m]
  5. [malli.transform :as mt]
  6. [malli.util :as mu]))
  7. (goog-define RTC-E2E-TEST* false)
  8. (def RTC-E2E-TEST RTC-E2E-TEST*)
  9. (def block-pos-schema
  10. [:catn
  11. [:parent-uuid [:maybe :uuid]]
  12. [:order [:maybe db-malli-schema/block-order]]])
  13. (def av-schema
  14. [:cat
  15. :keyword
  16. [:or
  17. :uuid ;; reference type
  18. :string ;; all other type value convert to string by transit
  19. ]
  20. :int ;; t
  21. :boolean ;; add(true) or retract
  22. ])
  23. (def to-ws-op-schema
  24. [:multi {:dispatch first :decode/string #(update % 0 keyword)}
  25. [:move
  26. [:cat :keyword
  27. [:map
  28. [:block-uuid :uuid]
  29. [:pos block-pos-schema]]]]
  30. [:remove
  31. [:cat :keyword
  32. [:map
  33. [:block-uuids [:sequential :uuid]]]]]
  34. [:update-page
  35. [:cat :keyword
  36. [:map
  37. [:block-uuid :uuid]
  38. [:page-name :string]
  39. [:block/title :string]]]]
  40. [:remove-page
  41. [:cat :keyword
  42. [:map
  43. [:block-uuid :uuid]]]]
  44. [:update
  45. [:cat :keyword
  46. [:map
  47. [:block-uuid :uuid]
  48. [:db/ident {:optional true} :keyword]
  49. [:pos block-pos-schema]
  50. [:av-coll [:sequential av-schema]]
  51. [:card-one-attrs {:optional true} [:sequential :keyword]]]]]
  52. [:update-schema
  53. [:cat :keyword
  54. [:map
  55. [:block-uuid :uuid]
  56. [:db/ident :keyword]
  57. [:db/valueType :keyword]
  58. [:db/cardinality {:optional true} :keyword]
  59. [:db/index {:optional true} :boolean]]]]])
  60. (comment
  61. (def to-ws-ops-validator (m/validator [:sequential to-ws-op-schema])))
  62. (def to-ws-ops-decoder (m/decoder [:sequential to-ws-op-schema] mt/string-transformer))
  63. (def ^:private extra-attr-map-schema
  64. [:map-of
  65. :keyword
  66. [:or
  67. [:or :uuid :string]
  68. [:sequential [:or :uuid :string]]]])
  69. (def data-from-ws-schema
  70. "TODO: split this mix schema to multiple ones"
  71. [:map
  72. [:req-id :string]
  73. [:profile {:optional true} :map]
  74. [:t {:optional true} :int]
  75. [:t-before {:optional true} :int]
  76. [:failed-ops {:optional true} [:sequential to-ws-op-schema]]
  77. [:s3-presign-url {:optional true} :string]
  78. [:server-schema-version {:optional true} :int]
  79. [:server-builtin-db-idents {:optional true} [:set :keyword]]
  80. [:server-only-db-ident-blocks {:optional true} [:maybe :string] ;;transit
  81. ]
  82. [:users {:optional true} [:sequential
  83. [:map {:closed true}
  84. [:user/uuid :uuid]
  85. [:user/name :string]
  86. [:user/email :string]
  87. [:user/online? :boolean]
  88. [:user/avatar {:optional true} :string]
  89. [:graph<->user/user-type :keyword]]]]
  90. [:online-users {:optional true} [:sequential
  91. [:map {:closed true}
  92. [:user/uuid :uuid]
  93. [:user/name :string]
  94. [:user/email :string]
  95. [:user/avatar {:optional true} :string]]]]
  96. [:refed-blocks {:optional true}
  97. [:maybe
  98. [:sequential
  99. [:map
  100. [:block/uuid :uuid]
  101. [:db/ident {:optional true} :keyword]
  102. [:block/order {:optional true} db-malli-schema/block-order]
  103. [:block/parent {:optional true} :uuid]
  104. [::m/default extra-attr-map-schema]]]]]
  105. [:affected-blocks {:optional true}
  106. [:map-of :uuid
  107. [:multi {:dispatch :op :decode/string #(update % :op keyword)}
  108. [:move
  109. [:map {:closed true}
  110. [:op :keyword]
  111. [:self :uuid]
  112. [:parents [:sequential :uuid]]
  113. [:block/order {:optional true} db-malli-schema/block-order]
  114. [:hash {:optional true} :int]
  115. [:db/ident {:optional true} :keyword]]]
  116. [:remove
  117. [:map
  118. [:op :keyword]
  119. [:block-uuid :uuid]]]
  120. [:update-attrs
  121. [:map
  122. [:op :keyword]
  123. [:self :uuid]
  124. [:parents {:optional true} [:sequential :uuid]]
  125. [:block/order {:optional true} db-malli-schema/block-order]
  126. [:hash {:optional true} :int]
  127. [:db/ident {:optional true} :keyword]
  128. [::m/default extra-attr-map-schema]]]
  129. [:move+update-attrs
  130. [:map
  131. [:op :keyword]
  132. [:self :uuid]
  133. [:parents {:optional true} [:sequential :uuid]]
  134. [:block/order {:optional true} db-malli-schema/block-order]
  135. [:hash {:optional true} :int]
  136. [:db/ident {:optional true} :keyword]
  137. [::m/default extra-attr-map-schema]]]
  138. [:update-page
  139. [:map
  140. [:op :keyword]
  141. [:self :uuid]
  142. [:block/title :string]
  143. [:db/ident {:optional true} :keyword]
  144. [:block/order {:optional true} db-malli-schema/block-order]
  145. [::m/default extra-attr-map-schema]]]
  146. [:remove-page
  147. [:map
  148. [:op :keyword]
  149. [:block-uuid :uuid]]]]]]
  150. [:asset-uuid->url {:optional true} [:map-of :uuid :string]]
  151. [:uploaded-assets {:optional true} [:map-of :uuid :map]]
  152. [:ex-data {:optional true} [:map [:type :keyword]]]
  153. [:ex-message {:optional true} :string]])
  154. (def data-from-ws-coercer (m/coercer data-from-ws-schema mt/string-transformer))
  155. (def data-from-ws-validator (m/validator data-from-ws-schema))
  156. (defn- with-shared-schema-attrs
  157. [malli-schema]
  158. (let [[head api-schema-seq] (split-at 2 malli-schema)]
  159. (vec
  160. (concat
  161. head
  162. (map
  163. (fn [api-schema]
  164. (let [[api-name [type']] api-schema]
  165. (if (= :map type')
  166. [api-name (vec (concat (second api-schema) [[:req-id :string]
  167. [:action :string]
  168. [:profile {:optional true} :boolean]]))]
  169. api-schema)))
  170. api-schema-seq)))))
  171. (def ^:large-vars/data-var data-to-ws-schema
  172. (mu/closed-schema
  173. (with-shared-schema-attrs
  174. [:multi {:dispatch :action}
  175. ["list-graphs"
  176. [:map]]
  177. ["register-graph-updates"
  178. [:map
  179. [:graph-uuid :string]]]
  180. ["apply-ops"
  181. [:or
  182. [:map
  183. [:req-id :string]
  184. [:action :string]
  185. [:profile {:optional true} :boolean]
  186. [:graph-uuid :string]
  187. [:ops [:sequential to-ws-op-schema]]
  188. [:t-before :int]]
  189. [:map
  190. [:req-id :string]
  191. [:action :string]
  192. [:profile {:optional true} :boolean]
  193. [:s3-key :string]]]]
  194. ["presign-put-temp-s3-obj"
  195. [:map]]
  196. ["upload-graph"
  197. [:map
  198. [:s3-key :string]
  199. [:graph-name :string]]]
  200. ["download-graph"
  201. [:map
  202. [:graph-uuid :string]]]
  203. ["download-info-list"
  204. [:map
  205. [:graph-uuid :string]]]
  206. ["snapshot-list"
  207. [:map
  208. [:graph-uuid :string]]]
  209. ["snapshot-graph"
  210. [:map
  211. [:graph-uuid :string]]]
  212. ["grant-access"
  213. [:map
  214. [:graph-uuid :uuid]
  215. [:target-user-uuids {:optional true} [:sequential :uuid]]
  216. [:target-user-emails {:optional true} [:sequential :string]]]]
  217. ["get-users-info"
  218. [:map
  219. [:graph-uuid :uuid]]]
  220. ["inject-users-info"
  221. [:map
  222. [:graph-uuid :uuid]]]
  223. ["delete-graph"
  224. [:map
  225. [:graph-uuid :uuid]]]
  226. ["query-block-content-versions"
  227. [:map
  228. [:graph-uuid :string]
  229. [:block-uuids [:sequential :uuid]]]]
  230. ["calibrate-graph-skeleton"
  231. [:map
  232. [:graph-uuid :string]
  233. [:t :int]
  234. [:schema-version :int]
  235. [:db-ident-blocks [:sequential
  236. [:map
  237. [:db/ident :keyword]
  238. [::m/default extra-attr-map-schema]]]]]]
  239. ["get-graph-skeleton"
  240. [:map
  241. [:graph-uuid :string]]]
  242. ["get-assets-upload-urls"
  243. [:map
  244. [:graph-uuid :string]
  245. [:asset-uuid->metadata [:map-of :uuid [:map-of :string :string]]]]]
  246. ["get-assets-download-urls"
  247. [:map
  248. [:graph-uuid :string]
  249. [:asset-uuids [:sequential :uuid]]]]
  250. ["delete-assets"
  251. [:map
  252. [:graph-uuid :string]
  253. [:asset-uuids [:sequential :uuid]]]]
  254. ["get-user-devices"
  255. [:map]]
  256. ["add-user-device"
  257. [:map
  258. [:device-name :string]]]
  259. ["remove-user-device"
  260. [:map
  261. [:device-uuid :uuid]]]
  262. ["update-user-device-name"
  263. [:map
  264. [:device-uuid :uuid]
  265. [:device-name :string]]]
  266. ["add-device-public-key"
  267. [:map
  268. [:device-uuid :uuid]
  269. [:key-name :string]
  270. [:public-key :string]]]
  271. ["remove-device-public-key"
  272. [:map
  273. [:device-uuid :uuid]
  274. [:key-name :string]]]
  275. ["sync-encrypted-aes-key"
  276. [:map
  277. [:device-uuid->encrypted-aes-key [:map-of :uuid :string]]
  278. [:graph-uuid :uuid]]]])))
  279. (def data-to-ws-encoder (m/encoder data-to-ws-schema (mt/transformer
  280. mt/string-transformer
  281. (mt/key-transformer {:encode m/-keyword->string}))))
  282. (def data-to-ws-coercer (m/coercer data-to-ws-schema mt/string-transformer nil
  283. #(do
  284. (prn ::data-to-ws-schema %)
  285. (m/-fail! ::data-to-ws-schema %))))