Line data Source code
1 4 : (ns matchscore.api.presence
2 : (:require
3 : [matchscore.realtime.core :as realtime]
4 : [matchscore.realtime.subscriptions :as subscriptions]
5 : [ring.util.http-status :as status]
6 : [schemas.api.core :as api-schemas]
7 : [taoensso.telemere :as t]))
8 :
9 :
10 : ;; Handlers
11 4 : (defn presence-away-handler
12 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/PresenceAwayResponseSchema]
13 : :malli/parameters {:query api-schemas/PresenceAwayQuerySchema}
14 : :authz {:public? true}
15 : :summary "Signal a connection is going away/offline"
16 : :description
17 : "Best-effort presence beacon sent by the client as the page is torn down (tab
18 : closed / app suspended), when a buffered WebSocket frame would not survive the
19 : unload. Flips the calling connection (`client-id`) to away/offline across every
20 : event the user is present on and broadcasts the resulting presence change, so
21 : watchers see it leave immediately instead of waiting for the heartbeat reaper.
22 : Auth reuses the standard TWA (`?twa=`) / session-cookie resolution; `uid` is
23 : taken from the resolved auth headers, never trusted from the client."}
24 : [{:keys [redis auth-headers params]
25 : :as _request}]
26 4 : (let [uid (:user/uuid auth-headers)
27 1 : {:keys [client-id reason]} params
28 7 : reason (keyword (or reason "offline"))]
29 100 : (t/log! :info (str "Presence beacon: " uid "/" client-id " going " reason))
30 31 : (doseq [event-uuid (subscriptions/unsubscribe-all-for-connection! redis uid client-id reason)]
31 3 : (realtime/relay-presence-change!
32 : nil
33 1 : redis
34 1 : event-uuid
35 1 : uid
36 5 : (subscriptions/get-user-devices redis event-uuid uid)))
37 4 : {:status status/ok
38 3 : :body {:ok true}}))
|