From db22931fb5fa2bb7aefe9ae25686f1592faf61c5 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 21 Sep 2021 14:06:23 -0700 Subject: [PATCH] Do not send user joined messages for already active users (#1416) * Do not send user joined messages for active users * Reduce from active within 10min to 5 --- core/chat/events.go | 1 + core/chat/server.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/chat/events.go b/core/chat/events.go index 18acd7709..284a007c9 100644 --- a/core/chat/events.go +++ b/core/chat/events.go @@ -112,4 +112,5 @@ func (s *Server) userMessageSent(eventData chatClientEvent) { SaveUserMessage(event) eventData.client.MessageCount = eventData.client.MessageCount + 1 + _lastSeenCache[event.User.ID] = time.Now() } diff --git a/core/chat/server.go b/core/chat/server.go index bd3c2fed7..76ee44059 100644 --- a/core/chat/server.go +++ b/core/chat/server.go @@ -20,6 +20,9 @@ import ( var _server *Server +// a map of user IDs and when they last were active. +var _lastSeenCache = map[string]time.Time{} + // Server represents an instance of the chat server. type Server struct { mu sync.RWMutex @@ -83,11 +86,18 @@ func (s *Server) Addclient(conn *websocket.Conn, user *user.User, accessToken st ConnectedAt: time.Now(), } + // Do not send user re-joined broadcast message if they've been active within 5 minutes. + shouldSendJoinedMessages := true + if previouslyLastSeen, ok := _lastSeenCache[user.ID]; ok && time.Since(previouslyLastSeen) < time.Minute*5 { + shouldSendJoinedMessages = false + } + s.mu.Lock() { client.id = s.seq s.clients[client.id] = client s.seq++ + _lastSeenCache[user.ID] = time.Now() } s.mu.Unlock() @@ -99,7 +109,9 @@ func (s *Server) Addclient(conn *websocket.Conn, user *user.User, accessToken st client.sendConnectedClientInfo() if getStatus().Online { - s.sendUserJoinedMessage(client) + if shouldSendJoinedMessages { + s.sendUserJoinedMessage(client) + } s.sendWelcomeMessageToClient(client) }