From 8875a40fead8a1e8396530e4ad35f677be04a2cc Mon Sep 17 00:00:00 2001 From: Noah Date: Mon, 8 Apr 2024 15:44:12 -0500 Subject: [PATCH] Combine queued messages into one to save on message spam (fix #18) --- matrix.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/matrix.go b/matrix.go index b9806c1..299da37 100644 --- a/matrix.go +++ b/matrix.go @@ -188,17 +188,28 @@ func initMatrix() { }) go func() { - for range time.Tick(time.Second * 6) { + for range time.Tick(time.Second * 2) { log.Printf("Scanning for queued messages...") - for _, msg := range MatrixOut { - _, err := client.SendMessageEvent(context.Background(), id.RoomID(configuration.matrix.room_id), event.EventMessage, format.RenderMarkdown(msg.text, true, false)) + if len(MatrixOut) > 0 { + text := "" + for i, msg := range MatrixOut { + text += msg.text + if i != len(MatrixOut) { + text += "\n" + } + } + MatrixOut = []MatrixMessage{} + _, err := client.SendMessageEvent(context.Background(), + id.RoomID(configuration.matrix.room_id), + event.EventMessage, + format.RenderMarkdown(text, true, false), + ) if err != nil { log.Printf("[Matrix] ERROR : %s", err) continue } - log.Print("[Matrix] Sent queued message successfully.") + log.Print("[Matrix] Sent queued message(s) successfully.") } - MatrixOut = []MatrixMessage{} } }()