diff --git a/core/chat/chat.go b/core/chat/chat.go index 85141a033..50a7b9bc4 100644 --- a/core/chat/chat.go +++ b/core/chat/chat.go @@ -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 } diff --git a/core/chat/persistence.go b/core/chat/persistence.go index cc85fe867..9795eaf27 100644 --- a/core/chat/persistence.go +++ b/core/chat/persistence.go @@ -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) }