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