package webhook import ( "encoding/json" "fmt" "jannex/admin-dashboard-backend/modules/config" "jannex/admin-dashboard-backend/modules/logger" "jannex/admin-dashboard-backend/modules/notification" "jannex/admin-dashboard-backend/modules/structs" "git.ex.umbach.dev/Alex/roese-utils/rslogger" "github.com/gofiber/fiber/v2" ) type EventData struct { Type string `json:"type"` } type PrinterData struct { ID int `json:"id"` Name string `json:"name"` } type PrintData struct { ID int `json:"id"` Filename string `json:"filename"` } type Message struct { Event EventData `json:"event"` Printer PrinterData `json:"printer"` Print PrintData `json:"print"` ImgURL string `json:"img_url"` } func Webhook(c *fiber.Ctx) error { logger.AddSystemLog(rslogger.LogTypeInfo, "Received webhook: %v query: %v", string(c.Body()), c.Query("auth")) if c.Query("auth") == "oThBd3apemoexlRyyNmLvgLRswGH6Fp9niCzxuxZgFg8ahnYqE" { var message Message if err := json.Unmarshal(c.Body(), &message); err != nil { logger.AddSystemLog(rslogger.LogTypeError, "Failed to unmarshal body, err: %v", err.Error()) return c.SendStatus(fiber.StatusOK) } var state uint8 if message.Event.Type == "PrintFailure" { state = 4 } else if message.Event.Type == "PrintPaused" { state = 3 } else { state = 1 } notification.AddNotification(nil, structs.AddNotificationRequest{ UserIds: config.Cfg.NotificationUserIds, Type: state, Title: fmt.Sprintf("Event: %s\nPrinter ID: %d\nPrinter Name: %s\nPrint ID: %d\nPrint Filename: %s\nImg Url: %s", message.Event.Type, message.Printer.ID, message.Printer.Name, message.Print.ID, message.Print.Filename, message.ImgURL), }) return c.SendStatus(fiber.StatusOK) } logger.AddSystemLog(rslogger.LogTypeWarning, "Received webhook with unknown auth") return c.SendStatus(fiber.StatusBadRequest) }