diff --git a/Makefile b/Makefile index 8f70dcd8..ce88c70c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=0.79.2 +VERSION=0.79.3 BINARY_NAME=statup GOPATH:=$(GOPATH) GOCMD=go diff --git a/core/notifier/notifiers.go b/core/notifier/notifiers.go index ce797606..fbba5c97 100644 --- a/core/notifier/notifiers.go +++ b/core/notifier/notifiers.go @@ -16,6 +16,7 @@ package notifier import ( + "database/sql" "encoding/json" "errors" "fmt" @@ -46,7 +47,7 @@ type Notification struct { Var2 string `gorm:"not null;column:var2" json:"var2,omitempty"` ApiKey string `gorm:"not null;column:api_key" json:"api_key,omitempty"` ApiSecret string `gorm:"not null;column:api_secret" json:"api_secret,omitempty"` - Enabled bool `gorm:"column:enabled;type:boolean;default:false" json:"enabled"` + Enabled sql.NullBool `gorm:"column:enabled;type:boolean;default:false" json:"enabled"` Limits int `gorm:"not null;column:limits" json:"limits"` Removable bool `gorm:"column:removable" json:"-"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` @@ -199,11 +200,14 @@ func SelectNotification(n Notifier) (*Notification, error) { // Update will update the notification into the database func Update(n Notifier, notif *Notification) (*Notification, error) { + notif.ResetQueue() err := db.Model(&Notification{}).Update(notif) - if notif.Enabled { + if notif.Enabled.Bool { notif.close() notif.start() go Queue(n) + } else { + notif.close() } return notif, err.Error } @@ -253,7 +257,7 @@ func startAllNotifiers() { for _, comm := range AllCommunications { if isType(comm, new(Notifier)) { notify := comm.(Notifier) - if notify.Select().Enabled { + if notify.Select().Enabled.Bool { notify.Select().close() notify.Select().start() go Queue(notify) @@ -376,8 +380,8 @@ func isType(n interface{}, obj interface{}) bool { // isEnabled returns true if the notifier is enabled func isEnabled(n interface{}) bool { - notifier, _ := SelectNotification(n.(Notifier)) - return notifier.Enabled + notifier := n.(Notifier).Select() + return notifier.Enabled.Bool } // inLimits will return true if the notifier is within sending limits diff --git a/handlers/api.go b/handlers/api.go index 7313e9dd..37af4f8a 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -16,6 +16,7 @@ package handlers import ( + "database/sql" "encoding/json" "fmt" "github.com/gorilla/mux" @@ -341,7 +342,7 @@ func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) { return } w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(notifierObj.Select()) + json.NewEncoder(w).Encode(notifierObj) } func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) { @@ -351,33 +352,34 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) { } vars := mux.Vars(r) var notification *notifier.Notification + fmt.Println(r.Body) decoder := json.NewDecoder(r.Body) decoder.Decode(¬ification) - notif, not, err := notifier.SelectNotifier(vars["notifier"]) + notifer, not, err := notifier.SelectNotifier(vars["notifier"]) if err != nil { http.Error(w, fmt.Sprintf("%v notifier was not found", vars["notifier"]), http.StatusInternalServerError) return } - notif.Var1 = notification.Var1 - notif.Var2 = notification.Var2 - notif.Host = notification.Host - notif.Port = notification.Port - notif.Password = notification.Password - notif.Username = notification.Username - notif.Enabled = notification.Enabled - notif.ApiKey = notification.ApiKey - notif.ApiSecret = notification.ApiSecret + notifer.Var1 = notification.Var1 + notifer.Var2 = notification.Var2 + notifer.Host = notification.Host + notifer.Port = notification.Port + notifer.Password = notification.Password + notifer.Username = notification.Username + notifer.Enabled = sql.NullBool{notification.Enabled.Bool, true} + notifer.ApiKey = notification.ApiKey + notifer.ApiSecret = notification.ApiSecret - _, err = notifier.Update(not, notif) + _, err = notifier.Update(not, notifer) if err != nil { utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err)) } - notifier.OnSave(notif.Method) + notifier.OnSave(notifer.Method) w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(notif) + json.NewEncoder(w).Encode(notifer) } func isAPIAuthorized(r *http.Request) bool { diff --git a/handlers/settings.go b/handlers/settings.go index 6cc4a215..7e0ac893 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -16,6 +16,7 @@ package handlers import ( + "database/sql" "fmt" "github.com/gorilla/mux" "github.com/hunterlong/statup/core" @@ -188,7 +189,8 @@ func saveNotificationHandler(w http.ResponseWriter, r *http.Request) { if limits != 0 { notifer.Limits = limits } - notifer.Enabled = enabled == "on" + notifer.Enabled = sql.NullBool{enabled == "on", true} + _, err = notifier.Update(notif, notifer) if err != nil { utils.Log(3, fmt.Sprintf("issue updating notifier: %v", err)) @@ -253,7 +255,7 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) { if limits != 0 { notifer.Limits = limits } - notifer.Enabled = enabled == "on" + notifer.Enabled = sql.NullBool{enabled == "on", true} err = notif.(notifier.Tester).OnTest() if err == nil { diff --git a/source/tmpl/form_notifier.html b/source/tmpl/form_notifier.html index 832a5692..dddf6d22 100644 --- a/source/tmpl/form_notifier.html +++ b/source/tmpl/form_notifier.html @@ -33,7 +33,7 @@