From 3abc7a3ab8b46973218b8c44599ca0d9729155c9 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 13 Dec 2022 19:17:32 -0800 Subject: [PATCH] Expose connected client ID to moderator client info api --- controllers/moderation/moderation.go | 2 ++ core/chat/chatclient.go | 8 ++++---- core/chat/events.go | 4 ++-- core/chat/server.go | 14 +++++++------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/controllers/moderation/moderation.go b/controllers/moderation/moderation.go index d799e4608..c59323b1d 100644 --- a/controllers/moderation/moderation.go +++ b/controllers/moderation/moderation.go @@ -16,6 +16,7 @@ import ( // GetUserDetails returns the details of a chat user for moderators. func GetUserDetails(w http.ResponseWriter, r *http.Request) { type connectedClient struct { + Id uint `json:"id"` MessageCount int `json:"messageCount"` UserAgent string `json:"userAgent"` ConnectedAt time.Time `json:"connectedAt"` @@ -42,6 +43,7 @@ func GetUserDetails(w http.ResponseWriter, r *http.Request) { clients := make([]connectedClient, len(c)) for i, c := range c { client := connectedClient{ + Id: c.Id, MessageCount: c.MessageCount, UserAgent: c.UserAgent, ConnectedAt: c.ConnectedAt, diff --git a/core/chat/chatclient.go b/core/chat/chatclient.go index 3e02e7158..771d7dbd1 100644 --- a/core/chat/chatclient.go +++ b/core/chat/chatclient.go @@ -20,7 +20,7 @@ import ( // Client represents a single chat client. type Client struct { mu sync.RWMutex - id uint + Id uint `json:"-"` accessToken string conn *websocket.Conn User *user.User `json:"user"` @@ -123,7 +123,7 @@ func (c *Client) readPump() { // Guard against floods. if !c.passesRateLimit() { - log.Warnln("Client", c.id, c.User.DisplayName, "has exceeded the messaging rate limiting thresholds and messages are being rejected temporarily.") + log.Warnln("Client", c.Id, c.User.DisplayName, "has exceeded the messaging rate limiting thresholds and messages are being rejected temporarily.") c.startChatRejectionTimeout() continue @@ -186,14 +186,14 @@ func (c *Client) handleEvent(data []byte) { } func (c *Client) close() { - log.Traceln("client closed:", c.User.DisplayName, c.id, c.IPAddress) + log.Traceln("client closed:", c.User.DisplayName, c.Id, c.IPAddress) c.mu.Lock() defer c.mu.Unlock() if c.send != nil { _ = c.conn.Close() - c.server.unregister <- c.id + c.server.unregister <- c.Id close(c.send) c.send = nil } diff --git a/core/chat/events.go b/core/chat/events.go index 892d26421..081a1532c 100644 --- a/core/chat/events.go +++ b/core/chat/events.go @@ -90,7 +90,7 @@ func (s *Server) userNameChanged(eventData chatClientEvent) { // Send chat user name changed webhook receivedEvent.User = savedUser - receivedEvent.ClientID = eventData.client.id + receivedEvent.ClientID = eventData.client.Id webhooks.SendChatEventUsernameChanged(receivedEvent) // Resend the client's user so their username is in sync. @@ -128,7 +128,7 @@ func (s *Server) userMessageSent(eventData chatClientEvent) { } event.SetDefaults() - event.ClientID = eventData.client.id + event.ClientID = eventData.client.Id // Ignore empty messages if event.Empty() { diff --git a/core/chat/server.go b/core/chat/server.go index d8692a8ca..7d49757aa 100644 --- a/core/chat/server.go +++ b/core/chat/server.go @@ -99,14 +99,14 @@ func (s *Server) Addclient(conn *websocket.Conn, user *user.User, accessToken st s.mu.Lock() { - client.id = s.seq - s.clients[client.id] = client + client.Id = s.seq + s.clients[client.Id] = client s.seq++ _lastSeenCache[user.ID] = time.Now() } s.mu.Unlock() - log.Traceln("Adding client", client.id, "total count:", len(s.clients)) + log.Traceln("Adding client", client.Id, "total count:", len(s.clients)) go client.writePump() go client.readPump() @@ -132,7 +132,7 @@ func (s *Server) sendUserJoinedMessage(c *Client) { userJoinedEvent := events.UserJoinedEvent{} userJoinedEvent.SetDefaults() userJoinedEvent.User = c.User - userJoinedEvent.ClientID = c.id + userJoinedEvent.ClientID = c.Id if err := s.Broadcast(userJoinedEvent.GetBroadcastPayload()); err != nil { log.Errorln("error adding client to chat server", err) @@ -148,9 +148,9 @@ func (s *Server) ClientClosed(c *Client) { defer s.mu.Unlock() c.close() - if _, ok := s.clients[c.id]; ok { - log.Debugln("Deleting", c.id) - delete(s.clients, c.id) + if _, ok := s.clients[c.Id]; ok { + log.Debugln("Deleting", c.Id) + delete(s.clients, c.Id) } }