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:
parent
9ecb51f680
commit
db22931fb5
@ -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()
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user