From 2a79d08e7147c1aa1941b7624147e3e2a5b436f2 Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Sun, 5 Dec 2021 15:30:58 +0000 Subject: [PATCH] Correctly get room ID Signed-off-by: Marcus Noble --- alertmanager.go | 2 +- main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ payload.go | 44 +------------------------------------------- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/alertmanager.go b/alertmanager.go index 0a1c160..c6062ec 100644 --- a/alertmanager.go +++ b/alertmanager.go @@ -31,7 +31,7 @@ func HandleAlertmanagerPayloadPost(c *fiber.Ctx) error { message = fmt.Sprintf("☑️ %s", alert.Annotations["description"]) } - _, err := matrixClient.SendText(id.RoomID(*defaultRoom), message) + _, err := matrixClient.SendText(id.RoomID(getRoom(*defaultRoom)), message) if err != nil { fmt.Println("Failed sending to Matrix", err) if httpErr, ok := err.(mautrix.HTTPError); ok { diff --git a/main.go b/main.go index 2017e08..4271fa8 100644 --- a/main.go +++ b/main.go @@ -58,6 +58,50 @@ func main() { app.Listen(fmt.Sprintf(":%s", port)) } +func getRoom(roomID string) string { + room := parseRoom(roomID) + if strings.HasPrefix(room, "#") || !strings.HasPrefix("!", room) { + if !strings.HasPrefix(room, "#") { + room = fmt.Sprintf("#%s", room) + } + + resp, err := matrixClient.ResolveAlias(id.RoomAlias(room)) + if err == nil { + room = resp.RoomID.String() + } else { + room = strings.Replace(room, "#", "!", 1) + } + } + + return room +} + +func parseRoom(room string) string { + prefix := "" + local := "" + domain := "" + + parts := strings.Split(room, ":") + if len(parts) == 2 { + domain = parts[1] + } else { + domain = matrixClient.HomeserverURL.Host + } + + if strings.HasPrefix(parts[0], "!") { + prefix = "!" + parts[0] = strings.TrimPrefix(parts[0], "!") + } + if strings.HasPrefix(parts[0], "#") { + prefix = "#" + parts[0] = strings.TrimPrefix(parts[0], "#") + } + + local = parts[0] + + return fmt.Sprintf("%s%s:%s", prefix, local, domain) +} + func HandlePayloadPost(c *fiber.Ctx) error { payload := Payload{} diff --git a/payload.go b/payload.go index 16380ed..1ad9790 100644 --- a/payload.go +++ b/payload.go @@ -2,10 +2,6 @@ package main import ( "errors" - "fmt" - "strings" - - "maunium.net/go/mautrix/id" ) type PayloadType string @@ -38,45 +34,7 @@ func (p *Payload) Validate(defaultRoom *string) error { return errors.New("'roomID' is required") } - room := parseRoom(p.RoomID) - if strings.HasPrefix(room, "#") || !strings.HasPrefix("!", room) { - if !strings.HasPrefix(room, "#") { - room = fmt.Sprintf("#%s", room) - } - - resp, err := matrixClient.ResolveAlias(id.RoomAlias(room)) - if err == nil { - p.RoomID = resp.RoomID.String() - } else { - p.RoomID = strings.Replace(room, "#", "!", 1) - } - } + p.RoomID = getRoom(p.RoomID) return nil } - -func parseRoom(room string) string { - prefix := "" - local := "" - domain := "" - - parts := strings.Split(room, ":") - if len(parts) == 2 { - domain = parts[1] - } else { - domain = matrixClient.HomeserverURL.Host - } - - if strings.HasPrefix(parts[0], "!") { - prefix = "!" - parts[0] = strings.TrimPrefix(parts[0], "!") - } - if strings.HasPrefix(parts[0], "#") { - prefix = "#" - parts[0] = strings.TrimPrefix(parts[0], "#") - } - - local = parts[0] - - return fmt.Sprintf("%s%s:%s", prefix, local, domain) -}