Просмотр исходного кода

fix: no need to broadcast online users when updating presence

Tienson Qin 3 недель назад
Родитель
Сommit
63c9c65c95

+ 6 - 2
deps/db-sync/src/logseq/db_sync/malli_schema.cljs

@@ -45,8 +45,7 @@
    [:user-id :string]
    [:email {:optional true} [:maybe :string]]
    [:username {:optional true} [:maybe :string]]
-   [:name {:optional true} [:maybe :string]]
-   [:editing-block-uuid {:optional true} [:maybe :string]]])
+   [:name {:optional true} [:maybe :string]]])
 
 (def online-users-schema
   [:map
@@ -71,6 +70,11 @@
      [:type [:= "hello"]]
      [:t :int]]]
    ["online-users" online-users-schema]
+   ["presence"
+    [:map
+     [:type [:= "presence"]]
+     [:user-id :string]
+     [:editing-block-uuid :string]]]
    ["pull/ok" pull-ok-schema]
    ["tx/batch/ok" tx-batch-ok-schema]
    ["changed"

+ 5 - 2
deps/db-sync/src/logseq/db_sync/worker/handler/ws.cljs

@@ -17,9 +17,12 @@
         (ws/send! ws {:type "pong"})
 
         "presence"
-        (let [editing-block-uuid (:editing-block-uuid message)]
+        (let [editing-block-uuid (:editing-block-uuid message)
+              user (presence/get-user self ws)]
           (presence/update-presence! self ws {:editing-block-uuid editing-block-uuid})
-          (presence/broadcast-online-users! self))
+          (ws/broadcast! self nil {:type "presence"
+                                   :editing-block-uuid editing-block-uuid
+                                   :user-id (:user-id user)}))
 
         "pull"
         (let [raw-since (:since message)

+ 4 - 0
deps/db-sync/src/logseq/db_sync/worker/presence.cljs

@@ -50,6 +50,10 @@
                (assoc presence ws user'))
              presence))))
 
+(defn get-user
+  [^js self ^js ws]
+  (get @(presence* self) ws))
+
 (defn remove-presence!
   [^js self ^js ws]
   (swap! (presence* self) dissoc ws))

+ 1 - 1
docs/agent-guide/db-sync/protocol.md

@@ -22,7 +22,7 @@
 - `{"type":"hello","t":<t>}`
   - Server hello with current t.
 - `{"type":"online-users","online-users":[{"user-id":"...","email":"...","username":"...","name":"..."}...]}`
-  - Presence update with currently online users (fields may be omitted).
+  - Presence update
   - Optional `editing-block-uuid` indicates the block the user is editing.
 - `{"type":"pull/ok","t":<t>,"txs":[{"t":<t>,"tx":"<tx-transit>"}...]}`
   - Pull response with txs.

+ 18 - 5
src/main/frontend/worker/sync.cljs

@@ -13,6 +13,7 @@
             [frontend.worker.sync.const :as rtc-const]
             [frontend.worker.sync.crypt :as sync-crypt]
             [lambdaisland.glogi :as log]
+            [logseq.common.util :as common-util]
             [logseq.db :as ldb]
             [logseq.db-sync.cycle :as sync-cycle]
             [logseq.db-sync.malli-schema :as db-sync-schema]
@@ -56,15 +57,13 @@
 (defn- normalize-online-users
   [users]
   (->> users
-       (keep (fn [{:keys [user-id email username name editing-block-uuid]}]
+       (keep (fn [{:keys [user-id email username name]}]
                (when (string? user-id)
                  (let [display-name (or username name user-id)]
                    (cond-> {:user/uuid user-id
                             :user/name display-name}
-                     (string? email) (assoc :user/email email)
-                     (and (string? editing-block-uuid)
-                          (not (string/blank? editing-block-uuid)))
-                     (assoc :user/editing-block-uuid editing-block-uuid))))))
+                     (string? email) (assoc :user/email email))))))
+       (common-util/distinct-by :user/uuid)
        (vec)))
 
 (defn- broadcast-rtc-state!
@@ -98,6 +97,18 @@
     (reset! *online-users (normalize-online-users users))
     (broadcast-rtc-state! client)))
 
+(defn- update-user-presence!
+  [client user-id* editing-block-uuid]
+  (when (and user-id* editing-block-uuid)
+    (when-let [*online-users (:online-users client)]
+      (swap! *online-users
+             (fn [users]
+               (mapv (fn [user]
+                       (if (= user-id* (:user/uuid user))
+                         (assoc user :user/editing-block-uuid editing-block-uuid)
+                         user)) users)))
+      (broadcast-rtc-state! client))))
+
 (defn- enabled?
   []
   (true? (:enabled? @worker-state/*db-sync-config)))
@@ -868,6 +879,8 @@
                            (fail-fast :db-sync/invalid-field
                                       {:repo repo :type "online-users" :field :online-users}))
                          (update-online-users! client (or users [])))
+        "presence" (let [{:keys [user-id editing-block-uuid]} message]
+                     (update-user-presence! client user-id editing-block-uuid))
         ;; Upload response
         "tx/batch/ok" (do
                         (require-non-negative remote-tx {:repo repo :type "tx/batch/ok"})