From d3d1b1676cf8220f55ce43ad767eb2e8791a314a Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 24 Oct 2020 22:34:18 +0200 Subject: [PATCH] Set Matrix online status and mark encountered messages as read --- main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.go b/main.go index 9f91039..71f62c3 100644 --- a/main.go +++ b/main.go @@ -233,9 +233,34 @@ func statusHandler(w http.ResponseWriter, r *http.Request) { } } +func syncMatrix() { + syncer := matrix.Syncer.(*gomatrix.DefaultSyncer) + syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) { + if ev.Sender != matrix.UserID { + matrix.MarkRead(ev.RoomID, ev.ID) + } + }) + + go func() { // set online status every 15 seconds + for { + if err := matrix.SetStatus("online", "up and running"); err != nil { + panic(fmt.Sprintf("error setting matrix status: %s", err)) + } + time.Sleep(15 * time.Second) + } + }() + + for { + if err := matrix.Sync(); err != nil { + fmt.Println("Sync() returned ", err) + } + } +} + func main() { go updateUptime() go checkClosure() + go syncMatrix() http.HandleFunc("/", rootHandler) http.HandleFunc("/api", apiHandler) http.HandleFunc("/img", imgHandler)