shoutrrr alerts / settings page

This commit is contained in:
Henry Dollman
2024-09-12 19:39:27 -04:00
parent 2889d151ea
commit 9710d0d2f1
16 changed files with 450 additions and 78 deletions

View File

@@ -4,6 +4,7 @@ import (
"beszel"
"beszel/internal/alerts"
"beszel/internal/entities/system"
"beszel/internal/entities/user"
"beszel/internal/records"
"beszel/site"
"context"
@@ -167,6 +168,36 @@ func (h *Hub) Run() {
go h.updateSystem(e.Model.(*models.Record))
return nil
})
// default user settings
h.app.OnModelBeforeCreate("user_settings").Add(func(e *core.ModelEvent) error {
record := e.Model.(*models.Record)
// intialize settings with defaults
settings := user.UserSettings{
// Language: "en",
ChartTime: "1h",
NotificationEmails: []string{},
NotificationWebhooks: []string{},
}
record.UnmarshalJSONField("settings", &settings)
if len(settings.NotificationEmails) == 0 {
// get user email from auth record
if errs := h.app.Dao().ExpandRecord(record, []string{"user"}, nil); len(errs) == 0 {
// app.Logger().Error("failed to expand user relation", "errs", errs)
if user := record.ExpandedOne("user"); user != nil {
settings.NotificationEmails = []string{user.GetString("email")}
} else {
log.Println("Failed to get user email from auth record")
}
} else {
log.Println("failed to expand user relation", "errs", errs)
}
}
// if len(settings.NotificationWebhooks) == 0 {
// settings.NotificationWebhooks = []string{""}
// }
record.Set("settings", settings)
return nil
})
// do things after a systems record is updated
h.app.OnModelAfterUpdate("systems").Add(func(e *core.ModelEvent) error {