0

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
This commit is contained in:
Gabe Kangas 2021-09-21 14:06:23 -07:00 committed by GitHub
parent 9ecb51f680
commit db22931fb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

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

View File

@ -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 {
if shouldSendJoinedMessages {
s.sendUserJoinedMessage(client)
}
s.sendWelcomeMessageToClient(client)
}