0

Limit the backlog of chat messages to recent messages. For #47

This commit is contained in:
Gabe Kangas 2020-07-15 22:06:24 -07:00
parent aebbce4b76
commit a277012650
2 changed files with 19 additions and 2 deletions

View File

@ -80,5 +80,21 @@ func GetMessages() []models.ChatMessage {
return []models.ChatMessage{}
}
return _server.Messages
return getRecentMessages()
}
func getRecentMessages() []models.ChatMessage {
if len(_server.Messages) < 100 {
return _server.Messages
}
maxAgeInHours := float64(2)
messages := make([]models.ChatMessage, 0)
for _, message := range _server.Messages {
if time.Since(message.Timestamp).Hours() < maxAgeInHours {
messages = append(messages, message)
}
}
return messages
}

View File

@ -67,7 +67,8 @@ func addMessage(message models.ChatMessage) {
func getChatHistory() []models.ChatMessage {
history := make([]models.ChatMessage, 0)
rows, err := _db.Query("SELECT * FROM messages WHERE visible = 1")
// Get all messages sent within the past day
rows, err := _db.Query("SELECT * FROM messages WHERE visible = 1 AND datetime(timestamp) >=datetime('now', '-1 Day')")
if err != nil {
log.Fatal(err)
}