From 21ceb91d717b0149a5f1ab03d488b128953c0880 Mon Sep 17 00:00:00 2001
From: Janne Mareike Koschinski <janne@kuschku.de>
Date: Fri, 28 Feb 2025 19:01:14 +0100
Subject: [PATCH] feat: improve customizability

---
 api_setpusher.go | 16 ++--------------
 matrixbot.go     | 43 +++++++++++++++++++++++++++++--------------
 2 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/api_setpusher.go b/api_setpusher.go
index e1ed92e..c77a8ae 100644
--- a/api_setpusher.go
+++ b/api_setpusher.go
@@ -26,20 +26,8 @@ type PusherData struct {
 	Url    string `json:"url"`
 }
 
-func SetPusher(token MatrixToken, url string) error {
-	body, err := json.Marshal(PusherRequest{
-		AppDisplayName: "webhook",
-		AppId:          "de.justjanne.webhook",
-		Append:         false,
-		Data: PusherData{
-			Url: url,
-		},
-		DeviceDisplayName: "webhook",
-		Kind:              "http",
-		Lang:              "en",
-		ProfileTag:        "abcdef",
-		PushKey:           "webhook",
-	})
+func SetPusher(token MatrixToken, pusher PusherRequest) error {
+	body, err := json.Marshal(pusher)
 	if err != nil {
 		return err
 	}
diff --git a/matrixbot.go b/matrixbot.go
index 9c7afb2..ae16982 100644
--- a/matrixbot.go
+++ b/matrixbot.go
@@ -11,17 +11,13 @@ import (
 )
 
 type MatrixBot struct {
-	Token    *MatrixToken
-	PushUrl  *url.URL
-	handlers map[string]func(bot *MatrixBot, notification Notification) error
-}
-
-func NewMatrixBot(pushUrl *url.URL) *MatrixBot {
-	return &MatrixBot{
-		Token:    nil,
-		PushUrl:  pushUrl,
-		handlers: make(map[string]func(bot *MatrixBot, notification Notification) error),
-	}
+	AppName    string
+	AppId      string
+	ProfileTag string
+	PushKey    string
+	PushUrl    *url.URL
+	Token      *MatrixToken
+	handlers   map[string]func(bot *MatrixBot, notification Notification) error
 }
 
 func (bot *MatrixBot) RefreshToken() error {
@@ -65,11 +61,30 @@ func (bot *MatrixBot) RefreshTask() {
 }
 
 func (bot *MatrixBot) RegisterPusher() error {
-	return SetPusher(*bot.Token, bot.PushUrl.String())
+	return SetPusher(*bot.Token, PusherRequest{
+		AppDisplayName: bot.AppName,
+		AppId:          bot.AppId,
+		Append:         false,
+		Data: PusherData{
+			Url: bot.PushUrl.String(),
+		},
+		DeviceDisplayName: bot.AppName,
+		Kind:              "http",
+		Lang:              "en",
+		ProfileTag:        bot.ProfileTag,
+		PushKey:           bot.AppId,
+	})
+}
+
+func (bot *MatrixBot) ensureHandlers() map[string]func(bot *MatrixBot, notification Notification) error {
+	if bot.handlers == nil {
+		bot.handlers = make(map[string]func(bot *MatrixBot, notification Notification) error)
+	}
+	return bot.handlers
 }
 
 func (bot *MatrixBot) HandleFunc(command string, handler func(bot *MatrixBot, notification Notification) error) {
-	bot.handlers[command] = handler
+	bot.ensureHandlers()[command] = handler
 }
 
 func (bot *MatrixBot) Serve(endpoint string) {
@@ -93,7 +108,7 @@ func (bot *MatrixBot) Serve(endpoint string) {
 			return
 		}
 		command := strings.SplitN(notification.Content.Body, " ", 2)[0]
-		handler, ok := bot.handlers[command]
+		handler, ok := bot.ensureHandlers()[command]
 		if !ok {
 			log.Printf("could not find handler for '%s'\n", command)
 			return
-- 
GitLab