fix: no need to broadcast online users when updating presence

This commit is contained in:
Tienson Qin
2026-01-30 15:39:37 +08:00
parent d46b66c228
commit 63c9c65c95
5 changed files with 34 additions and 10 deletions

View File

@@ -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"

View File

@@ -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)

View File

@@ -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))

View File

@@ -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.

View File

@@ -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"})